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