src/Pure/System/options.scala
author wenzelm
Thu Apr 17 10:54:10 2014 +0200 (2014-04-17)
changeset 56609 5ac67041ccf8
parent 56600 628e039cc34d
child 56631 89269bb8e7ca
permissions -rw-r--r--
capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm@48365
     1
/*  Title:      Pure/System/options.scala
wenzelm@48365
     2
    Author:     Makarius
wenzelm@48365
     3
wenzelm@51945
     4
System options with external string representation.
wenzelm@48365
     5
*/
wenzelm@48365
     6
wenzelm@48365
     7
package isabelle
wenzelm@48365
     8
wenzelm@48365
     9
wenzelm@48807
    10
import java.util.Calendar
wenzelm@48807
    11
wenzelm@48807
    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@56599
    24
    def print: String = Word.lowercase(toString)
wenzelm@48365
    25
  }
wenzelm@49246
    26
  case object Bool extends Type
wenzelm@49246
    27
  case object Int extends Type
wenzelm@49246
    28
  case object Real extends Type
wenzelm@49246
    29
  case object String extends Type
wenzelm@49246
    30
  case object Unknown extends Type
wenzelm@48365
    31
wenzelm@56465
    32
  case class Opt(
wenzelm@56465
    33
    public: Boolean,
wenzelm@56465
    34
    pos: Position.T,
wenzelm@56465
    35
    name: String,
wenzelm@56465
    36
    typ: Type,
wenzelm@56465
    37
    value: String,
wenzelm@56465
    38
    default_value: String,
wenzelm@56465
    39
    description: String,
wenzelm@56465
    40
    section: String)
wenzelm@48860
    41
  {
wenzelm@49289
    42
    private def print(default: Boolean): String =
wenzelm@49289
    43
    {
wenzelm@49289
    44
      val x = if (default) default_value else value
wenzelm@49247
    45
      "option " + name + " : " + typ.print + " = " +
wenzelm@49289
    46
        (if (typ == Options.String) quote(x) else x) +
wenzelm@49289
    47
        (if (description == "") "" else "\n  -- " + quote(description))
wenzelm@49289
    48
    }
wenzelm@49289
    49
wenzelm@49289
    50
    def print: String = print(false)
wenzelm@49289
    51
    def print_default: String = print(true)
wenzelm@49247
    52
wenzelm@56559
    53
    def title(strip: String = ""): String =
wenzelm@49270
    54
    {
wenzelm@56600
    55
      val words = Word.explode('_', name)
wenzelm@49270
    56
      val words1 =
wenzelm@49270
    57
        words match {
wenzelm@49270
    58
          case word :: rest if word == strip => rest
wenzelm@49270
    59
          case _ => words
wenzelm@49270
    60
        }
wenzelm@56609
    61
      Word.implode(words1.map(Word.perhaps_capitalize(_)))
wenzelm@49270
    62
    }
wenzelm@49270
    63
wenzelm@48860
    64
    def unknown: Boolean = typ == Unknown
wenzelm@48860
    65
  }
wenzelm@48365
    66
wenzelm@48365
    67
wenzelm@48365
    68
  /* parsing */
wenzelm@48365
    69
wenzelm@49270
    70
  private val SECTION = "section"
wenzelm@52065
    71
  private val PUBLIC = "public"
wenzelm@48795
    72
  private val OPTION = "option"
wenzelm@48807
    73
  private val OPTIONS = Path.explode("etc/options")
wenzelm@50793
    74
  private val PREFS_DIR = Path.explode("$ISABELLE_HOME_USER/etc")
wenzelm@50793
    75
  private val PREFS = PREFS_DIR + Path.basic("preferences")
wenzelm@48713
    76
wenzelm@49270
    77
  lazy val options_syntax =
wenzelm@49270
    78
    Outer_Syntax.init() + ":" + "=" + "--" +
wenzelm@52065
    79
      (SECTION, Keyword.THY_HEADING2) + (PUBLIC, Keyword.THY_DECL) + (OPTION, Keyword.THY_DECL)
wenzelm@49270
    80
wenzelm@48807
    81
  lazy val prefs_syntax = Outer_Syntax.init() + "="
wenzelm@48713
    82
wenzelm@48807
    83
  object Parser extends Parse.Parser
wenzelm@48365
    84
  {
wenzelm@48807
    85
    val option_name = atom("option name", _.is_xname)
wenzelm@48807
    86
    val option_type = atom("option type", _.is_ident)
wenzelm@48807
    87
    val option_value =
wenzelm@48807
    88
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
wenzelm@48807
    89
        { case s ~ n => if (s.isDefined) "-" + n else n } |
wenzelm@48807
    90
      atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48807
    91
wenzelm@48807
    92
    val option_entry: Parser[Options => Options] =
wenzelm@48365
    93
    {
wenzelm@49270
    94
      command(SECTION) ~! text ^^
wenzelm@49295
    95
        { case _ ~ a => (options: Options) => options.set_section(a) } |
wenzelm@52065
    96
      opt(command(PUBLIC)) ~ command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48580
    97
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
wenzelm@56465
    98
        { case a ~ pos ~ (b ~ _ ~ c ~ _ ~ d ~ e) =>
wenzelm@56465
    99
            (options: Options) => options.declare(a.isDefined, pos, b, c, d, e) }
wenzelm@48365
   100
    }
wenzelm@48365
   101
wenzelm@48807
   102
    val prefs_entry: Parser[Options => Options] =
wenzelm@48807
   103
    {
wenzelm@48807
   104
      option_name ~ (keyword("=") ~! option_value) ^^
wenzelm@48860
   105
      { case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
wenzelm@48807
   106
    }
wenzelm@48807
   107
wenzelm@48807
   108
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
wenzelm@48807
   109
      options: Options, file: Path): Options =
wenzelm@48365
   110
    {
wenzelm@48807
   111
      val toks = syntax.scan(File.read(file))
wenzelm@48807
   112
      val ops =
wenzelm@48807
   113
        parse_all(rep(parser), Token.reader(toks, file.implode)) match {
wenzelm@48807
   114
          case Success(result, _) => result
wenzelm@48807
   115
          case bad => error(bad.toString)
wenzelm@48807
   116
        }
wenzelm@49270
   117
      try { (options.set_section("") /: ops) { case (opts, op) => op(opts) } }
wenzelm@48992
   118
      catch { case ERROR(msg) => error(msg + Position.here(file.position)) }
wenzelm@48365
   119
    }
wenzelm@48365
   120
  }
wenzelm@48365
   121
wenzelm@48807
   122
  def init_defaults(): Options =
wenzelm@48365
   123
  {
wenzelm@48365
   124
    var options = empty
wenzelm@48365
   125
    for {
wenzelm@48365
   126
      dir <- Isabelle_System.components()
wenzelm@48548
   127
      file = dir + OPTIONS if file.is_file
wenzelm@48807
   128
    } { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
wenzelm@48365
   129
    options
wenzelm@48365
   130
  }
wenzelm@48457
   131
wenzelm@48807
   132
  def init(): Options = init_defaults().load_prefs()
wenzelm@48807
   133
wenzelm@48457
   134
wenzelm@48457
   135
  /* encode */
wenzelm@48457
   136
wenzelm@48457
   137
  val encode: XML.Encode.T[Options] = (options => options.encode)
wenzelm@48693
   138
wenzelm@48693
   139
wenzelm@48693
   140
  /* command line entry point */
wenzelm@48693
   141
wenzelm@48693
   142
  def main(args: Array[String])
wenzelm@48693
   143
  {
wenzelm@48693
   144
    Command_Line.tool {
wenzelm@48693
   145
      args.toList match {
wenzelm@52735
   146
        case get_option :: export_file :: more_options =>
wenzelm@48807
   147
          val options = (Options.init() /: more_options)(_ + _)
wenzelm@48693
   148
wenzelm@52735
   149
          if (get_option != "")
wenzelm@55618
   150
            System.out.println(options.check_name(get_option).value)
wenzelm@52737
   151
wenzelm@52737
   152
          if (export_file != "")
wenzelm@52735
   153
            File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
wenzelm@52737
   154
wenzelm@52737
   155
          if (get_option == "" && export_file == "")
wenzelm@55618
   156
            System.out.println(options.print)
wenzelm@48693
   157
wenzelm@48693
   158
          0
wenzelm@48693
   159
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48693
   160
      }
wenzelm@48693
   161
    }
wenzelm@48693
   162
  }
wenzelm@48365
   163
}
wenzelm@48365
   164
wenzelm@48365
   165
wenzelm@49270
   166
final class Options private(
wenzelm@49296
   167
  val options: Map[String, Options.Opt] = Map.empty,
wenzelm@49270
   168
  val section: String = "")
wenzelm@48365
   169
{
wenzelm@48365
   170
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
   171
wenzelm@54347
   172
  private def print_opt(opt: Options.Opt): String =
wenzelm@54347
   173
    if (opt.public) "public " + opt.print else opt.print
wenzelm@54347
   174
wenzelm@54347
   175
  def print: String = cat_lines(options.toList.sortBy(_._1).map(p => print_opt(p._2)))
wenzelm@48693
   176
wenzelm@49245
   177
  def description(name: String): String = check_name(name).description
wenzelm@49245
   178
wenzelm@48365
   179
wenzelm@48370
   180
  /* check */
wenzelm@48365
   181
wenzelm@49246
   182
  def check_name(name: String): Options.Opt =
wenzelm@48365
   183
    options.get(name) match {
wenzelm@48860
   184
      case Some(opt) if !opt.unknown => opt
wenzelm@48860
   185
      case _ => error("Unknown option " + quote(name))
wenzelm@48365
   186
    }
wenzelm@48365
   187
wenzelm@48365
   188
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   189
  {
wenzelm@48365
   190
    val opt = check_name(name)
wenzelm@48365
   191
    if (opt.typ == typ) opt
wenzelm@48365
   192
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   193
  }
wenzelm@48365
   194
wenzelm@48370
   195
wenzelm@48370
   196
  /* basic operations */
wenzelm@48370
   197
wenzelm@48370
   198
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   199
  {
wenzelm@48370
   200
    val opt = check_type(name, typ)
wenzelm@49270
   201
    new Options(options + (name -> opt.copy(value = value)), section)
wenzelm@48370
   202
  }
wenzelm@48370
   203
wenzelm@48365
   204
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   205
  {
wenzelm@48365
   206
    val opt = check_type(name, typ)
wenzelm@48365
   207
    parse(opt.value) match {
wenzelm@48365
   208
      case Some(x) => x
wenzelm@48365
   209
      case None =>
wenzelm@48365
   210
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   211
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   212
    }
wenzelm@48365
   213
  }
wenzelm@48365
   214
wenzelm@48365
   215
wenzelm@48365
   216
  /* internal lookup and update */
wenzelm@48365
   217
wenzelm@49954
   218
  class Bool_Access
wenzelm@48365
   219
  {
wenzelm@48365
   220
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   221
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   222
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   223
  }
wenzelm@49954
   224
  val bool = new Bool_Access
wenzelm@48365
   225
wenzelm@49954
   226
  class Int_Access
wenzelm@48365
   227
  {
wenzelm@48365
   228
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   229
    def update(name: String, x: Int): Options =
wenzelm@48365
   230
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   231
  }
wenzelm@49954
   232
  val int = new Int_Access
wenzelm@48365
   233
wenzelm@49954
   234
  class Real_Access
wenzelm@48365
   235
  {
wenzelm@48365
   236
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   237
    def update(name: String, x: Double): Options =
wenzelm@48365
   238
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   239
  }
wenzelm@49954
   240
  val real = new Real_Access
wenzelm@48365
   241
wenzelm@49954
   242
  class String_Access
wenzelm@48365
   243
  {
wenzelm@48365
   244
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   245
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   246
  }
wenzelm@49954
   247
  val string = new String_Access
wenzelm@48370
   248
wenzelm@50207
   249
  class Seconds_Access
wenzelm@50207
   250
  {
wenzelm@50207
   251
    def apply(name: String): Time = Time.seconds(real(name))
wenzelm@50207
   252
  }
wenzelm@50207
   253
  val seconds = new Seconds_Access
wenzelm@50207
   254
wenzelm@48370
   255
wenzelm@48807
   256
  /* external updates */
wenzelm@48370
   257
wenzelm@48370
   258
  private def check_value(name: String): Options =
wenzelm@48370
   259
  {
wenzelm@48370
   260
    val opt = check_name(name)
wenzelm@48370
   261
    opt.typ match {
wenzelm@48370
   262
      case Options.Bool => bool(name); this
wenzelm@48370
   263
      case Options.Int => int(name); this
wenzelm@48370
   264
      case Options.Real => real(name); this
wenzelm@48370
   265
      case Options.String => string(name); this
wenzelm@48860
   266
      case Options.Unknown => this
wenzelm@48370
   267
    }
wenzelm@48370
   268
  }
wenzelm@48370
   269
wenzelm@56465
   270
  def declare(
wenzelm@56465
   271
    public: Boolean,
wenzelm@56465
   272
    pos: Position.T,
wenzelm@56465
   273
    name: String,
wenzelm@56465
   274
    typ_name: String,
wenzelm@56465
   275
    value: String,
wenzelm@56465
   276
    description: String): Options =
wenzelm@48370
   277
  {
wenzelm@48370
   278
    options.get(name) match {
wenzelm@56465
   279
      case Some(other) =>
wenzelm@56465
   280
        error("Duplicate declaration of option " + quote(name) + Position.here(pos) +
wenzelm@56465
   281
          Position.here(other.pos))
wenzelm@48370
   282
      case None =>
wenzelm@48370
   283
        val typ =
wenzelm@48370
   284
          typ_name match {
wenzelm@48370
   285
            case "bool" => Options.Bool
wenzelm@48370
   286
            case "int" => Options.Int
wenzelm@48370
   287
            case "real" => Options.Real
wenzelm@48370
   288
            case "string" => Options.String
wenzelm@56465
   289
            case _ =>
wenzelm@56465
   290
              error("Unknown type for option " + quote(name) + " : " + quote(typ_name) +
wenzelm@56465
   291
                Position.here(pos))
wenzelm@48370
   292
          }
wenzelm@56465
   293
        val opt = Options.Opt(public, pos, name, typ, value, value, description, section)
wenzelm@49270
   294
        (new Options(options + (name -> opt), section)).check_value(name)
wenzelm@48370
   295
    }
wenzelm@48370
   296
  }
wenzelm@48370
   297
wenzelm@48860
   298
  def add_permissive(name: String, value: String): Options =
wenzelm@48860
   299
  {
wenzelm@48860
   300
    if (options.isDefinedAt(name)) this + (name, value)
wenzelm@56465
   301
    else {
wenzelm@56465
   302
      val opt = Options.Opt(false, Position.none, name, Options.Unknown, value, value, "", "")
wenzelm@56465
   303
      new Options(options + (name -> opt), section)
wenzelm@56465
   304
    }
wenzelm@48860
   305
  }
wenzelm@48860
   306
wenzelm@48807
   307
  def + (name: String, value: String): Options =
wenzelm@48370
   308
  {
wenzelm@48370
   309
    val opt = check_name(name)
wenzelm@49270
   310
    (new Options(options + (name -> opt.copy(value = value)), section)).check_value(name)
wenzelm@48370
   311
  }
wenzelm@48370
   312
wenzelm@48807
   313
  def + (name: String, opt_value: Option[String]): Options =
wenzelm@48370
   314
  {
wenzelm@48370
   315
    val opt = check_name(name)
wenzelm@48370
   316
    opt_value match {
wenzelm@48807
   317
      case Some(value) => this + (name, value)
wenzelm@48807
   318
      case None if opt.typ == Options.Bool => this + (name, "true")
wenzelm@48370
   319
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   320
    }
wenzelm@48370
   321
  }
wenzelm@48370
   322
wenzelm@48807
   323
  def + (str: String): Options =
wenzelm@48370
   324
  {
wenzelm@48370
   325
    str.indexOf('=') match {
wenzelm@48807
   326
      case -1 => this + (str, None)
wenzelm@48807
   327
      case i => this + (str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   328
    }
wenzelm@48370
   329
  }
wenzelm@48456
   330
wenzelm@48807
   331
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48807
   332
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
wenzelm@48807
   333
wenzelm@48456
   334
wenzelm@49270
   335
  /* sections */
wenzelm@49270
   336
wenzelm@49270
   337
  def set_section(new_section: String): Options =
wenzelm@49270
   338
    new Options(options, new_section)
wenzelm@49270
   339
wenzelm@49270
   340
  def sections: List[(String, List[Options.Opt])] =
wenzelm@49270
   341
    options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) })
wenzelm@49270
   342
wenzelm@49270
   343
wenzelm@48456
   344
  /* encode */
wenzelm@48456
   345
wenzelm@48456
   346
  def encode: XML.Body =
wenzelm@48456
   347
  {
wenzelm@48860
   348
    val opts =
wenzelm@56465
   349
      for ((_, opt) <- options.toList; if !opt.unknown)
wenzelm@56465
   350
        yield (opt.pos, (opt.name, (opt.typ.print, opt.value)))
wenzelm@48860
   351
wenzelm@56465
   352
    import XML.Encode.{string => string_, _}
wenzelm@56465
   353
    list(pair(properties, pair(string_, pair(string_, string_))))(opts)
wenzelm@48456
   354
  }
wenzelm@48807
   355
wenzelm@48807
   356
wenzelm@48807
   357
  /* user preferences */
wenzelm@48807
   358
wenzelm@48807
   359
  def load_prefs(): Options =
wenzelm@48807
   360
    if (Options.PREFS.is_file)
wenzelm@48807
   361
      Options.Parser.parse_file(
wenzelm@48807
   362
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
wenzelm@48807
   363
    else this
wenzelm@48807
   364
wenzelm@48807
   365
  def save_prefs()
wenzelm@48807
   366
  {
wenzelm@48860
   367
    val defaults = Options.init_defaults()
wenzelm@48807
   368
    val changed =
wenzelm@48807
   369
      (for {
wenzelm@48860
   370
        (name, opt2) <- options.iterator
wenzelm@48860
   371
        opt1 = defaults.options.get(name)
wenzelm@48860
   372
        if (opt1.isEmpty || opt1.get.value != opt2.value)
wenzelm@48860
   373
      } yield (name, opt2.value, if (opt1.isEmpty) "  (* unknown *)" else "")).toList
wenzelm@48860
   374
wenzelm@48807
   375
    val prefs =
wenzelm@48807
   376
      changed.sortBy(_._1)
wenzelm@48860
   377
        .map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString
wenzelm@48807
   378
wenzelm@50893
   379
    Isabelle_System.mkdirs(Options.PREFS_DIR)
wenzelm@53336
   380
    File.write_backup(Options.PREFS,
wenzelm@48807
   381
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
wenzelm@48807
   382
  }
wenzelm@48365
   383
}
wenzelm@49245
   384
wenzelm@49245
   385
wenzelm@49245
   386
class Options_Variable
wenzelm@49245
   387
{
wenzelm@51620
   388
  private var options = Options.empty
wenzelm@49245
   389
wenzelm@51620
   390
  def value: Options = synchronized { options }
wenzelm@51620
   391
  def update(new_options: Options): Unit = synchronized { options = new_options }
wenzelm@49245
   392
wenzelm@51620
   393
  def + (name: String, x: String): Unit = synchronized { options = options + (name, x) }
wenzelm@49245
   394
wenzelm@49954
   395
  class Bool_Access
wenzelm@49245
   396
  {
wenzelm@51620
   397
    def apply(name: String): Boolean = synchronized { options.bool(name) }
wenzelm@51620
   398
    def update(name: String, x: Boolean): Unit =
wenzelm@51620
   399
      synchronized { options = options.bool.update(name, x) }
wenzelm@49245
   400
  }
wenzelm@49954
   401
  val bool = new Bool_Access
wenzelm@49245
   402
wenzelm@49954
   403
  class Int_Access
wenzelm@49245
   404
  {
wenzelm@51620
   405
    def apply(name: String): Int = synchronized { options.int(name) }
wenzelm@51620
   406
    def update(name: String, x: Int): Unit =
wenzelm@51620
   407
      synchronized { options = options.int.update(name, x) }
wenzelm@49245
   408
  }
wenzelm@49954
   409
  val int = new Int_Access
wenzelm@49245
   410
wenzelm@49954
   411
  class Real_Access
wenzelm@49245
   412
  {
wenzelm@51620
   413
    def apply(name: String): Double = synchronized { options.real(name) }
wenzelm@51620
   414
    def update(name: String, x: Double): Unit =
wenzelm@51620
   415
      synchronized { options = options.real.update(name, x) }
wenzelm@49245
   416
  }
wenzelm@49954
   417
  val real = new Real_Access
wenzelm@49245
   418
wenzelm@49954
   419
  class String_Access
wenzelm@49245
   420
  {
wenzelm@51620
   421
    def apply(name: String): String = synchronized { options.string(name) }
wenzelm@51620
   422
    def update(name: String, x: String): Unit =
wenzelm@51620
   423
      synchronized { options = options.string.update(name, x) }
wenzelm@49245
   424
  }
wenzelm@49954
   425
  val string = new String_Access
wenzelm@50207
   426
wenzelm@50207
   427
  class Seconds_Access
wenzelm@50207
   428
  {
wenzelm@51620
   429
    def apply(name: String): Time = synchronized { options.seconds(name) }
wenzelm@50207
   430
  }
wenzelm@50207
   431
  val seconds = new Seconds_Access
wenzelm@49245
   432
}
wenzelm@49245
   433