author | wenzelm |
Sat, 27 Feb 2016 19:47:53 +0100 | |
changeset 62437 | bccad0374407 |
parent 62227 | 6eeaaefcea56 |
child 62454 | 38c89353b349 |
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 { |
62437 | 148 |
var build_options = false |
149 |
var get_option = "" |
|
150 |
var list_options = false |
|
151 |
var export_file = "" |
|
152 |
||
153 |
val getopts = Getopts(() => """ |
|
154 |
Usage: isabelle options [OPTIONS] [MORE_OPTIONS ...] |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
155 |
|
62437 | 156 |
Options are: |
157 |
-b include $ISABELLE_BUILD_OPTIONS |
|
158 |
-g OPTION get value of OPTION |
|
159 |
-l list options |
|
160 |
-x FILE export options to FILE in YXML format |
|
161 |
||
162 |
Report Isabelle system options, augmented by MORE_OPTIONS given as |
|
163 |
arguments NAME=VAL or NAME. |
|
164 |
""", |
|
165 |
"b" -> (_ => build_options = true), |
|
166 |
"g:" -> (arg => get_option = arg), |
|
167 |
"l" -> (_ => list_options = true), |
|
168 |
"x:" -> (arg => export_file = arg)) |
|
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
169 |
|
62437 | 170 |
val more_options = getopts(args) |
171 |
if (get_option == "" && !list_options && export_file == "") getopts.usage() |
|
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
172 |
|
62437 | 173 |
val options = |
174 |
{ |
|
175 |
val options0 = Options.init() |
|
176 |
val options1 = |
|
177 |
if (build_options) |
|
178 |
(options0 /: Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS")))(_ + _) |
|
179 |
else options0 |
|
180 |
(options1 /: more_options)(_ + _) |
|
181 |
} |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
182 |
|
62437 | 183 |
if (get_option != "") |
184 |
Console.println(options.check_name(get_option).value) |
|
185 |
||
186 |
if (export_file != "") |
|
187 |
File.write(Path.explode(export_file), YXML.string_of_body(options.encode)) |
|
188 |
||
189 |
if (get_option == "" && export_file == "") |
|
190 |
Console.println(options.print) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
191 |
} |
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
192 |
} |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
193 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
194 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
195 |
|
49270 | 196 |
final class Options private( |
49296 | 197 |
val options: Map[String, Options.Opt] = Map.empty, |
49270 | 198 |
val section: String = "") |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
199 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
200 |
override def toString: String = options.iterator.mkString("Options (", ",", ")") |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
201 |
|
54347 | 202 |
private def print_opt(opt: Options.Opt): String = |
203 |
if (opt.public) "public " + opt.print else opt.print |
|
204 |
||
205 |
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
|
206 |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
207 |
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
|
208 |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
209 |
|
48370 | 210 |
/* check */ |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
211 |
|
49246 | 212 |
def check_name(name: String): Options.Opt = |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
213 |
options.get(name) match { |
48860 | 214 |
case Some(opt) if !opt.unknown => opt |
215 |
case _ => error("Unknown option " + quote(name)) |
|
48365
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 |
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
|
219 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
220 |
val opt = check_name(name) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
221 |
if (opt.typ == typ) opt |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
222 |
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
|
223 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
224 |
|
48370 | 225 |
|
226 |
/* basic operations */ |
|
227 |
||
228 |
private def put[A](name: String, typ: Options.Type, value: String): Options = |
|
229 |
{ |
|
230 |
val opt = check_type(name, typ) |
|
49270 | 231 |
new Options(options + (name -> opt.copy(value = value)), section) |
48370 | 232 |
} |
233 |
||
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
234 |
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
|
235 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
236 |
val opt = check_type(name, typ) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
237 |
parse(opt.value) match { |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
238 |
case Some(x) => x |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
239 |
case None => |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
240 |
error("Malformed value for option " + quote(name) + |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
241 |
" : " + typ.print + " =\n" + quote(opt.value)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
242 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
243 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
244 |
|
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 |
/* internal lookup and update */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
247 |
|
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
|
248 |
class Bool_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
249 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
250 |
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
|
251 |
def update(name: String, x: Boolean): Options = |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
252 |
put(name, Options.Bool, Properties.Value.Boolean(x)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
253 |
} |
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
|
254 |
val bool = new Bool_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
255 |
|
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
|
256 |
class Int_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
257 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
258 |
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
|
259 |
def update(name: String, x: Int): Options = |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
260 |
put(name, Options.Int, Properties.Value.Int(x)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
261 |
} |
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
|
262 |
val int = new Int_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
263 |
|
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
|
264 |
class Real_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
265 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
266 |
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
|
267 |
def update(name: String, x: Double): Options = |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
268 |
put(name, Options.Real, Properties.Value.Double(x)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
269 |
} |
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
|
270 |
val real = new Real_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
271 |
|
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
|
272 |
class String_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
273 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
274 |
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
|
275 |
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
|
276 |
} |
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
|
277 |
val string = new String_Access |
48370 | 278 |
|
50207 | 279 |
class Seconds_Access |
280 |
{ |
|
281 |
def apply(name: String): Time = Time.seconds(real(name)) |
|
282 |
} |
|
283 |
val seconds = new Seconds_Access |
|
284 |
||
48370 | 285 |
|
48807 | 286 |
/* external updates */ |
48370 | 287 |
|
288 |
private def check_value(name: String): Options = |
|
289 |
{ |
|
290 |
val opt = check_name(name) |
|
291 |
opt.typ match { |
|
292 |
case Options.Bool => bool(name); this |
|
293 |
case Options.Int => int(name); this |
|
294 |
case Options.Real => real(name); this |
|
295 |
case Options.String => string(name); this |
|
48860 | 296 |
case Options.Unknown => this |
48370 | 297 |
} |
298 |
} |
|
299 |
||
56465 | 300 |
def declare( |
301 |
public: Boolean, |
|
302 |
pos: Position.T, |
|
303 |
name: String, |
|
304 |
typ_name: String, |
|
305 |
value: String, |
|
306 |
description: String): Options = |
|
48370 | 307 |
{ |
308 |
options.get(name) match { |
|
56465 | 309 |
case Some(other) => |
310 |
error("Duplicate declaration of option " + quote(name) + Position.here(pos) + |
|
311 |
Position.here(other.pos)) |
|
48370 | 312 |
case None => |
313 |
val typ = |
|
314 |
typ_name match { |
|
315 |
case "bool" => Options.Bool |
|
316 |
case "int" => Options.Int |
|
317 |
case "real" => Options.Real |
|
318 |
case "string" => Options.String |
|
56465 | 319 |
case _ => |
320 |
error("Unknown type for option " + quote(name) + " : " + quote(typ_name) + |
|
321 |
Position.here(pos)) |
|
48370 | 322 |
} |
56465 | 323 |
val opt = Options.Opt(public, pos, name, typ, value, value, description, section) |
49270 | 324 |
(new Options(options + (name -> opt), section)).check_value(name) |
48370 | 325 |
} |
326 |
} |
|
327 |
||
48860 | 328 |
def add_permissive(name: String, value: String): Options = |
329 |
{ |
|
330 |
if (options.isDefinedAt(name)) this + (name, value) |
|
56465 | 331 |
else { |
332 |
val opt = Options.Opt(false, Position.none, name, Options.Unknown, value, value, "", "") |
|
333 |
new Options(options + (name -> opt), section) |
|
334 |
} |
|
48860 | 335 |
} |
336 |
||
48807 | 337 |
def + (name: String, value: String): Options = |
48370 | 338 |
{ |
339 |
val opt = check_name(name) |
|
49270 | 340 |
(new Options(options + (name -> opt.copy(value = value)), section)).check_value(name) |
48370 | 341 |
} |
342 |
||
48807 | 343 |
def + (name: String, opt_value: Option[String]): Options = |
48370 | 344 |
{ |
345 |
val opt = check_name(name) |
|
346 |
opt_value match { |
|
48807 | 347 |
case Some(value) => this + (name, value) |
348 |
case None if opt.typ == Options.Bool => this + (name, "true") |
|
48370 | 349 |
case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print) |
350 |
} |
|
351 |
} |
|
352 |
||
48807 | 353 |
def + (str: String): Options = |
48370 | 354 |
{ |
355 |
str.indexOf('=') match { |
|
48807 | 356 |
case -1 => this + (str, None) |
357 |
case i => this + (str.substring(0, i), str.substring(i + 1)) |
|
48370 | 358 |
} |
359 |
} |
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
360 |
|
48807 | 361 |
def ++ (specs: List[Options.Spec]): Options = |
362 |
(this /: specs)({ case (x, (y, z)) => x + (y, z) }) |
|
363 |
||
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
364 |
|
49270 | 365 |
/* sections */ |
366 |
||
367 |
def set_section(new_section: String): Options = |
|
368 |
new Options(options, new_section) |
|
369 |
||
370 |
def sections: List[(String, List[Options.Opt])] = |
|
371 |
options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) }) |
|
372 |
||
373 |
||
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
374 |
/* encode */ |
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
375 |
|
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
376 |
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
|
377 |
{ |
48860 | 378 |
val opts = |
56465 | 379 |
for ((_, opt) <- options.toList; if !opt.unknown) |
380 |
yield (opt.pos, (opt.name, (opt.typ.print, opt.value))) |
|
48860 | 381 |
|
56465 | 382 |
import XML.Encode.{string => string_, _} |
383 |
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
|
384 |
} |
48807 | 385 |
|
386 |
||
387 |
/* user preferences */ |
|
388 |
||
389 |
def load_prefs(): Options = |
|
390 |
if (Options.PREFS.is_file) |
|
391 |
Options.Parser.parse_file( |
|
392 |
Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS) |
|
393 |
else this |
|
394 |
||
395 |
def save_prefs() |
|
396 |
{ |
|
48860 | 397 |
val defaults = Options.init_defaults() |
48807 | 398 |
val changed = |
399 |
(for { |
|
48860 | 400 |
(name, opt2) <- options.iterator |
401 |
opt1 = defaults.options.get(name) |
|
60215 | 402 |
if opt1.isEmpty || opt1.get.value != opt2.value |
48860 | 403 |
} yield (name, opt2.value, if (opt1.isEmpty) " (* unknown *)" else "")).toList |
404 |
||
48807 | 405 |
val prefs = |
406 |
changed.sortBy(_._1) |
|
48860 | 407 |
.map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString |
48807 | 408 |
|
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
|
409 |
Isabelle_System.mkdirs(Options.PREFS_DIR) |
53336 | 410 |
File.write_backup(Options.PREFS, |
48807 | 411 |
"(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs) |
412 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
413 |
} |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
414 |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
415 |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
416 |
class Options_Variable |
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
417 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
418 |
private var options: Option[Options] = None |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
419 |
|
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
420 |
def store(new_options: Options): Unit = synchronized { options = Some(new_options) } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
421 |
|
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
422 |
def value: Options = synchronized { |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
423 |
options match { |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
424 |
case Some(opts) => opts |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
425 |
case None => error("Uninitialized Isabelle system options") |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
426 |
} |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
427 |
} |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
428 |
|
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
429 |
private def upd(f: Options => Options): Unit = synchronized { options = Some(f(value)) } |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
430 |
|
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
431 |
def + (name: String, x: String): Unit = upd(opts => opts + (name, x)) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
432 |
|
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
|
433 |
class Bool_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
434 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
435 |
def apply(name: String): Boolean = value.bool(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
436 |
def update(name: String, x: Boolean): Unit = upd(opts => opts.bool.update(name, x)) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
437 |
} |
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
|
438 |
val bool = new Bool_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
439 |
|
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
|
440 |
class Int_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
441 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
442 |
def apply(name: String): Int = value.int(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
443 |
def update(name: String, x: Int): Unit = upd(opts => opts.int.update(name, x)) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
444 |
} |
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
|
445 |
val int = new Int_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
446 |
|
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
|
447 |
class Real_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
448 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
449 |
def apply(name: String): Double = value.real(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
450 |
def update(name: String, x: Double): Unit = upd(opts => opts.real.update(name, x)) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
451 |
} |
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
|
452 |
val real = new Real_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
453 |
|
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
|
454 |
class String_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
455 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
456 |
def apply(name: String): String = value.string(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
457 |
def update(name: String, x: String): Unit = upd(opts => opts.string.update(name, x)) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
458 |
} |
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
|
459 |
val string = new String_Access |
50207 | 460 |
|
461 |
class Seconds_Access |
|
462 |
{ |
|
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
463 |
def apply(name: String): Time = value.seconds(name) |
50207 | 464 |
} |
465 |
val seconds = new Seconds_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
466 |
} |