src/Pure/System/options.scala
author wenzelm
Sat Jul 21 22:13:50 2012 +0200 (2012-07-21)
changeset 48421 c4d337782de4
parent 48411 5b3440850d36
child 48456 d8ff14f44a40
permissions -rw-r--r--
propagate defined options;
misc tuning;
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@48409
    10
import java.io.{File => JFile}
wenzelm@48365
    11
wenzelm@48365
    12
wenzelm@48365
    13
object Options
wenzelm@48365
    14
{
wenzelm@48421
    15
  type Spec = (String, Option[String])
wenzelm@48421
    16
wenzelm@48421
    17
  val empty: Options = new Options()
wenzelm@48421
    18
wenzelm@48421
    19
wenzelm@48421
    20
  /* representation */
wenzelm@48421
    21
wenzelm@48421
    22
  sealed abstract class Type
wenzelm@48365
    23
  {
wenzelm@48365
    24
    def print: String = toString.toLowerCase
wenzelm@48365
    25
  }
wenzelm@48421
    26
  private case object Bool extends Type
wenzelm@48421
    27
  private case object Int extends Type
wenzelm@48421
    28
  private case object Real extends Type
wenzelm@48421
    29
  private case object String extends Type
wenzelm@48365
    30
wenzelm@48370
    31
  case class Opt(typ: Type, value: String, description: String)
wenzelm@48365
    32
wenzelm@48365
    33
wenzelm@48365
    34
  /* parsing */
wenzelm@48365
    35
wenzelm@48365
    36
  private object Parser extends Parse.Parser
wenzelm@48365
    37
  {
wenzelm@48365
    38
    val DECLARE = "declare"
wenzelm@48365
    39
    val DEFINE = "define"
wenzelm@48365
    40
wenzelm@48365
    41
    val syntax = Outer_Syntax.empty + ":" + "=" + DECLARE + DEFINE
wenzelm@48365
    42
wenzelm@48365
    43
    val entry: Parser[Options => Options] =
wenzelm@48365
    44
    {
wenzelm@48365
    45
      val option_name = atom("option name", _.is_xname)
wenzelm@48365
    46
      val option_type = atom("option type", _.is_ident)
wenzelm@48365
    47
      val option_value = atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48365
    48
wenzelm@48370
    49
      keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48370
    50
      keyword("=") ~ option_value ~ opt(text)) ^^
wenzelm@48370
    51
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) =>
wenzelm@48370
    52
            (options: Options) => options.declare(a, b, c, d.getOrElse("")) } |
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@48409
    57
    def parse_entries(file: JFile): List[Options => Options] =
wenzelm@48365
    58
    {
wenzelm@48411
    59
      parse_all(rep(entry), Token.reader(syntax.scan(File.read(file)), file.toString)) 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@48373
    73
      file = (dir + OPTIONS).file
wenzelm@48365
    74
      if file.isFile
wenzelm@48365
    75
      entry <- Parser.parse_entries(file)
wenzelm@48365
    76
    } {
wenzelm@48365
    77
      try { options = entry(options) }
wenzelm@48370
    78
      catch { case ERROR(msg) => error(msg + Position.str_of(Position.file(file))) }
wenzelm@48365
    79
    }
wenzelm@48365
    80
    options
wenzelm@48365
    81
  }
wenzelm@48365
    82
}
wenzelm@48365
    83
wenzelm@48365
    84
wenzelm@48365
    85
final class Options private(options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
    86
{
wenzelm@48365
    87
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
    88
wenzelm@48365
    89
wenzelm@48370
    90
  /* check */
wenzelm@48365
    91
wenzelm@48365
    92
  private def check_name(name: String): Options.Opt =
wenzelm@48365
    93
    options.get(name) match {
wenzelm@48365
    94
      case Some(opt) => opt
wenzelm@48368
    95
      case None => error("Unknown option " + quote(name))
wenzelm@48365
    96
    }
wenzelm@48365
    97
wenzelm@48365
    98
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
    99
  {
wenzelm@48365
   100
    val opt = check_name(name)
wenzelm@48365
   101
    if (opt.typ == typ) opt
wenzelm@48365
   102
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   103
  }
wenzelm@48365
   104
wenzelm@48370
   105
wenzelm@48370
   106
  /* basic operations */
wenzelm@48370
   107
wenzelm@48370
   108
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   109
  {
wenzelm@48370
   110
    val opt = check_type(name, typ)
wenzelm@48370
   111
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   112
  }
wenzelm@48370
   113
wenzelm@48365
   114
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   115
  {
wenzelm@48365
   116
    val opt = check_type(name, typ)
wenzelm@48365
   117
    parse(opt.value) match {
wenzelm@48365
   118
      case Some(x) => x
wenzelm@48365
   119
      case None =>
wenzelm@48365
   120
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   121
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   122
    }
wenzelm@48365
   123
  }
wenzelm@48365
   124
wenzelm@48365
   125
wenzelm@48365
   126
  /* internal lookup and update */
wenzelm@48365
   127
wenzelm@48365
   128
  val bool = new Object
wenzelm@48365
   129
  {
wenzelm@48365
   130
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   131
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   132
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   133
  }
wenzelm@48365
   134
wenzelm@48365
   135
  val int = new Object
wenzelm@48365
   136
  {
wenzelm@48365
   137
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   138
    def update(name: String, x: Int): Options =
wenzelm@48365
   139
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   140
  }
wenzelm@48365
   141
wenzelm@48365
   142
  val real = new Object
wenzelm@48365
   143
  {
wenzelm@48365
   144
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   145
    def update(name: String, x: Double): Options =
wenzelm@48365
   146
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   147
  }
wenzelm@48365
   148
wenzelm@48365
   149
  val string = new Object
wenzelm@48365
   150
  {
wenzelm@48365
   151
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   152
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   153
  }
wenzelm@48370
   154
wenzelm@48370
   155
wenzelm@48370
   156
  /* external declare and define */
wenzelm@48370
   157
wenzelm@48370
   158
  private def check_value(name: String): Options =
wenzelm@48370
   159
  {
wenzelm@48370
   160
    val opt = check_name(name)
wenzelm@48370
   161
    opt.typ match {
wenzelm@48370
   162
      case Options.Bool => bool(name); this
wenzelm@48370
   163
      case Options.Int => int(name); this
wenzelm@48370
   164
      case Options.Real => real(name); this
wenzelm@48370
   165
      case Options.String => string(name); this
wenzelm@48370
   166
    }
wenzelm@48370
   167
  }
wenzelm@48370
   168
wenzelm@48370
   169
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   170
  {
wenzelm@48370
   171
    options.get(name) match {
wenzelm@48370
   172
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   173
      case None =>
wenzelm@48370
   174
        val typ =
wenzelm@48370
   175
          typ_name match {
wenzelm@48370
   176
            case "bool" => Options.Bool
wenzelm@48370
   177
            case "int" => Options.Int
wenzelm@48370
   178
            case "real" => Options.Real
wenzelm@48370
   179
            case "string" => Options.String
wenzelm@48370
   180
            case _ => error("Malformed type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   181
          }
wenzelm@48370
   182
        (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
wenzelm@48370
   183
    }
wenzelm@48370
   184
  }
wenzelm@48370
   185
wenzelm@48370
   186
  def define(name: String, value: String): Options =
wenzelm@48370
   187
  {
wenzelm@48370
   188
    val opt = check_name(name)
wenzelm@48370
   189
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   190
  }
wenzelm@48370
   191
wenzelm@48370
   192
  def define(name: String, opt_value: Option[String]): Options =
wenzelm@48370
   193
  {
wenzelm@48370
   194
    val opt = check_name(name)
wenzelm@48370
   195
    opt_value match {
wenzelm@48370
   196
      case Some(value) => define(name, value)
wenzelm@48370
   197
      case None if opt.typ == Options.Bool => define(name, "true")
wenzelm@48370
   198
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   199
    }
wenzelm@48370
   200
  }
wenzelm@48370
   201
wenzelm@48421
   202
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48421
   203
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
wenzelm@48421
   204
wenzelm@48370
   205
  def define_simple(str: String): Options =
wenzelm@48370
   206
  {
wenzelm@48370
   207
    str.indexOf('=') match {
wenzelm@48370
   208
      case -1 => define(str, None)
wenzelm@48370
   209
      case i => define(str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   210
    }
wenzelm@48370
   211
  }
wenzelm@48365
   212
}