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