1 (* Title: Pure/Isar/outer_syntax.ML
2 Author: Markus Wenzel, TU Muenchen
4 The global Isabelle/Isar outer syntax.
6 Note: the syntax for files is statically determined at the very
7 beginning; for interactive processing it may change dynamically.
10 signature OUTER_SYNTAX =
13 val is_markup: outer_syntax -> Thy_Output.markup -> string -> bool
14 val get_syntax: unit -> (Scan.lexicon * Scan.lexicon) * outer_syntax
15 val command: string -> string -> Keyword.T ->
16 (Toplevel.transition -> Toplevel.transition) parser -> unit
17 val markup_command: Thy_Output.markup -> string -> string -> Keyword.T ->
18 (Toplevel.transition -> Toplevel.transition) parser -> unit
19 val improper_command: string -> string -> Keyword.T ->
20 (Toplevel.transition -> Toplevel.transition) parser -> unit
21 val internal_command: string ->
22 (Toplevel.transition -> Toplevel.transition) parser -> unit
23 val local_theory': string -> string -> Keyword.T ->
24 (bool -> local_theory -> local_theory) parser -> unit
25 val local_theory: string -> string -> Keyword.T ->
26 (local_theory -> local_theory) parser -> unit
27 val local_theory_to_proof': string -> string -> Keyword.T ->
28 (bool -> local_theory -> Proof.state) parser -> unit
29 val local_theory_to_proof: string -> string -> Keyword.T ->
30 (local_theory -> Proof.state) parser -> unit
31 val print_outer_syntax: unit -> unit
32 val scan: Position.T -> string -> Token.T list
33 val parse: Position.T -> string -> Toplevel.transition list
34 val process_file: Path.T -> theory -> theory
36 val isar: TextIO.instream -> bool -> isar
37 val read_command: Position.T -> string -> Toplevel.transition
38 val read_element: outer_syntax -> (unit -> theory) -> Thy_Syntax.element ->
39 (Toplevel.transition * Toplevel.transition list) list
42 structure Outer_Syntax: OUTER_SYNTAX =
49 datatype command = Command of
51 markup: Thy_Output.markup option,
53 parse: (Toplevel.transition -> Toplevel.transition) parser};
55 fun make_command comment markup int_only parse =
56 Command {comment = comment, markup = markup, int_only = int_only, parse = parse};
63 fun terminate false = Scan.succeed ()
65 Parse.group (fn () => "end of input")
66 (Scan.option Parse.sync -- Parse.semicolon >> K ());
68 fun body cmd (name, _) =
70 SOME (Command {int_only, parse, ...}) =>
71 Parse.!!! (Scan.prompt (name ^ "# ") (Parse.tags |-- parse >> pair int_only))
72 | NONE => raise Fail ("No parser for outer syntax command " ^ quote name));
76 fun parse_command do_terminate cmd =
77 Parse.semicolon >> K NONE ||
78 Parse.sync >> K NONE ||
79 (Parse.position Parse.command :-- body cmd) --| terminate do_terminate
80 >> (fn ((name, pos), (int_only, f)) =>
81 SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
82 Toplevel.interactive int_only |> f));
87 (* type outer_syntax *)
89 datatype outer_syntax = Outer_Syntax of
90 {commands: command Symtab.table,
91 markups: (string * Thy_Output.markup) list};
93 fun make_outer_syntax commands markups =
94 Outer_Syntax {commands = commands, markups = markups};
96 val empty_outer_syntax = make_outer_syntax Symtab.empty [];
99 fun map_commands f (Outer_Syntax {commands, ...}) =
101 val commands' = f commands;
103 Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
105 in make_outer_syntax commands' markups' end;
107 fun dest_commands (Outer_Syntax {commands, ...}) =
108 commands |> Symtab.dest |> sort_wrt #1
109 |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
111 fun lookup_commands (Outer_Syntax {commands, ...}) = Symtab.lookup commands;
113 fun is_markup (Outer_Syntax {markups, ...}) kind name =
114 AList.lookup (op =) markups name = SOME kind;
118 (** global outer syntax **)
122 (*synchronized wrt. Keywords*)
123 val global_outer_syntax = Unsynchronized.ref empty_outer_syntax;
125 fun add_command name kind cmd = CRITICAL (fn () =>
126 (Keyword.command name kind;
127 Unsynchronized.change global_outer_syntax (map_commands (fn commands =>
128 (if not (Symtab.defined commands name) then ()
129 else warning ("Redefining outer syntax command " ^ quote name);
130 Symtab.update (name, cmd) commands)))));
134 fun get_syntax () = CRITICAL (fn () => (Keyword.get_lexicons (), ! global_outer_syntax));
136 fun lookup_commands_dynamic () = lookup_commands (! global_outer_syntax);
138 fun command name comment kind parse =
139 add_command name kind (make_command comment NONE false parse);
141 fun markup_command markup name comment kind parse =
142 add_command name kind (make_command comment (SOME markup) false parse);
144 fun improper_command name comment kind parse =
145 add_command name kind (make_command comment NONE true parse);
147 fun internal_command name parse =
148 command name "(internal)" Keyword.control (parse >> (fn tr => Toplevel.no_timing o tr));
153 (* local_theory commands *)
155 fun local_theory_command do_print trans name comment kind parse =
156 command name comment kind (Parse.opt_target -- parse
157 >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
159 val local_theory' = local_theory_command false Toplevel.local_theory';
160 val local_theory = local_theory_command false Toplevel.local_theory;
161 val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
162 val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
167 fun print_outer_syntax () =
169 val (keywords, outer_syntax) =
170 CRITICAL (fn () => (Keyword.dest_keywords (), #2 (get_syntax ())));
171 fun pretty_cmd (name, comment, _) =
172 Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
173 val (int_cmds, cmds) = List.partition #3 (dest_commands outer_syntax);
175 [Pretty.strs ("syntax keywords:" :: map quote keywords),
176 Pretty.big_list "commands:" (map pretty_cmd cmds),
177 Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
178 |> Pretty.chunks |> Pretty.writeln
183 (** toplevel parsing **)
187 fun toplevel_source term do_recover cmd src =
190 Scan.unless Parse.semicolon (Scan.one (Token.not_sync andf Token.not_eof));
192 (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
195 |> Token.source_proper
196 |> Source.source Token.stopper
197 (Scan.bulk (Parse.$$$ "--" -- Parse.!!! Parse.doc_source >> K NONE || Parse.not_eof >> SOME))
198 (Option.map recover do_recover)
199 |> Source.map_filter I
200 |> Source.source Token.stopper
201 (Scan.bulk (fn xs => Parse.!!! (parse_command term (cmd ())) xs))
202 (Option.map recover do_recover)
203 |> Source.map_filter I
207 (* off-line scanning/parsing *)
212 |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
218 |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
219 |> toplevel_source false NONE lookup_commands_dynamic
225 fun process_file path thy =
227 val trs = parse (Path.position path) (File.read path);
228 val init = Toplevel.init_theory (K thy) Toplevel.empty;
229 val result = fold Toplevel.command (init :: trs) Toplevel.toplevel;
231 (case (Toplevel.is_theory result, Toplevel.generic_theory_of result) of
232 (true, Context.Theory thy') => thy'
233 | _ => error "Bad result state: global theory expected")
237 (* interactive source of toplevel transformers *)
240 (Toplevel.transition, (Toplevel.transition option,
241 (Token.T, (Token.T option, (Token.T, (Token.T,
242 (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
243 Source.source) Source.source) Source.source) Source.source)
244 Source.source) Source.source) Source.source) Source.source;
246 fun isar in_stream term : isar =
249 |> Token.source {do_recover = SOME true} Keyword.get_lexicons Position.none
250 |> toplevel_source term (SOME true) lookup_commands_dynamic;
253 (* read toplevel commands -- fail-safe *)
255 val not_singleton = "Exactly one command expected";
257 fun read_span outer_syntax toks =
259 val commands = lookup_commands outer_syntax;
260 val range_pos = Position.set_range (Token.range toks);
261 val _ = Position.reports (maps Thy_Syntax.reports_of_token toks);
263 (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
265 if Keyword.is_control (Toplevel.name_of tr) then
266 (Toplevel.malformed (Toplevel.pos_of tr) "Illegal control command", true)
268 | [] => (Toplevel.ignored range_pos, false)
269 | _ => (Toplevel.malformed range_pos not_singleton, true))
270 handle ERROR msg => (Toplevel.malformed range_pos msg, true)
273 fun read_command pos str =
275 val (lexs, outer_syntax) = get_syntax ();
276 val toks = Thy_Syntax.parse_tokens lexs pos str;
277 in #1 (read_span outer_syntax toks) end;
279 fun read_element outer_syntax init {head, proof, proper_proof} =
281 val read = read_span outer_syntax o Thy_Syntax.span_content;
282 val (tr, proper_head) = read head |>> Toplevel.modify_init init;
283 val proof_trs = map read proof |> filter #2 |> map #1;
285 if proper_head andalso proper_proof then [(tr, proof_trs)]
286 else map (rpair []) (if proper_head then tr :: proof_trs else proof_trs)