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