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