src/Pure/System/options.scala
author wenzelm
Fri Jul 20 15:48:22 2012 +0200 (2012-07-20)
changeset 48365 d88aefda01c4
child 48368 dc538eef2cf2
permissions -rw-r--r--
basic support for stand-alone options with external string representation;
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
import java.io.File
wenzelm@48365
    11
wenzelm@48365
    12
wenzelm@48365
    13
object Options
wenzelm@48365
    14
{
wenzelm@48365
    15
  abstract class Type
wenzelm@48365
    16
  {
wenzelm@48365
    17
    def print: String = toString.toLowerCase
wenzelm@48365
    18
    def init: String
wenzelm@48365
    19
  }
wenzelm@48365
    20
  case object Bool extends Type { def init = "false" }
wenzelm@48365
    21
  case object Int extends Type { def init = "0" }
wenzelm@48365
    22
  case object Real extends Type { def init = "0.0" }
wenzelm@48365
    23
  case object String extends Type { def init = "" }
wenzelm@48365
    24
wenzelm@48365
    25
  case class Opt(typ: Type, description: String, value: String)
wenzelm@48365
    26
wenzelm@48365
    27
  val empty: Options = new Options()
wenzelm@48365
    28
wenzelm@48365
    29
wenzelm@48365
    30
  /* parsing */
wenzelm@48365
    31
wenzelm@48365
    32
  private object Parser extends Parse.Parser
wenzelm@48365
    33
  {
wenzelm@48365
    34
    val DECLARE = "declare"
wenzelm@48365
    35
    val DEFINE = "define"
wenzelm@48365
    36
wenzelm@48365
    37
    val syntax = Outer_Syntax.empty + ":" + "=" + DECLARE + DEFINE
wenzelm@48365
    38
wenzelm@48365
    39
    val entry: Parser[Options => Options] =
wenzelm@48365
    40
    {
wenzelm@48365
    41
      val option_name = atom("option name", _.is_xname)
wenzelm@48365
    42
      val option_type = atom("option type", _.is_ident)
wenzelm@48365
    43
      val option_value = atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48365
    44
wenzelm@48365
    45
      keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~ opt(text)) ^^
wenzelm@48365
    46
        { case _ ~ (x ~ _ ~ y ~ z) => (options: Options) =>
wenzelm@48365
    47
            options.declare(x, y, z.getOrElse("")) } |
wenzelm@48365
    48
      keyword(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
wenzelm@48365
    49
        { case _ ~ (x ~ _ ~ y) => (options: Options) =>
wenzelm@48365
    50
            options.define(x, y) }
wenzelm@48365
    51
    }
wenzelm@48365
    52
wenzelm@48365
    53
    def parse_entries(file: File): List[Options => Options] =
wenzelm@48365
    54
    {
wenzelm@48365
    55
      val toks = syntax.scan(Standard_System.read_file(file))
wenzelm@48365
    56
      parse_all(rep(entry), Token.reader(toks, file.toString)) match {
wenzelm@48365
    57
        case Success(result, _) => result
wenzelm@48365
    58
        case bad => error(bad.toString)
wenzelm@48365
    59
      }
wenzelm@48365
    60
    }
wenzelm@48365
    61
  }
wenzelm@48365
    62
wenzelm@48365
    63
  val OPTIONS = Path.explode("etc/options")
wenzelm@48365
    64
wenzelm@48365
    65
  def init(): Options =
wenzelm@48365
    66
  {
wenzelm@48365
    67
    var options = empty
wenzelm@48365
    68
    for {
wenzelm@48365
    69
      dir <- Isabelle_System.components()
wenzelm@48365
    70
      file = Isabelle_System.platform_file(dir + OPTIONS)
wenzelm@48365
    71
      if file.isFile
wenzelm@48365
    72
      entry <- Parser.parse_entries(file)
wenzelm@48365
    73
    } {
wenzelm@48365
    74
      try { options = entry(options) }
wenzelm@48365
    75
      catch { case ERROR(msg) => error(msg + " (file " + quote(file.toString) + ")") }
wenzelm@48365
    76
    }
wenzelm@48365
    77
    options
wenzelm@48365
    78
  }
wenzelm@48365
    79
}
wenzelm@48365
    80
wenzelm@48365
    81
wenzelm@48365
    82
final class Options private(options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
    83
{
wenzelm@48365
    84
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
    85
wenzelm@48365
    86
wenzelm@48365
    87
  /* basic operations */
wenzelm@48365
    88
wenzelm@48365
    89
  private def check_name(name: String): Options.Opt =
wenzelm@48365
    90
    options.get(name) match {
wenzelm@48365
    91
      case Some(opt) => opt
wenzelm@48365
    92
      case None => error("Undeclared option " + quote(name))
wenzelm@48365
    93
    }
wenzelm@48365
    94
wenzelm@48365
    95
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
    96
  {
wenzelm@48365
    97
    val opt = check_name(name)
wenzelm@48365
    98
    if (opt.typ == typ) opt
wenzelm@48365
    99
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   100
  }
wenzelm@48365
   101
wenzelm@48365
   102
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   103
  {
wenzelm@48365
   104
    val opt = check_type(name, typ)
wenzelm@48365
   105
    parse(opt.value) match {
wenzelm@48365
   106
      case Some(x) => x
wenzelm@48365
   107
      case None =>
wenzelm@48365
   108
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   109
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   110
    }
wenzelm@48365
   111
  }
wenzelm@48365
   112
wenzelm@48365
   113
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48365
   114
  {
wenzelm@48365
   115
    val opt = check_type(name, typ)
wenzelm@48365
   116
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48365
   117
  }
wenzelm@48365
   118
wenzelm@48365
   119
wenzelm@48365
   120
wenzelm@48365
   121
wenzelm@48365
   122
  /* external declare and define */
wenzelm@48365
   123
wenzelm@48365
   124
  def declare(name: String, typ_name: String, description: String = ""): Options =
wenzelm@48365
   125
  {
wenzelm@48365
   126
    options.get(name) match {
wenzelm@48365
   127
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48365
   128
      case None =>
wenzelm@48365
   129
        val typ =
wenzelm@48365
   130
          typ_name match {
wenzelm@48365
   131
            case "bool" => Options.Bool
wenzelm@48365
   132
            case "int" => Options.Int
wenzelm@48365
   133
            case "real" => Options.Real
wenzelm@48365
   134
            case "string" => Options.String
wenzelm@48365
   135
            case _ => error("Malformed type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48365
   136
          }
wenzelm@48365
   137
        new Options(options + (name -> Options.Opt(typ, description, typ.init)))
wenzelm@48365
   138
    }
wenzelm@48365
   139
  }
wenzelm@48365
   140
wenzelm@48365
   141
  def define(name: String, value: String): Options =
wenzelm@48365
   142
  {
wenzelm@48365
   143
    val opt = check_name(name)
wenzelm@48365
   144
    val result = new Options(options + (name -> opt.copy(value = value)))
wenzelm@48365
   145
    opt.typ match {
wenzelm@48365
   146
      case Options.Bool => result.bool(name); ()
wenzelm@48365
   147
      case Options.Int => result.int(name); ()
wenzelm@48365
   148
      case Options.Real => result.real(name); ()
wenzelm@48365
   149
      case Options.String => result.string(name); ()
wenzelm@48365
   150
    }
wenzelm@48365
   151
    result
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@48365
   183
}