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