author | wenzelm |
Thu, 07 Oct 1999 12:36:39 +0200 | |
changeset 7774 | 6da9b544a12d |
parent 7768 | b106e4af1301 |
child 7789 | 57d20133224e |
permissions | -rw-r--r-- |
5829 | 1 |
(* Title: Pure/Isar/outer_syntax.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
The global Isabelle/Isar outer syntax. |
|
6 |
*) |
|
7 |
||
8 |
signature BASIC_OUTER_SYNTAX = |
|
9 |
sig |
|
10 |
val main: unit -> unit |
|
5883 | 11 |
val loop: unit -> unit |
6860 | 12 |
val sync_main: unit -> unit |
13 |
val sync_loop: unit -> unit |
|
5829 | 14 |
val help: unit -> unit |
15 |
end; |
|
16 |
||
17 |
signature OUTER_SYNTAX = |
|
18 |
sig |
|
19 |
include BASIC_OUTER_SYNTAX |
|
6722 | 20 |
structure Keyword: |
21 |
sig |
|
22 |
val control: string |
|
23 |
val diag: string |
|
24 |
val thy_begin: string |
|
7104 | 25 |
val thy_switch: string |
6722 | 26 |
val thy_end: string |
27 |
val thy_heading: string |
|
28 |
val thy_decl: string |
|
29 |
val thy_goal: string |
|
30 |
val qed: string |
|
6733 | 31 |
val qed_block: string |
6722 | 32 |
val prf_goal: string |
33 |
val prf_block: string |
|
34 |
val prf_chain: string |
|
35 |
val prf_decl: string |
|
6868 | 36 |
val prf_asm: string |
7676 | 37 |
val prf_asm_goal: string |
6722 | 38 |
val prf_script: string |
39 |
val kinds: string list |
|
40 |
end |
|
5829 | 41 |
type token |
42 |
type parser |
|
6722 | 43 |
val command: string -> string -> string -> |
6373 | 44 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser |
7750 | 45 |
val markup_command: string -> string -> string -> |
46 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser |
|
6722 | 47 |
val improper_command: string -> string -> string -> |
6373 | 48 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser |
7026 | 49 |
val dest_keywords: unit -> string list |
50 |
val dest_parsers: unit -> (string * string * string * bool) list |
|
5883 | 51 |
val print_outer_syntax: unit -> unit |
7367 | 52 |
val print_help: Toplevel.transition -> Toplevel.transition |
5829 | 53 |
val add_keywords: string list -> unit |
54 |
val add_parsers: parser list -> unit |
|
6247 | 55 |
val theory_header: token list -> (string * string list * (string * bool) list) * token list |
7192 | 56 |
val deps_thy: string -> Path.T -> string list * Path.T list |
6199 | 57 |
val load_thy: string -> bool -> bool -> Path.T -> unit |
7333 | 58 |
val isar: bool -> bool -> Toplevel.isar |
5829 | 59 |
end; |
60 |
||
61 |
structure OuterSyntax: OUTER_SYNTAX = |
|
62 |
struct |
|
63 |
||
7750 | 64 |
structure T = OuterLex; |
6860 | 65 |
structure P = OuterParse; |
66 |
||
5829 | 67 |
|
68 |
(** outer syntax **) |
|
69 |
||
6722 | 70 |
(* command keyword classification *) |
71 |
||
72 |
structure Keyword = |
|
73 |
struct |
|
74 |
val control = "control"; |
|
75 |
val diag = "diag"; |
|
76 |
val thy_begin = "theory-begin"; |
|
7104 | 77 |
val thy_switch = "theory-switch"; |
6722 | 78 |
val thy_end = "theory-end"; |
79 |
val thy_heading = "theory-heading"; |
|
80 |
val thy_decl = "theory-decl"; |
|
81 |
val thy_goal = "theory-goal"; |
|
82 |
val qed = "qed"; |
|
6733 | 83 |
val qed_block = "qed-block"; |
6722 | 84 |
val prf_goal = "proof-goal"; |
85 |
val prf_block = "proof-block"; |
|
86 |
val prf_chain = "proof-chain"; |
|
87 |
val prf_decl = "proof-decl"; |
|
6868 | 88 |
val prf_asm = "proof-asm"; |
7676 | 89 |
val prf_asm_goal = "proof-asm-goal"; |
6722 | 90 |
val prf_script = "proof-script"; |
91 |
||
7104 | 92 |
val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal, |
7676 | 93 |
qed, qed_block, prf_goal, prf_block, prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script]; |
6722 | 94 |
end; |
95 |
||
96 |
||
5829 | 97 |
(* parsers *) |
98 |
||
7750 | 99 |
type token = T.token; |
5829 | 100 |
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list; |
101 |
||
102 |
datatype parser = |
|
7750 | 103 |
Parser of string * (string * string * bool) * bool * parser_fn; |
5829 | 104 |
|
7750 | 105 |
fun parser int_only markup name comment kind parse = |
106 |
Parser (name, (comment, kind, markup), int_only, parse); |
|
5829 | 107 |
|
108 |
||
109 |
(* parse command *) |
|
110 |
||
6860 | 111 |
local |
6199 | 112 |
|
5829 | 113 |
fun command_body cmd (name, _) = |
7026 | 114 |
(case cmd name of |
115 |
Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) |
|
116 |
| None => sys_error ("no parser for outer syntax command " ^ quote name)); |
|
6860 | 117 |
|
118 |
fun terminator false = Scan.succeed () |
|
119 |
| terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ()); |
|
120 |
||
121 |
in |
|
5829 | 122 |
|
6860 | 123 |
fun command term cmd = |
124 |
P.$$$ ";" >> K None || |
|
125 |
P.sync >> K None || |
|
7026 | 126 |
(P.position P.command :-- command_body cmd) --| terminator term |
6860 | 127 |
>> (fn ((name, pos), (int_only, f)) => |
128 |
Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |> |
|
129 |
Toplevel.interactive int_only |> f)); |
|
5829 | 130 |
|
6199 | 131 |
end; |
132 |
||
5829 | 133 |
|
134 |
||
135 |
(** global syntax state **) |
|
136 |
||
7026 | 137 |
local |
138 |
||
139 |
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon); |
|
7750 | 140 |
val global_parsers = |
141 |
ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * bool) Symtab.table); |
|
142 |
val global_markups = ref ([]: string list); |
|
5952 | 143 |
|
7026 | 144 |
fun change_lexicons f = |
145 |
let val lexs = f (! global_lexicons) in |
|
146 |
(case (op inter_string) (pairself Scan.dest_lexicon lexs) of |
|
147 |
[] => global_lexicons := lexs |
|
148 |
| bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads)) |
|
149 |
end; |
|
5829 | 150 |
|
7750 | 151 |
fun get_markup (names, (name, (_, true))) = name :: names |
152 |
| get_markup (names, _) = names; |
|
153 |
||
154 |
fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers); |
|
155 |
fun change_parsers f = (global_parsers := f (! global_parsers); make_markups ()); |
|
6722 | 156 |
|
7026 | 157 |
in |
158 |
||
7750 | 159 |
|
160 |
(* get current syntax *) |
|
7026 | 161 |
|
162 |
(*Note: the syntax for files is statically determined at the very |
|
163 |
beginning; for interactive processing it may change dynamically.*) |
|
164 |
||
165 |
fun get_lexicons () = ! global_lexicons; |
|
166 |
fun get_parsers () = ! global_parsers; |
|
7750 | 167 |
fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers); |
168 |
fun is_markup name = name mem_string ! global_markups; |
|
5829 | 169 |
|
170 |
||
171 |
(* augment syntax *) |
|
172 |
||
7026 | 173 |
fun add_keywords keywords = change_lexicons (apfst (fn lex => |
174 |
(Scan.extend_lexicon lex (map Symbol.explode keywords)))); |
|
5829 | 175 |
|
7750 | 176 |
fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) = |
5829 | 177 |
(if is_none (Symtab.lookup (tab, name)) then () |
178 |
else warning ("Redefined outer syntax command " ^ quote name); |
|
7750 | 179 |
Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab)); |
5829 | 180 |
|
181 |
fun add_parsers parsers = |
|
7026 | 182 |
(change_parsers (fn tab => foldl add_parser (tab, parsers)); |
183 |
change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex |
|
184 |
(map (fn Parser (name, _, _, _) => Symbol.explode name) parsers)))); |
|
185 |
||
186 |
end; |
|
5829 | 187 |
|
188 |
||
7026 | 189 |
(* print syntax *) |
190 |
||
191 |
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ())); |
|
192 |
||
193 |
fun dest_parsers () = |
|
7750 | 194 |
map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only)) |
7026 | 195 |
(Symtab.dest (get_parsers ())); |
5829 | 196 |
|
7026 | 197 |
fun print_outer_syntax () = |
198 |
let |
|
199 |
fun pretty_cmd (name, comment, _, _) = |
|
200 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
|
201 |
val (int_cmds, cmds) = partition #4 (dest_parsers ()); |
|
202 |
in |
|
203 |
Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ()))); |
|
204 |
Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds)); |
|
205 |
Pretty.writeln (Pretty.big_list "improper commands (interactive-only):" |
|
206 |
(map pretty_cmd int_cmds)) |
|
207 |
end; |
|
5829 | 208 |
|
7367 | 209 |
val print_help = |
210 |
Toplevel.keep (fn state => |
|
7613 | 211 |
let val opt_thy = try Toplevel.theory_of state in |
212 |
print_outer_syntax (); |
|
213 |
Method.help_methods opt_thy; |
|
214 |
Attrib.help_attributes opt_thy |
|
215 |
end); |
|
7367 | 216 |
|
5829 | 217 |
|
218 |
||
219 |
(** read theory **) |
|
220 |
||
7746 | 221 |
(* special keywords *) |
6247 | 222 |
|
7746 | 223 |
val headerN = "header"; |
6247 | 224 |
val theoryN = "theory"; |
7746 | 225 |
|
7750 | 226 |
val theory_keyword = P.$$$ theoryN; |
227 |
val header_keyword = P.$$$ headerN; |
|
7746 | 228 |
val semicolon = P.$$$ ";"; |
6247 | 229 |
|
230 |
||
7683 | 231 |
(* sources *) |
5829 | 232 |
|
6860 | 233 |
local |
5829 | 234 |
|
6860 | 235 |
val no_terminator = |
7750 | 236 |
Scan.unless semicolon (Scan.one (T.not_sync andf T.not_eof)); |
6860 | 237 |
|
238 |
val recover = Scan.prompt "recover# " (Scan.repeat no_terminator); |
|
239 |
||
240 |
in |
|
5829 | 241 |
|
6860 | 242 |
fun source term do_recover cmd src = |
5829 | 243 |
src |
7750 | 244 |
|> Source.source T.stopper |
245 |
(Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs)) |
|
6860 | 246 |
(if do_recover then Some recover else None) |
5829 | 247 |
|> Source.mapfilter I; |
248 |
||
6860 | 249 |
end; |
250 |
||
7683 | 251 |
fun token_source (src, pos) = |
252 |
src |
|
253 |
|> Symbol.source false |
|
7750 | 254 |
|> T.source false (K (get_lexicons ())) pos; |
7683 | 255 |
|
256 |
fun filter_proper src = |
|
257 |
src |
|
7750 | 258 |
|> Source.filter T.is_proper; |
7683 | 259 |
|
5829 | 260 |
|
7746 | 261 |
(* scan header *) |
5829 | 262 |
|
7026 | 263 |
fun scan_header get_lex scan (src, pos) = |
5829 | 264 |
src |
265 |
|> Symbol.source false |
|
7750 | 266 |
|> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos |
7683 | 267 |
|> filter_proper |
7750 | 268 |
|> Source.source T.stopper (Scan.single scan) None |
5829 | 269 |
|> (fst o the o Source.get_single); |
270 |
||
7746 | 271 |
|
272 |
(* detect new/old header *) |
|
273 |
||
274 |
local |
|
5829 | 275 |
|
7746 | 276 |
val check_header_lexicon = Scan.make_lexicon [Symbol.explode headerN, Symbol.explode theoryN]; |
277 |
val check_header = Scan.option (header_keyword || theory_keyword); |
|
278 |
||
279 |
in |
|
280 |
||
281 |
fun is_old_theory src = is_none (scan_header (K check_header_lexicon) check_header src); |
|
282 |
||
283 |
end; |
|
6199 | 284 |
|
285 |
||
286 |
(* deps_thy --- inspect theory header *) |
|
287 |
||
7746 | 288 |
local |
289 |
||
6247 | 290 |
val header_lexicon = |
7746 | 291 |
Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", ";", "=", "files", headerN, theoryN]); |
6199 | 292 |
|
7026 | 293 |
val file_name = |
294 |
(P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true; |
|
6247 | 295 |
|
7746 | 296 |
in |
297 |
||
298 |
val theory_header = |
|
6860 | 299 |
(P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) -- |
7746 | 300 |
Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --| P.$$$ ":") |
6247 | 301 |
>> (fn ((A, Bs), files) => (A, Bs, files)); |
302 |
||
7746 | 303 |
val new_header = |
304 |
header_keyword |-- (P.!!! (P.text -- Scan.option semicolon -- theory_keyword |-- theory_header)) |
|
305 |
|| theory_keyword |-- P.!!! theory_header; |
|
6199 | 306 |
|
307 |
val old_header = |
|
6860 | 308 |
P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name)) |
6247 | 309 |
>> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list)); |
6199 | 310 |
|
7192 | 311 |
fun deps_thy name path = |
6199 | 312 |
let |
7735 | 313 |
val src = Source.of_string (File.read path); |
314 |
val pos = Path.position path; |
|
6247 | 315 |
val (name', parents, files) = |
6199 | 316 |
(*Note: old style headers dynamically depend on the current lexicon :-( *) |
7746 | 317 |
if is_old_theory (src, pos) then |
318 |
scan_header ThySyn.get_lexicon (Scan.error old_header) (src, pos) |
|
7735 | 319 |
else scan_header (K header_lexicon) (Scan.error new_header) (src, pos); |
6199 | 320 |
|
321 |
val ml_path = ThyLoad.ml_path name; |
|
7192 | 322 |
val ml_file = if is_none (ThyLoad.check_file ml_path) then [] else [ml_path]; |
6199 | 323 |
in |
324 |
if name <> name' then |
|
325 |
error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name) |
|
6247 | 326 |
else (parents, map (Path.unpack o #1) files @ ml_file) |
6199 | 327 |
end; |
328 |
||
7746 | 329 |
end; |
330 |
||
6199 | 331 |
|
7750 | 332 |
(* present theory source *) |
333 |
||
334 |
local |
|
335 |
||
7755 | 336 |
val improper = Scan.any (not o T.is_proper); |
337 |
val markup = Scan.one (T.is_kind T.Command andf is_markup o T.val_of) >> T.val_of; |
|
7750 | 338 |
|
7755 | 339 |
val present_token = |
340 |
improper |-- markup -- (improper |-- P.text --| (improper -- Scan.option semicolon -- improper)) |
|
341 |
>> Present.markup_token || |
|
342 |
(P.$$$ "--" >> K "cmt") -- (improper |-- P.text) >> Present.markup_token || |
|
343 |
Scan.one T.not_eof >> Present.basic_token; |
|
7750 | 344 |
|
345 |
in |
|
346 |
||
347 |
(*note: lazy evaluation ahead*) |
|
348 |
||
349 |
fun present_toks text pos () = |
|
350 |
token_source (Source.of_list (Library.untabify text), pos) |
|
7755 | 351 |
|> Source.source T.stopper (Scan.bulk present_token) None |
352 |
|> Source.exhaust; |
|
7750 | 353 |
|
354 |
fun present_text text () = |
|
355 |
Source.exhaust (Symbol.source false (Source.of_list (Library.untabify text))); |
|
356 |
||
357 |
end; |
|
358 |
||
359 |
||
6199 | 360 |
(* load_thy --- read text (including header) *) |
361 |
||
7746 | 362 |
local |
363 |
||
6247 | 364 |
fun try_ml_file name ml time = |
6199 | 365 |
let |
366 |
val path = ThyLoad.ml_path name; |
|
7243 | 367 |
val tr = Toplevel.imperative (fn () => ThyInfo.may_load_file ml time path); |
6247 | 368 |
val tr_name = if time then "time_use" else "use"; |
6199 | 369 |
in |
7243 | 370 |
if is_none (ThyLoad.check_file path) then () |
7062 | 371 |
else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr] |
6199 | 372 |
end; |
373 |
||
7683 | 374 |
fun parse_thy src_pos = |
7746 | 375 |
src_pos |
376 |
|> token_source |
|
377 |
|> filter_proper |
|
378 |
|> source false false (K (get_parser ())) |
|
379 |
|> Source.exhaust; |
|
5829 | 380 |
|
6247 | 381 |
fun run_thy name path = |
7683 | 382 |
let |
7735 | 383 |
val text = explode (File.read path); |
384 |
val src = Source.of_list text; |
|
385 |
val pos = Path.position path; |
|
7683 | 386 |
in |
7735 | 387 |
Present.init_theory name; |
7768 | 388 |
Present.verbatim_source name (present_text text); |
7735 | 389 |
if is_old_theory (src, pos) then ThySyn.load_thy name text |
7774
6da9b544a12d
Present.token_source after load (better errors!?);
wenzelm
parents:
7768
diff
changeset
|
390 |
else (Toplevel.excursion_error (parse_thy (src, pos)); |
6da9b544a12d
Present.token_source after load (better errors!?);
wenzelm
parents:
7768
diff
changeset
|
391 |
Present.token_source name (present_toks text pos)) |
6247 | 392 |
end; |
6199 | 393 |
|
7746 | 394 |
in |
395 |
||
6199 | 396 |
fun load_thy name ml time path = |
6247 | 397 |
(if time then |
398 |
timeit (fn () => |
|
399 |
(writeln ("\n**** Starting theory " ^ quote name ^ " ****"); |
|
400 |
setmp Goals.proof_timing true (run_thy name) path; |
|
401 |
writeln ("**** Finished theory " ^ quote name ^ " ****\n"))) |
|
402 |
else run_thy name path; |
|
403 |
Context.context (ThyInfo.get_theory name); |
|
404 |
try_ml_file name ml time); |
|
5829 | 405 |
|
7746 | 406 |
end; |
407 |
||
5829 | 408 |
|
409 |
(* interactive source of state transformers *) |
|
410 |
||
7333 | 411 |
fun isar term no_pos = |
5829 | 412 |
Source.tty |
413 |
|> Symbol.source true |
|
7750 | 414 |
|> T.source true get_lexicons |
7604 | 415 |
(if no_pos then Position.none else Position.line_name 1 "stdin") |
7683 | 416 |
|> filter_proper |
6860 | 417 |
|> source term true get_parser; |
5829 | 418 |
|
419 |
||
420 |
||
421 |
(** the read-eval-print loop **) |
|
422 |
||
5923 | 423 |
(* main loop *) |
424 |
||
7333 | 425 |
fun gen_loop term no_pos = |
426 |
(Context.reset_context (); |
|
427 |
Toplevel.loop (isar term no_pos)); |
|
5829 | 428 |
|
7333 | 429 |
fun gen_main term no_pos = |
5829 | 430 |
(Toplevel.set_state Toplevel.toplevel; |
431 |
ml_prompts "ML> " "ML# "; |
|
6199 | 432 |
writeln (Session.welcome ()); |
7333 | 433 |
gen_loop term no_pos); |
6860 | 434 |
|
7333 | 435 |
fun main () = gen_main false false; |
436 |
fun loop () = gen_loop false false; |
|
437 |
fun sync_main () = gen_main true true; |
|
438 |
fun sync_loop () = gen_loop true true; |
|
5829 | 439 |
|
440 |
||
441 |
(* help *) |
|
442 |
||
443 |
fun help () = |
|
444 |
writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\ |
|
5883 | 445 |
\invoke 'loop();' to enter the Isar loop."); |
5829 | 446 |
|
447 |
||
6373 | 448 |
(*final declarations of this structure!*) |
7750 | 449 |
val command = parser false false; |
450 |
val markup_command = parser false true; |
|
451 |
val improper_command = parser true false; |
|
6685 | 452 |
|
6373 | 453 |
|
5829 | 454 |
end; |
455 |
||
6199 | 456 |
(*setup theory syntax dependent operations*) |
457 |
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy; |
|
458 |
ThyLoad.load_thy_fn := OuterSyntax.load_thy; |
|
459 |
structure ThyLoad: THY_LOAD = ThyLoad; |
|
460 |
||
5829 | 461 |
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax; |
462 |
open BasicOuterSyntax; |