5826
|
1 |
(* Title: Pure/Isar/outer_parse.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Generic parsers for Isabelle/Isar outer syntax.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature OUTER_PARSE =
|
|
9 |
sig
|
|
10 |
type token
|
|
11 |
val group: string -> (token list -> 'a) -> token list -> 'a
|
|
12 |
val !!! : (token list -> 'a) -> token list -> 'a
|
|
13 |
val $$$ : string -> token list -> string * token list
|
|
14 |
val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
|
7026
|
15 |
val command: token list -> string * token list
|
5826
|
16 |
val keyword: token list -> string * token list
|
|
17 |
val short_ident: token list -> string * token list
|
|
18 |
val long_ident: token list -> string * token list
|
|
19 |
val sym_ident: token list -> string * token list
|
|
20 |
val term_var: token list -> string * token list
|
|
21 |
val type_ident: token list -> string * token list
|
|
22 |
val type_var: token list -> string * token list
|
|
23 |
val number: token list -> string * token list
|
|
24 |
val string: token list -> string * token list
|
|
25 |
val verbatim: token list -> string * token list
|
6860
|
26 |
val sync: token list -> string * token list
|
5826
|
27 |
val eof: token list -> string * token list
|
|
28 |
val not_eof: token list -> token * token list
|
|
29 |
val nat: token list -> int * token list
|
|
30 |
val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
|
|
31 |
val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
|
|
32 |
val list: (token list -> 'a * token list) -> token list -> 'a list * token list
|
|
33 |
val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
|
6013
|
34 |
val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
|
|
35 |
val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
|
5826
|
36 |
val name: token list -> bstring * token list
|
|
37 |
val xname: token list -> xstring * token list
|
|
38 |
val text: token list -> string * token list
|
6553
|
39 |
val comment: token list -> Comment.text * token list
|
|
40 |
val marg_comment: token list -> Comment.text * token list
|
|
41 |
val interest: token list -> Comment.interest * token list
|
5826
|
42 |
val sort: token list -> xsort * token list
|
|
43 |
val arity: token list -> (xsort list * xsort) * token list
|
6372
|
44 |
val simple_arity: token list -> (xsort list * xclass) * token list
|
5826
|
45 |
val type_args: token list -> string list * token list
|
|
46 |
val typ: token list -> string * token list
|
|
47 |
val opt_infix: token list -> Syntax.mixfix * token list
|
|
48 |
val opt_mixfix: token list -> Syntax.mixfix * token list
|
|
49 |
val const: token list -> (bstring * string * Syntax.mixfix) * token list
|
|
50 |
val term: token list -> string * token list
|
|
51 |
val prop: token list -> string * token list
|
6935
|
52 |
val propp: token list -> (string * (string list * string list)) * token list
|
6949
|
53 |
val termp: token list -> (string * string list) * token list
|
5826
|
54 |
val attribs: token list -> Args.src list * token list
|
|
55 |
val opt_attribs: token list -> Args.src list * token list
|
5917
|
56 |
val thm_name: string -> token list -> (bstring * Args.src list) * token list
|
|
57 |
val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
|
6372
|
58 |
val spec_name: token list -> ((bstring * string) * Args.src list) * token list
|
|
59 |
val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
|
5917
|
60 |
val xthm: token list -> (xstring * Args.src list) * token list
|
|
61 |
val xthms1: token list -> (xstring * Args.src list) list * token list
|
5826
|
62 |
val method: token list -> Method.text * token list
|
|
63 |
val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
|
|
64 |
val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
|
6430
|
65 |
val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
|
5826
|
66 |
end;
|
|
67 |
|
|
68 |
structure OuterParse: OUTER_PARSE =
|
|
69 |
struct
|
|
70 |
|
|
71 |
type token = OuterLex.token;
|
|
72 |
|
|
73 |
|
|
74 |
(** error handling **)
|
|
75 |
|
|
76 |
(* group atomic parsers (no cuts!) *)
|
|
77 |
|
|
78 |
fun fail_with s = Scan.fail_with
|
|
79 |
(fn [] => s ^ " expected (past end-of-file!)"
|
|
80 |
| (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
|
|
81 |
quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
|
|
82 |
|
|
83 |
fun group s scan = scan || fail_with s;
|
|
84 |
|
|
85 |
|
5877
|
86 |
(* cut *)
|
5826
|
87 |
|
|
88 |
fun !!! scan =
|
|
89 |
let
|
|
90 |
fun get_pos [] = " (past end-of-file!)"
|
|
91 |
| get_pos (tok :: _) = OuterLex.pos_of tok;
|
|
92 |
|
|
93 |
fun err (toks, None) = "Outer syntax error" ^ get_pos toks
|
|
94 |
| err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
|
|
95 |
in Scan.!! err scan end;
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
(** basic parsers **)
|
|
100 |
|
|
101 |
(* utils *)
|
|
102 |
|
|
103 |
fun triple1 ((x, y), z) = (x, y, z);
|
|
104 |
fun triple2 (x, (y, z)) = (x, y, z);
|
6430
|
105 |
fun triple_swap ((x, y), z) = ((x, z), y);
|
5826
|
106 |
|
|
107 |
|
|
108 |
(* tokens *)
|
|
109 |
|
|
110 |
fun position scan =
|
|
111 |
(Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
|
|
112 |
|
|
113 |
fun kind k =
|
|
114 |
group (OuterLex.str_of_kind k)
|
|
115 |
(Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
|
|
116 |
|
7026
|
117 |
val command = kind OuterLex.Command;
|
5826
|
118 |
val keyword = kind OuterLex.Keyword;
|
|
119 |
val short_ident = kind OuterLex.Ident;
|
|
120 |
val long_ident = kind OuterLex.LongIdent;
|
|
121 |
val sym_ident = kind OuterLex.SymIdent;
|
|
122 |
val term_var = kind OuterLex.Var;
|
|
123 |
val type_ident = kind OuterLex.TypeIdent;
|
|
124 |
val type_var = kind OuterLex.TypeVar;
|
|
125 |
val number = kind OuterLex.Nat;
|
|
126 |
val string = kind OuterLex.String;
|
|
127 |
val verbatim = kind OuterLex.Verbatim;
|
6860
|
128 |
val sync = kind OuterLex.Sync;
|
5826
|
129 |
val eof = kind OuterLex.EOF;
|
|
130 |
|
|
131 |
fun $$$ x =
|
|
132 |
group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
|
7026
|
133 |
(Scan.one (OuterLex.keyword_with (equal x)) >> OuterLex.val_of);
|
5826
|
134 |
|
5877
|
135 |
val nat = number >> (fst o Term.read_int o Symbol.explode);
|
5826
|
136 |
|
|
137 |
val not_eof = Scan.one OuterLex.not_eof;
|
|
138 |
|
|
139 |
|
|
140 |
(* enumerations *)
|
|
141 |
|
6003
|
142 |
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
|
5826
|
143 |
fun enum sep scan = enum1 sep scan || Scan.succeed [];
|
|
144 |
|
|
145 |
fun list1 scan = enum1 "," scan;
|
|
146 |
fun list scan = enum "," scan;
|
|
147 |
|
6013
|
148 |
fun and_list1 scan = enum1 "and" scan;
|
|
149 |
fun and_list scan = enum "and" scan;
|
|
150 |
|
5826
|
151 |
|
5960
|
152 |
(* names and text *)
|
5826
|
153 |
|
5877
|
154 |
val name = group "name declaration" (short_ident || sym_ident || string);
|
|
155 |
val xname = group "name reference" (short_ident || long_ident || sym_ident || string);
|
6553
|
156 |
val text = group "text" (short_ident || long_ident || sym_ident || string || verbatim);
|
|
157 |
|
|
158 |
|
|
159 |
(* formal comments *)
|
|
160 |
|
|
161 |
val comment = text >> Comment.plain;
|
6726
|
162 |
val marg_comment = Scan.optional ($$$ "--" |-- comment) Comment.none;
|
7171
|
163 |
|
|
164 |
val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest ||
|
|
165 |
$$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest;
|
5826
|
166 |
|
|
167 |
|
6372
|
168 |
(* sorts and arities *)
|
5826
|
169 |
|
|
170 |
val sort =
|
|
171 |
xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
|
|
172 |
|
6372
|
173 |
fun gen_arity cod =
|
7352
|
174 |
Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
|
6372
|
175 |
|
|
176 |
val arity = gen_arity sort;
|
|
177 |
val simple_arity = gen_arity name;
|
5826
|
178 |
|
|
179 |
|
|
180 |
(* types *)
|
|
181 |
|
|
182 |
val typ =
|
6553
|
183 |
group "type" (short_ident || long_ident || sym_ident || type_ident || type_var || string);
|
5826
|
184 |
|
|
185 |
val type_args =
|
|
186 |
type_ident >> single ||
|
|
187 |
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
|
|
188 |
Scan.succeed [];
|
|
189 |
|
|
190 |
|
|
191 |
(* mixfix annotations *)
|
|
192 |
|
|
193 |
val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
|
|
194 |
val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
|
|
195 |
|
|
196 |
val binder =
|
|
197 |
$$$ "binder" |--
|
|
198 |
!!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
|
|
199 |
>> (Syntax.Binder o triple2);
|
|
200 |
|
|
201 |
|
|
202 |
val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
|
|
203 |
|
|
204 |
val mixfix =
|
|
205 |
string -- opt_pris -- Scan.optional nat Syntax.max_pri
|
|
206 |
>> (Syntax.Mixfix o triple1);
|
|
207 |
|
|
208 |
fun opt_fix fix =
|
|
209 |
Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
|
|
210 |
|
|
211 |
val opt_infix = opt_fix (infxl || infxr);
|
6355
|
212 |
val opt_mixfix = opt_fix (mixfix || binder || infxl || infxr);
|
5826
|
213 |
|
|
214 |
|
|
215 |
(* consts *)
|
|
216 |
|
|
217 |
val const =
|
|
218 |
name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
|
|
219 |
|
|
220 |
|
|
221 |
(* terms *)
|
|
222 |
|
7477
|
223 |
val trm = short_ident || long_ident || sym_ident || term_var || number || string;
|
5826
|
224 |
|
|
225 |
val term = group "term" trm;
|
|
226 |
val prop = group "proposition" trm;
|
|
227 |
|
|
228 |
|
6949
|
229 |
(* patterns *)
|
6935
|
230 |
|
6949
|
231 |
val is_terms = Scan.repeat1 ($$$ "is" |-- term);
|
6935
|
232 |
val is_props = Scan.repeat1 ($$$ "is" |-- prop);
|
|
233 |
val concl_props = $$$ "concl" |-- !!! is_props;
|
7418
|
234 |
val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
|
6935
|
235 |
|
|
236 |
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
|
6949
|
237 |
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
|
6935
|
238 |
|
|
239 |
|
5826
|
240 |
(* arguments *)
|
|
241 |
|
7026
|
242 |
fun keyword_symid is_symid = Scan.one (OuterLex.keyword_with is_symid) >> OuterLex.val_of;
|
5826
|
243 |
|
6983
|
244 |
fun atom_arg is_symid blk =
|
5826
|
245 |
group "argument"
|
7477
|
246 |
(position (short_ident || long_ident || sym_ident || term_var ||
|
5877
|
247 |
type_ident || type_var || number) >> Args.ident ||
|
6983
|
248 |
position (keyword_symid is_symid) >> Args.keyword ||
|
5877
|
249 |
position string >> Args.string ||
|
|
250 |
position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
|
5826
|
251 |
|
5877
|
252 |
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
|
5826
|
253 |
>> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
|
|
254 |
|
6983
|
255 |
fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
|
|
256 |
and args1 is_symid blk x =
|
5826
|
257 |
((Scan.repeat1
|
6983
|
258 |
(Scan.repeat1 (atom_arg is_symid blk) ||
|
|
259 |
paren_args "(" ")" (args is_symid) ||
|
|
260 |
paren_args "{" "}" (args is_symid) ||
|
|
261 |
paren_args "[" "]" (args is_symid))) >> flat) x;
|
5826
|
262 |
|
|
263 |
|
6372
|
264 |
(* theorem specifications *)
|
5826
|
265 |
|
6983
|
266 |
val attrib = position (xname -- !!! (args OuterLex.is_sid false)) >> Args.src;
|
5826
|
267 |
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
|
|
268 |
val opt_attribs = Scan.optional attribs [];
|
|
269 |
|
6398
|
270 |
fun thm_name s = name -- opt_attribs --| $$$ s;
|
6511
|
271 |
fun opt_thm_name s =
|
|
272 |
Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
|
5917
|
273 |
|
6372
|
274 |
val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
|
|
275 |
val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
|
|
276 |
|
5917
|
277 |
val xthm = xname -- opt_attribs;
|
6372
|
278 |
val xthms1 = Scan.repeat1 xthm;
|
5826
|
279 |
|
|
280 |
|
|
281 |
(* proof methods *)
|
|
282 |
|
6983
|
283 |
fun is_symid_meth s =
|
7418
|
284 |
s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.is_sid s;
|
6983
|
285 |
|
5826
|
286 |
fun meth4 x =
|
5877
|
287 |
(position (xname >> rpair []) >> (Method.Source o Args.src) ||
|
6983
|
288 |
$$$ "(" |-- !!! (meth0 --| $$$ ")")) x
|
5826
|
289 |
and meth3 x =
|
|
290 |
(meth4 --| $$$ "?" >> Method.Try ||
|
|
291 |
meth4 --| $$$ "+" >> Method.Repeat1 ||
|
5940
|
292 |
meth4) x
|
|
293 |
and meth2 x =
|
6983
|
294 |
(position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
|
5826
|
295 |
meth3) x
|
|
296 |
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
|
|
297 |
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
|
|
298 |
|
6558
|
299 |
val method = meth3;
|
5826
|
300 |
|
|
301 |
|
|
302 |
end;
|