src/Pure/System/options.scala
author wenzelm
Sun Aug 19 19:31:45 2012 +0200 (2012-08-19)
changeset 48860 56ec76f769c0
parent 48807 fde8c3d84ff5
child 48992 0518bf89c777
permissions -rw-r--r--
retain unknown options within preferences;
tuned print;
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@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@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@48860
    30
  private case object Unknown extends Type
wenzelm@48365
    31
wenzelm@48370
    32
  case class Opt(typ: Type, value: String, description: String)
wenzelm@48860
    33
  {
wenzelm@48860
    34
    def unknown: Boolean = typ == Unknown
wenzelm@48860
    35
  }
wenzelm@48365
    36
wenzelm@48365
    37
wenzelm@48365
    38
  /* parsing */
wenzelm@48365
    39
wenzelm@48795
    40
  private val OPTION = "option"
wenzelm@48807
    41
  private val OPTIONS = Path.explode("etc/options")
wenzelm@48807
    42
  private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences")
wenzelm@48807
    43
  private val PREFS_BACKUP = Path.explode("$ISABELLE_HOME_USER/etc/preferences~")
wenzelm@48713
    44
wenzelm@48807
    45
  lazy val options_syntax = Outer_Syntax.init() + ":" + "=" + "--" + (OPTION, Keyword.THY_DECL)
wenzelm@48807
    46
  lazy val prefs_syntax = Outer_Syntax.init() + "="
wenzelm@48713
    47
wenzelm@48807
    48
  object Parser extends Parse.Parser
wenzelm@48365
    49
  {
wenzelm@48807
    50
    val option_name = atom("option name", _.is_xname)
wenzelm@48807
    51
    val option_type = atom("option type", _.is_ident)
wenzelm@48807
    52
    val option_value =
wenzelm@48807
    53
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
wenzelm@48807
    54
        { case s ~ n => if (s.isDefined) "-" + n else n } |
wenzelm@48807
    55
      atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48807
    56
wenzelm@48807
    57
    val option_entry: Parser[Options => Options] =
wenzelm@48365
    58
    {
wenzelm@48795
    59
      command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48580
    60
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
wenzelm@48795
    61
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
wenzelm@48365
    62
    }
wenzelm@48365
    63
wenzelm@48807
    64
    val prefs_entry: Parser[Options => Options] =
wenzelm@48807
    65
    {
wenzelm@48807
    66
      option_name ~ (keyword("=") ~! option_value) ^^
wenzelm@48860
    67
      { case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
wenzelm@48807
    68
    }
wenzelm@48807
    69
wenzelm@48807
    70
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
wenzelm@48807
    71
      options: Options, file: Path): Options =
wenzelm@48365
    72
    {
wenzelm@48807
    73
      val toks = syntax.scan(File.read(file))
wenzelm@48807
    74
      val ops =
wenzelm@48807
    75
        parse_all(rep(parser), Token.reader(toks, file.implode)) match {
wenzelm@48807
    76
          case Success(result, _) => result
wenzelm@48807
    77
          case bad => error(bad.toString)
wenzelm@48807
    78
        }
wenzelm@48807
    79
      try { (options /: ops) { case (opts, op) => op(opts) } }
wenzelm@48807
    80
      catch { case ERROR(msg) => error(msg + Position.str_of(file.position)) }
wenzelm@48365
    81
    }
wenzelm@48365
    82
  }
wenzelm@48365
    83
wenzelm@48807
    84
  def init_defaults(): Options =
wenzelm@48365
    85
  {
wenzelm@48365
    86
    var options = empty
wenzelm@48365
    87
    for {
wenzelm@48365
    88
      dir <- Isabelle_System.components()
wenzelm@48548
    89
      file = dir + OPTIONS if file.is_file
wenzelm@48807
    90
    } { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
wenzelm@48365
    91
    options
wenzelm@48365
    92
  }
wenzelm@48457
    93
wenzelm@48807
    94
  def init(): Options = init_defaults().load_prefs()
wenzelm@48807
    95
wenzelm@48457
    96
wenzelm@48457
    97
  /* encode */
wenzelm@48457
    98
wenzelm@48457
    99
  val encode: XML.Encode.T[Options] = (options => options.encode)
wenzelm@48693
   100
wenzelm@48693
   101
wenzelm@48693
   102
  /* command line entry point */
wenzelm@48693
   103
wenzelm@48693
   104
  def main(args: Array[String])
wenzelm@48693
   105
  {
wenzelm@48693
   106
    Command_Line.tool {
wenzelm@48693
   107
      args.toList match {
wenzelm@48693
   108
        case export_file :: more_options =>
wenzelm@48807
   109
          val options = (Options.init() /: more_options)(_ + _)
wenzelm@48693
   110
wenzelm@48693
   111
          if (export_file == "") java.lang.System.out.println(options.print)
wenzelm@48693
   112
          else File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
wenzelm@48693
   113
wenzelm@48693
   114
          0
wenzelm@48693
   115
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48693
   116
      }
wenzelm@48693
   117
    }
wenzelm@48693
   118
  }
wenzelm@48365
   119
}
wenzelm@48365
   120
wenzelm@48365
   121
wenzelm@48807
   122
final class Options private(protected val options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
   123
{
wenzelm@48365
   124
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
   125
wenzelm@48693
   126
  def print: String =
wenzelm@48693
   127
    cat_lines(options.toList.sortBy(_._1).map({ case (name, opt) =>
wenzelm@48795
   128
      "option " + name + " : " + opt.typ.print + " = " +
wenzelm@48693
   129
        (if (opt.typ == Options.String) quote(opt.value) else opt.value) +
wenzelm@48860
   130
      (if (opt.description == "") "" else "\n  -- " + quote(opt.description)) }))
wenzelm@48693
   131
wenzelm@48365
   132
wenzelm@48370
   133
  /* check */
wenzelm@48365
   134
wenzelm@48365
   135
  private def check_name(name: String): Options.Opt =
wenzelm@48365
   136
    options.get(name) match {
wenzelm@48860
   137
      case Some(opt) if !opt.unknown => opt
wenzelm@48860
   138
      case _ => error("Unknown option " + quote(name))
wenzelm@48365
   139
    }
wenzelm@48365
   140
wenzelm@48365
   141
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   142
  {
wenzelm@48365
   143
    val opt = check_name(name)
wenzelm@48365
   144
    if (opt.typ == typ) opt
wenzelm@48365
   145
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   146
  }
wenzelm@48365
   147
wenzelm@48370
   148
wenzelm@48370
   149
  /* basic operations */
wenzelm@48370
   150
wenzelm@48370
   151
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   152
  {
wenzelm@48370
   153
    val opt = check_type(name, typ)
wenzelm@48370
   154
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   155
  }
wenzelm@48370
   156
wenzelm@48365
   157
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   158
  {
wenzelm@48365
   159
    val opt = check_type(name, typ)
wenzelm@48365
   160
    parse(opt.value) match {
wenzelm@48365
   161
      case Some(x) => x
wenzelm@48365
   162
      case None =>
wenzelm@48365
   163
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   164
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   165
    }
wenzelm@48365
   166
  }
wenzelm@48365
   167
wenzelm@48365
   168
wenzelm@48365
   169
  /* internal lookup and update */
wenzelm@48365
   170
wenzelm@48365
   171
  val bool = new Object
wenzelm@48365
   172
  {
wenzelm@48365
   173
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   174
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   175
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   176
  }
wenzelm@48365
   177
wenzelm@48365
   178
  val int = new Object
wenzelm@48365
   179
  {
wenzelm@48365
   180
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   181
    def update(name: String, x: Int): Options =
wenzelm@48365
   182
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   183
  }
wenzelm@48365
   184
wenzelm@48365
   185
  val real = new Object
wenzelm@48365
   186
  {
wenzelm@48365
   187
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   188
    def update(name: String, x: Double): Options =
wenzelm@48365
   189
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   190
  }
wenzelm@48365
   191
wenzelm@48365
   192
  val string = new Object
wenzelm@48365
   193
  {
wenzelm@48365
   194
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   195
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   196
  }
wenzelm@48370
   197
wenzelm@48370
   198
wenzelm@48807
   199
  /* external updates */
wenzelm@48370
   200
wenzelm@48370
   201
  private def check_value(name: String): Options =
wenzelm@48370
   202
  {
wenzelm@48370
   203
    val opt = check_name(name)
wenzelm@48370
   204
    opt.typ match {
wenzelm@48370
   205
      case Options.Bool => bool(name); this
wenzelm@48370
   206
      case Options.Int => int(name); this
wenzelm@48370
   207
      case Options.Real => real(name); this
wenzelm@48370
   208
      case Options.String => string(name); this
wenzelm@48860
   209
      case Options.Unknown => this
wenzelm@48370
   210
    }
wenzelm@48370
   211
  }
wenzelm@48370
   212
wenzelm@48370
   213
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   214
  {
wenzelm@48370
   215
    options.get(name) match {
wenzelm@48370
   216
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   217
      case None =>
wenzelm@48370
   218
        val typ =
wenzelm@48370
   219
          typ_name match {
wenzelm@48370
   220
            case "bool" => Options.Bool
wenzelm@48370
   221
            case "int" => Options.Int
wenzelm@48370
   222
            case "real" => Options.Real
wenzelm@48370
   223
            case "string" => Options.String
wenzelm@48456
   224
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   225
          }
wenzelm@48807
   226
        val opt = Options.Opt(typ, value, description)
wenzelm@48807
   227
        (new Options(options + (name -> opt))).check_value(name)
wenzelm@48370
   228
    }
wenzelm@48370
   229
  }
wenzelm@48370
   230
wenzelm@48860
   231
  def add_permissive(name: String, value: String): Options =
wenzelm@48860
   232
  {
wenzelm@48860
   233
    if (options.isDefinedAt(name)) this + (name, value)
wenzelm@48860
   234
    else new Options(options + (name -> Options.Opt(Options.Unknown, value, "")))
wenzelm@48860
   235
  }
wenzelm@48860
   236
wenzelm@48807
   237
  def + (name: String, value: String): Options =
wenzelm@48370
   238
  {
wenzelm@48370
   239
    val opt = check_name(name)
wenzelm@48370
   240
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   241
  }
wenzelm@48370
   242
wenzelm@48807
   243
  def + (name: String, opt_value: Option[String]): Options =
wenzelm@48370
   244
  {
wenzelm@48370
   245
    val opt = check_name(name)
wenzelm@48370
   246
    opt_value match {
wenzelm@48807
   247
      case Some(value) => this + (name, value)
wenzelm@48807
   248
      case None if opt.typ == Options.Bool => this + (name, "true")
wenzelm@48370
   249
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   250
    }
wenzelm@48370
   251
  }
wenzelm@48370
   252
wenzelm@48807
   253
  def + (str: String): Options =
wenzelm@48370
   254
  {
wenzelm@48370
   255
    str.indexOf('=') match {
wenzelm@48807
   256
      case -1 => this + (str, None)
wenzelm@48807
   257
      case i => this + (str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   258
    }
wenzelm@48370
   259
  }
wenzelm@48456
   260
wenzelm@48807
   261
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48807
   262
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
wenzelm@48807
   263
wenzelm@48456
   264
wenzelm@48456
   265
  /* encode */
wenzelm@48456
   266
wenzelm@48456
   267
  def encode: XML.Body =
wenzelm@48456
   268
  {
wenzelm@48860
   269
    val opts =
wenzelm@48860
   270
      for ((name, opt) <- options.toList; if !opt.unknown)
wenzelm@48860
   271
        yield (name, opt.typ.print, opt.value)
wenzelm@48860
   272
wenzelm@48456
   273
    import XML.Encode.{string => str, _}
wenzelm@48860
   274
    list(triple(str, str, str))(opts)
wenzelm@48456
   275
  }
wenzelm@48807
   276
wenzelm@48807
   277
wenzelm@48807
   278
  /* user preferences */
wenzelm@48807
   279
wenzelm@48807
   280
  def load_prefs(): Options =
wenzelm@48807
   281
    if (Options.PREFS.is_file)
wenzelm@48807
   282
      Options.Parser.parse_file(
wenzelm@48807
   283
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
wenzelm@48807
   284
    else this
wenzelm@48807
   285
wenzelm@48807
   286
  def save_prefs()
wenzelm@48807
   287
  {
wenzelm@48860
   288
    val defaults = Options.init_defaults()
wenzelm@48807
   289
    val changed =
wenzelm@48807
   290
      (for {
wenzelm@48860
   291
        (name, opt2) <- options.iterator
wenzelm@48860
   292
        opt1 = defaults.options.get(name)
wenzelm@48860
   293
        if (opt1.isEmpty || opt1.get.value != opt2.value)
wenzelm@48860
   294
      } yield (name, opt2.value, if (opt1.isEmpty) "  (* unknown *)" else "")).toList
wenzelm@48860
   295
wenzelm@48807
   296
    val prefs =
wenzelm@48807
   297
      changed.sortBy(_._1)
wenzelm@48860
   298
        .map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString
wenzelm@48807
   299
wenzelm@48807
   300
    Options.PREFS.file renameTo Options.PREFS_BACKUP.file
wenzelm@48807
   301
    File.write(Options.PREFS,
wenzelm@48807
   302
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
wenzelm@48807
   303
  }
wenzelm@48365
   304
}