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