1.1 --- a/src/Pure/System/options.scala Fri Jul 20 17:43:55 2012 +0200
1.2 +++ b/src/Pure/System/options.scala Fri Jul 20 18:50:33 2012 +0200
1.3 @@ -15,14 +15,13 @@
1.4 abstract class Type
1.5 {
1.6 def print: String = toString.toLowerCase
1.7 - def init: String
1.8 }
1.9 - case object Bool extends Type { def init = "false" }
1.10 - case object Int extends Type { def init = "0" }
1.11 - case object Real extends Type { def init = "0.0" }
1.12 - case object String extends Type { def init = "" }
1.13 + case object Bool extends Type
1.14 + case object Int extends Type
1.15 + case object Real extends Type
1.16 + case object String extends Type
1.17
1.18 - case class Opt(typ: Type, description: String, value: String)
1.19 + case class Opt(typ: Type, value: String, description: String)
1.20
1.21 val empty: Options = new Options()
1.22
1.23 @@ -42,12 +41,12 @@
1.24 val option_type = atom("option type", _.is_ident)
1.25 val option_value = atom("option value", tok => tok.is_name || tok.is_float)
1.26
1.27 - keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~ opt(text)) ^^
1.28 - { case _ ~ (x ~ _ ~ y ~ z) => (options: Options) =>
1.29 - options.declare(x, y, z.getOrElse("")) } |
1.30 + keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
1.31 + keyword("=") ~ option_value ~ opt(text)) ^^
1.32 + { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) =>
1.33 + (options: Options) => options.declare(a, b, c, d.getOrElse("")) } |
1.34 keyword(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
1.35 - { case _ ~ (x ~ _ ~ y) => (options: Options) =>
1.36 - options.define(x, y) }
1.37 + { case _ ~ (a ~ _ ~ b) => (options: Options) => options.define(a, b) }
1.38 }
1.39
1.40 def parse_entries(file: File): List[Options => Options] =
1.41 @@ -72,7 +71,7 @@
1.42 entry <- Parser.parse_entries(file)
1.43 } {
1.44 try { options = entry(options) }
1.45 - catch { case ERROR(msg) => error(msg + " (file " + quote(file.toString) + ")") }
1.46 + catch { case ERROR(msg) => error(msg + Position.str_of(Position.file(file))) }
1.47 }
1.48 options
1.49 }
1.50 @@ -84,7 +83,7 @@
1.51 override def toString: String = options.iterator.mkString("Options (", ",", ")")
1.52
1.53
1.54 - /* basic operations */
1.55 + /* check */
1.56
1.57 private def check_name(name: String): Options.Opt =
1.58 options.get(name) match {
1.59 @@ -99,6 +98,15 @@
1.60 else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
1.61 }
1.62
1.63 +
1.64 + /* basic operations */
1.65 +
1.66 + private def put[A](name: String, typ: Options.Type, value: String): Options =
1.67 + {
1.68 + val opt = check_type(name, typ)
1.69 + new Options(options + (name -> opt.copy(value = value)))
1.70 + }
1.71 +
1.72 private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
1.73 {
1.74 val opt = check_type(name, typ)
1.75 @@ -110,63 +118,6 @@
1.76 }
1.77 }
1.78
1.79 - private def put[A](name: String, typ: Options.Type, value: String): Options =
1.80 - {
1.81 - val opt = check_type(name, typ)
1.82 - new Options(options + (name -> opt.copy(value = value)))
1.83 - }
1.84 -
1.85 -
1.86 - /* external declare and define */
1.87 -
1.88 - def declare(name: String, typ_name: String, description: String = ""): Options =
1.89 - {
1.90 - options.get(name) match {
1.91 - case Some(_) => error("Duplicate declaration of option " + quote(name))
1.92 - case None =>
1.93 - val typ =
1.94 - typ_name match {
1.95 - case "bool" => Options.Bool
1.96 - case "int" => Options.Int
1.97 - case "real" => Options.Real
1.98 - case "string" => Options.String
1.99 - case _ => error("Malformed type for option " + quote(name) + " : " + quote(typ_name))
1.100 - }
1.101 - new Options(options + (name -> Options.Opt(typ, description, typ.init)))
1.102 - }
1.103 - }
1.104 -
1.105 - def define(name: String, value: String): Options =
1.106 - {
1.107 - val opt = check_name(name)
1.108 - val result = new Options(options + (name -> opt.copy(value = value)))
1.109 - opt.typ match {
1.110 - case Options.Bool => result.bool(name); ()
1.111 - case Options.Int => result.int(name); ()
1.112 - case Options.Real => result.real(name); ()
1.113 - case Options.String => result.string(name); ()
1.114 - }
1.115 - result
1.116 - }
1.117 -
1.118 - def define(name: String, opt_value: Option[String]): Options =
1.119 - {
1.120 - val opt = check_name(name)
1.121 - opt_value match {
1.122 - case Some(value) => define(name, value)
1.123 - case None if opt.typ == Options.Bool => define(name, "true")
1.124 - case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
1.125 - }
1.126 - }
1.127 -
1.128 - def define_simple(str: String): Options =
1.129 - {
1.130 - str.indexOf('=') match {
1.131 - case -1 => define(str, None)
1.132 - case i => define(str.substring(0, i), str.substring(i + 1))
1.133 - }
1.134 - }
1.135 -
1.136
1.137 /* internal lookup and update */
1.138
1.139 @@ -196,4 +147,59 @@
1.140 def apply(name: String): String = get(name, Options.String, s => Some(s))
1.141 def update(name: String, x: String): Options = put(name, Options.String, x)
1.142 }
1.143 +
1.144 +
1.145 + /* external declare and define */
1.146 +
1.147 + private def check_value(name: String): Options =
1.148 + {
1.149 + val opt = check_name(name)
1.150 + opt.typ match {
1.151 + case Options.Bool => bool(name); this
1.152 + case Options.Int => int(name); this
1.153 + case Options.Real => real(name); this
1.154 + case Options.String => string(name); this
1.155 + }
1.156 + }
1.157 +
1.158 + def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
1.159 + {
1.160 + options.get(name) match {
1.161 + case Some(_) => error("Duplicate declaration of option " + quote(name))
1.162 + case None =>
1.163 + val typ =
1.164 + typ_name match {
1.165 + case "bool" => Options.Bool
1.166 + case "int" => Options.Int
1.167 + case "real" => Options.Real
1.168 + case "string" => Options.String
1.169 + case _ => error("Malformed type for option " + quote(name) + " : " + quote(typ_name))
1.170 + }
1.171 + (new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)
1.172 + }
1.173 + }
1.174 +
1.175 + def define(name: String, value: String): Options =
1.176 + {
1.177 + val opt = check_name(name)
1.178 + (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
1.179 + }
1.180 +
1.181 + def define(name: String, opt_value: Option[String]): Options =
1.182 + {
1.183 + val opt = check_name(name)
1.184 + opt_value match {
1.185 + case Some(value) => define(name, value)
1.186 + case None if opt.typ == Options.Bool => define(name, "true")
1.187 + case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
1.188 + }
1.189 + }
1.190 +
1.191 + def define_simple(str: String): Options =
1.192 + {
1.193 + str.indexOf('=') match {
1.194 + case -1 => define(str, None)
1.195 + case i => define(str.substring(0, i), str.substring(i + 1))
1.196 + }
1.197 + }
1.198 }