author | wenzelm |
Sat, 12 Apr 2008 17:00:35 +0200 | |
changeset 26626 | c6231d64d264 |
parent 26620 | 722cf4fdd4dd |
child 26700 | 493db7848904 |
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 OUTER_SYNTAX = |
|
11 |
sig |
|
24868 | 12 |
type parser_fn = OuterLex.token list -> |
13 |
(Toplevel.transition -> Toplevel.transition) * OuterLex.token list |
|
23722 | 14 |
val get_lexicons: unit -> Scan.lexicon * Scan.lexicon |
23796 | 15 |
val command_keyword: string -> OuterKeyword.T option |
14687 | 16 |
val is_keyword: string -> bool |
24868 | 17 |
val keywords: string list -> unit |
18 |
val command: string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
19 |
val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
20 |
val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit |
|
7026 | 21 |
val dest_keywords: unit -> string list |
22 |
val dest_parsers: unit -> (string * string * string * bool) list |
|
5883 | 23 |
val print_outer_syntax: unit -> unit |
24872 | 24 |
val report: unit -> unit |
19060 | 25 |
val check_text: string * Position.T -> Toplevel.node option -> unit |
16195 | 26 |
val scan: string -> OuterLex.token list |
25580 | 27 |
val parse: Position.T -> string -> Toplevel.transition list |
26431 | 28 |
val process_file: Path.T -> theory -> theory |
26600 | 29 |
type isar |
30 |
val isar: bool -> isar |
|
26611 | 31 |
val load_thy: Path.T -> string -> Position.T -> string list -> bool -> unit |
5829 | 32 |
end; |
33 |
||
26600 | 34 |
structure OuterSyntax: OUTER_SYNTAX = |
5829 | 35 |
struct |
36 |
||
7750 | 37 |
structure T = OuterLex; |
6860 | 38 |
structure P = OuterParse; |
39 |
||
5829 | 40 |
|
41 |
(** outer syntax **) |
|
42 |
||
24872 | 43 |
(* diagnostics *) |
44 |
||
45 |
fun report_keyword name = |
|
46 |
Pretty.markup (Markup.keyword_decl name) |
|
47 |
[Pretty.str ("Outer syntax keyword: " ^ quote name)]; |
|
48 |
||
49 |
fun report_command name kind = |
|
50 |
Pretty.markup (Markup.command_decl name kind) |
|
51 |
[Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")")]; |
|
52 |
||
53 |
||
5829 | 54 |
(* parsers *) |
55 |
||
24868 | 56 |
type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list; |
5829 | 57 |
|
24868 | 58 |
datatype parser = Parser of |
59 |
{comment: string, |
|
60 |
kind: OuterKeyword.T, |
|
61 |
markup: ThyOutput.markup option, |
|
62 |
int_only: bool, |
|
63 |
parse: parser_fn}; |
|
5829 | 64 |
|
24868 | 65 |
fun make_parser comment kind markup int_only parse = |
66 |
Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse}; |
|
5829 | 67 |
|
68 |
||
69 |
(* parse command *) |
|
70 |
||
6860 | 71 |
local |
6199 | 72 |
|
14925
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
73 |
fun terminate false = Scan.succeed () |
0f86a8a694f8
added read (provides transition names and sources);
wenzelm
parents:
14687
diff
changeset
|
74 |
| 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
|
75 |
|
26620 | 76 |
fun body cmd (name, _) = |
7026 | 77 |
(case cmd name of |
24868 | 78 |
SOME (Parser {int_only, parse, ...}) => |
26620 | 79 |
P.!!! (Scan.prompt (name ^ "# ") (P.tags |-- parse >> pair int_only)) |
15531 | 80 |
| NONE => sys_error ("no parser for outer syntax command " ^ quote name)); |
6860 | 81 |
|
82 |
in |
|
5829 | 83 |
|
26620 | 84 |
fun parse_command do_terminate cmd = |
15531 | 85 |
P.semicolon >> K NONE || |
86 |
P.sync >> K NONE || |
|
26620 | 87 |
(P.position P.command :-- body cmd) --| terminate do_terminate |
6860 | 88 |
>> (fn ((name, pos), (int_only, f)) => |
15531 | 89 |
SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |> |
6860 | 90 |
Toplevel.interactive int_only |> f)); |
5829 | 91 |
|
6199 | 92 |
end; |
93 |
||
5829 | 94 |
|
95 |
||
9132 | 96 |
(** global outer syntax **) |
5829 | 97 |
|
7026 | 98 |
local |
99 |
||
100 |
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon); |
|
24868 | 101 |
val global_parsers = ref (Symtab.empty: parser Symtab.table); |
22120 | 102 |
val global_markups = ref ([]: (string * ThyOutput.markup) list); |
5952 | 103 |
|
23939 | 104 |
fun change_lexicons f = CRITICAL (fn () => |
7026 | 105 |
let val lexs = f (! global_lexicons) in |
106 |
(case (op inter_string) (pairself Scan.dest_lexicon lexs) of |
|
107 |
[] => global_lexicons := lexs |
|
108 |
| bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads)) |
|
23939 | 109 |
end); |
5829 | 110 |
|
23939 | 111 |
fun change_parsers f = CRITICAL (fn () => |
112 |
(change global_parsers f; |
|
113 |
global_markups := |
|
24868 | 114 |
Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I) |
115 |
(! global_parsers) [])); |
|
6722 | 116 |
|
7026 | 117 |
in |
118 |
||
9132 | 119 |
(* access current syntax *) |
7026 | 120 |
|
24868 | 121 |
fun get_lexicons () = CRITICAL (fn () => ! global_lexicons); |
122 |
fun get_parsers () = CRITICAL (fn () => ! global_parsers); |
|
123 |
fun get_markups () = CRITICAL (fn () => ! global_markups); |
|
7026 | 124 |
|
24868 | 125 |
fun get_parser () = Symtab.lookup (get_parsers ()); |
7789 | 126 |
|
23796 | 127 |
fun command_keyword name = |
24868 | 128 |
(case Symtab.lookup (get_parsers ()) name of |
129 |
SOME (Parser {kind, ...}) => SOME kind |
|
130 |
| NONE => NONE); |
|
131 |
||
26600 | 132 |
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
|
133 |
|
24868 | 134 |
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind; |
5829 | 135 |
|
136 |
||
137 |
(* augment syntax *) |
|
138 |
||
24872 | 139 |
fun keywords names = |
140 |
(change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names))); |
|
141 |
List.app (Pretty.writeln o report_keyword) names); |
|
24868 | 142 |
|
143 |
||
24872 | 144 |
fun add_parser (name, parser as Parser {kind, ...}) = |
24868 | 145 |
(if not (Symtab.defined (get_parsers ()) name) then () |
146 |
else warning ("Redefining outer syntax command " ^ quote name); |
|
147 |
change_parsers (Symtab.update (name, parser)); |
|
24872 | 148 |
change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name])); |
149 |
Pretty.writeln (report_command name (OuterKeyword.kind_of kind))); |
|
5829 | 150 |
|
24868 | 151 |
fun command name comment kind parse = |
152 |
add_parser (name, make_parser comment kind NONE false parse); |
|
5829 | 153 |
|
24868 | 154 |
fun markup_command markup name comment kind parse = |
155 |
add_parser (name, make_parser comment kind (SOME markup) false parse); |
|
156 |
||
157 |
fun improper_command name comment kind parse = |
|
158 |
add_parser (name, make_parser comment kind NONE true parse); |
|
7026 | 159 |
|
160 |
end; |
|
5829 | 161 |
|
162 |
||
24872 | 163 |
(* inspect syntax *) |
7026 | 164 |
|
14687 | 165 |
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s); |
7026 | 166 |
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ())); |
167 |
||
168 |
fun dest_parsers () = |
|
16727 | 169 |
get_parsers () |> Symtab.dest |> sort_wrt #1 |
24868 | 170 |
|> map (fn (name, Parser {comment, kind, int_only, ...}) => |
171 |
(name, comment, OuterKeyword.kind_of kind, int_only)); |
|
5829 | 172 |
|
9223 | 173 |
fun print_outer_syntax () = |
7026 | 174 |
let |
175 |
fun pretty_cmd (name, comment, _, _) = |
|
176 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
|
15570 | 177 |
val (int_cmds, cmds) = List.partition #4 (dest_parsers ()); |
7026 | 178 |
in |
8720 | 179 |
[Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())), |
18326 | 180 |
Pretty.big_list "commands:" (map pretty_cmd cmds), |
181 |
Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)] |
|
9223 | 182 |
|> Pretty.chunks |> Pretty.writeln |
7026 | 183 |
end; |
5829 | 184 |
|
24872 | 185 |
fun report () = |
186 |
(map report_keyword (dest_keywords ()) @ |
|
187 |
map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ())) |
|
188 |
|> Pretty.chunks |> Pretty.writeln; |
|
7367 | 189 |
|
5829 | 190 |
|
191 |
||
9132 | 192 |
(** toplevel parsing **) |
5829 | 193 |
|
9132 | 194 |
(* basic sources *) |
6860 | 195 |
|
26620 | 196 |
fun toplevel_source term 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 |
26620 | 210 |
(Scan.bulk (fn xs => P.!!! (parse_command term (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 |
|
25580 | 216 |
(* off-line scanning/parsing *) |
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 |
||
25580 | 224 |
fun parse pos str = |
225 |
Source.of_string str |
|
226 |
|> Symbol.source false |
|
227 |
|> T.source (SOME false) get_lexicons pos |
|
26620 | 228 |
|> toplevel_source false NONE get_parser |
25580 | 229 |
|> Source.exhaust; |
230 |
||
14091 | 231 |
|
26431 | 232 |
(* process file *) |
233 |
||
234 |
fun process_file path thy = |
|
235 |
let |
|
236 |
val result = ref thy; |
|
237 |
val trs = parse (Position.path path) (File.read path); |
|
238 |
val init = Toplevel.init_theory (K thy) (fn thy' => result := thy') (K ()); |
|
239 |
val _ = Toplevel.excursion (init Toplevel.empty :: trs @ [Toplevel.exit Toplevel.empty]); |
|
240 |
in ! result end; |
|
241 |
||
242 |
||
24868 | 243 |
(* interactive source of toplevel transformers *) |
244 |
||
26600 | 245 |
type isar = |
246 |
(Toplevel.transition, (Toplevel.transition option, |
|
247 |
(OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token, |
|
248 |
Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source) |
|
249 |
Source.source) Source.source) Source.source) Source.source) Source.source) Source.source; |
|
250 |
||
251 |
fun isar term : isar = |
|
24868 | 252 |
Source.tty |
253 |
|> Symbol.source true |
|
254 |
|> T.source (SOME true) get_lexicons Position.none |
|
26620 | 255 |
|> toplevel_source term (SOME true) get_parser; |
24868 | 256 |
|
257 |
||
16195 | 258 |
|
9132 | 259 |
(** read theory **) |
6247 | 260 |
|
12943 | 261 |
(* check_text *) |
262 |
||
22120 | 263 |
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ()); |
12943 | 264 |
|
265 |
||
26611 | 266 |
(* load_thy *) |
7746 | 267 |
|
26323
73efc70edeef
theory loader: discontinued *attached* ML scripts;
wenzelm
parents:
26291
diff
changeset
|
268 |
fun load_thy dir name pos text time = |
7683 | 269 |
let |
24065 | 270 |
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
|
271 |
|
17932 | 272 |
val _ = Present.init_theory name; |
24065 | 273 |
val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src)); |
274 |
val toks = text_src |
|
17932 | 275 |
|> Symbol.source false |
24065 | 276 |
|> T.source NONE (K (get_lexicons ())) pos |
17932 | 277 |
|> Source.exhausted; |
278 |
val trs = toks |
|
26620 | 279 |
|> toplevel_source false NONE (K (get_parser ())) |
17932 | 280 |
|> Source.exhaust; |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
281 |
|
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
282 |
val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else (); |
25685 | 283 |
val _ = cond_timeit time "" (fn () => |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
284 |
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
|
285 |
|> Buffer.content |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
286 |
|> Present.theory_output name); |
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
287 |
val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else (); |
24065 | 288 |
in () end; |
23866
5295671034f8
moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents:
23796
diff
changeset
|
289 |
|
5829 | 290 |
end; |
291 |