1 /* Title: Pure/System/options.scala
4 Stand-alone options with external string representation.
17 def print: String = toString.toLowerCase
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 = "" }
25 case class Opt(typ: Type, description: String, value: String)
27 val empty: Options = new Options()
32 private object Parser extends Parse.Parser
34 val DECLARE = "declare"
37 val syntax = Outer_Syntax.empty + ":" + "=" + DECLARE + DEFINE
39 val entry: Parser[Options => Options] =
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)
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) }
53 def parse_entries(file: File): List[Options => Options] =
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)
63 val OPTIONS = Path.explode("etc/options")
69 dir <- Isabelle_System.components()
70 file = Isabelle_System.platform_file(dir + OPTIONS)
72 entry <- Parser.parse_entries(file)
74 try { options = entry(options) }
75 catch { case ERROR(msg) => error(msg + " (file " + quote(file.toString) + ")") }
82 final class Options private(options: Map[String, Options.Opt] = Map.empty)
84 override def toString: String = options.iterator.mkString("Options (", ",", ")")
87 /* basic operations */
89 private def check_name(name: String): Options.Opt =
90 options.get(name) match {
92 case None => error("Undeclared option " + quote(name))
95 private def check_type(name: String, typ: Options.Type): Options.Opt =
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)
102 private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
104 val opt = check_type(name, typ)
105 parse(opt.value) match {
108 error("Malformed value for option " + quote(name) +
109 " : " + typ.print + " =\n" + quote(opt.value))
113 private def put[A](name: String, typ: Options.Type, value: String): Options =
115 val opt = check_type(name, typ)
116 new Options(options + (name -> opt.copy(value = value)))
122 /* external declare and define */
124 def declare(name: String, typ_name: String, description: String = ""): Options =
126 options.get(name) match {
127 case Some(_) => error("Duplicate declaration of option " + quote(name))
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))
137 new Options(options + (name -> Options.Opt(typ, description, typ.init)))
141 def define(name: String, value: String): Options =
143 val opt = check_name(name)
144 val result = new Options(options + (name -> opt.copy(value = value)))
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); ()
155 /* internal lookup and update */
157 val bool = new Object
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))
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))
171 val real = new Object
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))
178 val string = new Object
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)