author | wenzelm |
Mon, 17 May 2021 14:07:51 +0200 | |
changeset 73715 | bf51c23f3f99 |
parent 73712 | 3eba8d4b624b |
child 73815 | 43882e34c038 |
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 |
|
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 |
{ |
56599 | 21 |
def print: String = Word.lowercase(toString) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
22 |
} |
49246 | 23 |
case object Bool extends Type |
24 |
case object Int extends Type |
|
25 |
case object Real extends Type |
|
26 |
case object String extends Type |
|
27 |
case object Unknown extends Type |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
28 |
|
56465 | 29 |
case class Opt( |
30 |
public: Boolean, |
|
31 |
pos: Position.T, |
|
32 |
name: String, |
|
33 |
typ: Type, |
|
34 |
value: String, |
|
35 |
default_value: String, |
|
36 |
description: String, |
|
37 |
section: String) |
|
48860 | 38 |
{ |
49289 | 39 |
private def print(default: Boolean): String = |
40 |
{ |
|
41 |
val x = if (default) default_value else value |
|
49247 | 42 |
"option " + name + " : " + typ.print + " = " + |
49289 | 43 |
(if (typ == Options.String) quote(x) else x) + |
44 |
(if (description == "") "" else "\n -- " + quote(description)) |
|
45 |
} |
|
46 |
||
47 |
def print: String = print(false) |
|
48 |
def print_default: String = print(true) |
|
49247 | 49 |
|
56559 | 50 |
def title(strip: String = ""): String = |
49270 | 51 |
{ |
56600 | 52 |
val words = Word.explode('_', name) |
49270 | 53 |
val words1 = |
54 |
words match { |
|
55 |
case word :: rest if word == strip => rest |
|
56 |
case _ => words |
|
57 |
} |
|
71601 | 58 |
Word.implode(words1.map(Word.perhaps_capitalize)) |
49270 | 59 |
} |
60 |
||
48860 | 61 |
def unknown: Boolean = typ == Unknown |
62 |
} |
|
48365
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 |
/* parsing */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
66 |
|
49270 | 67 |
private val SECTION = "section" |
52065
78f2475aa126
explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents:
51945
diff
changeset
|
68 |
private val PUBLIC = "public" |
48795
bece259ee055
clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents:
48718
diff
changeset
|
69 |
private val OPTION = "option" |
48807 | 70 |
private val OPTIONS = Path.explode("etc/options") |
67845 | 71 |
private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences") |
48713
de26cf3191a3
more token markers, based on actual outer syntax;
wenzelm
parents:
48693
diff
changeset
|
72 |
|
71601 | 73 |
val options_syntax: Outer_Syntax = |
67004
af72fa58f71b
clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents:
66984
diff
changeset
|
74 |
Outer_Syntax.empty + ":" + "=" + "--" + Symbol.comment + Symbol.comment_decoded + |
63441 | 75 |
(SECTION, Keyword.DOCUMENT_HEADING) + |
76 |
(PUBLIC, Keyword.BEFORE_COMMAND) + |
|
77 |
(OPTION, Keyword.THY_DECL) |
|
49270 | 78 |
|
71601 | 79 |
val prefs_syntax: Outer_Syntax = Outer_Syntax.empty + "=" |
48713
de26cf3191a3
more token markers, based on actual outer syntax;
wenzelm
parents:
48693
diff
changeset
|
80 |
|
62968 | 81 |
trait Parser extends Parse.Parser |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
82 |
{ |
71601 | 83 |
val option_name: Parser[String] = atom("option name", _.is_name) |
84 |
val option_type: Parser[String] = atom("option type", _.is_name) |
|
85 |
val option_value: Parser[String] = |
|
48807 | 86 |
opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^ |
87 |
{ case s ~ n => if (s.isDefined) "-" + n else n } | |
|
88 |
atom("option value", tok => tok.is_name || tok.is_float) |
|
62968 | 89 |
} |
48807 | 90 |
|
68841 | 91 |
private object Parser extends Parser |
62968 | 92 |
{ |
61579 | 93 |
def comment_marker: Parser[String] = |
94 |
$$$("--") | $$$(Symbol.comment) | $$$(Symbol.comment_decoded) |
|
95 |
||
48807 | 96 |
val option_entry: Parser[Options => Options] = |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
97 |
{ |
49270 | 98 |
command(SECTION) ~! text ^^ |
49295 | 99 |
{ case _ ~ a => (options: Options) => options.set_section(a) } | |
60133
a90982bbe8b4
clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents:
59811
diff
changeset
|
100 |
opt($$$(PUBLIC)) ~ command(OPTION) ~! (position(option_name) ~ $$$(":") ~ option_type ~ |
61579 | 101 |
$$$("=") ~ option_value ~ (comment_marker ~! text ^^ { case _ ~ x => x } | success(""))) ^^ |
59811 | 102 |
{ case a ~ _ ~ ((b, pos) ~ _ ~ c ~ _ ~ d ~ e) => |
56465 | 103 |
(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
|
104 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
105 |
|
48807 | 106 |
val prefs_entry: Parser[Options => Options] = |
107 |
{ |
|
58908 | 108 |
option_name ~ ($$$("=") ~! option_value) ^^ |
48860 | 109 |
{ case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) } |
48807 | 110 |
} |
111 |
||
67845 | 112 |
def parse_file(options: Options, file_name: String, content: String, |
113 |
syntax: Outer_Syntax = options_syntax, |
|
114 |
parser: Parser[Options => Options] = option_entry): Options = |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
115 |
{ |
67845 | 116 |
val toks = Token.explode(syntax.keywords, content) |
48807 | 117 |
val ops = |
67845 | 118 |
parse_all(rep(parser), Token.reader(toks, Token.Pos.file(file_name))) match { |
48807 | 119 |
case Success(result, _) => result |
120 |
case bad => error(bad.toString) |
|
121 |
} |
|
73359 | 122 |
try { ops.foldLeft(options.set_section("")) { case (opts, op) => op(opts) } } |
73166 | 123 |
catch { case ERROR(msg) => error(msg + Position.here(Position.File(file_name))) } |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
124 |
} |
67845 | 125 |
|
126 |
def parse_prefs(options: Options, content: String): Options = |
|
69366 | 127 |
parse_file(options, PREFS.file_name, content, syntax = prefs_syntax, parser = prefs_entry) |
48365
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 |
|
67845 | 130 |
def read_prefs(file: Path = PREFS): String = |
131 |
if (file.is_file) File.read(file) else "" |
|
64186
49816908ae42
support for separate sub-system options, independent of main Isabelle options;
wenzelm
parents:
64151
diff
changeset
|
132 |
|
67847 | 133 |
def init(prefs: String = read_prefs(PREFS), opts: List[String] = Nil): Options = |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
134 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
135 |
var options = empty |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
136 |
for { |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
137 |
dir <- Isabelle_System.components() |
48548 | 138 |
file = dir + OPTIONS if file.is_file |
67845 | 139 |
} { options = Parser.parse_file(options, file.implode, File.read(file)) } |
73359 | 140 |
opts.foldLeft(Options.Parser.parse_prefs(options, prefs))(_ + _) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
141 |
} |
48457 | 142 |
|
143 |
||
144 |
/* encode */ |
|
145 |
||
146 |
val encode: XML.Encode.T[Options] = (options => options.encode) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
147 |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
148 |
|
62832 | 149 |
/* Isabelle tool wrapper */ |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
150 |
|
72763 | 151 |
val isabelle_tool = Isabelle_Tool("options", "print Isabelle system options", |
152 |
Scala_Project.here, args => |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
153 |
{ |
62832 | 154 |
var build_options = false |
155 |
var get_option = "" |
|
156 |
var list_options = false |
|
157 |
var export_file = "" |
|
62437 | 158 |
|
62832 | 159 |
val getopts = Getopts(""" |
62437 | 160 |
Usage: isabelle options [OPTIONS] [MORE_OPTIONS ...] |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
161 |
|
62437 | 162 |
Options are: |
163 |
-b include $ISABELLE_BUILD_OPTIONS |
|
164 |
-g OPTION get value of OPTION |
|
165 |
-l list options |
|
166 |
-x FILE export options to FILE in YXML format |
|
167 |
||
168 |
Report Isabelle system options, augmented by MORE_OPTIONS given as |
|
169 |
arguments NAME=VAL or NAME. |
|
170 |
""", |
|
62832 | 171 |
"b" -> (_ => build_options = true), |
172 |
"g:" -> (arg => get_option = arg), |
|
173 |
"l" -> (_ => list_options = true), |
|
174 |
"x:" -> (arg => export_file = arg)) |
|
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
175 |
|
62832 | 176 |
val more_options = getopts(args) |
177 |
if (get_option == "" && !list_options && export_file == "") getopts.usage() |
|
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
178 |
|
62832 | 179 |
val options = |
180 |
{ |
|
181 |
val options0 = Options.init() |
|
182 |
val options1 = |
|
183 |
if (build_options) |
|
73359 | 184 |
Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS")).foldLeft(options0)(_ + _) |
62832 | 185 |
else options0 |
73359 | 186 |
more_options.foldLeft(options1)(_ + _) |
62832 | 187 |
} |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
188 |
|
62832 | 189 |
if (get_option != "") |
67178 | 190 |
Output.writeln(options.check_name(get_option).value, stdout = true) |
62437 | 191 |
|
62832 | 192 |
if (export_file != "") |
193 |
File.write(Path.explode(export_file), YXML.string_of_body(options.encode)) |
|
62437 | 194 |
|
62832 | 195 |
if (get_option == "" && export_file == "") |
67178 | 196 |
Output.writeln(options.print, stdout = true) |
62832 | 197 |
}) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
198 |
} |
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 |
|
49270 | 201 |
final class Options private( |
49296 | 202 |
val options: Map[String, Options.Opt] = Map.empty, |
49270 | 203 |
val section: String = "") |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
204 |
{ |
64186
49816908ae42
support for separate sub-system options, independent of main Isabelle options;
wenzelm
parents:
64151
diff
changeset
|
205 |
override def toString: String = options.iterator.mkString("Options(", ",", ")") |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
206 |
|
54347 | 207 |
private def print_opt(opt: Options.Opt): String = |
208 |
if (opt.public) "public " + opt.print else opt.print |
|
209 |
||
210 |
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
|
211 |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
212 |
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
|
213 |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
214 |
|
48370 | 215 |
/* check */ |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
216 |
|
49246 | 217 |
def check_name(name: String): Options.Opt = |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
218 |
options.get(name) match { |
48860 | 219 |
case Some(opt) if !opt.unknown => opt |
220 |
case _ => error("Unknown option " + quote(name)) |
|
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 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
223 |
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
|
224 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
225 |
val opt = check_name(name) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
226 |
if (opt.typ == typ) opt |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
227 |
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
|
228 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
229 |
|
48370 | 230 |
|
231 |
/* basic operations */ |
|
232 |
||
233 |
private def put[A](name: String, typ: Options.Type, value: String): Options = |
|
234 |
{ |
|
235 |
val opt = check_type(name, typ) |
|
49270 | 236 |
new Options(options + (name -> opt.copy(value = value)), section) |
48370 | 237 |
} |
238 |
||
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
239 |
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
|
240 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
241 |
val opt = check_type(name, typ) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
242 |
parse(opt.value) match { |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
243 |
case Some(x) => x |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
244 |
case None => |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
245 |
error("Malformed value for option " + quote(name) + |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
246 |
" : " + typ.print + " =\n" + quote(opt.value)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
247 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
248 |
} |
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 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
251 |
/* internal lookup and update */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
252 |
|
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
|
253 |
class Bool_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
254 |
{ |
63805 | 255 |
def apply(name: String): Boolean = get(name, Options.Bool, Value.Boolean.unapply) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
256 |
def update(name: String, x: Boolean): Options = |
63805 | 257 |
put(name, Options.Bool, Value.Boolean(x)) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
258 |
} |
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
|
259 |
val bool = new Bool_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
260 |
|
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
|
261 |
class Int_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
262 |
{ |
63805 | 263 |
def apply(name: String): Int = get(name, Options.Int, Value.Int.unapply) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
264 |
def update(name: String, x: Int): Options = |
63805 | 265 |
put(name, Options.Int, Value.Int(x)) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
266 |
} |
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
|
267 |
val int = new Int_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
268 |
|
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
|
269 |
class Real_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
270 |
{ |
63805 | 271 |
def apply(name: String): Double = get(name, Options.Real, Value.Double.unapply) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
272 |
def update(name: String, x: Double): Options = |
63805 | 273 |
put(name, Options.Real, Value.Double(x)) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
274 |
} |
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
|
275 |
val real = new Real_Access |
48365
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 |
class String_Access |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
278 |
{ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
279 |
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
|
280 |
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
|
281 |
} |
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
|
282 |
val string = new String_Access |
48370 | 283 |
|
69074 | 284 |
def proper_string(name: String): Option[String] = |
285 |
Library.proper_string(string(name)) |
|
286 |
||
69073 | 287 |
def seconds(name: String): Time = Time.seconds(real(name)) |
50207 | 288 |
|
48370 | 289 |
|
48807 | 290 |
/* external updates */ |
48370 | 291 |
|
292 |
private def check_value(name: String): Options = |
|
293 |
{ |
|
294 |
val opt = check_name(name) |
|
295 |
opt.typ match { |
|
296 |
case Options.Bool => bool(name); this |
|
297 |
case Options.Int => int(name); this |
|
298 |
case Options.Real => real(name); this |
|
299 |
case Options.String => string(name); this |
|
48860 | 300 |
case Options.Unknown => this |
48370 | 301 |
} |
302 |
} |
|
303 |
||
56465 | 304 |
def declare( |
305 |
public: Boolean, |
|
306 |
pos: Position.T, |
|
307 |
name: String, |
|
308 |
typ_name: String, |
|
309 |
value: String, |
|
310 |
description: String): Options = |
|
48370 | 311 |
{ |
312 |
options.get(name) match { |
|
56465 | 313 |
case Some(other) => |
314 |
error("Duplicate declaration of option " + quote(name) + Position.here(pos) + |
|
315 |
Position.here(other.pos)) |
|
48370 | 316 |
case None => |
317 |
val typ = |
|
318 |
typ_name match { |
|
319 |
case "bool" => Options.Bool |
|
320 |
case "int" => Options.Int |
|
321 |
case "real" => Options.Real |
|
322 |
case "string" => Options.String |
|
56465 | 323 |
case _ => |
324 |
error("Unknown type for option " + quote(name) + " : " + quote(typ_name) + |
|
325 |
Position.here(pos)) |
|
48370 | 326 |
} |
56465 | 327 |
val opt = Options.Opt(public, pos, name, typ, value, value, description, section) |
49270 | 328 |
(new Options(options + (name -> opt), section)).check_value(name) |
48370 | 329 |
} |
330 |
} |
|
331 |
||
48860 | 332 |
def add_permissive(name: String, value: String): Options = |
333 |
{ |
|
334 |
if (options.isDefinedAt(name)) this + (name, value) |
|
56465 | 335 |
else { |
336 |
val opt = Options.Opt(false, Position.none, name, Options.Unknown, value, value, "", "") |
|
337 |
new Options(options + (name -> opt), section) |
|
338 |
} |
|
48860 | 339 |
} |
340 |
||
48807 | 341 |
def + (name: String, value: String): Options = |
48370 | 342 |
{ |
343 |
val opt = check_name(name) |
|
49270 | 344 |
(new Options(options + (name -> opt.copy(value = value)), section)).check_value(name) |
48370 | 345 |
} |
346 |
||
48807 | 347 |
def + (name: String, opt_value: Option[String]): Options = |
48370 | 348 |
{ |
349 |
val opt = check_name(name) |
|
350 |
opt_value match { |
|
48807 | 351 |
case Some(value) => this + (name, value) |
352 |
case None if opt.typ == Options.Bool => this + (name, "true") |
|
48370 | 353 |
case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print) |
354 |
} |
|
355 |
} |
|
356 |
||
48807 | 357 |
def + (str: String): Options = |
73712 | 358 |
str match { |
73715
bf51c23f3f99
clarified signature -- avoid odd warning about scala/bug#6675;
wenzelm
parents:
73712
diff
changeset
|
359 |
case Properties.Eq(a, b) => this + (a, b) |
73712 | 360 |
case _ => this + (str, None) |
48370 | 361 |
} |
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
362 |
|
48807 | 363 |
def ++ (specs: List[Options.Spec]): Options = |
73359 | 364 |
specs.foldLeft(this) { case (x, (y, z)) => x + (y, z) } |
48807 | 365 |
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
366 |
|
49270 | 367 |
/* sections */ |
368 |
||
369 |
def set_section(new_section: String): Options = |
|
370 |
new Options(options, new_section) |
|
371 |
||
372 |
def sections: List[(String, List[Options.Opt])] = |
|
373 |
options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) }) |
|
374 |
||
375 |
||
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
376 |
/* encode */ |
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
377 |
|
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
378 |
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
|
379 |
{ |
48860 | 380 |
val opts = |
56465 | 381 |
for ((_, opt) <- options.toList; if !opt.unknown) |
382 |
yield (opt.pos, (opt.name, (opt.typ.print, opt.value))) |
|
48860 | 383 |
|
56465 | 384 |
import XML.Encode.{string => string_, _} |
385 |
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
|
386 |
} |
48807 | 387 |
|
388 |
||
67845 | 389 |
/* save preferences */ |
48807 | 390 |
|
73340 | 391 |
def save_prefs(file: Path = Options.PREFS): Unit = |
48807 | 392 |
{ |
67845 | 393 |
val defaults: Options = Options.init(prefs = "") |
48807 | 394 |
val changed = |
395 |
(for { |
|
48860 | 396 |
(name, opt2) <- options.iterator |
397 |
opt1 = defaults.options.get(name) |
|
60215 | 398 |
if opt1.isEmpty || opt1.get.value != opt2.value |
48860 | 399 |
} yield (name, opt2.value, if (opt1.isEmpty) " (* unknown *)" else "")).toList |
400 |
||
48807 | 401 |
val prefs = |
402 |
changed.sortBy(_._1) |
|
48860 | 403 |
.map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString |
48807 | 404 |
|
72375 | 405 |
Isabelle_System.make_directory(file.dir) |
67845 | 406 |
File.write_backup(file, "(* generated by Isabelle " + Date.now() + " *)\n\n" + prefs) |
48807 | 407 |
} |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
408 |
} |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
409 |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
410 |
|
65236
4fa82bbb394e
afford early initialization of JEdit_Options, but it may lead to messy exception trace for malformed etc/preferences (see also 6eeaaefcea56);
wenzelm
parents:
64186
diff
changeset
|
411 |
class Options_Variable(init_options: Options) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
412 |
{ |
65236
4fa82bbb394e
afford early initialization of JEdit_Options, but it may lead to messy exception trace for malformed etc/preferences (see also 6eeaaefcea56);
wenzelm
parents:
64186
diff
changeset
|
413 |
private var options = init_options |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
414 |
|
65236
4fa82bbb394e
afford early initialization of JEdit_Options, but it may lead to messy exception trace for malformed etc/preferences (see also 6eeaaefcea56);
wenzelm
parents:
64186
diff
changeset
|
415 |
def value: Options = synchronized { options } |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
416 |
|
65236
4fa82bbb394e
afford early initialization of JEdit_Options, but it may lead to messy exception trace for malformed etc/preferences (see also 6eeaaefcea56);
wenzelm
parents:
64186
diff
changeset
|
417 |
private def upd(f: Options => Options): Unit = synchronized { options = f(options) } |
4fa82bbb394e
afford early initialization of JEdit_Options, but it may lead to messy exception trace for malformed etc/preferences (see also 6eeaaefcea56);
wenzelm
parents:
64186
diff
changeset
|
418 |
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
|
419 |
|
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
|
420 |
class Bool_Access |
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 apply(name: String): Boolean = value.bool(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
423 |
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
|
424 |
} |
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
|
425 |
val bool = new Bool_Access |
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 |
class Int_Access |
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 |
def apply(name: String): Int = value.int(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
430 |
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
|
431 |
} |
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
|
432 |
val int = new Int_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
433 |
|
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
|
434 |
class Real_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
435 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
436 |
def apply(name: String): Double = value.real(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
437 |
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
|
438 |
} |
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
|
439 |
val real = new Real_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
440 |
|
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
|
441 |
class String_Access |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
442 |
{ |
62227
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
443 |
def apply(name: String): String = value.string(name) |
6eeaaefcea56
clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents:
61579
diff
changeset
|
444 |
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
|
445 |
} |
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
|
446 |
val string = new String_Access |
50207 | 447 |
|
69074 | 448 |
def proper_string(name: String): Option[String] = |
449 |
Library.proper_string(string(name)) |
|
450 |
||
69073 | 451 |
def seconds(name: String): Time = value.seconds(name) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
452 |
} |