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