author | wenzelm |
Thu, 10 Apr 2008 14:53:27 +0200 | |
changeset 26611 | 03455add4801 |
parent 26600 | f11515535c83 |
child 26620 | 722cf4fdd4dd |
permissions | -rw-r--r-- |
5829 | 1 |
(* Title: Pure/Isar/outer_syntax.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
24868 | 5 |
The global Isabelle/Isar outer syntax. Note: the syntax for files is |
6 |
statically determined at the very beginning; for interactive processing |
|
7 |
it may change dynamically. |
|
5829 | 8 |
*) |
9 |
||
10 |
signature OUTER_SYNTAX = |
|
11 |
sig |
|
24868 | 12 |
type parser_fn = OuterLex.token list -> |
13 |
(Toplevel.transition -> Toplevel.transition) * OuterLex.token list |
|
23722 | 14 |
val get_lexicons: unit -> Scan.lexicon * Scan.lexicon |
23796 | 15 |
val command_keyword: string -> OuterKeyword.T option |
14687 | 16 |
val is_keyword: string -> bool |
24868 | 17 |
val keywords: string list -> unit |
18 |
val command: string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
19 |
val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
20 |
val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
7026 | 21 |
val dest_keywords: unit -> string list |
22 |
val dest_parsers: unit -> (string * string * string * bool) list |
|
5883 | 23 |
val print_outer_syntax: unit -> unit |
24872 | 24 |
val report: unit -> unit |
19060 | 25 |
val check_text: string * Position.T -> Toplevel.node option -> unit |
16195 | 26 |
val scan: string -> OuterLex.token list |
27 |
val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list |
|
25580 | 28 |
val parse: Position.T -> string -> Toplevel.transition list |
26431 | 29 |
val process_file: Path.T -> theory -> theory |
26600 | 30 |
type isar |
31 |
val isar: bool -> isar |
|
26611 | 32 |
val load_thy: Path.T -> string -> Position.T -> string list -> bool -> unit |
5829 | 33 |
end; |
34 |
||
26600 | 35 |
structure OuterSyntax: OUTER_SYNTAX = |
5829 | 36 |
struct |
37 |
||
7750 | 38 |
structure T = OuterLex; |
6860 | 39 |
structure P = OuterParse; |
40 |
||
5829 | 41 |
|
42 |
(** outer syntax **) |
|
43 |
||
24872 | 44 |
(* diagnostics *) |
45 |
||
46 |
fun report_keyword name = |
|
47 |
Pretty.markup (Markup.keyword_decl name) |
|
48 |
[Pretty.str ("Outer syntax keyword: " ^ quote name)]; |
|
49 |
||
50 |
fun report_command name kind = |
|
51 |
Pretty.markup (Markup.command_decl name kind) |
|
52 |
[Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")")]; |
|
53 |
||
54 |
||
5829 | 55 |
(* parsers *) |
56 |
||
24868 | 57 |
type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list; |
5829 | 58 |
|
24868 | 59 |
datatype parser = Parser of |
60 |
{comment: string, |
|
61 |
kind: OuterKeyword.T, |
|
62 |
markup: ThyOutput.markup option, |
|
63 |
int_only: bool, |
|
64 |
parse: parser_fn}; |
|
5829 | 65 |
|
24868 | 66 |
fun make_parser comment kind markup int_only parse = |
67 |
Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse}; |
|
5829 | 68 |
|
69 |
||
70 |
(* parse command *) |
|
71 |
||
6860 | 72 |
local |
6199 | 73 |
|
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
74 |
fun terminate false = Scan.succeed () |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
75 |
| terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ()); |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
76 |
|
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
77 |
fun trace false parse = parse |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
78 |
| trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks); |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
79 |
|
17071
f753d6dd9bd0
moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents:
16894
diff
changeset
|
80 |
fun body cmd do_trace (name, _) = |
7026 | 81 |
(case cmd name of |
24868 | 82 |
SOME (Parser {int_only, parse, ...}) => |
17118 | 83 |
P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only)) |
15531 | 84 |
| NONE => sys_error ("no parser for outer syntax command " ^ quote name)); |
6860 | 85 |
|
86 |
in |
|
5829 | 87 |
|
24868 | 88 |
fun parse_command do_terminate do_trace cmd = |
15531 | 89 |
P.semicolon >> K NONE || |
90 |
P.sync >> K NONE || |
|
17118 | 91 |
(P.position P.command :-- body cmd do_trace) --| terminate do_terminate |
6860 | 92 |
>> (fn ((name, pos), (int_only, f)) => |
15531 | 93 |
SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |> |
6860 | 94 |
Toplevel.interactive int_only |> f)); |
5829 | 95 |
|
6199 | 96 |
end; |
97 |
||
5829 | 98 |
|
99 |
||
9132 | 100 |
(** global outer syntax **) |
5829 | 101 |
|
7026 | 102 |
local |
103 |
||
104 |
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon); |
|
24868 | 105 |
val global_parsers = ref (Symtab.empty: parser Symtab.table); |
22120 | 106 |
val global_markups = ref ([]: (string * ThyOutput.markup) list); |
5952 | 107 |
|
23939 | 108 |
fun change_lexicons f = CRITICAL (fn () => |
7026 | 109 |
let val lexs = f (! global_lexicons) in |
110 |
(case (op inter_string) (pairself Scan.dest_lexicon lexs) of |
|
111 |
[] => global_lexicons := lexs |
|
112 |
| bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads)) |
|
23939 | 113 |
end); |
5829 | 114 |
|
23939 | 115 |
fun change_parsers f = CRITICAL (fn () => |
116 |
(change global_parsers f; |
|
117 |
global_markups := |
|
24868 | 118 |
Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I) |
119 |
(! global_parsers) [])); |
|
6722 | 120 |
|
7026 | 121 |
in |
122 |
||
9132 | 123 |
(* access current syntax *) |
7026 | 124 |
|
24868 | 125 |
fun get_lexicons () = CRITICAL (fn () => ! global_lexicons); |
126 |
fun get_parsers () = CRITICAL (fn () => ! global_parsers); |
|
127 |
fun get_markups () = CRITICAL (fn () => ! global_markups); |
|
7026 | 128 |
|
24868 | 129 |
fun get_parser () = Symtab.lookup (get_parsers ()); |
7789 | 130 |
|
23796 | 131 |
fun command_keyword name = |
24868 | 132 |
(case Symtab.lookup (get_parsers ()) name of |
133 |
SOME (Parser {kind, ...}) => SOME kind |
|
134 |
| NONE => NONE); |
|
135 |
||
26600 | 136 |
fun command_tags name = these (Option.map OuterKeyword.tags_of (command_keyword name)); |
17071
f753d6dd9bd0
moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents:
16894
diff
changeset
|
137 |
|
24868 | 138 |
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind; |
5829 | 139 |
|
140 |
||
141 |
(* augment syntax *) |
|
142 |
||
24872 | 143 |
fun keywords names = |
144 |
(change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names))); |
|
145 |
List.app (Pretty.writeln o report_keyword) names); |
|
24868 | 146 |
|
147 |
||
24872 | 148 |
fun add_parser (name, parser as Parser {kind, ...}) = |
24868 | 149 |
(if not (Symtab.defined (get_parsers ()) name) then () |
150 |
else warning ("Redefining outer syntax command " ^ quote name); |
|
151 |
change_parsers (Symtab.update (name, parser)); |
|
24872 | 152 |
change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name])); |
153 |
Pretty.writeln (report_command name (OuterKeyword.kind_of kind))); |
|
5829 | 154 |
|
24868 | 155 |
fun command name comment kind parse = |
156 |
add_parser (name, make_parser comment kind NONE false parse); |
|
5829 | 157 |
|
24868 | 158 |
fun markup_command markup name comment kind parse = |
159 |
add_parser (name, make_parser comment kind (SOME markup) false parse); |
|
160 |
||
161 |
fun improper_command name comment kind parse = |
|
162 |
add_parser (name, make_parser comment kind NONE true parse); |
|
7026 | 163 |
|
164 |
end; |
|
5829 | 165 |
|
166 |
||
24872 | 167 |
(* inspect syntax *) |
7026 | 168 |
|
14687 | 169 |
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s); |
7026 | 170 |
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ())); |
171 |
||
172 |
fun dest_parsers () = |
|
16727 | 173 |
get_parsers () |> Symtab.dest |> sort_wrt #1 |
24868 | 174 |
|> map (fn (name, Parser {comment, kind, int_only, ...}) => |
175 |
(name, comment, OuterKeyword.kind_of kind, int_only)); |
|
5829 | 176 |
|
9223 | 177 |
fun print_outer_syntax () = |
7026 | 178 |
let |
179 |
fun pretty_cmd (name, comment, _, _) = |
|
180 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
|
15570 | 181 |
val (int_cmds, cmds) = List.partition #4 (dest_parsers ()); |
7026 | 182 |
in |
8720 | 183 |
[Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())), |
18326 | 184 |
Pretty.big_list "commands:" (map pretty_cmd cmds), |
185 |
Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)] |
|
9223 | 186 |
|> Pretty.chunks |> Pretty.writeln |
7026 | 187 |
end; |
5829 | 188 |
|
24872 | 189 |
fun report () = |
190 |
(map report_keyword (dest_keywords ()) @ |
|
191 |
map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ())) |
|
192 |
|> Pretty.chunks |> Pretty.writeln; |
|
7367 | 193 |
|
5829 | 194 |
|
195 |
||
9132 | 196 |
(** toplevel parsing **) |
5829 | 197 |
|
9132 | 198 |
(* basic sources *) |
6860 | 199 |
|
17071
f753d6dd9bd0
moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents:
16894
diff
changeset
|
200 |
fun toplevel_source term do_trace do_recover cmd src = |
9132 | 201 |
let |
202 |
val no_terminator = |
|
203 |
Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof)); |
|
23682
cf4773532006
nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents:
23679
diff
changeset
|
204 |
fun recover int = |
cf4773532006
nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents:
23679
diff
changeset
|
205 |
(int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]); |
9132 | 206 |
in |
207 |
src |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
208 |
|> T.source_proper |
9132 | 209 |
|> Source.source T.stopper |
15531 | 210 |
(Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME)) |
23682
cf4773532006
nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents:
23679
diff
changeset
|
211 |
(Option.map recover do_recover) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19060
diff
changeset
|
212 |
|> Source.map_filter I |
24868 | 213 |
|> Source.source T.stopper |
214 |
(Scan.bulk (fn xs => P.!!! (parse_command term do_trace (cmd ())) xs)) |
|
23682
cf4773532006
nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents:
23679
diff
changeset
|
215 |
(Option.map recover do_recover) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19060
diff
changeset
|
216 |
|> Source.map_filter I |
9132 | 217 |
end; |
5829 | 218 |
|
7746 | 219 |
|
25580 | 220 |
(* off-line scanning/parsing *) |
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
221 |
|
25580 | 222 |
(*tokens*) |
15144
85929e1b307d
Remove isar_readstring. Split read into scanner and parser.
aspinall
parents:
14981
diff
changeset
|
223 |
fun scan str = |
16195 | 224 |
Source.of_string str |
225 |
|> Symbol.source false |
|
23679
57dceb84d1a0
toplevel_source: interactive flag indicates intermittent error_msg;
wenzelm
parents:
22826
diff
changeset
|
226 |
|> T.source (SOME false) get_lexicons Position.none |
16195 | 227 |
|> Source.exhaust; |
228 |
||
25580 | 229 |
(*commands from tokens, with trace*) |
15144
85929e1b307d
Remove isar_readstring. Split read into scanner and parser.
aspinall
parents:
14981
diff
changeset
|
230 |
fun read toks = |
85929e1b307d
Remove isar_readstring. Split read into scanner and parser.
aspinall
parents:
14981
diff
changeset
|
231 |
Source.of_list toks |
25580 | 232 |
|> toplevel_source false true NONE get_parser |
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
233 |
|> Source.exhaust |
15973 | 234 |
|> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr)); |
14091 | 235 |
|
25580 | 236 |
(*commands from string, without trace*) |
237 |
fun parse pos str = |
|
238 |
Source.of_string str |
|
239 |
|> Symbol.source false |
|
240 |
|> T.source (SOME false) get_lexicons pos |
|
241 |
|> toplevel_source false false NONE get_parser |
|
242 |
|> Source.exhaust; |
|
243 |
||
14091 | 244 |
|
26431 | 245 |
(* process file *) |
246 |
||
247 |
fun process_file path thy = |
|
248 |
let |
|
249 |
val result = ref thy; |
|
250 |
val trs = parse (Position.path path) (File.read path); |
|
251 |
val init = Toplevel.init_theory (K thy) (fn thy' => result := thy') (K ()); |
|
252 |
val _ = Toplevel.excursion (init Toplevel.empty :: trs @ [Toplevel.exit Toplevel.empty]); |
|
253 |
in ! result end; |
|
254 |
||
255 |
||
24868 | 256 |
(* interactive source of toplevel transformers *) |
257 |
||
26600 | 258 |
type isar = |
259 |
(Toplevel.transition, (Toplevel.transition option, |
|
260 |
(OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token, |
|
261 |
Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source) |
|
262 |
Source.source) Source.source) Source.source) Source.source) Source.source) Source.source; |
|
263 |
||
264 |
fun isar term : isar = |
|
24868 | 265 |
Source.tty |
266 |
|> Symbol.source true |
|
267 |
|> T.source (SOME true) get_lexicons Position.none |
|
268 |
|> toplevel_source term false (SOME true) get_parser; |
|
269 |
||
270 |
||
16195 | 271 |
|
9132 | 272 |
(** read theory **) |
6247 | 273 |
|
12943 | 274 |
(* check_text *) |
275 |
||
22120 | 276 |
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ()); |
12943 | 277 |
|
278 |
||
26611 | 279 |
(* load_thy *) |
7746 | 280 |
|
26323
73efc70edeef
theory loader: discontinued *attached* ML scripts;
wenzelm
parents:
26291
diff
changeset
|
281 |
fun load_thy dir name pos text time = |
7683 | 282 |
let |
24065 | 283 |
val text_src = Source.of_list (Library.untabify text); |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
284 |
|
17932 | 285 |
val _ = Present.init_theory name; |
24065 | 286 |
val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src)); |
287 |
val toks = text_src |
|
17932 | 288 |
|> Symbol.source false |
24065 | 289 |
|> T.source NONE (K (get_lexicons ())) pos |
17932 | 290 |
|> Source.exhausted; |
291 |
val trs = toks |
|
23679
57dceb84d1a0
toplevel_source: interactive flag indicates intermittent error_msg;
wenzelm
parents:
22826
diff
changeset
|
292 |
|> toplevel_source false false NONE (K (get_parser ())) |
17932 | 293 |
|> Source.exhaust; |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
294 |
|
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
295 |
val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else (); |
25685 | 296 |
val _ = cond_timeit time "" (fn () => |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
297 |
ThyOutput.process_thy (#1 (get_lexicons ())) command_tags is_markup trs toks |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
298 |
|> Buffer.content |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
299 |
|> Present.theory_output name); |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
300 |
val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else (); |
24065 | 301 |
in () end; |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
302 |
|
5829 | 303 |
end; |
304 |