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