src/Pure/System/options.scala
author wenzelm
Tue Jul 24 00:29:36 2012 +0200 (2012-07-24)
changeset 48457 fd9e28d5a143
parent 48456 d8ff14f44a40
child 48548 49afe0e92163
permissions -rw-r--r--
pass build options to ML;
some imitation of usedir Session.init;
wenzelm@48365
     1
/*  Title:      Pure/System/options.scala
wenzelm@48365
     2
    Author:     Makarius
wenzelm@48365
     3
wenzelm@48365
     4
Stand-alone options with external string representation.
wenzelm@48365
     5
*/
wenzelm@48365
     6
wenzelm@48365
     7
package isabelle
wenzelm@48365
     8
wenzelm@48365
     9
wenzelm@48409
    10
import java.io.{File => JFile}
wenzelm@48365
    11
wenzelm@48365
    12
wenzelm@48365
    13
object Options
wenzelm@48365
    14
{
wenzelm@48421
    15
  type Spec = (String, Option[String])
wenzelm@48421
    16
wenzelm@48421
    17
  val empty: Options = new Options()
wenzelm@48421
    18
wenzelm@48421
    19
wenzelm@48421
    20
  /* representation */
wenzelm@48421
    21
wenzelm@48421
    22
  sealed abstract class Type
wenzelm@48365
    23
  {
wenzelm@48365
    24
    def print: String = toString.toLowerCase
wenzelm@48365
    25
  }
wenzelm@48421
    26
  private case object Bool extends Type
wenzelm@48421
    27
  private case object Int extends Type
wenzelm@48421
    28
  private case object Real extends Type
wenzelm@48421
    29
  private case object String extends Type
wenzelm@48365
    30
wenzelm@48370
    31
  case class Opt(typ: Type, value: String, description: String)
wenzelm@48365
    32
wenzelm@48365
    33
wenzelm@48365
    34
  /* parsing */
wenzelm@48365
    35
wenzelm@48365
    36
  private object Parser extends Parse.Parser
wenzelm@48365
    37
  {
wenzelm@48365
    38
    val DECLARE = "declare"
wenzelm@48365
    39
    val DEFINE = "define"
wenzelm@48365
    40
wenzelm@48365
    41
    val syntax = Outer_Syntax.empty + ":" + "=" + DECLARE + DEFINE
wenzelm@48365
    42
wenzelm@48365
    43
    val entry: Parser[Options => Options] =
wenzelm@48365
    44
    {
wenzelm@48365
    45
      val option_name = atom("option name", _.is_xname)
wenzelm@48365
    46
      val option_type = atom("option type", _.is_ident)
wenzelm@48365
    47
      val option_value = atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48365
    48
wenzelm@48370
    49
      keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48370
    50
      keyword("=") ~ option_value ~ opt(text)) ^^
wenzelm@48370
    51
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) =>
wenzelm@48370
    52
            (options: Options) => options.declare(a, b, c, d.getOrElse("")) } |
wenzelm@48365
    53
      keyword(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
wenzelm@48370
    54
        { case _ ~ (a ~ _ ~ b) => (options: Options) => options.define(a, b) }
wenzelm@48365
    55
    }
wenzelm@48365
    56
wenzelm@48409
    57
    def parse_entries(file: JFile): List[Options => Options] =
wenzelm@48365
    58
    {
wenzelm@48411
    59
      parse_all(rep(entry), Token.reader(syntax.scan(File.read(file)), file.toString)) match {
wenzelm@48365
    60
        case Success(result, _) => result
wenzelm@48365
    61
        case bad => error(bad.toString)
wenzelm@48365
    62
      }
wenzelm@48365
    63
    }
wenzelm@48365
    64
  }
wenzelm@48365
    65
wenzelm@48421
    66
  private val OPTIONS = Path.explode("etc/options")
wenzelm@48365
    67
wenzelm@48365
    68
  def init(): Options =
wenzelm@48365
    69
  {
wenzelm@48365
    70
    var options = empty
wenzelm@48365
    71
    for {
wenzelm@48365
    72
      dir <- Isabelle_System.components()
wenzelm@48373
    73
      file = (dir + OPTIONS).file
wenzelm@48365
    74
      if file.isFile
wenzelm@48365
    75
      entry <- Parser.parse_entries(file)
wenzelm@48365
    76
    } {
wenzelm@48365
    77
      try { options = entry(options) }
wenzelm@48370
    78
      catch { case ERROR(msg) => error(msg + Position.str_of(Position.file(file))) }
wenzelm@48365
    79
    }
wenzelm@48365
    80
    options
wenzelm@48365
    81
  }
wenzelm@48457
    82
wenzelm@48457
    83
wenzelm@48457
    84
  /* encode */
wenzelm@48457
    85
wenzelm@48457
    86
  val encode: XML.Encode.T[Options] = (options => options.encode)
wenzelm@48365
    87
}
wenzelm@48365
    88
wenzelm@48365
    89
wenzelm@48365
    90
final class Options private(options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
    91
{
wenzelm@48365
    92
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
    93
wenzelm@48365
    94
wenzelm@48370
    95
  /* check */
wenzelm@48365
    96
wenzelm@48365
    97
  private def check_name(name: String): Options.Opt =
wenzelm@48365
    98
    options.get(name) match {
wenzelm@48365
    99
      case Some(opt) => opt
wenzelm@48368
   100
      case None => error("Unknown option " + quote(name))
wenzelm@48365
   101
    }
wenzelm@48365
   102
wenzelm@48365
   103
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   104
  {
wenzelm@48365
   105
    val opt = check_name(name)
wenzelm@48365
   106
    if (opt.typ == typ) opt
wenzelm@48365
   107
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   108
  }
wenzelm@48365
   109
wenzelm@48370
   110
wenzelm@48370
   111
  /* basic operations */
wenzelm@48370
   112
wenzelm@48370
   113
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   114
  {
wenzelm@48370
   115
    val opt = check_type(name, typ)
wenzelm@48370
   116
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   117
  }
wenzelm@48370
   118
wenzelm@48365
   119
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   120
  {
wenzelm@48365
   121
    val opt = check_type(name, typ)
wenzelm@48365
   122
    parse(opt.value) match {
wenzelm@48365
   123
      case Some(x) => x
wenzelm@48365
   124
      case None =>
wenzelm@48365
   125
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   126
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   127
    }
wenzelm@48365
   128
  }
wenzelm@48365
   129
wenzelm@48365
   130
wenzelm@48365
   131
  /* internal lookup and update */
wenzelm@48365
   132
wenzelm@48365
   133
  val bool = new Object
wenzelm@48365
   134
  {
wenzelm@48365
   135
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   136
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   137
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   138
  }
wenzelm@48365
   139
wenzelm@48365
   140
  val int = new Object
wenzelm@48365
   141
  {
wenzelm@48365
   142
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   143
    def update(name: String, x: Int): Options =
wenzelm@48365
   144
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   145
  }
wenzelm@48365
   146
wenzelm@48365
   147
  val real = new Object
wenzelm@48365
   148
  {
wenzelm@48365
   149
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   150
    def update(name: String, x: Double): Options =
wenzelm@48365
   151
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   152
  }
wenzelm@48365
   153
wenzelm@48365
   154
  val string = new Object
wenzelm@48365
   155
  {
wenzelm@48365
   156
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   157
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   158
  }
wenzelm@48370
   159
wenzelm@48370
   160
wenzelm@48370
   161
  /* external declare and define */
wenzelm@48370
   162
wenzelm@48370
   163
  private def check_value(name: String): Options =
wenzelm@48370
   164
  {
wenzelm@48370
   165
    val opt = check_name(name)
wenzelm@48370
   166
    opt.typ match {
wenzelm@48370
   167
      case Options.Bool => bool(name); this
wenzelm@48370
   168
      case Options.Int => int(name); this
wenzelm@48370
   169
      case Options.Real => real(name); this
wenzelm@48370
   170
      case Options.String => string(name); this
wenzelm@48370
   171
    }
wenzelm@48370
   172
  }
wenzelm@48370
   173
wenzelm@48370
   174
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   175
  {
wenzelm@48370
   176
    options.get(name) match {
wenzelm@48370
   177
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   178
      case None =>
wenzelm@48370
   179
        val typ =
wenzelm@48370
   180
          typ_name match {
wenzelm@48370
   181
            case "bool" => Options.Bool
wenzelm@48370
   182
            case "int" => Options.Int
wenzelm@48370
   183
            case "real" => Options.Real
wenzelm@48370
   184
            case "string" => Options.String
wenzelm@48456
   185
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   186
          }
wenzelm@48370
   187
        (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
wenzelm@48370
   188
    }
wenzelm@48370
   189
  }
wenzelm@48370
   190
wenzelm@48370
   191
  def define(name: String, value: String): Options =
wenzelm@48370
   192
  {
wenzelm@48370
   193
    val opt = check_name(name)
wenzelm@48370
   194
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   195
  }
wenzelm@48370
   196
wenzelm@48370
   197
  def define(name: String, opt_value: Option[String]): Options =
wenzelm@48370
   198
  {
wenzelm@48370
   199
    val opt = check_name(name)
wenzelm@48370
   200
    opt_value match {
wenzelm@48370
   201
      case Some(value) => define(name, value)
wenzelm@48370
   202
      case None if opt.typ == Options.Bool => define(name, "true")
wenzelm@48370
   203
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   204
    }
wenzelm@48370
   205
  }
wenzelm@48370
   206
wenzelm@48421
   207
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48421
   208
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
wenzelm@48421
   209
wenzelm@48370
   210
  def define_simple(str: String): Options =
wenzelm@48370
   211
  {
wenzelm@48370
   212
    str.indexOf('=') match {
wenzelm@48370
   213
      case -1 => define(str, None)
wenzelm@48370
   214
      case i => define(str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   215
    }
wenzelm@48370
   216
  }
wenzelm@48456
   217
wenzelm@48456
   218
wenzelm@48456
   219
  /* encode */
wenzelm@48456
   220
wenzelm@48456
   221
  def encode: XML.Body =
wenzelm@48456
   222
  {
wenzelm@48456
   223
    import XML.Encode.{string => str, _}
wenzelm@48456
   224
    list(triple(str, str, str))(
wenzelm@48456
   225
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
wenzelm@48456
   226
  }
wenzelm@48365
   227
}