src/Pure/System/options.scala
author wenzelm
Tue Aug 14 13:01:09 2012 +0200 (2012-08-14)
changeset 48795 bece259ee055
parent 48718 73e6c22e2d94
child 48807 fde8c3d84ff5
permissions -rw-r--r--
clarified format of etc/options: only declarations, not re-definitions;
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@48365
    10
object Options
wenzelm@48365
    11
{
wenzelm@48421
    12
  type Spec = (String, Option[String])
wenzelm@48421
    13
wenzelm@48421
    14
  val empty: Options = new Options()
wenzelm@48421
    15
wenzelm@48421
    16
wenzelm@48421
    17
  /* representation */
wenzelm@48421
    18
wenzelm@48421
    19
  sealed abstract class Type
wenzelm@48365
    20
  {
wenzelm@48365
    21
    def print: String = toString.toLowerCase
wenzelm@48365
    22
  }
wenzelm@48421
    23
  private case object Bool extends Type
wenzelm@48421
    24
  private case object Int extends Type
wenzelm@48421
    25
  private case object Real extends Type
wenzelm@48421
    26
  private case object String extends Type
wenzelm@48365
    27
wenzelm@48370
    28
  case class Opt(typ: Type, value: String, description: String)
wenzelm@48365
    29
wenzelm@48365
    30
wenzelm@48365
    31
  /* parsing */
wenzelm@48365
    32
wenzelm@48795
    33
  private val OPTION = "option"
wenzelm@48713
    34
wenzelm@48718
    35
  lazy val options_syntax =
wenzelm@48795
    36
    Outer_Syntax.init() + ":" + "=" + "--" + (OPTION, Keyword.THY_DECL)
wenzelm@48713
    37
wenzelm@48365
    38
  private object Parser extends Parse.Parser
wenzelm@48365
    39
  {
wenzelm@48365
    40
    val entry: Parser[Options => Options] =
wenzelm@48365
    41
    {
wenzelm@48365
    42
      val option_name = atom("option name", _.is_xname)
wenzelm@48365
    43
      val option_type = atom("option type", _.is_ident)
wenzelm@48605
    44
wenzelm@48605
    45
      val option_value =
wenzelm@48605
    46
        opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
wenzelm@48605
    47
          { case s ~ n => if (s.isDefined) "-" + n else n } |
wenzelm@48605
    48
        atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48365
    49
wenzelm@48795
    50
      command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48580
    51
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
wenzelm@48795
    52
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
wenzelm@48365
    53
    }
wenzelm@48365
    54
wenzelm@48548
    55
    def parse_entries(file: Path): List[Options => Options] =
wenzelm@48365
    56
    {
wenzelm@48713
    57
      val toks = options_syntax.scan(File.read(file))
wenzelm@48713
    58
      parse_all(rep(entry), Token.reader(toks, file.implode)) match {
wenzelm@48365
    59
        case Success(result, _) => result
wenzelm@48365
    60
        case bad => error(bad.toString)
wenzelm@48365
    61
      }
wenzelm@48365
    62
    }
wenzelm@48365
    63
  }
wenzelm@48365
    64
wenzelm@48421
    65
  private val OPTIONS = Path.explode("etc/options")
wenzelm@48365
    66
wenzelm@48365
    67
  def init(): Options =
wenzelm@48365
    68
  {
wenzelm@48365
    69
    var options = empty
wenzelm@48365
    70
    for {
wenzelm@48365
    71
      dir <- Isabelle_System.components()
wenzelm@48548
    72
      file = dir + OPTIONS if file.is_file
wenzelm@48365
    73
      entry <- Parser.parse_entries(file)
wenzelm@48365
    74
    } {
wenzelm@48365
    75
      try { options = entry(options) }
wenzelm@48548
    76
      catch { case ERROR(msg) => error(msg + Position.str_of(file.position)) }
wenzelm@48365
    77
    }
wenzelm@48365
    78
    options
wenzelm@48365
    79
  }
wenzelm@48457
    80
wenzelm@48457
    81
wenzelm@48457
    82
  /* encode */
wenzelm@48457
    83
wenzelm@48457
    84
  val encode: XML.Encode.T[Options] = (options => options.encode)
wenzelm@48693
    85
wenzelm@48693
    86
wenzelm@48693
    87
  /* command line entry point */
wenzelm@48693
    88
wenzelm@48693
    89
  def main(args: Array[String])
wenzelm@48693
    90
  {
wenzelm@48693
    91
    Command_Line.tool {
wenzelm@48693
    92
      args.toList match {
wenzelm@48693
    93
        case export_file :: more_options =>
wenzelm@48693
    94
          val options = (Options.init() /: more_options)(_.define_simple(_))
wenzelm@48693
    95
wenzelm@48693
    96
          if (export_file == "") java.lang.System.out.println(options.print)
wenzelm@48693
    97
          else File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
wenzelm@48693
    98
wenzelm@48693
    99
          0
wenzelm@48693
   100
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48693
   101
      }
wenzelm@48693
   102
    }
wenzelm@48693
   103
  }
wenzelm@48365
   104
}
wenzelm@48365
   105
wenzelm@48365
   106
wenzelm@48365
   107
final class Options private(options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
   108
{
wenzelm@48365
   109
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
   110
wenzelm@48693
   111
  def print: String =
wenzelm@48693
   112
    cat_lines(options.toList.sortBy(_._1).map({ case (name, opt) =>
wenzelm@48795
   113
      "option " + name + " : " + opt.typ.print + " = " +
wenzelm@48693
   114
        (if (opt.typ == Options.String) quote(opt.value) else opt.value) +
wenzelm@48693
   115
      "\n  -- " + quote(opt.description) }))
wenzelm@48693
   116
wenzelm@48365
   117
wenzelm@48370
   118
  /* check */
wenzelm@48365
   119
wenzelm@48365
   120
  private def check_name(name: String): Options.Opt =
wenzelm@48365
   121
    options.get(name) match {
wenzelm@48365
   122
      case Some(opt) => opt
wenzelm@48368
   123
      case None => error("Unknown option " + quote(name))
wenzelm@48365
   124
    }
wenzelm@48365
   125
wenzelm@48365
   126
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   127
  {
wenzelm@48365
   128
    val opt = check_name(name)
wenzelm@48365
   129
    if (opt.typ == typ) opt
wenzelm@48365
   130
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   131
  }
wenzelm@48365
   132
wenzelm@48370
   133
wenzelm@48370
   134
  /* basic operations */
wenzelm@48370
   135
wenzelm@48370
   136
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   137
  {
wenzelm@48370
   138
    val opt = check_type(name, typ)
wenzelm@48370
   139
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   140
  }
wenzelm@48370
   141
wenzelm@48365
   142
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   143
  {
wenzelm@48365
   144
    val opt = check_type(name, typ)
wenzelm@48365
   145
    parse(opt.value) match {
wenzelm@48365
   146
      case Some(x) => x
wenzelm@48365
   147
      case None =>
wenzelm@48365
   148
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   149
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   150
    }
wenzelm@48365
   151
  }
wenzelm@48365
   152
wenzelm@48365
   153
wenzelm@48365
   154
  /* internal lookup and update */
wenzelm@48365
   155
wenzelm@48365
   156
  val bool = new Object
wenzelm@48365
   157
  {
wenzelm@48365
   158
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   159
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   160
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   161
  }
wenzelm@48365
   162
wenzelm@48365
   163
  val int = new Object
wenzelm@48365
   164
  {
wenzelm@48365
   165
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   166
    def update(name: String, x: Int): Options =
wenzelm@48365
   167
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   168
  }
wenzelm@48365
   169
wenzelm@48365
   170
  val real = new Object
wenzelm@48365
   171
  {
wenzelm@48365
   172
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   173
    def update(name: String, x: Double): Options =
wenzelm@48365
   174
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   175
  }
wenzelm@48365
   176
wenzelm@48365
   177
  val string = new Object
wenzelm@48365
   178
  {
wenzelm@48365
   179
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   180
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   181
  }
wenzelm@48370
   182
wenzelm@48370
   183
wenzelm@48370
   184
  /* external declare and define */
wenzelm@48370
   185
wenzelm@48370
   186
  private def check_value(name: String): Options =
wenzelm@48370
   187
  {
wenzelm@48370
   188
    val opt = check_name(name)
wenzelm@48370
   189
    opt.typ match {
wenzelm@48370
   190
      case Options.Bool => bool(name); this
wenzelm@48370
   191
      case Options.Int => int(name); this
wenzelm@48370
   192
      case Options.Real => real(name); this
wenzelm@48370
   193
      case Options.String => string(name); this
wenzelm@48370
   194
    }
wenzelm@48370
   195
  }
wenzelm@48370
   196
wenzelm@48370
   197
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   198
  {
wenzelm@48370
   199
    options.get(name) match {
wenzelm@48370
   200
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   201
      case None =>
wenzelm@48370
   202
        val typ =
wenzelm@48370
   203
          typ_name match {
wenzelm@48370
   204
            case "bool" => Options.Bool
wenzelm@48370
   205
            case "int" => Options.Int
wenzelm@48370
   206
            case "real" => Options.Real
wenzelm@48370
   207
            case "string" => Options.String
wenzelm@48456
   208
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   209
          }
wenzelm@48370
   210
        (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
wenzelm@48370
   211
    }
wenzelm@48370
   212
  }
wenzelm@48370
   213
wenzelm@48370
   214
  def define(name: String, value: String): Options =
wenzelm@48370
   215
  {
wenzelm@48370
   216
    val opt = check_name(name)
wenzelm@48370
   217
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   218
  }
wenzelm@48370
   219
wenzelm@48370
   220
  def define(name: String, opt_value: Option[String]): Options =
wenzelm@48370
   221
  {
wenzelm@48370
   222
    val opt = check_name(name)
wenzelm@48370
   223
    opt_value match {
wenzelm@48370
   224
      case Some(value) => define(name, value)
wenzelm@48370
   225
      case None if opt.typ == Options.Bool => define(name, "true")
wenzelm@48370
   226
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   227
    }
wenzelm@48370
   228
  }
wenzelm@48370
   229
wenzelm@48421
   230
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48421
   231
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
wenzelm@48421
   232
wenzelm@48370
   233
  def define_simple(str: String): Options =
wenzelm@48370
   234
  {
wenzelm@48370
   235
    str.indexOf('=') match {
wenzelm@48370
   236
      case -1 => define(str, None)
wenzelm@48370
   237
      case i => define(str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   238
    }
wenzelm@48370
   239
  }
wenzelm@48456
   240
wenzelm@48456
   241
wenzelm@48456
   242
  /* encode */
wenzelm@48456
   243
wenzelm@48456
   244
  def encode: XML.Body =
wenzelm@48456
   245
  {
wenzelm@48456
   246
    import XML.Encode.{string => str, _}
wenzelm@48456
   247
    list(triple(str, str, str))(
wenzelm@48456
   248
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
wenzelm@48456
   249
  }
wenzelm@48365
   250
}