author | wenzelm |
Thu, 05 Nov 2015 00:02:30 +0100 | |
changeset 61579 | 634cd44bb1d3 |
parent 60215 | 5fb4990dfc73 |
child 62227 | 6eeaaefcea56 |
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 |
|
51945 | 4 |
System options with external string representation. |
48365
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 |
|
48807 | 10 |
import java.util.Calendar |
11 |
||
12 |
||
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
13 |
object Options |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
14 |
{ |
48421 | 15 |
type Spec = (String, Option[String]) |
16 |
||
17 |
val empty: Options = new Options() |
|
18 |
||
19 |
||
20 |
/* representation */ |
|
21 |
||
22 |
sealed abstract class Type |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
23 |
{ |
56599 | 24 |
def print: String = Word.lowercase(toString) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
25 |
} |
49246 | 26 |
case object Bool extends Type |
27 |
case object Int extends Type |
|
28 |
case object Real extends Type |
|
29 |
case object String extends Type |
|
30 |
case object Unknown extends Type |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
31 |
|
56465 | 32 |
case class Opt( |
33 |
public: Boolean, |
|
34 |
pos: Position.T, |
|
35 |
name: String, |
|
36 |
typ: Type, |
|
37 |
value: String, |
|
38 |
default_value: String, |
|
39 |
description: String, |
|
40 |
section: String) |
|
48860 | 41 |
{ |
49289 | 42 |
private def print(default: Boolean): String = |
43 |
{ |
|
44 |
val x = if (default) default_value else value |
|
49247 | 45 |
"option " + name + " : " + typ.print + " = " + |
49289 | 46 |
(if (typ == Options.String) quote(x) else x) + |
47 |
(if (description == "") "" else "\n -- " + quote(description)) |
|
48 |
} |
|
49 |
||
50 |
def print: String = print(false) |
|
51 |
def print_default: String = print(true) |
|
49247 | 52 |
|
56559 | 53 |
def title(strip: String = ""): String = |
49270 | 54 |
{ |
56600 | 55 |
val words = Word.explode('_', name) |
49270 | 56 |
val words1 = |
57 |
words match { |
|
58 |
case word :: rest if word == strip => rest |
|
59 |
case _ => words |
|
60 |
} |
|
56609
5ac67041ccf8
capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents:
56600
diff
changeset
|
61 |
Word.implode(words1.map(Word.perhaps_capitalize(_))) |
49270 | 62 |
} |
63 |
||
48860 | 64 |
def unknown: Boolean = typ == Unknown |
65 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
66 |
|
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 |
/* parsing */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
69 |
|
49270 | 70 |
private val SECTION = "section" |
52065
78f2475aa126
explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents:
51945
diff
changeset
|
71 |
private val PUBLIC = "public" |
48795
bece259ee055
clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents:
48718
diff
changeset
|
72 |
private val OPTION = "option" |
48807 | 73 |
private val OPTIONS = Path.explode("etc/options") |
50793 | 74 |
private val PREFS_DIR = Path.explode("$ISABELLE_HOME_USER/etc") |
75 |
private val PREFS = PREFS_DIR + Path.basic("preferences") |
|
48713
de26cf3191a3
more token markers, based on actual outer syntax;
wenzelm
parents:
48693
diff
changeset
|
76 |
|
49270 | 77 |
lazy val options_syntax = |
61579 | 78 |
Outer_Syntax.init() + ":" + "=" + "--" + Symbol.comment + Symbol.comment_decoded + |
60133
a90982bbe8b4
clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents:
59811
diff
changeset
|
79 |
(SECTION, Keyword.DOCUMENT_HEADING) + PUBLIC + (OPTION, Keyword.THY_DECL) |
49270 | 80 |
|
48807 | 81 |
lazy val prefs_syntax = Outer_Syntax.init() + "=" |
48713
de26cf3191a3
more token markers, based on actual outer syntax;
wenzelm
parents:
48693
diff
changeset
|
82 |
|
48807 | 83 |
object Parser extends Parse.Parser |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
84 |
{ |
48807 | 85 |
val option_name = atom("option name", _.is_xname) |
86 |
val option_type = atom("option type", _.is_ident) |
|
87 |
val option_value = |
|
88 |
opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^ |
|
89 |
{ case s ~ n => if (s.isDefined) "-" + n else n } | |
|
90 |
atom("option value", tok => tok.is_name || tok.is_float) |
|
91 |
||
61579 | 92 |
def comment_marker: Parser[String] = |
93 |
$$$("--") | $$$(Symbol.comment) | $$$(Symbol.comment_decoded) |
|
94 |
||
48807 | 95 |
val option_entry: Parser[Options => Options] = |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
96 |
{ |
49270 | 97 |
command(SECTION) ~! text ^^ |
49295 | 98 |
{ case _ ~ a => (options: Options) => options.set_section(a) } | |
60133
a90982bbe8b4
clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents:
59811
diff
changeset
|
99 |
opt($$$(PUBLIC)) ~ command(OPTION) ~! (position(option_name) ~ $$$(":") ~ option_type ~ |
61579 | 100 |
$$$("=") ~ option_value ~ (comment_marker ~! text ^^ { case _ ~ x => x } | success(""))) ^^ |
59811 | 101 |
{ case a ~ _ ~ ((b, pos) ~ _ ~ c ~ _ ~ d ~ e) => |
56465 | 102 |
(options: Options) => options.declare(a.isDefined, pos, b, c, d, e) } |
48365
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 |
|
48807 | 105 |
val prefs_entry: Parser[Options => Options] = |
106 |
{ |
|
58908 | 107 |
option_name ~ ($$$("=") ~! option_value) ^^ |
48860 | 108 |
{ case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) } |
48807 | 109 |
} |
110 |
||
111 |
def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options], |
|
112 |
options: Options, file: Path): Options = |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
113 |
{ |
59083 | 114 |
val toks = Token.explode(syntax.keywords, File.read(file)) |
48807 | 115 |
val ops = |
59705 | 116 |
parse_all(rep(parser), Token.reader(toks, Token.Pos.file(file.implode))) match { |
48807 | 117 |
case Success(result, _) => result |
118 |
case bad => error(bad.toString) |
|
119 |
} |
|
49270 | 120 |
try { (options.set_section("") /: ops) { case (opts, op) => op(opts) } } |
48992 | 121 |
catch { case ERROR(msg) => error(msg + Position.here(file.position)) } |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
122 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
123 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
124 |
|
48807 | 125 |
def init_defaults(): Options = |
48365
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 |
var options = empty |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
128 |
for { |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
129 |
dir <- Isabelle_System.components() |
48548 | 130 |
file = dir + OPTIONS if file.is_file |
48807 | 131 |
} { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) } |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
132 |
options |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
133 |
} |
48457 | 134 |
|
48807 | 135 |
def init(): Options = init_defaults().load_prefs() |
136 |
||
48457 | 137 |
|
138 |
/* encode */ |
|
139 |
||
140 |
val encode: XML.Encode.T[Options] = (options => options.encode) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
141 |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
142 |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
143 |
/* command line entry point */ |
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
144 |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
145 |
def main(args: Array[String]) |
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
146 |
{ |
56631 | 147 |
Command_Line.tool0 { |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
148 |
args.toList match { |
52735 | 149 |
case get_option :: export_file :: more_options => |
48807 | 150 |
val options = (Options.init() /: more_options)(_ + _) |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
151 |
|
52735 | 152 |
if (get_option != "") |
56831
e3ccf0809d51
prefer scala.Console with its support for thread-local redirection;
wenzelm
parents:
56631
diff
changeset
|
153 |
Console.println(options.check_name(get_option).value) |
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
154 |
|
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
155 |
if (export_file != "") |
52735 | 156 |
File.write(Path.explode(export_file), YXML.string_of_body(options.encode)) |
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
157 |
|
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
158 |
if (get_option == "" && export_file == "") |
56831
e3ccf0809d51
prefer scala.Console with its support for thread-local redirection;
wenzelm
parents:
56631
diff
changeset
|
159 |
Console.println(options.print) |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
160 |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
161 |
case _ => error("Bad arguments:\n" + cat_lines(args)) |
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
162 |
} |
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
163 |
} |
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
164 |
} |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
165 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
166 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
167 |
|
49270 | 168 |
final class Options private( |
49296 | 169 |
val options: Map[String, Options.Opt] = Map.empty, |
49270 | 170 |
val section: String = "") |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
171 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
172 |
override def toString: String = options.iterator.mkString("Options (", ",", ")") |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
173 |
|
54347 | 174 |
private def print_opt(opt: Options.Opt): String = |
175 |
if (opt.public) "public " + opt.print else opt.print |
|
176 |
||
177 |
def print: String = cat_lines(options.toList.sortBy(_._1).map(p => print_opt(p._2))) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
178 |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
179 |
def description(name: String): String = check_name(name).description |
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
180 |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
181 |
|
48370 | 182 |
/* check */ |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
183 |
|
49246 | 184 |
def check_name(name: String): Options.Opt = |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
185 |
options.get(name) match { |
48860 | 186 |
case Some(opt) if !opt.unknown => opt |
187 |
case _ => error("Unknown option " + quote(name)) |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
188 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
189 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
190 |
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
|
191 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
192 |
val opt = check_name(name) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
193 |
if (opt.typ == typ) opt |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
194 |
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
|
195 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
196 |
|
48370 | 197 |
|
198 |
/* basic operations */ |
|
199 |
||
200 |
private def put[A](name: String, typ: Options.Type, value: String): Options = |
|
201 |
{ |
|
202 |
val opt = check_type(name, typ) |
|
49270 | 203 |
new Options(options + (name -> opt.copy(value = value)), section) |
48370 | 204 |
} |
205 |
||
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
206 |
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
|
207 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
208 |
val opt = check_type(name, typ) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
209 |
parse(opt.value) match { |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
210 |
case Some(x) => x |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
211 |
case None => |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
212 |
error("Malformed value for option " + quote(name) + |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
213 |
" : " + typ.print + " =\n" + quote(opt.value)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
214 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
215 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
216 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
217 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
218 |
/* internal lookup and update */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
219 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
220 |
class Bool_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
221 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
222 |
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
|
223 |
def update(name: String, x: Boolean): Options = |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
224 |
put(name, Options.Bool, Properties.Value.Boolean(x)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
225 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
226 |
val bool = new Bool_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
227 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
228 |
class Int_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
229 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
230 |
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
|
231 |
def update(name: String, x: Int): Options = |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
232 |
put(name, Options.Int, Properties.Value.Int(x)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
233 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
234 |
val int = new Int_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
235 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
236 |
class Real_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
237 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
238 |
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
|
239 |
def update(name: String, x: Double): Options = |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
240 |
put(name, Options.Real, Properties.Value.Double(x)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
241 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
242 |
val real = new Real_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
243 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
244 |
class String_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
245 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
246 |
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
|
247 |
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
|
248 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
249 |
val string = new String_Access |
48370 | 250 |
|
50207 | 251 |
class Seconds_Access |
252 |
{ |
|
253 |
def apply(name: String): Time = Time.seconds(real(name)) |
|
254 |
} |
|
255 |
val seconds = new Seconds_Access |
|
256 |
||
48370 | 257 |
|
48807 | 258 |
/* external updates */ |
48370 | 259 |
|
260 |
private def check_value(name: String): Options = |
|
261 |
{ |
|
262 |
val opt = check_name(name) |
|
263 |
opt.typ match { |
|
264 |
case Options.Bool => bool(name); this |
|
265 |
case Options.Int => int(name); this |
|
266 |
case Options.Real => real(name); this |
|
267 |
case Options.String => string(name); this |
|
48860 | 268 |
case Options.Unknown => this |
48370 | 269 |
} |
270 |
} |
|
271 |
||
56465 | 272 |
def declare( |
273 |
public: Boolean, |
|
274 |
pos: Position.T, |
|
275 |
name: String, |
|
276 |
typ_name: String, |
|
277 |
value: String, |
|
278 |
description: String): Options = |
|
48370 | 279 |
{ |
280 |
options.get(name) match { |
|
56465 | 281 |
case Some(other) => |
282 |
error("Duplicate declaration of option " + quote(name) + Position.here(pos) + |
|
283 |
Position.here(other.pos)) |
|
48370 | 284 |
case None => |
285 |
val typ = |
|
286 |
typ_name match { |
|
287 |
case "bool" => Options.Bool |
|
288 |
case "int" => Options.Int |
|
289 |
case "real" => Options.Real |
|
290 |
case "string" => Options.String |
|
56465 | 291 |
case _ => |
292 |
error("Unknown type for option " + quote(name) + " : " + quote(typ_name) + |
|
293 |
Position.here(pos)) |
|
48370 | 294 |
} |
56465 | 295 |
val opt = Options.Opt(public, pos, name, typ, value, value, description, section) |
49270 | 296 |
(new Options(options + (name -> opt), section)).check_value(name) |
48370 | 297 |
} |
298 |
} |
|
299 |
||
48860 | 300 |
def add_permissive(name: String, value: String): Options = |
301 |
{ |
|
302 |
if (options.isDefinedAt(name)) this + (name, value) |
|
56465 | 303 |
else { |
304 |
val opt = Options.Opt(false, Position.none, name, Options.Unknown, value, value, "", "") |
|
305 |
new Options(options + (name -> opt), section) |
|
306 |
} |
|
48860 | 307 |
} |
308 |
||
48807 | 309 |
def + (name: String, value: String): Options = |
48370 | 310 |
{ |
311 |
val opt = check_name(name) |
|
49270 | 312 |
(new Options(options + (name -> opt.copy(value = value)), section)).check_value(name) |
48370 | 313 |
} |
314 |
||
48807 | 315 |
def + (name: String, opt_value: Option[String]): Options = |
48370 | 316 |
{ |
317 |
val opt = check_name(name) |
|
318 |
opt_value match { |
|
48807 | 319 |
case Some(value) => this + (name, value) |
320 |
case None if opt.typ == Options.Bool => this + (name, "true") |
|
48370 | 321 |
case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print) |
322 |
} |
|
323 |
} |
|
324 |
||
48807 | 325 |
def + (str: String): Options = |
48370 | 326 |
{ |
327 |
str.indexOf('=') match { |
|
48807 | 328 |
case -1 => this + (str, None) |
329 |
case i => this + (str.substring(0, i), str.substring(i + 1)) |
|
48370 | 330 |
} |
331 |
} |
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
332 |
|
48807 | 333 |
def ++ (specs: List[Options.Spec]): Options = |
334 |
(this /: specs)({ case (x, (y, z)) => x + (y, z) }) |
|
335 |
||
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
336 |
|
49270 | 337 |
/* sections */ |
338 |
||
339 |
def set_section(new_section: String): Options = |
|
340 |
new Options(options, new_section) |
|
341 |
||
342 |
def sections: List[(String, List[Options.Opt])] = |
|
343 |
options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) }) |
|
344 |
||
345 |
||
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
346 |
/* encode */ |
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
347 |
|
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
348 |
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
|
349 |
{ |
48860 | 350 |
val opts = |
56465 | 351 |
for ((_, opt) <- options.toList; if !opt.unknown) |
352 |
yield (opt.pos, (opt.name, (opt.typ.print, opt.value))) |
|
48860 | 353 |
|
56465 | 354 |
import XML.Encode.{string => string_, _} |
355 |
list(pair(properties, pair(string_, pair(string_, string_))))(opts) |
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
356 |
} |
48807 | 357 |
|
358 |
||
359 |
/* user preferences */ |
|
360 |
||
361 |
def load_prefs(): Options = |
|
362 |
if (Options.PREFS.is_file) |
|
363 |
Options.Parser.parse_file( |
|
364 |
Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS) |
|
365 |
else this |
|
366 |
||
367 |
def save_prefs() |
|
368 |
{ |
|
48860 | 369 |
val defaults = Options.init_defaults() |
48807 | 370 |
val changed = |
371 |
(for { |
|
48860 | 372 |
(name, opt2) <- options.iterator |
373 |
opt1 = defaults.options.get(name) |
|
60215 | 374 |
if opt1.isEmpty || opt1.get.value != opt2.value |
48860 | 375 |
} yield (name, opt2.value, if (opt1.isEmpty) " (* unknown *)" else "")).toList |
376 |
||
48807 | 377 |
val prefs = |
378 |
changed.sortBy(_._1) |
|
48860 | 379 |
.map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString |
48807 | 380 |
|
50893
d55eb82ae77b
Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
wenzelm
parents:
50793
diff
changeset
|
381 |
Isabelle_System.mkdirs(Options.PREFS_DIR) |
53336 | 382 |
File.write_backup(Options.PREFS, |
48807 | 383 |
"(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs) |
384 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
385 |
} |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
386 |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
387 |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
388 |
class Options_Variable |
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
389 |
{ |
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
390 |
private var options = Options.empty |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
391 |
|
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
392 |
def value: Options = synchronized { options } |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
393 |
def update(new_options: Options): Unit = synchronized { options = new_options } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
394 |
|
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
395 |
def + (name: String, x: String): Unit = synchronized { options = options + (name, x) } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
396 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
397 |
class Bool_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
398 |
{ |
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
399 |
def apply(name: String): Boolean = synchronized { options.bool(name) } |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
400 |
def update(name: String, x: Boolean): Unit = |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
401 |
synchronized { options = options.bool.update(name, x) } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
402 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
403 |
val bool = new Bool_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
404 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
405 |
class Int_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
406 |
{ |
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
407 |
def apply(name: String): Int = synchronized { options.int(name) } |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
408 |
def update(name: String, x: Int): Unit = |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
409 |
synchronized { options = options.int.update(name, x) } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
410 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
411 |
val int = new Int_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
412 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
413 |
class Real_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
414 |
{ |
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
415 |
def apply(name: String): Double = synchronized { options.real(name) } |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
416 |
def update(name: String, x: Double): Unit = |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
417 |
synchronized { options = options.real.update(name, x) } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
418 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
419 |
val real = new Real_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
420 |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
421 |
class String_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
422 |
{ |
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
423 |
def apply(name: String): String = synchronized { options.string(name) } |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
424 |
def update(name: String, x: String): Unit = |
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
425 |
synchronized { options = options.string.update(name, x) } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
426 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
427 |
val string = new String_Access |
50207 | 428 |
|
429 |
class Seconds_Access |
|
430 |
{ |
|
51620
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
wenzelm
parents:
50893
diff
changeset
|
431 |
def apply(name: String): Time = synchronized { options.seconds(name) } |
50207 | 432 |
} |
433 |
val seconds = new Seconds_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
434 |
} |