src/Pure/System/options.scala
author wenzelm
Thu Aug 02 12:36:54 2012 +0200 (2012-08-02)
changeset 48646 91281e9472d8
parent 48605 e777363440d6
child 48693 ceeea46bdeba
permissions -rw-r--r--
more official command specifications, including source position;
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@48365
    86
}
wenzelm@48365
    87
wenzelm@48365
    88
wenzelm@48365
    89
final class Options private(options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
    90
{
wenzelm@48365
    91
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
    92
wenzelm@48365
    93
wenzelm@48370
    94
  /* check */
wenzelm@48365
    95
wenzelm@48365
    96
  private def check_name(name: String): Options.Opt =
wenzelm@48365
    97
    options.get(name) match {
wenzelm@48365
    98
      case Some(opt) => opt
wenzelm@48368
    99
      case None => error("Unknown option " + quote(name))
wenzelm@48365
   100
    }
wenzelm@48365
   101
wenzelm@48365
   102
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   103
  {
wenzelm@48365
   104
    val opt = check_name(name)
wenzelm@48365
   105
    if (opt.typ == typ) opt
wenzelm@48365
   106
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   107
  }
wenzelm@48365
   108
wenzelm@48370
   109
wenzelm@48370
   110
  /* basic operations */
wenzelm@48370
   111
wenzelm@48370
   112
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   113
  {
wenzelm@48370
   114
    val opt = check_type(name, typ)
wenzelm@48370
   115
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   116
  }
wenzelm@48370
   117
wenzelm@48365
   118
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   119
  {
wenzelm@48365
   120
    val opt = check_type(name, typ)
wenzelm@48365
   121
    parse(opt.value) match {
wenzelm@48365
   122
      case Some(x) => x
wenzelm@48365
   123
      case None =>
wenzelm@48365
   124
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   125
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   126
    }
wenzelm@48365
   127
  }
wenzelm@48365
   128
wenzelm@48365
   129
wenzelm@48365
   130
  /* internal lookup and update */
wenzelm@48365
   131
wenzelm@48365
   132
  val bool = new Object
wenzelm@48365
   133
  {
wenzelm@48365
   134
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   135
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   136
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   137
  }
wenzelm@48365
   138
wenzelm@48365
   139
  val int = new Object
wenzelm@48365
   140
  {
wenzelm@48365
   141
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   142
    def update(name: String, x: Int): Options =
wenzelm@48365
   143
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   144
  }
wenzelm@48365
   145
wenzelm@48365
   146
  val real = new Object
wenzelm@48365
   147
  {
wenzelm@48365
   148
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   149
    def update(name: String, x: Double): Options =
wenzelm@48365
   150
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   151
  }
wenzelm@48365
   152
wenzelm@48365
   153
  val string = new Object
wenzelm@48365
   154
  {
wenzelm@48365
   155
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   156
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   157
  }
wenzelm@48370
   158
wenzelm@48370
   159
wenzelm@48370
   160
  /* external declare and define */
wenzelm@48370
   161
wenzelm@48370
   162
  private def check_value(name: String): Options =
wenzelm@48370
   163
  {
wenzelm@48370
   164
    val opt = check_name(name)
wenzelm@48370
   165
    opt.typ match {
wenzelm@48370
   166
      case Options.Bool => bool(name); this
wenzelm@48370
   167
      case Options.Int => int(name); this
wenzelm@48370
   168
      case Options.Real => real(name); this
wenzelm@48370
   169
      case Options.String => string(name); this
wenzelm@48370
   170
    }
wenzelm@48370
   171
  }
wenzelm@48370
   172
wenzelm@48370
   173
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   174
  {
wenzelm@48370
   175
    options.get(name) match {
wenzelm@48370
   176
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   177
      case None =>
wenzelm@48370
   178
        val typ =
wenzelm@48370
   179
          typ_name match {
wenzelm@48370
   180
            case "bool" => Options.Bool
wenzelm@48370
   181
            case "int" => Options.Int
wenzelm@48370
   182
            case "real" => Options.Real
wenzelm@48370
   183
            case "string" => Options.String
wenzelm@48456
   184
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   185
          }
wenzelm@48370
   186
        (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
wenzelm@48370
   187
    }
wenzelm@48370
   188
  }
wenzelm@48370
   189
wenzelm@48370
   190
  def define(name: String, value: String): Options =
wenzelm@48370
   191
  {
wenzelm@48370
   192
    val opt = check_name(name)
wenzelm@48370
   193
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   194
  }
wenzelm@48370
   195
wenzelm@48370
   196
  def define(name: String, opt_value: Option[String]): Options =
wenzelm@48370
   197
  {
wenzelm@48370
   198
    val opt = check_name(name)
wenzelm@48370
   199
    opt_value match {
wenzelm@48370
   200
      case Some(value) => define(name, value)
wenzelm@48370
   201
      case None if opt.typ == Options.Bool => define(name, "true")
wenzelm@48370
   202
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   203
    }
wenzelm@48370
   204
  }
wenzelm@48370
   205
wenzelm@48421
   206
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48421
   207
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
wenzelm@48421
   208
wenzelm@48370
   209
  def define_simple(str: String): Options =
wenzelm@48370
   210
  {
wenzelm@48370
   211
    str.indexOf('=') match {
wenzelm@48370
   212
      case -1 => define(str, None)
wenzelm@48370
   213
      case i => define(str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   214
    }
wenzelm@48370
   215
  }
wenzelm@48456
   216
wenzelm@48456
   217
wenzelm@48456
   218
  /* encode */
wenzelm@48456
   219
wenzelm@48456
   220
  def encode: XML.Body =
wenzelm@48456
   221
  {
wenzelm@48456
   222
    import XML.Encode.{string => str, _}
wenzelm@48456
   223
    list(triple(str, str, str))(
wenzelm@48456
   224
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
wenzelm@48456
   225
  }
wenzelm@48365
   226
}