src/Pure/System/options.scala
author wenzelm
Fri, 20 Jul 2012 18:50:33 +0200
changeset 49385 d0fa3efec93b
parent 49384 10b534e64209
child 49388 527e2bad7cca
permissions -rw-r--r--
require explicit initialization of options;
more explicit Position operations;
     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   }
    19   case object Bool extends Type
    20   case object Int extends Type
    21   case object Real extends Type
    22   case object String extends Type
    23 
    24   case class Opt(typ: Type, value: String, description: String)
    25 
    26   val empty: Options = new Options()
    27 
    28 
    29   /* parsing */
    30 
    31   private object Parser extends Parse.Parser
    32   {
    33     val DECLARE = "declare"
    34     val DEFINE = "define"
    35 
    36     val syntax = Outer_Syntax.empty + ":" + "=" + DECLARE + DEFINE
    37 
    38     val entry: Parser[Options => Options] =
    39     {
    40       val option_name = atom("option name", _.is_xname)
    41       val option_type = atom("option type", _.is_ident)
    42       val option_value = atom("option value", tok => tok.is_name || tok.is_float)
    43 
    44       keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
    45       keyword("=") ~ option_value ~ opt(text)) ^^
    46         { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) =>
    47             (options: Options) => options.declare(a, b, c, d.getOrElse("")) } |
    48       keyword(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
    49         { case _ ~ (a ~ _ ~ b) => (options: Options) => options.define(a, b) }
    50     }
    51 
    52     def parse_entries(file: File): List[Options => Options] =
    53     {
    54       val toks = syntax.scan(Standard_System.read_file(file))
    55       parse_all(rep(entry), Token.reader(toks, file.toString)) match {
    56         case Success(result, _) => result
    57         case bad => error(bad.toString)
    58       }
    59     }
    60   }
    61 
    62   val OPTIONS = Path.explode("etc/options")
    63 
    64   def init(): Options =
    65   {
    66     var options = empty
    67     for {
    68       dir <- Isabelle_System.components()
    69       file = Isabelle_System.platform_file(dir + OPTIONS)
    70       if file.isFile
    71       entry <- Parser.parse_entries(file)
    72     } {
    73       try { options = entry(options) }
    74       catch { case ERROR(msg) => error(msg + Position.str_of(Position.file(file))) }
    75     }
    76     options
    77   }
    78 }
    79 
    80 
    81 final class Options private(options: Map[String, Options.Opt] = Map.empty)
    82 {
    83   override def toString: String = options.iterator.mkString("Options (", ",", ")")
    84 
    85 
    86   /* check */
    87 
    88   private def check_name(name: String): Options.Opt =
    89     options.get(name) match {
    90       case Some(opt) => opt
    91       case None => error("Unknown option " + quote(name))
    92     }
    93 
    94   private def check_type(name: String, typ: Options.Type): Options.Opt =
    95   {
    96     val opt = check_name(name)
    97     if (opt.typ == typ) opt
    98     else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
    99   }
   100 
   101 
   102   /* basic operations */
   103 
   104   private def put[A](name: String, typ: Options.Type, value: String): Options =
   105   {
   106     val opt = check_type(name, typ)
   107     new Options(options + (name -> opt.copy(value = value)))
   108   }
   109 
   110   private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
   111   {
   112     val opt = check_type(name, typ)
   113     parse(opt.value) match {
   114       case Some(x) => x
   115       case None =>
   116         error("Malformed value for option " + quote(name) +
   117           " : " + typ.print + " =\n" + quote(opt.value))
   118     }
   119   }
   120 
   121 
   122   /* internal lookup and update */
   123 
   124   val bool = new Object
   125   {
   126     def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
   127     def update(name: String, x: Boolean): Options =
   128       put(name, Options.Bool, Properties.Value.Boolean(x))
   129   }
   130 
   131   val int = new Object
   132   {
   133     def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
   134     def update(name: String, x: Int): Options =
   135       put(name, Options.Int, Properties.Value.Int(x))
   136   }
   137 
   138   val real = new Object
   139   {
   140     def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
   141     def update(name: String, x: Double): Options =
   142       put(name, Options.Real, Properties.Value.Double(x))
   143   }
   144 
   145   val string = new Object
   146   {
   147     def apply(name: String): String = get(name, Options.String, s => Some(s))
   148     def update(name: String, x: String): Options = put(name, Options.String, x)
   149   }
   150 
   151 
   152   /* external declare and define */
   153 
   154   private def check_value(name: String): Options =
   155   {
   156     val opt = check_name(name)
   157     opt.typ match {
   158       case Options.Bool => bool(name); this
   159       case Options.Int => int(name); this
   160       case Options.Real => real(name); this
   161       case Options.String => string(name); this
   162     }
   163   }
   164 
   165   def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
   166   {
   167     options.get(name) match {
   168       case Some(_) => error("Duplicate declaration of option " + quote(name))
   169       case None =>
   170         val typ =
   171           typ_name match {
   172             case "bool" => Options.Bool
   173             case "int" => Options.Int
   174             case "real" => Options.Real
   175             case "string" => Options.String
   176             case _ => error("Malformed type for option " + quote(name) + " : " + quote(typ_name))
   177           }
   178         (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
   179     }
   180   }
   181 
   182   def define(name: String, value: String): Options =
   183   {
   184     val opt = check_name(name)
   185     (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
   186   }
   187 
   188   def define(name: String, opt_value: Option[String]): Options =
   189   {
   190     val opt = check_name(name)
   191     opt_value match {
   192       case Some(value) => define(name, value)
   193       case None if opt.typ == Options.Bool => define(name, "true")
   194       case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
   195     }
   196   }
   197 
   198   def define_simple(str: String): Options =
   199   {
   200     str.indexOf('=') match {
   201       case -1 => define(str, None)
   202       case i => define(str.substring(0, i), str.substring(i + 1))
   203     }
   204   }
   205 }