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