| author | wenzelm | 
| Mon, 30 Jul 2012 15:31:00 +0200 | |
| changeset 48605 | e777363440d6 | 
| parent 48580 | 9df76dd45900 | 
| child 48693 | ceeea46bdeba | 
| permissions | -rw-r--r-- | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
1  | 
/* Title: Pure/System/options.scala  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
2  | 
Author: Makarius  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
3  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
4  | 
Stand-alone options with external string representation.  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
5  | 
*/  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
6  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
7  | 
package isabelle  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
8  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
9  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
10  | 
object Options  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
11  | 
{
 | 
| 48421 | 12  | 
type Spec = (String, Option[String])  | 
13  | 
||
14  | 
val empty: Options = new Options()  | 
|
15  | 
||
16  | 
||
17  | 
/* representation */  | 
|
18  | 
||
19  | 
sealed abstract class Type  | 
|
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
20  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
21  | 
def print: String = toString.toLowerCase  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
22  | 
}  | 
| 48421 | 23  | 
private case object Bool extends Type  | 
24  | 
private case object Int extends Type  | 
|
25  | 
private case object Real extends Type  | 
|
26  | 
private case object String extends Type  | 
|
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
27  | 
|
| 48370 | 28  | 
case class Opt(typ: Type, value: String, description: String)  | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
29  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
30  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
31  | 
/* parsing */  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
32  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
33  | 
private object Parser extends Parse.Parser  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
34  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
35  | 
val DECLARE = "declare"  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
36  | 
val DEFINE = "define"  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
37  | 
|
| 48580 | 38  | 
val syntax = Outer_Syntax.empty + ":" + "=" + "--" + DECLARE + DEFINE  | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
39  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
40  | 
val entry: Parser[Options => Options] =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
41  | 
    {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
42  | 
      val option_name = atom("option name", _.is_xname)
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
43  | 
      val option_type = atom("option type", _.is_ident)
 | 
| 
48605
 
e777363440d6
allow negative int values as well, according to real = int | float;
 
wenzelm 
parents: 
48580 
diff
changeset
 | 
44  | 
|
| 
 
e777363440d6
allow negative int values as well, according to real = int | float;
 
wenzelm 
parents: 
48580 
diff
changeset
 | 
45  | 
val option_value =  | 
| 
 
e777363440d6
allow negative int values as well, according to real = int | float;
 
wenzelm 
parents: 
48580 
diff
changeset
 | 
46  | 
        opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
 | 
| 
 
e777363440d6
allow negative int values as well, according to real = int | float;
 
wenzelm 
parents: 
48580 
diff
changeset
 | 
47  | 
          { case s ~ n => if (s.isDefined) "-" + n else n } |
 | 
| 
 
e777363440d6
allow negative int values as well, according to real = int | float;
 
wenzelm 
parents: 
48580 
diff
changeset
 | 
48  | 
        atom("option value", tok => tok.is_name || tok.is_float)
 | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
49  | 
|
| 48370 | 50  | 
      keyword(DECLARE) ~! (option_name ~ keyword(":") ~ option_type ~
 | 
| 48580 | 51  | 
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
 | 
52  | 
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) } |
 | 
|
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
53  | 
      keyword(DEFINE) ~! (option_name ~ keyword("=") ~ option_value) ^^
 | 
| 48370 | 54  | 
        { case _ ~ (a ~ _ ~ b) => (options: Options) => options.define(a, b) }
 | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
55  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
56  | 
|
| 48548 | 57  | 
def parse_entries(file: Path): List[Options => Options] =  | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
58  | 
    {
 | 
| 48548 | 59  | 
      parse_all(rep(entry), Token.reader(syntax.scan(File.read(file)), file.implode)) match {
 | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
60  | 
case Success(result, _) => result  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
61  | 
case bad => error(bad.toString)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
62  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
63  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
64  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
65  | 
|
| 48421 | 66  | 
  private val OPTIONS = Path.explode("etc/options")
 | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
67  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
68  | 
def init(): Options =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
69  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
70  | 
var options = empty  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
71  | 
    for {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
72  | 
dir <- Isabelle_System.components()  | 
| 48548 | 73  | 
file = dir + OPTIONS if file.is_file  | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
74  | 
entry <- Parser.parse_entries(file)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
75  | 
    } {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
76  | 
      try { options = entry(options) }
 | 
| 48548 | 77  | 
      catch { case ERROR(msg) => error(msg + Position.str_of(file.position)) }
 | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
78  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
79  | 
options  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
80  | 
}  | 
| 48457 | 81  | 
|
82  | 
||
83  | 
/* encode */  | 
|
84  | 
||
85  | 
val encode: XML.Encode.T[Options] = (options => options.encode)  | 
|
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
86  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
87  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
88  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
89  | 
final class Options private(options: Map[String, Options.Opt] = Map.empty)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
90  | 
{
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
91  | 
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
92  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
93  | 
|
| 48370 | 94  | 
/* check */  | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
95  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
96  | 
private def check_name(name: String): Options.Opt =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
97  | 
    options.get(name) match {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
98  | 
case Some(opt) => opt  | 
| 48368 | 99  | 
      case None => error("Unknown option " + quote(name))
 | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
100  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
101  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
102  | 
private def check_type(name: String, typ: Options.Type): Options.Opt =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
103  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
104  | 
val opt = check_name(name)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
105  | 
if (opt.typ == typ) opt  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
106  | 
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
107  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
108  | 
|
| 48370 | 109  | 
|
110  | 
/* basic operations */  | 
|
111  | 
||
112  | 
private def put[A](name: String, typ: Options.Type, value: String): Options =  | 
|
113  | 
  {
 | 
|
114  | 
val opt = check_type(name, typ)  | 
|
115  | 
new Options(options + (name -> opt.copy(value = value)))  | 
|
116  | 
}  | 
|
117  | 
||
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
118  | 
private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
119  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
120  | 
val opt = check_type(name, typ)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
121  | 
    parse(opt.value) match {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
122  | 
case Some(x) => x  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
123  | 
case None =>  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
124  | 
        error("Malformed value for option " + quote(name) +
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
125  | 
" : " + typ.print + " =\n" + quote(opt.value))  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
126  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
127  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
128  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
129  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
130  | 
/* internal lookup and update */  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
131  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
132  | 
val bool = new Object  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
133  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
134  | 
def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
135  | 
def update(name: String, x: Boolean): Options =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
136  | 
put(name, Options.Bool, Properties.Value.Boolean(x))  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
137  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
138  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
139  | 
val int = new Object  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
140  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
141  | 
def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
142  | 
def update(name: String, x: Int): Options =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
143  | 
put(name, Options.Int, Properties.Value.Int(x))  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
144  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
145  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
146  | 
val real = new Object  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
147  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
148  | 
def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
149  | 
def update(name: String, x: Double): Options =  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
150  | 
put(name, Options.Real, Properties.Value.Double(x))  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
151  | 
}  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
152  | 
|
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
153  | 
val string = new Object  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
154  | 
  {
 | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
155  | 
def apply(name: String): String = get(name, Options.String, s => Some(s))  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
156  | 
def update(name: String, x: String): Options = put(name, Options.String, x)  | 
| 
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
157  | 
}  | 
| 48370 | 158  | 
|
159  | 
||
160  | 
/* external declare and define */  | 
|
161  | 
||
162  | 
private def check_value(name: String): Options =  | 
|
163  | 
  {
 | 
|
164  | 
val opt = check_name(name)  | 
|
165  | 
    opt.typ match {
 | 
|
166  | 
case Options.Bool => bool(name); this  | 
|
167  | 
case Options.Int => int(name); this  | 
|
168  | 
case Options.Real => real(name); this  | 
|
169  | 
case Options.String => string(name); this  | 
|
170  | 
}  | 
|
171  | 
}  | 
|
172  | 
||
173  | 
def declare(name: String, typ_name: String, value: String, description: String = ""): Options =  | 
|
174  | 
  {
 | 
|
175  | 
    options.get(name) match {
 | 
|
176  | 
      case Some(_) => error("Duplicate declaration of option " + quote(name))
 | 
|
177  | 
case None =>  | 
|
178  | 
val typ =  | 
|
179  | 
          typ_name match {
 | 
|
180  | 
case "bool" => Options.Bool  | 
|
181  | 
case "int" => Options.Int  | 
|
182  | 
case "real" => Options.Real  | 
|
183  | 
case "string" => Options.String  | 
|
| 
48456
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
184  | 
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
 | 
| 48370 | 185  | 
}  | 
186  | 
(new Options(options + (name -> Options.Opt(typ, value, description)))).check_value(name)  | 
|
187  | 
}  | 
|
188  | 
}  | 
|
189  | 
||
190  | 
def define(name: String, value: String): Options =  | 
|
191  | 
  {
 | 
|
192  | 
val opt = check_name(name)  | 
|
193  | 
(new Options(options + (name -> opt.copy(value = value)))).check_value(name)  | 
|
194  | 
}  | 
|
195  | 
||
196  | 
def define(name: String, opt_value: Option[String]): Options =  | 
|
197  | 
  {
 | 
|
198  | 
val opt = check_name(name)  | 
|
199  | 
    opt_value match {
 | 
|
200  | 
case Some(value) => define(name, value)  | 
|
201  | 
case None if opt.typ == Options.Bool => define(name, "true")  | 
|
202  | 
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
 | 
|
203  | 
}  | 
|
204  | 
}  | 
|
205  | 
||
| 48421 | 206  | 
def ++ (specs: List[Options.Spec]): Options =  | 
207  | 
    (this /: specs)({ case (x, (y, z)) => x.define(y, z) })
 | 
|
208  | 
||
| 48370 | 209  | 
def define_simple(str: String): Options =  | 
210  | 
  {
 | 
|
211  | 
    str.indexOf('=') match {
 | 
|
212  | 
case -1 => define(str, None)  | 
|
213  | 
case i => define(str.substring(0, i), str.substring(i + 1))  | 
|
214  | 
}  | 
|
215  | 
}  | 
|
| 
48456
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
216  | 
|
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
217  | 
|
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
218  | 
/* encode */  | 
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
219  | 
|
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
220  | 
def encode: XML.Body =  | 
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
221  | 
  {
 | 
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
222  | 
    import XML.Encode.{string => str, _}
 | 
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
223  | 
list(triple(str, str, str))(  | 
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
224  | 
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
 | 
| 
 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 
wenzelm 
parents: 
48421 
diff
changeset
 | 
225  | 
}  | 
| 
48365
 
d88aefda01c4
basic support for stand-alone options with external string representation;
 
wenzelm 
parents:  
diff
changeset
 | 
226  | 
}  |