author | wenzelm |
Mon, 25 Feb 2002 20:51:48 +0100 | |
changeset 12943 | 1db24da0537b |
parent 12876 | a70df1e5bf10 |
child 14091 | ad6ba9c55190 |
permissions | -rw-r--r-- |
5829 | 1 |
(* Title: Pure/Isar/outer_syntax.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
8807 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5829 | 5 |
|
6 |
The global Isabelle/Isar outer syntax. |
|
7 |
*) |
|
8 |
||
9 |
signature BASIC_OUTER_SYNTAX = |
|
10 |
sig |
|
11 |
val main: unit -> unit |
|
5883 | 12 |
val loop: unit -> unit |
6860 | 13 |
val sync_main: unit -> unit |
14 |
val sync_loop: unit -> unit |
|
5829 | 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 |
|
9588 | 29 |
val thy_script: string |
6722 | 30 |
val thy_goal: string |
31 |
val qed: string |
|
6733 | 32 |
val qed_block: string |
8209 | 33 |
val qed_global: string |
9552 | 34 |
val prf_heading: string |
6722 | 35 |
val prf_goal: string |
36 |
val prf_block: string |
|
9056 | 37 |
val prf_open: string |
38 |
val prf_close: string |
|
6722 | 39 |
val prf_chain: string |
40 |
val prf_decl: string |
|
6868 | 41 |
val prf_asm: string |
7676 | 42 |
val prf_asm_goal: string |
6722 | 43 |
val prf_script: string |
44 |
val kinds: string list |
|
45 |
end |
|
5829 | 46 |
type token |
47 |
type parser |
|
6722 | 48 |
val command: string -> string -> string -> |
6373 | 49 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser |
9132 | 50 |
val markup_command: IsarOutput.markup -> string -> string -> string -> |
7789 | 51 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser |
6722 | 52 |
val improper_command: string -> string -> string -> |
6373 | 53 |
(token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser |
7026 | 54 |
val dest_keywords: unit -> string list |
55 |
val dest_parsers: unit -> (string * string * string * bool) list |
|
5883 | 56 |
val print_outer_syntax: unit -> unit |
9223 | 57 |
val print_commands: Toplevel.transition -> Toplevel.transition |
5829 | 58 |
val add_keywords: string list -> unit |
59 |
val add_parsers: parser list -> unit |
|
12943 | 60 |
val check_text: string * Position.T -> bool -> Toplevel.state -> unit |
7940 | 61 |
val deps_thy: string -> bool -> Path.T -> string list * Path.T list |
6199 | 62 |
val load_thy: string -> bool -> bool -> Path.T -> unit |
7333 | 63 |
val isar: bool -> bool -> Toplevel.isar |
5829 | 64 |
end; |
65 |
||
66 |
structure OuterSyntax: OUTER_SYNTAX = |
|
67 |
struct |
|
68 |
||
7750 | 69 |
structure T = OuterLex; |
6860 | 70 |
structure P = OuterParse; |
71 |
||
5829 | 72 |
|
73 |
(** outer syntax **) |
|
74 |
||
6722 | 75 |
(* command keyword classification *) |
76 |
||
77 |
structure Keyword = |
|
78 |
struct |
|
79 |
val control = "control"; |
|
80 |
val diag = "diag"; |
|
81 |
val thy_begin = "theory-begin"; |
|
7104 | 82 |
val thy_switch = "theory-switch"; |
6722 | 83 |
val thy_end = "theory-end"; |
84 |
val thy_heading = "theory-heading"; |
|
85 |
val thy_decl = "theory-decl"; |
|
9588 | 86 |
val thy_script = "theory-script"; |
6722 | 87 |
val thy_goal = "theory-goal"; |
88 |
val qed = "qed"; |
|
6733 | 89 |
val qed_block = "qed-block"; |
8209 | 90 |
val qed_global = "qed-global"; |
9552 | 91 |
val prf_heading = "proof-heading"; |
6722 | 92 |
val prf_goal = "proof-goal"; |
93 |
val prf_block = "proof-block"; |
|
9056 | 94 |
val prf_open = "proof-open"; |
95 |
val prf_close = "proof-close"; |
|
6722 | 96 |
val prf_chain = "proof-chain"; |
97 |
val prf_decl = "proof-decl"; |
|
6868 | 98 |
val prf_asm = "proof-asm"; |
7676 | 99 |
val prf_asm_goal = "proof-asm-goal"; |
6722 | 100 |
val prf_script = "proof-script"; |
101 |
||
9588 | 102 |
val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_script, |
103 |
thy_goal, qed, qed_block, qed_global, prf_heading, prf_goal, prf_block, prf_open, prf_close, |
|
104 |
prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script]; |
|
6722 | 105 |
end; |
106 |
||
107 |
||
5829 | 108 |
(* parsers *) |
109 |
||
7750 | 110 |
type token = T.token; |
5829 | 111 |
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list; |
112 |
||
113 |
datatype parser = |
|
9132 | 114 |
Parser of string * (string * string * IsarOutput.markup option) * bool * parser_fn; |
5829 | 115 |
|
7750 | 116 |
fun parser int_only markup name comment kind parse = |
117 |
Parser (name, (comment, kind, markup), int_only, parse); |
|
5829 | 118 |
|
119 |
||
120 |
(* parse command *) |
|
121 |
||
6860 | 122 |
local |
6199 | 123 |
|
5829 | 124 |
fun command_body cmd (name, _) = |
7026 | 125 |
(case cmd name of |
126 |
Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) |
|
127 |
| None => sys_error ("no parser for outer syntax command " ^ quote name)); |
|
6860 | 128 |
|
129 |
fun terminator false = Scan.succeed () |
|
9132 | 130 |
| terminator true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ()); |
6860 | 131 |
|
132 |
in |
|
5829 | 133 |
|
6860 | 134 |
fun command term cmd = |
9132 | 135 |
P.semicolon >> K None || |
6860 | 136 |
P.sync >> K None || |
7026 | 137 |
(P.position P.command :-- command_body cmd) --| terminator term |
6860 | 138 |
>> (fn ((name, pos), (int_only, f)) => |
139 |
Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |> |
|
140 |
Toplevel.interactive int_only |> f)); |
|
5829 | 141 |
|
6199 | 142 |
end; |
143 |
||
5829 | 144 |
|
145 |
||
9132 | 146 |
(** global outer syntax **) |
5829 | 147 |
|
7026 | 148 |
local |
149 |
||
150 |
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon); |
|
7750 | 151 |
val global_parsers = |
9132 | 152 |
ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * IsarOutput.markup option) |
153 |
Symtab.table); |
|
154 |
val global_markups = ref ([]: (string * IsarOutput.markup) list); |
|
5952 | 155 |
|
7026 | 156 |
fun change_lexicons f = |
157 |
let val lexs = f (! global_lexicons) in |
|
158 |
(case (op inter_string) (pairself Scan.dest_lexicon lexs) of |
|
159 |
[] => global_lexicons := lexs |
|
160 |
| bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads)) |
|
161 |
end; |
|
5829 | 162 |
|
7789 | 163 |
fun get_markup (ms, (name, (_, Some m))) = (name, m) :: ms |
164 |
| get_markup (ms, _) = ms; |
|
7750 | 165 |
|
166 |
fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers); |
|
9132 | 167 |
fun change_parsers f = (Library.change global_parsers f; make_markups ()); |
6722 | 168 |
|
7026 | 169 |
in |
170 |
||
7750 | 171 |
|
9132 | 172 |
(* access current syntax *) |
7026 | 173 |
|
174 |
(*Note: the syntax for files is statically determined at the very |
|
175 |
beginning; for interactive processing it may change dynamically.*) |
|
176 |
||
177 |
fun get_lexicons () = ! global_lexicons; |
|
178 |
fun get_parsers () = ! global_parsers; |
|
7750 | 179 |
fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers); |
7789 | 180 |
|
9132 | 181 |
fun is_markup kind name = |
182 |
(case assoc (! global_markups, name) of Some k => k = kind | None => false); |
|
183 |
fun markup kind = Scan.one (T.is_kind T.Command andf is_markup kind o T.val_of); |
|
5829 | 184 |
|
185 |
||
186 |
(* augment syntax *) |
|
187 |
||
7026 | 188 |
fun add_keywords keywords = change_lexicons (apfst (fn lex => |
189 |
(Scan.extend_lexicon lex (map Symbol.explode keywords)))); |
|
5829 | 190 |
|
7750 | 191 |
fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) = |
5829 | 192 |
(if is_none (Symtab.lookup (tab, name)) then () |
193 |
else warning ("Redefined outer syntax command " ^ quote name); |
|
7750 | 194 |
Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab)); |
5829 | 195 |
|
196 |
fun add_parsers parsers = |
|
7026 | 197 |
(change_parsers (fn tab => foldl add_parser (tab, parsers)); |
198 |
change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex |
|
199 |
(map (fn Parser (name, _, _, _) => Symbol.explode name) parsers)))); |
|
200 |
||
201 |
end; |
|
5829 | 202 |
|
203 |
||
7026 | 204 |
(* print syntax *) |
205 |
||
206 |
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ())); |
|
207 |
||
208 |
fun dest_parsers () = |
|
7750 | 209 |
map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only)) |
7026 | 210 |
(Symtab.dest (get_parsers ())); |
5829 | 211 |
|
9223 | 212 |
fun print_outer_syntax () = |
7026 | 213 |
let |
214 |
fun pretty_cmd (name, comment, _, _) = |
|
215 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
|
216 |
val (int_cmds, cmds) = partition #4 (dest_parsers ()); |
|
217 |
in |
|
8720 | 218 |
[Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())), |
219 |
Pretty.big_list "proper commands:" (map pretty_cmd cmds), |
|
220 |
Pretty.big_list "improper commands (interactive-only):" (map pretty_cmd int_cmds)] |
|
9223 | 221 |
|> Pretty.chunks |> Pretty.writeln |
7026 | 222 |
end; |
5829 | 223 |
|
9223 | 224 |
val print_commands = Toplevel.imperative print_outer_syntax; |
7367 | 225 |
|
5829 | 226 |
|
227 |
||
9132 | 228 |
(** toplevel parsing **) |
5829 | 229 |
|
9132 | 230 |
(* basic sources *) |
6860 | 231 |
|
9132 | 232 |
fun toplevel_source term do_recover cmd src = |
233 |
let |
|
234 |
val no_terminator = |
|
235 |
Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof)); |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
236 |
fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [None]) x; |
9132 | 237 |
in |
238 |
src |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
239 |
|> T.source_proper |
9132 | 240 |
|> Source.source T.stopper |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
241 |
(Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K None || P.not_eof >> Some)) |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
242 |
(if do_recover then Some recover else None) |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
243 |
|> Source.mapfilter I |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
244 |
|> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs)) |
9132 | 245 |
(if do_recover then Some recover else None) |
246 |
|> Source.mapfilter I |
|
247 |
end; |
|
5829 | 248 |
|
7746 | 249 |
|
9132 | 250 |
(* interactive source of toplevel transformers *) |
5829 | 251 |
|
9132 | 252 |
fun isar term no_pos = |
253 |
Source.tty |
|
254 |
|> Symbol.source true |
|
255 |
|> T.source true get_lexicons |
|
256 |
(if no_pos then Position.none else Position.line_name 1 "stdin") |
|
257 |
|> toplevel_source term true get_parser; |
|
6199 | 258 |
|
259 |
||
7746 | 260 |
|
9132 | 261 |
(** read theory **) |
6247 | 262 |
|
12943 | 263 |
(* check_text *) |
264 |
||
265 |
fun check_text s true state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ()) |
|
266 |
| check_text _ false _ = (); |
|
267 |
||
268 |
||
9132 | 269 |
(* deps_thy *) |
6199 | 270 |
|
7940 | 271 |
fun deps_thy name ml path = |
6199 | 272 |
let |
7735 | 273 |
val src = Source.of_string (File.read path); |
274 |
val pos = Path.position path; |
|
9132 | 275 |
val (name', parents, files) = ThyHeader.scan (src, pos); |
6199 | 276 |
val ml_path = ThyLoad.ml_path name; |
7940 | 277 |
val ml_file = if ml andalso is_some (ThyLoad.check_file ml_path) then [ml_path] else []; |
6199 | 278 |
in |
279 |
if name <> name' then |
|
7940 | 280 |
error ("Filename " ^ quote (Path.pack path) ^ |
8078 | 281 |
" does not agree with theory name " ^ quote name') |
6247 | 282 |
else (parents, map (Path.unpack o #1) files @ ml_file) |
6199 | 283 |
end; |
284 |
||
7746 | 285 |
|
9132 | 286 |
(* load_thy *) |
6199 | 287 |
|
7746 | 288 |
local |
289 |
||
7940 | 290 |
fun try_ml_file name time = |
6199 | 291 |
let |
292 |
val path = ThyLoad.ml_path name; |
|
7940 | 293 |
val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path); |
6247 | 294 |
val tr_name = if time then "time_use" else "use"; |
6199 | 295 |
in |
7243 | 296 |
if is_none (ThyLoad.check_file path) then () |
9132 | 297 |
else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr] |
6199 | 298 |
end; |
299 |
||
9132 | 300 |
fun parse_thy src = |
301 |
src |
|
302 |
|> toplevel_source false false (K (get_parser ())) |
|
7746 | 303 |
|> Source.exhaust; |
5829 | 304 |
|
6247 | 305 |
fun run_thy name path = |
7683 | 306 |
let |
7735 | 307 |
val pos = Path.position path; |
9132 | 308 |
val text = Library.untabify (explode (File.read path)); |
309 |
val text_src = Source.of_list text; |
|
310 |
fun present_text () = Source.exhaust (Symbol.source false text_src); |
|
7683 | 311 |
in |
7735 | 312 |
Present.init_theory name; |
9132 | 313 |
Present.verbatim_source name present_text; |
314 |
if ThyHeader.is_old (text_src, pos) then (ThySyn.load_thy name text; |
|
315 |
Present.old_symbol_source name present_text) (*note: text presented twice*) |
|
316 |
else |
|
317 |
let |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
318 |
val tok_src = text_src |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
319 |
|> Symbol.source false |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
320 |
|> T.source false (K (get_lexicons ())) pos |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
10749
diff
changeset
|
321 |
|> Source.exhausted; |
9132 | 322 |
val out = Toplevel.excursion_result |
323 |
(IsarOutput.parse_thy markup (#1 (get_lexicons ())) |
|
324 |
(parse_thy tok_src) tok_src); |
|
325 |
in Present.theory_output name (Buffer.content out) end |
|
6247 | 326 |
end; |
6199 | 327 |
|
7746 | 328 |
in |
329 |
||
6199 | 330 |
fun load_thy name ml time path = |
6247 | 331 |
(if time then |
332 |
timeit (fn () => |
|
333 |
(writeln ("\n**** Starting theory " ^ quote name ^ " ****"); |
|
9036 | 334 |
run_thy name path; |
6247 | 335 |
writeln ("**** Finished theory " ^ quote name ^ " ****\n"))) |
336 |
else run_thy name path; |
|
337 |
Context.context (ThyInfo.get_theory name); |
|
7940 | 338 |
if ml then try_ml_file name time else ()); |
5829 | 339 |
|
7746 | 340 |
end; |
341 |
||
5829 | 342 |
|
343 |
||
344 |
(** the read-eval-print loop **) |
|
345 |
||
5923 | 346 |
(* main loop *) |
347 |
||
7333 | 348 |
fun gen_loop term no_pos = |
349 |
(Context.reset_context (); |
|
350 |
Toplevel.loop (isar term no_pos)); |
|
5829 | 351 |
|
7333 | 352 |
fun gen_main term no_pos = |
5829 | 353 |
(Toplevel.set_state Toplevel.toplevel; |
6199 | 354 |
writeln (Session.welcome ()); |
7333 | 355 |
gen_loop term no_pos); |
6860 | 356 |
|
7333 | 357 |
fun main () = gen_main false false; |
358 |
fun loop () = gen_loop false false; |
|
359 |
fun sync_main () = gen_main true true; |
|
360 |
fun sync_loop () = gen_loop true true; |
|
5829 | 361 |
|
362 |
||
363 |
(* help *) |
|
364 |
||
365 |
fun help () = |
|
366 |
writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\ |
|
9223 | 367 |
\invoke 'Isar.loop();' to get back to the Isar read-eval-print loop."); |
5829 | 368 |
|
369 |
||
6373 | 370 |
(*final declarations of this structure!*) |
7789 | 371 |
val command = parser false None; |
9132 | 372 |
val markup_command = parser false o Some; |
7789 | 373 |
val improper_command = parser true None; |
6685 | 374 |
|
6373 | 375 |
|
5829 | 376 |
end; |
377 |
||
6199 | 378 |
(*setup theory syntax dependent operations*) |
379 |
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy; |
|
380 |
ThyLoad.load_thy_fn := OuterSyntax.load_thy; |
|
381 |
structure ThyLoad: THY_LOAD = ThyLoad; |
|
382 |
||
5829 | 383 |
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax; |
384 |
open BasicOuterSyntax; |