author | wenzelm |
Sun, 22 May 2005 16:51:18 +0200 | |
changeset 16030 | bbfb2f1378b3 |
parent 15979 | c81578ac2d31 |
child 16102 | c5f6726d9bb1 |
permissions | -rw-r--r-- |
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 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
13 |
val !!!! : (token list -> 'a) -> token list -> 'a |
12047 | 14 |
val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c |
15 |
val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c |
|
16 |
val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b |
|
15703 | 17 |
val not_eof: token list -> token * token list |
5826 | 18 |
val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b |
7026 | 19 |
val command: token list -> string * token list |
5826 | 20 |
val keyword: token list -> string * token list |
21 |
val short_ident: token list -> string * token list |
|
22 |
val long_ident: token list -> string * token list |
|
23 |
val sym_ident: token list -> string * token list |
|
15963 | 24 |
val minus: token list -> string * token list |
5826 | 25 |
val term_var: token list -> string * token list |
26 |
val type_ident: token list -> string * token list |
|
27 |
val type_var: token list -> string * token list |
|
28 |
val number: token list -> string * token list |
|
29 |
val string: token list -> string * token list |
|
30 |
val verbatim: token list -> string * token list |
|
6860 | 31 |
val sync: token list -> string * token list |
5826 | 32 |
val eof: token list -> string * token list |
15703 | 33 |
val $$$ : string -> token list -> string * token list |
16030 | 34 |
val reserved : string -> token list -> string * token list |
15703 | 35 |
val semicolon: token list -> string * token list |
36 |
val underscore: token list -> string * token list |
|
37 |
val maybe: (token list -> 'a * token list) -> token list -> 'a option * token list |
|
7930 | 38 |
val opt_unit: token list -> unit * token list |
14646 | 39 |
val opt_keyword: string -> token list -> bool * token list |
5826 | 40 |
val nat: token list -> int * token list |
41 |
val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
42 |
val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
43 |
val list: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
44 |
val list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
6013 | 45 |
val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list |
46 |
val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
5826 | 47 |
val name: token list -> bstring * token list |
48 |
val xname: token list -> xstring * token list |
|
49 |
val text: token list -> string * token list |
|
14949 | 50 |
val path: token list -> Path.T * token list |
8897 | 51 |
val sort: token list -> string * token list |
52 |
val arity: token list -> (string list * string) * 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 |
|
14646 | 59 |
val mixfix': token list -> Syntax.mixfix * token list |
5826 | 60 |
val const: token list -> (bstring * string * Syntax.mixfix) * token list |
61 |
val term: token list -> string * token list |
|
62 |
val prop: token list -> string * token list |
|
6935 | 63 |
val propp: token list -> (string * (string list * string list)) * token list |
6949 | 64 |
val termp: token list -> (string * string list) * token list |
9131 | 65 |
val arguments: token list -> Args.T list * token list |
15703 | 66 |
val attribs: token list -> Attrib.src list * token list |
67 |
val opt_attribs: token list -> Attrib.src list * token list |
|
68 |
val thm_name: string -> token list -> (bstring * Attrib.src list) * token list |
|
69 |
val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list |
|
70 |
val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list |
|
71 |
val spec_opt_name: token list -> ((bstring * string) * Attrib.src list) * token list |
|
72 |
val xthm: token list -> (thmref * Attrib.src list) * token list |
|
73 |
val xthms1: token list -> (thmref * Attrib.src list) list * token list |
|
12942 | 74 |
val locale_target: token list -> xstring option * token list |
12272 | 75 |
val locale_expr: token list -> Locale.expr * token list |
12942 | 76 |
val locale_keyword: token list -> string * token list |
15703 | 77 |
val locale_element: token list -> Locale.element * token list |
78 |
val locale_elem_or_expr: token list -> Locale.element Locale.elem_expr * token list |
|
5826 | 79 |
val method: token list -> Method.text * token list |
80 |
end; |
|
81 |
||
82 |
structure OuterParse: OUTER_PARSE = |
|
83 |
struct |
|
84 |
||
9131 | 85 |
structure T = OuterLex; |
86 |
type token = T.token; |
|
5826 | 87 |
|
88 |
||
89 |
(** error handling **) |
|
90 |
||
91 |
(* group atomic parsers (no cuts!) *) |
|
92 |
||
93 |
fun fail_with s = Scan.fail_with |
|
94 |
(fn [] => s ^ " expected (past end-of-file!)" |
|
9155 | 95 |
| (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found"); |
5826 | 96 |
|
97 |
fun group s scan = scan || fail_with s; |
|
98 |
||
99 |
||
5877 | 100 |
(* cut *) |
5826 | 101 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
102 |
fun cut kind scan = |
5826 | 103 |
let |
104 |
fun get_pos [] = " (past end-of-file!)" |
|
9131 | 105 |
| get_pos (tok :: _) = T.pos_of tok; |
5826 | 106 |
|
15531 | 107 |
fun err (toks, NONE) = kind ^ get_pos toks |
108 |
| err (toks, SOME msg) = kind ^ get_pos toks ^ ": " ^ msg; |
|
5826 | 109 |
in Scan.!! err scan end; |
110 |
||
8586 | 111 |
fun !!! scan = cut "Outer syntax error" scan; |
112 |
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan; |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
113 |
|
5826 | 114 |
|
115 |
||
116 |
(** basic parsers **) |
|
117 |
||
118 |
(* utils *) |
|
119 |
||
120 |
fun triple1 ((x, y), z) = (x, y, z); |
|
121 |
fun triple2 (x, (y, z)) = (x, y, z); |
|
6430 | 122 |
fun triple_swap ((x, y), z) = ((x, z), y); |
5826 | 123 |
|
124 |
||
125 |
(* tokens *) |
|
126 |
||
15703 | 127 |
val not_eof = Scan.one T.not_eof; |
128 |
||
129 |
fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap; |
|
5826 | 130 |
|
131 |
fun kind k = |
|
9131 | 132 |
group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of); |
5826 | 133 |
|
9131 | 134 |
val command = kind T.Command; |
135 |
val keyword = kind T.Keyword; |
|
136 |
val short_ident = kind T.Ident; |
|
137 |
val long_ident = kind T.LongIdent; |
|
138 |
val sym_ident = kind T.SymIdent; |
|
139 |
val term_var = kind T.Var; |
|
140 |
val type_ident = kind T.TypeIdent; |
|
141 |
val type_var = kind T.TypeVar; |
|
142 |
val number = kind T.Nat; |
|
143 |
val string = kind T.String; |
|
144 |
val verbatim = kind T.Verbatim; |
|
145 |
val sync = kind T.Sync; |
|
146 |
val eof = kind T.EOF; |
|
5826 | 147 |
|
148 |
fun $$$ x = |
|
9131 | 149 |
group (T.str_of_kind T.Keyword ^ " " ^ quote x) |
150 |
(Scan.one (T.keyword_with (equal x)) >> T.val_of); |
|
151 |
||
16030 | 152 |
fun reserved x = |
153 |
group ("Reserved identifier " ^ quote x) |
|
154 |
(Scan.one (T.ident_with (equal x)) >> T.val_of); |
|
155 |
||
9131 | 156 |
val semicolon = $$$ ";"; |
5826 | 157 |
|
15703 | 158 |
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1; |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
159 |
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1; |
15703 | 160 |
fun maybe scan = underscore >> K NONE || scan >> SOME; |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
161 |
|
14835 | 162 |
val nat = number >> (#1 o Library.read_int o Symbol.explode); |
5826 | 163 |
|
7930 | 164 |
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) (); |
165 |
||
14646 | 166 |
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false; |
167 |
||
5826 | 168 |
|
169 |
(* enumerations *) |
|
170 |
||
6003 | 171 |
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::; |
5826 | 172 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
173 |
||
174 |
fun list1 scan = enum1 "," scan; |
|
175 |
fun list scan = enum "," scan; |
|
176 |
||
6013 | 177 |
fun and_list1 scan = enum1 "and" scan; |
178 |
fun and_list scan = enum "and" scan; |
|
179 |
||
5826 | 180 |
|
5960 | 181 |
(* names and text *) |
5826 | 182 |
|
8146 | 183 |
val name = group "name declaration" (short_ident || sym_ident || string || number); |
184 |
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number); |
|
185 |
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim); |
|
14949 | 186 |
val path = group "file name/path specification" name >> Path.unpack; |
6553 | 187 |
|
188 |
||
6372 | 189 |
(* sorts and arities *) |
5826 | 190 |
|
8897 | 191 |
val sort = group "sort" xname; |
5826 | 192 |
|
6372 | 193 |
fun gen_arity cod = |
7352 | 194 |
Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod; |
6372 | 195 |
|
196 |
val arity = gen_arity sort; |
|
5826 | 197 |
|
198 |
||
199 |
(* types *) |
|
200 |
||
8146 | 201 |
val typ = group "type" |
202 |
(short_ident || long_ident || sym_ident || type_ident || type_var || string || number); |
|
5826 | 203 |
|
204 |
val type_args = |
|
205 |
type_ident >> single || |
|
206 |
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") || |
|
207 |
Scan.succeed []; |
|
208 |
||
209 |
||
210 |
(* mixfix annotations *) |
|
211 |
||
11651 | 212 |
val infx = $$$ "infix" |-- !!! (nat >> Syntax.Infix || string -- nat >> Syntax.InfixName); |
5826 | 213 |
val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName); |
214 |
val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName); |
|
215 |
||
216 |
val binder = |
|
217 |
$$$ "binder" |-- |
|
218 |
!!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n)))) |
|
219 |
>> (Syntax.Binder o triple2); |
|
220 |
||
221 |
||
222 |
val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) []; |
|
223 |
||
224 |
val mixfix = |
|
8648 | 225 |
(string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri)) |
226 |
>> (Syntax.Mixfix o triple2); |
|
5826 | 227 |
|
14646 | 228 |
fun fix_decl guard fix = $$$ "(" |-- guard (fix --| $$$ ")"); |
229 |
fun opt_fix guard fix = Scan.optional (fix_decl guard fix) Syntax.NoSyn; |
|
5826 | 230 |
|
11651 | 231 |
val opt_infix = opt_fix !!! (infxl || infxr || infx); |
232 |
val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr || infx); |
|
233 |
val opt_infix' = opt_fix I (infxl || infxr || infx); |
|
234 |
val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr || infx); |
|
14646 | 235 |
val mixfix' = fix_decl !!! (mixfix || binder || infxl || infxr || infx); |
5826 | 236 |
|
237 |
||
238 |
(* consts *) |
|
239 |
||
240 |
val const = |
|
241 |
name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2; |
|
242 |
||
243 |
||
244 |
(* terms *) |
|
245 |
||
7477 | 246 |
val trm = short_ident || long_ident || sym_ident || term_var || number || string; |
5826 | 247 |
|
248 |
val term = group "term" trm; |
|
249 |
val prop = group "proposition" trm; |
|
250 |
||
251 |
||
6949 | 252 |
(* patterns *) |
6935 | 253 |
|
6949 | 254 |
val is_terms = Scan.repeat1 ($$$ "is" |-- term); |
6935 | 255 |
val is_props = Scan.repeat1 ($$$ "is" |-- prop); |
256 |
val concl_props = $$$ "concl" |-- !!! is_props; |
|
7418 | 257 |
val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props []; |
6935 | 258 |
|
259 |
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []); |
|
12047 | 260 |
val propp' = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) []; |
6949 | 261 |
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) []; |
6935 | 262 |
|
263 |
||
5826 | 264 |
(* arguments *) |
265 |
||
9131 | 266 |
fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of; |
267 |
val keyword_sid = keyword_symid T.is_sid; |
|
5826 | 268 |
|
6983 | 269 |
fun atom_arg is_symid blk = |
5826 | 270 |
group "argument" |
7477 | 271 |
(position (short_ident || long_ident || sym_ident || term_var || |
15703 | 272 |
type_ident || type_var || number) >> Args.mk_ident || |
273 |
position (keyword_symid is_symid) >> Args.mk_keyword || |
|
274 |
position (string || verbatim) >> Args.mk_string || |
|
275 |
position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword); |
|
5826 | 276 |
|
5877 | 277 |
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r)) |
15703 | 278 |
>> (fn (x, (ys, z)) => Args.mk_keyword x :: ys @ [Args.mk_keyword z]); |
5826 | 279 |
|
6983 | 280 |
fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x |
281 |
and args1 is_symid blk x = |
|
5826 | 282 |
((Scan.repeat1 |
6983 | 283 |
(Scan.repeat1 (atom_arg is_symid blk) || |
284 |
paren_args "(" ")" (args is_symid) || |
|
15570 | 285 |
paren_args "[" "]" (args is_symid))) >> List.concat) x; |
5826 | 286 |
|
9131 | 287 |
val arguments = args T.is_sid false; |
288 |
||
5826 | 289 |
|
6372 | 290 |
(* theorem specifications *) |
5826 | 291 |
|
9131 | 292 |
val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src; |
5826 | 293 |
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]"); |
294 |
val opt_attribs = Scan.optional attribs []; |
|
295 |
||
6398 | 296 |
fun thm_name s = name -- opt_attribs --| $$$ s; |
6511 | 297 |
fun opt_thm_name s = |
298 |
Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);; |
|
5917 | 299 |
|
6372 | 300 |
val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y)); |
301 |
val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y)); |
|
302 |
||
15456
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15206
diff
changeset
|
303 |
val thm_sel = $$$ "(" |-- list1 |
15703 | 304 |
(nat --| minus -- nat >> PureThy.FromTo || |
305 |
nat --| minus >> PureThy.From || |
|
306 |
nat >> PureThy.Single) --| $$$ ")"; |
|
15456
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15206
diff
changeset
|
307 |
|
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
berghofe
parents:
15206
diff
changeset
|
308 |
val xthm = xname -- Scan.option thm_sel -- opt_attribs; |
6372 | 309 |
val xthms1 = Scan.repeat1 xthm; |
5826 | 310 |
|
311 |
||
12047 | 312 |
(* locale elements *) |
313 |
||
12272 | 314 |
local |
315 |
||
15531 | 316 |
val loc_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K NONE || opt_mixfix >> SOME; |
12955 | 317 |
val loc_keyword = $$$ "fixes" || $$$ "assumes" || $$$ "defines" || $$$ "notes" || $$$ "includes"; |
12272 | 318 |
|
15127 | 319 |
val loc_element = |
320 |
$$$ "fixes" |-- !!! (and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- loc_mixfix |
|
321 |
>> triple1)) >> Locale.Fixes || |
|
322 |
$$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp)) |
|
323 |
>> Locale.Assumes || |
|
324 |
$$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp')) |
|
325 |
>> Locale.Defines || |
|
326 |
$$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1)) |
|
327 |
>> Locale.Notes; |
|
328 |
||
12272 | 329 |
fun plus1 scan = |
330 |
scan -- Scan.repeat ($$$ "+" |-- Scan.unless loc_keyword (!!! scan)) >> op ::; |
|
331 |
||
12268 | 332 |
fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x |
15703 | 333 |
and expr1 x = (expr2 -- Scan.repeat1 (maybe name) >> Locale.Rename || expr2) x |
12272 | 334 |
and expr0 x = (plus1 expr1 >> (fn [e] => e | es => Locale.Merge es)) x; |
12268 | 335 |
|
12272 | 336 |
in |
337 |
||
12942 | 338 |
val locale_target = Scan.option (($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")")); |
12272 | 339 |
val locale_expr = expr0; |
12942 | 340 |
val locale_keyword = loc_keyword; |
12047 | 341 |
|
15127 | 342 |
val locale_element = group "locale element" loc_element; |
343 |
||
344 |
val locale_elem_or_expr = group "locale element or includes" |
|
345 |
(loc_element >> Locale.Elem || |
|
12955 | 346 |
$$$ "includes" |-- !!! locale_expr >> Locale.Expr); |
12272 | 347 |
|
348 |
end; |
|
12047 | 349 |
|
350 |
||
5826 | 351 |
(* proof methods *) |
352 |
||
6983 | 353 |
fun is_symid_meth s = |
9131 | 354 |
s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s; |
6983 | 355 |
|
5826 | 356 |
fun meth4 x = |
5877 | 357 |
(position (xname >> rpair []) >> (Method.Source o Args.src) || |
6983 | 358 |
$$$ "(" |-- !!! (meth0 --| $$$ ")")) x |
5826 | 359 |
and meth3 x = |
360 |
(meth4 --| $$$ "?" >> Method.Try || |
|
361 |
meth4 --| $$$ "+" >> Method.Repeat1 || |
|
5940 | 362 |
meth4) x |
363 |
and meth2 x = |
|
6983 | 364 |
(position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) || |
5826 | 365 |
meth3) x |
366 |
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x |
|
367 |
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x; |
|
368 |
||
6558 | 369 |
val method = meth3; |
5826 | 370 |
|
371 |
||
372 |
end; |