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