author | wenzelm |
Fri, 07 Dec 2007 22:19:53 +0100 | |
changeset 25580 | 6623674df897 |
parent 25526 | 05ee7d85912e |
child 25583 | 72e71bcf4239 |
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 BASIC_OUTER_SYNTAX = |
|
11 |
sig |
|
15830 | 12 |
structure Isar: |
13 |
sig |
|
18064 | 14 |
val state: unit -> Toplevel.state |
21207 | 15 |
val exn: unit -> (exn * string) option |
20023 | 16 |
val context: unit -> Proof.context |
21401 | 17 |
val goal: unit -> thm list * thm |
15830 | 18 |
val main: unit -> unit |
19 |
val loop: unit -> unit |
|
20 |
val sync_main: unit -> unit |
|
21 |
val sync_loop: unit -> unit |
|
25526 | 22 |
val secure_main: unit -> unit |
18684 | 23 |
val toplevel: (unit -> 'a) -> 'a |
15830 | 24 |
end; |
5829 | 25 |
end; |
26 |
||
27 |
signature OUTER_SYNTAX = |
|
28 |
sig |
|
29 |
include BASIC_OUTER_SYNTAX |
|
24868 | 30 |
type parser_fn = OuterLex.token list -> |
31 |
(Toplevel.transition -> Toplevel.transition) * OuterLex.token list |
|
23722 | 32 |
val get_lexicons: unit -> Scan.lexicon * Scan.lexicon |
23796 | 33 |
val command_keyword: string -> OuterKeyword.T option |
14687 | 34 |
val is_keyword: string -> bool |
24868 | 35 |
val keywords: string list -> unit |
36 |
val command: string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
37 |
val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
38 |
val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
7026 | 39 |
val dest_keywords: unit -> string list |
40 |
val dest_parsers: unit -> (string * string * string * bool) list |
|
5883 | 41 |
val print_outer_syntax: unit -> unit |
24872 | 42 |
val report: unit -> unit |
19060 | 43 |
val check_text: string * Position.T -> Toplevel.node option -> unit |
16195 | 44 |
val scan: string -> OuterLex.token list |
45 |
val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list |
|
25580 | 46 |
val parse: Position.T -> string -> Toplevel.transition list |
24868 | 47 |
val isar: bool -> unit Toplevel.isar |
5829 | 48 |
end; |
49 |
||
15224
1bd35fd87963
Allow scanning to recover and reconstruct bad input
aspinall
parents:
15156
diff
changeset
|
50 |
structure OuterSyntax : OUTER_SYNTAX = |
5829 | 51 |
struct |
52 |
||
7750 | 53 |
structure T = OuterLex; |
6860 | 54 |
structure P = OuterParse; |
55 |
||
5829 | 56 |
|
57 |
(** outer syntax **) |
|
58 |
||
24872 | 59 |
(* diagnostics *) |
60 |
||
61 |
fun report_keyword name = |
|
62 |
Pretty.markup (Markup.keyword_decl name) |
|
63 |
[Pretty.str ("Outer syntax keyword: " ^ quote name)]; |
|
64 |
||
65 |
fun report_command name kind = |
|
66 |
Pretty.markup (Markup.command_decl name kind) |
|
67 |
[Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")")]; |
|
68 |
||
69 |
||
5829 | 70 |
(* parsers *) |
71 |
||
24868 | 72 |
type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list; |
5829 | 73 |
|
24868 | 74 |
datatype parser = Parser of |
75 |
{comment: string, |
|
76 |
kind: OuterKeyword.T, |
|
77 |
markup: ThyOutput.markup option, |
|
78 |
int_only: bool, |
|
79 |
parse: parser_fn}; |
|
5829 | 80 |
|
24868 | 81 |
fun make_parser comment kind markup int_only parse = |
82 |
Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse}; |
|
5829 | 83 |
|
84 |
||
85 |
(* parse command *) |
|
86 |
||
6860 | 87 |
local |
6199 | 88 |
|
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
89 |
fun terminate false = Scan.succeed () |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
90 |
| 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
|
91 |
|
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
92 |
fun trace false parse = parse |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
93 |
| 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
|
94 |
|
17071
f753d6dd9bd0
moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents:
16894
diff
changeset
|
95 |
fun body cmd do_trace (name, _) = |
7026 | 96 |
(case cmd name of |
24868 | 97 |
SOME (Parser {int_only, parse, ...}) => |
17118 | 98 |
P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only)) |
15531 | 99 |
| NONE => sys_error ("no parser for outer syntax command " ^ quote name)); |
6860 | 100 |
|
101 |
in |
|
5829 | 102 |
|
24868 | 103 |
fun parse_command do_terminate do_trace cmd = |
15531 | 104 |
P.semicolon >> K NONE || |
105 |
P.sync >> K NONE || |
|
17118 | 106 |
(P.position P.command :-- body cmd do_trace) --| terminate do_terminate |
6860 | 107 |
>> (fn ((name, pos), (int_only, f)) => |
15531 | 108 |
SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |> |
6860 | 109 |
Toplevel.interactive int_only |> f)); |
5829 | 110 |
|
6199 | 111 |
end; |
112 |
||
5829 | 113 |
|
114 |
||
9132 | 115 |
(** global outer syntax **) |
5829 | 116 |
|
7026 | 117 |
local |
118 |
||
119 |
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon); |
|
24868 | 120 |
val global_parsers = ref (Symtab.empty: parser Symtab.table); |
22120 | 121 |
val global_markups = ref ([]: (string * ThyOutput.markup) list); |
5952 | 122 |
|
23939 | 123 |
fun change_lexicons f = CRITICAL (fn () => |
7026 | 124 |
let val lexs = f (! global_lexicons) in |
125 |
(case (op inter_string) (pairself Scan.dest_lexicon lexs) of |
|
126 |
[] => global_lexicons := lexs |
|
127 |
| bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads)) |
|
23939 | 128 |
end); |
5829 | 129 |
|
23939 | 130 |
fun change_parsers f = CRITICAL (fn () => |
131 |
(change global_parsers f; |
|
132 |
global_markups := |
|
24868 | 133 |
Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I) |
134 |
(! global_parsers) [])); |
|
6722 | 135 |
|
7026 | 136 |
in |
137 |
||
9132 | 138 |
(* access current syntax *) |
7026 | 139 |
|
24868 | 140 |
fun get_lexicons () = CRITICAL (fn () => ! global_lexicons); |
141 |
fun get_parsers () = CRITICAL (fn () => ! global_parsers); |
|
142 |
fun get_markups () = CRITICAL (fn () => ! global_markups); |
|
7026 | 143 |
|
24868 | 144 |
fun get_parser () = Symtab.lookup (get_parsers ()); |
7789 | 145 |
|
23796 | 146 |
fun command_keyword name = |
24868 | 147 |
(case Symtab.lookup (get_parsers ()) name of |
148 |
SOME (Parser {kind, ...}) => SOME kind |
|
149 |
| NONE => NONE); |
|
150 |
||
23796 | 151 |
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
|
152 |
|
24868 | 153 |
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind; |
5829 | 154 |
|
155 |
||
156 |
(* augment syntax *) |
|
157 |
||
24872 | 158 |
fun keywords names = |
159 |
(change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names))); |
|
160 |
List.app (Pretty.writeln o report_keyword) names); |
|
24868 | 161 |
|
162 |
||
24872 | 163 |
fun add_parser (name, parser as Parser {kind, ...}) = |
24868 | 164 |
(if not (Symtab.defined (get_parsers ()) name) then () |
165 |
else warning ("Redefining outer syntax command " ^ quote name); |
|
166 |
change_parsers (Symtab.update (name, parser)); |
|
24872 | 167 |
change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name])); |
168 |
Pretty.writeln (report_command name (OuterKeyword.kind_of kind))); |
|
5829 | 169 |
|
24868 | 170 |
fun command name comment kind parse = |
171 |
add_parser (name, make_parser comment kind NONE false parse); |
|
5829 | 172 |
|
24868 | 173 |
fun markup_command markup name comment kind parse = |
174 |
add_parser (name, make_parser comment kind (SOME markup) false parse); |
|
175 |
||
176 |
fun improper_command name comment kind parse = |
|
177 |
add_parser (name, make_parser comment kind NONE true parse); |
|
7026 | 178 |
|
179 |
end; |
|
5829 | 180 |
|
181 |
||
24872 | 182 |
(* inspect syntax *) |
7026 | 183 |
|
14687 | 184 |
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s); |
7026 | 185 |
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ())); |
186 |
||
187 |
fun dest_parsers () = |
|
16727 | 188 |
get_parsers () |> Symtab.dest |> sort_wrt #1 |
24868 | 189 |
|> map (fn (name, Parser {comment, kind, int_only, ...}) => |
190 |
(name, comment, OuterKeyword.kind_of kind, int_only)); |
|
5829 | 191 |
|
9223 | 192 |
fun print_outer_syntax () = |
7026 | 193 |
let |
194 |
fun pretty_cmd (name, comment, _, _) = |
|
195 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
|
15570 | 196 |
val (int_cmds, cmds) = List.partition #4 (dest_parsers ()); |
7026 | 197 |
in |
8720 | 198 |
[Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())), |
18326 | 199 |
Pretty.big_list "commands:" (map pretty_cmd cmds), |
200 |
Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)] |
|
9223 | 201 |
|> Pretty.chunks |> Pretty.writeln |
7026 | 202 |
end; |
5829 | 203 |
|
24872 | 204 |
fun report () = |
205 |
(map report_keyword (dest_keywords ()) @ |
|
206 |
map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ())) |
|
207 |
|> Pretty.chunks |> Pretty.writeln; |
|
7367 | 208 |
|
5829 | 209 |
|
210 |
||
9132 | 211 |
(** toplevel parsing **) |
5829 | 212 |
|
9132 | 213 |
(* basic sources *) |
6860 | 214 |
|
17071
f753d6dd9bd0
moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents:
16894
diff
changeset
|
215 |
fun toplevel_source term do_trace do_recover cmd src = |
9132 | 216 |
let |
217 |
val no_terminator = |
|
218 |
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
|
219 |
fun recover int = |
cf4773532006
nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents:
23679
diff
changeset
|
220 |
(int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]); |
9132 | 221 |
in |
222 |
src |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
223 |
|> T.source_proper |
9132 | 224 |
|> Source.source T.stopper |
15531 | 225 |
(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
|
226 |
(Option.map recover do_recover) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19060
diff
changeset
|
227 |
|> Source.map_filter I |
24868 | 228 |
|> Source.source T.stopper |
229 |
(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
|
230 |
(Option.map recover do_recover) |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19060
diff
changeset
|
231 |
|> Source.map_filter I |
9132 | 232 |
end; |
5829 | 233 |
|
7746 | 234 |
|
25580 | 235 |
(* off-line scanning/parsing *) |
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
236 |
|
25580 | 237 |
(*tokens*) |
15144
85929e1b307d
Remove isar_readstring. Split read into scanner and parser.
aspinall
parents:
14981
diff
changeset
|
238 |
fun scan str = |
16195 | 239 |
Source.of_string str |
240 |
|> Symbol.source false |
|
23679
57dceb84d1a0
toplevel_source: interactive flag indicates intermittent error_msg;
wenzelm
parents:
22826
diff
changeset
|
241 |
|> T.source (SOME false) get_lexicons Position.none |
16195 | 242 |
|> Source.exhaust; |
243 |
||
25580 | 244 |
(*commands from tokens, with trace*) |
15144
85929e1b307d
Remove isar_readstring. Split read into scanner and parser.
aspinall
parents:
14981
diff
changeset
|
245 |
fun read toks = |
85929e1b307d
Remove isar_readstring. Split read into scanner and parser.
aspinall
parents:
14981
diff
changeset
|
246 |
Source.of_list toks |
25580 | 247 |
|> toplevel_source false true NONE get_parser |
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
248 |
|> Source.exhaust |
15973 | 249 |
|> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr)); |
14091 | 250 |
|
25580 | 251 |
(*commands from string, without trace*) |
252 |
fun parse pos str = |
|
253 |
Source.of_string str |
|
254 |
|> Symbol.source false |
|
255 |
|> T.source (SOME false) get_lexicons pos |
|
256 |
|> toplevel_source false false NONE get_parser |
|
257 |
|> Source.exhaust; |
|
258 |
||
14091 | 259 |
|
24868 | 260 |
(* interactive source of toplevel transformers *) |
261 |
||
262 |
fun isar term = |
|
263 |
Source.tty |
|
264 |
|> Symbol.source true |
|
265 |
|> T.source (SOME true) get_lexicons Position.none |
|
266 |
|> toplevel_source term false (SOME true) get_parser; |
|
267 |
||
268 |
||
16195 | 269 |
|
9132 | 270 |
(** read theory **) |
6247 | 271 |
|
12943 | 272 |
(* check_text *) |
273 |
||
22120 | 274 |
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ()); |
12943 | 275 |
|
276 |
||
9132 | 277 |
(* load_thy *) |
6199 | 278 |
|
7746 | 279 |
local |
280 |
||
23884
1d39ec4fe73f
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23866
diff
changeset
|
281 |
fun try_ml_file dir name time = |
20323 | 282 |
let val path = ThyLoad.ml_path name in |
23884
1d39ec4fe73f
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23866
diff
changeset
|
283 |
if is_none (ThyLoad.check_file dir path) then () |
20323 | 284 |
else |
285 |
let |
|
22826 | 286 |
val _ = legacy_feature ("loading attached ML script " ^ quote (Path.implode path)); |
20323 | 287 |
val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path); |
288 |
val tr_name = if time then "time_use" else "use"; |
|
289 |
in Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr] end |
|
6199 | 290 |
end; |
291 |
||
24065 | 292 |
fun run_thy dir name pos text time = |
7683 | 293 |
let |
24065 | 294 |
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
|
295 |
|
17932 | 296 |
val _ = Present.init_theory name; |
24065 | 297 |
val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src)); |
298 |
val toks = text_src |
|
17932 | 299 |
|> Symbol.source false |
24065 | 300 |
|> T.source NONE (K (get_lexicons ())) pos |
17932 | 301 |
|> Source.exhausted; |
302 |
val trs = toks |
|
23679
57dceb84d1a0
toplevel_source: interactive flag indicates intermittent error_msg;
wenzelm
parents:
22826
diff
changeset
|
303 |
|> toplevel_source false false NONE (K (get_parser ())) |
17932 | 304 |
|> Source.exhaust; |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
305 |
|
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
306 |
val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else (); |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
307 |
val _ = cond_timeit time (fn () => |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
308 |
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
|
309 |
|> Buffer.content |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
310 |
|> Present.theory_output name); |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
311 |
val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else (); |
24065 | 312 |
in () end; |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
313 |
|
24188 | 314 |
fun load_thy dir name pos text time = |
24065 | 315 |
(run_thy dir name pos text time; |
24071 | 316 |
CRITICAL (fn () => ML_Context.set_context (SOME (Context.Theory (ThyInfo.get_theory name)))); |
24188 | 317 |
try_ml_file dir name time); |
6199 | 318 |
|
7746 | 319 |
in |
320 |
||
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
321 |
val _ = ThyLoad.load_thy_fn := load_thy; |
5829 | 322 |
|
7746 | 323 |
end; |
324 |
||
5829 | 325 |
|
326 |
||
327 |
(** the read-eval-print loop **) |
|
328 |
||
5923 | 329 |
(* main loop *) |
330 |
||
25526 | 331 |
fun gen_loop secure terminated = |
24071 | 332 |
(CRITICAL (fn () => ML_Context.set_context NONE); |
25526 | 333 |
Toplevel.loop secure (isar terminated)); |
5829 | 334 |
|
25526 | 335 |
fun gen_main secure terminated = |
21957 | 336 |
(Toplevel.init_state (); |
6199 | 337 |
writeln (Session.welcome ()); |
25526 | 338 |
gen_loop secure terminated); |
6860 | 339 |
|
15830 | 340 |
structure Isar = |
341 |
struct |
|
18064 | 342 |
val state = Toplevel.get_state; |
343 |
val exn = Toplevel.exn; |
|
21401 | 344 |
|
21207 | 345 |
fun context () = |
21506 | 346 |
Toplevel.context_of (state ()) |
21207 | 347 |
handle Toplevel.UNDEF => error "Unknown context"; |
21401 | 348 |
|
349 |
fun goal () = |
|
350 |
#2 (Proof.get_goal (Toplevel.proof_of (state ()))) |
|
351 |
handle Toplevel.UNDEF => error "No goal present"; |
|
352 |
||
25526 | 353 |
fun main () = gen_main (Secure.is_secure ()) false; |
354 |
fun loop () = gen_loop (Secure.is_secure ()) false; |
|
355 |
fun sync_main () = gen_main (Secure.is_secure ()) true; |
|
356 |
fun sync_loop () = gen_loop (Secure.is_secure ()) true; |
|
357 |
fun secure_main () = gen_main true false; |
|
18684 | 358 |
val toplevel = Toplevel.program; |
15830 | 359 |
end; |
5829 | 360 |
|
361 |
end; |
|
362 |
||
6199 | 363 |
structure ThyLoad: THY_LOAD = ThyLoad; |
5829 | 364 |
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax; |
365 |
open BasicOuterSyntax; |