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