src/Pure/System/options.scala
author wenzelm
Tue, 14 Aug 2012 20:50:50 +0200
changeset 48807 fde8c3d84ff5
parent 48795 bece259ee055
child 48860 56ec76f769c0
permissions -rw-r--r--
some support for persistent user preferences;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/options.scala
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     3
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     4
Stand-alone options with external string representation.
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     5
*/
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     6
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     7
package isabelle
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     8
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     9
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    10
import java.util.Calendar
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    11
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    12
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    13
object Options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    14
{
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    15
  type Spec = (String, Option[String])
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    16
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    17
  val empty: Options = new Options()
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    18
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    19
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    20
  /* representation */
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    21
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    22
  sealed abstract class Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    23
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    24
    def print: String = toString.toLowerCase
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    25
  }
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    26
  private case object Bool extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    27
  private case object Int extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    28
  private case object Real extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    29
  private case object String extends Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    30
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    31
  case class Opt(typ: Type, value: String, description: String)
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    32
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    33
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    34
  /* parsing */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    35
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    36
  private val OPTION = "option"
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    37
  private val OPTIONS = Path.explode("etc/options")
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    38
  private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences")
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    39
  private val PREFS_BACKUP = Path.explode("$ISABELLE_HOME_USER/etc/preferences~")
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    40
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    41
  lazy val options_syntax = Outer_Syntax.init() + ":" + "=" + "--" + (OPTION, Keyword.THY_DECL)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    42
  lazy val prefs_syntax = Outer_Syntax.init() + "="
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    43
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    44
  object Parser extends Parse.Parser
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    45
  {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    46
    val option_name = atom("option name", _.is_xname)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    47
    val option_type = atom("option type", _.is_ident)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    48
    val option_value =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    49
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    50
        { case s ~ n => if (s.isDefined) "-" + n else n } |
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    51
      atom("option value", tok => tok.is_name || tok.is_float)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    52
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    53
    val option_entry: Parser[Options => Options] =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    54
    {
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    55
      command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
48580
9df76dd45900 some description of main build options;
wenzelm
parents: 48548
diff changeset
    56
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    57
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    58
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    59
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    60
    val prefs_entry: Parser[Options => Options] =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    61
    {
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    62
      option_name ~ (keyword("=") ~! option_value) ^^
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    63
      { case a ~ (_ ~ b) => (options: Options) => options + (a, b) }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    64
    }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    65
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    66
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    67
      options: Options, file: Path): Options =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    68
    {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    69
      val toks = syntax.scan(File.read(file))
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    70
      val ops =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    71
        parse_all(rep(parser), Token.reader(toks, file.implode)) match {
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    72
          case Success(result, _) => result
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    73
          case bad => error(bad.toString)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    74
        }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    75
      try { (options /: ops) { case (opts, op) => op(opts) } }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    76
      catch { case ERROR(msg) => error(msg + Position.str_of(file.position)) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    77
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    78
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    79
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    80
  def init_defaults(): Options =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    81
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    82
    var options = empty
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    83
    for {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    84
      dir <- Isabelle_System.components()
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
    85
      file = dir + OPTIONS if file.is_file
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    86
    } { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    87
    options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    88
  }
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    89
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    90
  def init(): Options = init_defaults().load_prefs()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    91
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    92
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    93
  /* encode */
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    94
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    95
  val encode: XML.Encode.T[Options] = (options => options.encode)
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    96
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    97
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    98
  /* command line entry point */
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    99
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   100
  def main(args: Array[String])
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   101
  {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   102
    Command_Line.tool {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   103
      args.toList match {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   104
        case export_file :: more_options =>
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   105
          val options = (Options.init() /: more_options)(_ + _)
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   106
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   107
          if (export_file == "") java.lang.System.out.println(options.print)
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   108
          else File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   109
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   110
          0
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   111
        case _ => error("Bad arguments:\n" + cat_lines(args))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   112
      }
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   113
    }
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   114
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   115
}
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   116
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   117
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   118
final class Options private(protected val options: Map[String, Options.Opt] = Map.empty)
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   119
{
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   120
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   121
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   122
  def print: String =
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   123
    cat_lines(options.toList.sortBy(_._1).map({ case (name, opt) =>
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
   124
      "option " + name + " : " + opt.typ.print + " = " +
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   125
        (if (opt.typ == Options.String) quote(opt.value) else opt.value) +
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   126
      "\n  -- " + quote(opt.description) }))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   127
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   128
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   129
  /* check */
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   130
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   131
  private def check_name(name: String): Options.Opt =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   132
    options.get(name) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   133
      case Some(opt) => opt
48368
dc538eef2cf2 define build_options from command line;
wenzelm
parents: 48365
diff changeset
   134
      case None => error("Unknown option " + quote(name))
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   135
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   136
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   137
  private def check_type(name: String, typ: Options.Type): Options.Opt =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   138
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   139
    val opt = check_name(name)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   140
    if (opt.typ == typ) opt
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   141
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   142
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   143
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   144
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   145
  /* basic operations */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   146
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   147
  private def put[A](name: String, typ: Options.Type, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   148
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   149
    val opt = check_type(name, typ)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   150
    new Options(options + (name -> opt.copy(value = value)))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   151
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   152
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   153
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   154
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   155
    val opt = check_type(name, typ)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   156
    parse(opt.value) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   157
      case Some(x) => x
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   158
      case None =>
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   159
        error("Malformed value for option " + quote(name) +
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   160
          " : " + typ.print + " =\n" + quote(opt.value))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   161
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   162
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   163
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   164
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   165
  /* internal lookup and update */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   166
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   167
  val bool = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   168
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   169
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   170
    def update(name: String, x: Boolean): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   171
      put(name, Options.Bool, Properties.Value.Boolean(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   172
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   173
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   174
  val int = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   175
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   176
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   177
    def update(name: String, x: Int): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   178
      put(name, Options.Int, Properties.Value.Int(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   179
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   180
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   181
  val real = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   182
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   183
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   184
    def update(name: String, x: Double): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   185
      put(name, Options.Real, Properties.Value.Double(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   186
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   187
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   188
  val string = new Object
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   189
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   190
    def apply(name: String): String = get(name, Options.String, s => Some(s))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   191
    def update(name: String, x: String): Options = put(name, Options.String, x)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   192
  }
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   193
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   194
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   195
  /* external updates */
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   196
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   197
  private def check_value(name: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   198
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   199
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   200
    opt.typ match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   201
      case Options.Bool => bool(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   202
      case Options.Int => int(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   203
      case Options.Real => real(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   204
      case Options.String => string(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   205
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   206
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   207
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   208
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   209
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   210
    options.get(name) match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   211
      case Some(_) => error("Duplicate declaration of option " + quote(name))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   212
      case None =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   213
        val typ =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   214
          typ_name match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   215
            case "bool" => Options.Bool
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   216
            case "int" => Options.Int
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   217
            case "real" => Options.Real
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   218
            case "string" => Options.String
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   219
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   220
          }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   221
        val opt = Options.Opt(typ, value, description)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   222
        (new Options(options + (name -> opt))).check_value(name)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   223
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   224
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   225
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   226
  def + (name: String, value: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   227
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   228
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   229
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   230
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   231
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   232
  def + (name: String, opt_value: Option[String]): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   233
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   234
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   235
    opt_value match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   236
      case Some(value) => this + (name, value)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   237
      case None if opt.typ == Options.Bool => this + (name, "true")
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   238
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   239
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   240
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   241
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   242
  def + (str: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   243
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   244
    str.indexOf('=') match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   245
      case -1 => this + (str, None)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   246
      case i => this + (str.substring(0, i), str.substring(i + 1))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   247
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   248
  }
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   249
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   250
  def ++ (specs: List[Options.Spec]): Options =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   251
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   252
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   253
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   254
  /* encode */
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   255
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   256
  def encode: XML.Body =
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   257
  {
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   258
    import XML.Encode.{string => str, _}
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   259
    list(triple(str, str, str))(
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   260
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   261
  }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   262
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   263
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   264
  /* user preferences */
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   265
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   266
  def load_prefs(): Options =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   267
    if (Options.PREFS.is_file)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   268
      Options.Parser.parse_file(
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   269
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   270
    else this
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   271
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   272
  def save_prefs()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   273
  {
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   274
    val current_defaults = Options.init_defaults()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   275
    val changed =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   276
      (for {
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   277
        (name, opt1) <- current_defaults.options.iterator
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   278
        opt2 <- options.get(name)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   279
        if (opt1.value != opt2.value)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   280
      } yield (name, opt2.value)).toList
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   281
    val prefs =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   282
      changed.sortBy(_._1)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   283
        .map({ case (x, y) => x + " = " + Outer_Syntax.quote_string(y) + "\n" }).mkString
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   284
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   285
    Options.PREFS.file renameTo Options.PREFS_BACKUP.file
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   286
    File.write(Options.PREFS,
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   287
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   288
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   289
}