author | wenzelm |
Mon, 06 Apr 2015 16:30:44 +0200 | |
changeset 59935 | 343905de27b1 |
parent 59932 | 74872a13f628 |
child 59939 | 7d46aa03696e |
permissions | -rw-r--r-- |
5829 | 1 |
(* Title: Pure/Isar/outer_syntax.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
4 |
Isabelle/Isar outer syntax. |
5829 | 5 |
*) |
6 |
||
7 |
signature OUTER_SYNTAX = |
|
8 |
sig |
|
58930 | 9 |
val help: theory -> string list -> unit |
10 |
val print_commands: theory -> unit |
|
59935 | 11 |
type command_keyword = string * Position.T |
12 |
val command: command_keyword -> string -> |
|
29311 | 13 |
(Toplevel.transition -> Toplevel.transition) parser -> unit |
59935 | 14 |
val local_theory': command_keyword -> string -> |
29380 | 15 |
(bool -> local_theory -> local_theory) parser -> unit |
59935 | 16 |
val local_theory: command_keyword -> string -> |
29311 | 17 |
(local_theory -> local_theory) parser -> unit |
59935 | 18 |
val local_theory_to_proof': command_keyword -> string -> |
29311 | 19 |
(bool -> local_theory -> Proof.state) parser -> unit |
59935 | 20 |
val local_theory_to_proof: command_keyword -> string -> |
29311 | 21 |
(local_theory -> Proof.state) parser -> unit |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
22 |
val parse: theory -> Position.T -> string -> Toplevel.transition list |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
23 |
val parse_tokens: theory -> Token.T list -> Toplevel.transition list |
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
24 |
val parse_spans: Token.T list -> Command_Span.span list |
52510 | 25 |
val side_comments: Token.T list -> Token.T list |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
26 |
val command_reports: theory -> Token.T -> Position.report_text list |
5829 | 27 |
end; |
28 |
||
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
29 |
structure Outer_Syntax: OUTER_SYNTAX = |
5829 | 30 |
struct |
31 |
||
32 |
(** outer syntax **) |
|
33 |
||
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
34 |
(* errors *) |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
35 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
36 |
fun err_command msg name ps = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
37 |
error (msg ^ quote (Markup.markup Markup.keyword1 name) ^ Position.here_list ps); |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
38 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
39 |
fun err_dup_command name ps = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
40 |
err_command "Duplicate outer syntax command " name ps; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
41 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
42 |
|
29311 | 43 |
(* command parsers *) |
5829 | 44 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
45 |
datatype command_parser = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
46 |
Parser of (Toplevel.transition -> Toplevel.transition) parser | |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
47 |
Private_Parser of Position.T option -> (Toplevel.transition -> Toplevel.transition) parser; |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
48 |
|
29311 | 49 |
datatype command = Command of |
24868 | 50 |
{comment: string, |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
51 |
command_parser: command_parser, |
48647
a5144c4c26a2
report commands as formal entities, with def/ref positions;
wenzelm
parents:
48646
diff
changeset
|
52 |
pos: Position.T, |
a5144c4c26a2
report commands as formal entities, with def/ref positions;
wenzelm
parents:
48646
diff
changeset
|
53 |
id: serial}; |
5829 | 54 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
55 |
fun eq_command (Command {id = id1, ...}, Command {id = id2, ...}) = id1 = id2; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
56 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
57 |
fun new_command comment command_parser pos = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
58 |
Command {comment = comment, command_parser = command_parser, pos = pos, id = serial ()}; |
48647
a5144c4c26a2
report commands as formal entities, with def/ref positions;
wenzelm
parents:
48646
diff
changeset
|
59 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
60 |
fun command_pos (Command {pos, ...}) = pos; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
61 |
|
48647
a5144c4c26a2
report commands as formal entities, with def/ref positions;
wenzelm
parents:
48646
diff
changeset
|
62 |
fun command_markup def (name, Command {pos, id, ...}) = |
a5144c4c26a2
report commands as formal entities, with def/ref positions;
wenzelm
parents:
48646
diff
changeset
|
63 |
Markup.properties (Position.entity_properties_of def id pos) |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49564
diff
changeset
|
64 |
(Markup.entity Markup.commandN name); |
5829 | 65 |
|
50213 | 66 |
fun pretty_command (cmd as (name, Command {comment, ...})) = |
67 |
Pretty.block |
|
68 |
(Pretty.marks_str |
|
50450
358b6020f8b6
generalized notion of active area, where sendback is just one application;
wenzelm
parents:
50215
diff
changeset
|
69 |
([Active.make_markup Markup.sendbackN {implicit = true, properties = [Markup.padding_line]}, |
50215 | 70 |
command_markup false cmd], name) :: Pretty.str ":" :: Pretty.brk 2 :: Pretty.text comment); |
50213 | 71 |
|
5829 | 72 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
73 |
(* theory data *) |
43711 | 74 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
75 |
structure Data = Theory_Data |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
76 |
( |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
77 |
type T = command Symtab.table; |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
78 |
val empty = Symtab.empty; |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
79 |
val extend = I; |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
80 |
fun merge data : T = |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
81 |
data |> Symtab.join (fn name => fn (cmd1, cmd2) => |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
82 |
if eq_command (cmd1, cmd2) then raise Symtab.SAME |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
83 |
else err_dup_command name [command_pos cmd1, command_pos cmd2]); |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
84 |
); |
43711 | 85 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
86 |
val get_commands = Data.get; |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
87 |
val dest_commands = get_commands #> Symtab.dest #> sort_wrt #1; |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
88 |
val lookup_commands = Symtab.lookup o get_commands; |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
89 |
|
58930 | 90 |
fun help thy pats = |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
91 |
dest_commands thy |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
92 |
|> filter (fn (name, _) => forall (fn pat => match_string pat name) pats) |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
93 |
|> map pretty_command |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
94 |
|> Pretty.writeln_chunks; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
95 |
|
58930 | 96 |
fun print_commands thy = |
43711 | 97 |
let |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
98 |
val keywords = Thy_Header.get_keywords thy; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
99 |
val minor = Scan.dest_lexicon (Keyword.minor_keywords keywords); |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
100 |
val commands = dest_commands thy; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
101 |
in |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
102 |
[Pretty.strs ("keywords:" :: map quote minor), |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
103 |
Pretty.big_list "commands:" (map pretty_command commands)] |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
104 |
|> Pretty.writeln_chunks |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
105 |
end; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
106 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
107 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
108 |
(* maintain commands *) |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
109 |
|
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
110 |
fun add_command name cmd thy = |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
111 |
let |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
112 |
val _ = |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
113 |
Keyword.is_command (Thy_Header.get_keywords thy) name orelse |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
114 |
err_command "Undeclared outer syntax command " name [command_pos cmd]; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
115 |
val _ = |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
116 |
(case lookup_commands thy name of |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
117 |
NONE => () |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
118 |
| SOME cmd' => err_dup_command name [command_pos cmd, command_pos cmd']); |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
119 |
val _ = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
120 |
Context_Position.report_generic (ML_Context.the_generic_context ()) |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
121 |
(command_pos cmd) (command_markup true (name, cmd)); |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
122 |
in Data.map (Symtab.update (name, cmd)) thy end; |
43711 | 123 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
124 |
val _ = Theory.setup (Theory.at_end (fn thy => |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
125 |
let |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
126 |
val command_keywords = |
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
127 |
Scan.dest_lexicon (Keyword.major_keywords (Thy_Header.get_keywords thy)); |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
128 |
val _ = |
58999
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents:
58934
diff
changeset
|
129 |
(case subtract (op =) (map #1 (dest_commands thy)) command_keywords of |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
130 |
[] => () |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
131 |
| missing => error ("Missing outer syntax command(s) " ^ commas_quote missing)) |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
132 |
in NONE end)); |
43711 | 133 |
|
5829 | 134 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
135 |
(* implicit theory setup *) |
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46958
diff
changeset
|
136 |
|
59935 | 137 |
type command_keyword = string * Position.T; |
5952 | 138 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
139 |
fun raw_command (name, pos) comment command_parser = |
59932 | 140 |
let val setup = add_command name (new_command comment command_parser pos) |
141 |
in Context.>> (Context.mapping setup (Local_Theory.background_theory setup)) end; |
|
26990 | 142 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
143 |
fun command (name, pos) comment parse = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
144 |
raw_command (name, pos) comment (Parser parse); |
59923
b21c82422d65
support private scope for individual local theory commands;
wenzelm
parents:
59083
diff
changeset
|
145 |
|
59935 | 146 |
fun local_theory_command trans command_keyword comment parse = |
147 |
raw_command command_keyword comment |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
148 |
(Private_Parser (fn private => |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
149 |
Parse.opt_target -- parse >> (fn (target, f) => trans private target f))); |
26990 | 150 |
|
56895
f058120aaad4
discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents:
56334
diff
changeset
|
151 |
val local_theory' = local_theory_command Toplevel.local_theory'; |
f058120aaad4
discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents:
56334
diff
changeset
|
152 |
val local_theory = local_theory_command Toplevel.local_theory; |
f058120aaad4
discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents:
56334
diff
changeset
|
153 |
val local_theory_to_proof' = local_theory_command Toplevel.local_theory_to_proof'; |
f058120aaad4
discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents:
56334
diff
changeset
|
154 |
val local_theory_to_proof = local_theory_command Toplevel.local_theory_to_proof; |
26990 | 155 |
|
156 |
||
5829 | 157 |
|
9132 | 158 |
(** toplevel parsing **) |
5829 | 159 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
160 |
(* parse commands *) |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
161 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
162 |
local |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
163 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
164 |
fun parse_command thy = |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
165 |
Scan.ahead (Parse.opt_private |-- Parse.position Parse.command_) :|-- (fn (name, pos) => |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
166 |
let |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
167 |
val command_tags = Parse.command_ -- Parse.tags; |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
168 |
val tr0 = Toplevel.empty |> Toplevel.name name |> Toplevel.position pos; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
169 |
in |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
170 |
(case lookup_commands thy name of |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
171 |
SOME (Command {command_parser = Parser parse, ...}) => |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
172 |
Parse.!!! (command_tags |-- parse) >> (fn f => f tr0) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
173 |
| SOME (Command {command_parser = Private_Parser parse, ...}) => |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
174 |
Parse.opt_private :|-- (fn private => |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
175 |
Parse.!!! (command_tags |-- parse private)) >> (fn f => f tr0) |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
176 |
| NONE => |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
177 |
Scan.succeed |
58934
385a4cc7426f
prefer externally provided keywords -- Command.read_thy may degenerate to bootstrap_thy in case of errors;
wenzelm
parents:
58930
diff
changeset
|
178 |
(tr0 |> Toplevel.imperative (fn () => err_command "Undefined command " name [pos]))) |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
179 |
end); |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
180 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
181 |
val parse_cmt = Parse.$$$ "--" -- Parse.!!! Parse.document_source; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
182 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
183 |
in |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
184 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
185 |
fun commands_source thy = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
186 |
Token.source_proper #> |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
187 |
Source.source Token.stopper (Scan.bulk (parse_cmt >> K NONE || Parse.not_eof >> SOME)) #> |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
188 |
Source.map_filter I #> |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
189 |
Source.source Token.stopper (Scan.bulk (fn xs => Parse.!!! (parse_command thy) xs)); |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
190 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
191 |
end; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
192 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
193 |
fun parse thy pos str = |
57918
f5d73caba4e5
tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents:
57905
diff
changeset
|
194 |
Source.of_string str |
f5d73caba4e5
tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents:
57905
diff
changeset
|
195 |
|> Symbol.source |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
196 |
|> Token.source (Thy_Header.get_keywords thy) pos |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
197 |
|> commands_source thy |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
198 |
|> Source.exhaust; |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
199 |
|
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
200 |
fun parse_tokens thy toks = |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
201 |
Source.of_list toks |
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
202 |
|> commands_source thy |
57918
f5d73caba4e5
tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents:
57905
diff
changeset
|
203 |
|> Source.exhaust; |
f5d73caba4e5
tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents:
57905
diff
changeset
|
204 |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
205 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
206 |
(* parse spans *) |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
207 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
208 |
local |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
209 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
210 |
fun ship span = |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
211 |
let |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
212 |
val kind = |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
213 |
if forall Token.is_improper span then Command_Span.Ignored_Span |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
214 |
else if exists Token.is_error span then Command_Span.Malformed_Span |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
215 |
else |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
216 |
(case find_first Token.is_command span of |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
217 |
NONE => Command_Span.Malformed_Span |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
218 |
| SOME cmd => Command_Span.Command_Span (Token.content_of cmd, Token.pos_of cmd)); |
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
219 |
in cons (Command_Span.Span (kind, span)) end; |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
220 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
221 |
fun flush (result, content, improper) = |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
222 |
result |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
223 |
|> not (null content) ? ship (rev content) |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
224 |
|> not (null improper) ? ship (rev improper); |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
225 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
226 |
fun parse tok (result, content, improper) = |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
227 |
if Token.is_improper tok then (result, content, tok :: improper) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
228 |
else if Token.is_private tok orelse |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
229 |
Token.is_command tok andalso |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
230 |
(not (exists Token.is_private content) orelse exists Token.is_command content) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59923
diff
changeset
|
231 |
then (flush (result, content, improper), [tok], []) |
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
232 |
else (result, tok :: (improper @ content), []); |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
233 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
234 |
in |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
235 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
236 |
fun parse_spans toks = |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
237 |
fold parse toks ([], [], []) |> flush |> rev; |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
238 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
239 |
end; |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57623
diff
changeset
|
240 |
|
14091 | 241 |
|
52510 | 242 |
(* side-comments *) |
48749
c197b3c3e7fa
some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents:
48647
diff
changeset
|
243 |
|
52510 | 244 |
fun cmts (t1 :: t2 :: toks) = |
245 |
if Token.keyword_with (fn s => s = "--") t1 then t2 :: cmts toks |
|
246 |
else cmts (t2 :: toks) |
|
247 |
| cmts _ = []; |
|
248 |
||
249 |
val side_comments = filter Token.is_proper #> cmts; |
|
250 |
||
251 |
||
252 |
(* read commands *) |
|
48647
a5144c4c26a2
report commands as formal entities, with def/ref positions;
wenzelm
parents:
48646
diff
changeset
|
253 |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
254 |
fun command_reports thy tok = |
52510 | 255 |
if Token.is_command tok then |
256 |
let val name = Token.content_of tok in |
|
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58918
diff
changeset
|
257 |
(case lookup_commands thy name of |
52510 | 258 |
NONE => [] |
55708 | 259 |
| SOME cmd => [((Token.pos_of tok, command_markup false (name, cmd)), "")]) |
52510 | 260 |
end |
261 |
else []; |
|
48749
c197b3c3e7fa
some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents:
48647
diff
changeset
|
262 |
|
5829 | 263 |
end; |