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