src/Pure/System/options.scala
author wenzelm
Wed, 08 Aug 2012 12:33:40 +0200
changeset 48731 a45ba78abcc1
parent 48718 73e6c22e2d94
child 48795 bece259ee055
permissions -rw-r--r--
more casual exit back to ML toplevel, to accomodate commit in SML/NJ which continues at the saved point;
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
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    10
object Options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    11
{
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    12
  type Spec = (String, Option[String])
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    13
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    14
  val empty: Options = new Options()
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    15
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    16
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    17
  /* representation */
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    18
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    19
  sealed abstract class Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    20
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    21
    def print: String = toString.toLowerCase
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    22
  }
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    23
  private case object Bool extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    24
  private case object Int extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    25
  private case object Real extends Type
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    26
  private case object String extends Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    27
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    28
  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
    29
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    30
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    31
  /* parsing */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    32
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    33
  private val DECLARE = "declare"
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    34
  private val DEFINE = "define"
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    35
48718
73e6c22e2d94 more structural parsing for minor modes;
wenzelm
parents: 48713
diff changeset
    36
  lazy val options_syntax =
73e6c22e2d94 more structural parsing for minor modes;
wenzelm
parents: 48713
diff changeset
    37
    Outer_Syntax.init() + ":" + "=" + "--" +
73e6c22e2d94 more structural parsing for minor modes;
wenzelm
parents: 48713
diff changeset
    38
      (DECLARE, Keyword.THY_DECL) + (DEFINE, Keyword.PRF_DECL)
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    39
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    40
  private object Parser extends Parse.Parser
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    41
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    42
    val entry: Parser[Options => Options] =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    43
    {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    44
      val option_name = atom("option name", _.is_xname)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    45
      val option_type = atom("option type", _.is_ident)
48605
e777363440d6 allow negative int values as well, according to real = int | float;
wenzelm
parents: 48580
diff changeset
    46
e777363440d6 allow negative int values as well, according to real = int | float;
wenzelm
parents: 48580
diff changeset
    47
      val option_value =
e777363440d6 allow negative int values as well, according to real = int | float;
wenzelm
parents: 48580
diff changeset
    48
        opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
e777363440d6 allow negative int values as well, according to real = int | float;
wenzelm
parents: 48580
diff changeset
    49
          { case s ~ n => if (s.isDefined) "-" + n else n } |
e777363440d6 allow negative int values as well, according to real = int | float;
wenzelm
parents: 48580
diff changeset
    50
        atom("option value", tok => tok.is_name || tok.is_float)
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    51
48718
73e6c22e2d94 more structural parsing for minor modes;
wenzelm
parents: 48713
diff changeset
    52
      command(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
48580
9df76dd45900 some description of main build options;
wenzelm
parents: 48548
diff changeset
    53
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
9df76dd45900 some description of main build options;
wenzelm
parents: 48548
diff changeset
    54
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) } |
48718
73e6c22e2d94 more structural parsing for minor modes;
wenzelm
parents: 48713
diff changeset
    55
      command(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
    56
        { case _ ~ (a ~ _ ~ b) => (options: Options) => options.define(a, b) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    57
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    58
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
    59
    def parse_entries(file: Path): List[Options => Options] =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    60
    {
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    61
      val toks = options_syntax.scan(File.read(file))
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    62
      parse_all(rep(entry), Token.reader(toks, file.implode)) match {
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    63
        case Success(result, _) => result
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    64
        case bad => error(bad.toString)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    65
      }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    66
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    67
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    68
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    69
  private val OPTIONS = Path.explode("etc/options")
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    70
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    71
  def init(): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    72
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    73
    var options = empty
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    74
    for {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    75
      dir <- Isabelle_System.components()
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
    76
      file = dir + OPTIONS if file.is_file
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    77
      entry <- Parser.parse_entries(file)
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
      try { options = entry(options) }
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
    80
      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
    81
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    82
    options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    83
  }
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    84
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    85
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    86
  /* encode */
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    87
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
    88
  val encode: XML.Encode.T[Options] = (options => options.encode)
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    89
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    90
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    91
  /* command line entry point */
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    92
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    93
  def main(args: Array[String])
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    94
  {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    95
    Command_Line.tool {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    96
      args.toList match {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    97
        case export_file :: more_options =>
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
    98
          val options = (Options.init() /: more_options)(_.define_simple(_))
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
          if (export_file == "") java.lang.System.out.println(options.print)
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   101
          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
   102
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   103
          0
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   104
        case _ => error("Bad arguments:\n" + cat_lines(args))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   105
      }
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
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   108
}
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   109
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   110
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   111
final class Options private(options: Map[String, Options.Opt] = Map.empty)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   112
{
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   113
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   114
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   115
  def print: String =
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   116
    cat_lines(options.toList.sortBy(_._1).map({ case (name, opt) =>
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   117
      name + " : " + opt.typ.print + " = " +
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   118
        (if (opt.typ == Options.String) quote(opt.value) else opt.value) +
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   119
      "\n  -- " + quote(opt.description) }))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   120
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   121
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   122
  /* check */
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   123
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   124
  private def check_name(name: String): Options.Opt =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   125
    options.get(name) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   126
      case Some(opt) => opt
48368
dc538eef2cf2 define build_options from command line;
wenzelm
parents: 48365
diff changeset
   127
      case None => error("Unknown option " + quote(name))
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   128
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   129
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   130
  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
   131
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   132
    val opt = check_name(name)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   133
    if (opt.typ == typ) opt
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   134
    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
   135
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   136
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   137
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   138
  /* basic operations */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   139
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   140
  private def put[A](name: String, typ: Options.Type, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   141
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   142
    val opt = check_type(name, typ)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   143
    new Options(options + (name -> opt.copy(value = value)))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   144
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   145
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   146
  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
   147
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   148
    val opt = check_type(name, typ)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   149
    parse(opt.value) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   150
      case Some(x) => x
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   151
      case None =>
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   152
        error("Malformed value for option " + quote(name) +
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   153
          " : " + typ.print + " =\n" + quote(opt.value))
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
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   156
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   157
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   158
  /* internal lookup and update */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   159
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   160
  val bool = new Object
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
    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
   163
    def update(name: String, x: Boolean): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   164
      put(name, Options.Bool, Properties.Value.Boolean(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   165
  }
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 int = 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): Int = get(name, Options.Int, Properties.Value.Int.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   170
    def update(name: String, x: Int): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   171
      put(name, Options.Int, Properties.Value.Int(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 real = 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): Double = get(name, Options.Real, Properties.Value.Double.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   177
    def update(name: String, x: Double): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   178
      put(name, Options.Real, Properties.Value.Double(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 string = 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): String = get(name, Options.String, s => Some(s))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   184
    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
   185
  }
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   186
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   187
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   188
  /* external declare and define */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   189
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   190
  private def check_value(name: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   191
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   192
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   193
    opt.typ match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   194
      case Options.Bool => bool(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   195
      case Options.Int => int(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   196
      case Options.Real => real(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   197
      case Options.String => string(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   198
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   199
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   200
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   201
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   202
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   203
    options.get(name) match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   204
      case Some(_) => error("Duplicate declaration of option " + quote(name))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   205
      case None =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   206
        val typ =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   207
          typ_name match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   208
            case "bool" => Options.Bool
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   209
            case "int" => Options.Int
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   210
            case "real" => Options.Real
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   211
            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
   212
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   213
          }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   214
        (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   215
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   216
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   217
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   218
  def define(name: String, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   219
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   220
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   221
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   222
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   223
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   224
  def define(name: String, opt_value: Option[String]): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   225
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   226
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   227
    opt_value match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   228
      case Some(value) => define(name, value)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   229
      case None if opt.typ == Options.Bool => define(name, "true")
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   230
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   231
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   232
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   233
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
   234
  def ++ (specs: List[Options.Spec]): Options =
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
   235
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
   236
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   237
  def define_simple(str: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   238
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   239
    str.indexOf('=') match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   240
      case -1 => define(str, None)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   241
      case i => define(str.substring(0, i), str.substring(i + 1))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   242
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   243
  }
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   244
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   245
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   246
  /* encode */
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   247
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   248
  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
   249
  {
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   250
    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
   251
    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
   252
      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
   253
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   254
}