author | wenzelm |
Sat, 28 Jun 2008 21:21:18 +0200 | |
changeset 27384 | bbb68fea688f |
parent 25999 | f8bcd311d501 |
child 27737 | 302e9c8c489b |
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 |
|
22119 | 30 |
val alt_string: token list -> string * token list |
5826 | 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 |
15703 | 34 |
val $$$ : string -> token list -> string * token list |
16030 | 35 |
val reserved : string -> token list -> string * token list |
15703 | 36 |
val semicolon: token list -> string * token list |
37 |
val underscore: token list -> string * token list |
|
38 |
val maybe: (token list -> 'a * token list) -> token list -> 'a option * token list |
|
17070 | 39 |
val tag_name: token list -> string * token list |
40 |
val tags: token list -> string list * token list |
|
7930 | 41 |
val opt_unit: token list -> unit * token list |
14646 | 42 |
val opt_keyword: string -> token list -> bool * token list |
20983 | 43 |
val begin: token list -> string * token list |
20961 | 44 |
val opt_begin: token list -> bool * token list |
5826 | 45 |
val nat: token list -> int * token list |
46 |
val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
47 |
val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
48 |
val list: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
49 |
val list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
6013 | 50 |
val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list |
51 |
val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
25795 | 52 |
val properties: token list -> Markup.property list * token list |
5826 | 53 |
val name: token list -> bstring * token list |
54 |
val xname: token list -> xstring * token list |
|
55 |
val text: token list -> string * token list |
|
14949 | 56 |
val path: token list -> Path.T * token list |
18898 | 57 |
val parname: token list -> string * token list |
8897 | 58 |
val sort: token list -> string * token list |
22331 | 59 |
val arity: token list -> (string * string list * string) * token list |
25541 | 60 |
val multi_arity: token list -> (string list * string list * string) * token list |
5826 | 61 |
val type_args: token list -> string list * token list |
62 |
val typ: token list -> string * token list |
|
18669 | 63 |
val mixfix: token list -> mixfix * token list |
21609 | 64 |
val mixfix': token list -> mixfix * token list |
18669 | 65 |
val opt_infix: token list -> mixfix * token list |
21609 | 66 |
val opt_infix': token list -> mixfix * token list |
18669 | 67 |
val opt_mixfix: token list -> mixfix * token list |
68 |
val opt_mixfix': token list -> mixfix * token list |
|
21400 | 69 |
val where_: token list -> string * token list |
18669 | 70 |
val const: token list -> (string * string * mixfix) * token list |
22119 | 71 |
val params: token list -> (string * string option) list * token list |
19845 | 72 |
val simple_fixes: token list -> (string * string option) list * token list |
18669 | 73 |
val fixes: token list -> (string * string option * mixfix) list * token list |
19845 | 74 |
val for_fixes: token list -> (string * string option * mixfix) list * token list |
21371 | 75 |
val for_simple_fixes: token list -> (string * string option) list * token list |
5826 | 76 |
val term: token list -> string * token list |
77 |
val prop: token list -> string * token list |
|
19585 | 78 |
val propp: token list -> (string * string list) * token list |
6949 | 79 |
val termp: token list -> (string * string list) * token list |
22119 | 80 |
val keyword_sid: token list -> string * token list |
27384 | 81 |
val generic_args1: (string -> bool) -> token list -> Args.T list * token list |
9131 | 82 |
val arguments: token list -> Args.T list * token list |
22119 | 83 |
val target: token list -> xstring * token list |
84 |
val opt_target: token list -> xstring option * token list |
|
5826 | 85 |
end; |
86 |
||
87 |
structure OuterParse: OUTER_PARSE = |
|
88 |
struct |
|
89 |
||
9131 | 90 |
structure T = OuterLex; |
91 |
type token = T.token; |
|
5826 | 92 |
|
93 |
||
94 |
(** error handling **) |
|
95 |
||
96 |
(* group atomic parsers (no cuts!) *) |
|
97 |
||
98 |
fun fail_with s = Scan.fail_with |
|
99 |
(fn [] => s ^ " expected (past end-of-file!)" |
|
23789
1993b865c5ac
replaced OuterLex.name_of by more sophisticated OuterLex.text_of;
wenzelm
parents:
22331
diff
changeset
|
100 |
| (tok :: _) => |
1993b865c5ac
replaced OuterLex.name_of by more sophisticated OuterLex.text_of;
wenzelm
parents:
22331
diff
changeset
|
101 |
(case T.text_of tok of |
1993b865c5ac
replaced OuterLex.name_of by more sophisticated OuterLex.text_of;
wenzelm
parents:
22331
diff
changeset
|
102 |
(txt, "") => s ^ " expected,\nbut " ^ txt ^ T.pos_of tok ^ " was found" |
1993b865c5ac
replaced OuterLex.name_of by more sophisticated OuterLex.text_of;
wenzelm
parents:
22331
diff
changeset
|
103 |
| (txt1, txt2) => s ^ " expected,\nbut " ^ txt1 ^ T.pos_of tok ^ " was found:\n" ^ txt2)); |
5826 | 104 |
|
105 |
fun group s scan = scan || fail_with s; |
|
106 |
||
107 |
||
5877 | 108 |
(* cut *) |
5826 | 109 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
110 |
fun cut kind scan = |
5826 | 111 |
let |
112 |
fun get_pos [] = " (past end-of-file!)" |
|
9131 | 113 |
| get_pos (tok :: _) = T.pos_of tok; |
5826 | 114 |
|
15531 | 115 |
fun err (toks, NONE) = kind ^ get_pos toks |
25625 | 116 |
| err (toks, SOME msg) = |
117 |
if String.isPrefix kind msg then msg |
|
118 |
else kind ^ get_pos toks ^ ": " ^ msg; |
|
5826 | 119 |
in Scan.!! err scan end; |
120 |
||
8586 | 121 |
fun !!! scan = cut "Outer syntax error" scan; |
122 |
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan; |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
123 |
|
5826 | 124 |
|
125 |
||
126 |
(** basic parsers **) |
|
127 |
||
128 |
(* utils *) |
|
129 |
||
130 |
fun triple1 ((x, y), z) = (x, y, z); |
|
131 |
fun triple2 (x, (y, z)) = (x, y, z); |
|
6430 | 132 |
fun triple_swap ((x, y), z) = ((x, z), y); |
5826 | 133 |
|
134 |
||
135 |
(* tokens *) |
|
136 |
||
15703 | 137 |
val not_eof = Scan.one T.not_eof; |
138 |
||
139 |
fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap; |
|
5826 | 140 |
|
141 |
fun kind k = |
|
9131 | 142 |
group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of); |
5826 | 143 |
|
9131 | 144 |
val command = kind T.Command; |
145 |
val keyword = kind T.Keyword; |
|
146 |
val short_ident = kind T.Ident; |
|
147 |
val long_ident = kind T.LongIdent; |
|
148 |
val sym_ident = kind T.SymIdent; |
|
149 |
val term_var = kind T.Var; |
|
150 |
val type_ident = kind T.TypeIdent; |
|
151 |
val type_var = kind T.TypeVar; |
|
152 |
val number = kind T.Nat; |
|
153 |
val string = kind T.String; |
|
17165 | 154 |
val alt_string = kind T.AltString; |
9131 | 155 |
val verbatim = kind T.Verbatim; |
156 |
val sync = kind T.Sync; |
|
157 |
val eof = kind T.EOF; |
|
5826 | 158 |
|
159 |
fun $$$ x = |
|
9131 | 160 |
group (T.str_of_kind T.Keyword ^ " " ^ quote x) |
161 |
(Scan.one (T.keyword_with (equal x)) >> T.val_of); |
|
162 |
||
16030 | 163 |
fun reserved x = |
164 |
group ("Reserved identifier " ^ quote x) |
|
24910 | 165 |
(Scan.one (T.ident_with (fn y => x = y)) >> T.val_of); |
16030 | 166 |
|
9131 | 167 |
val semicolon = $$$ ";"; |
5826 | 168 |
|
15703 | 169 |
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1; |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
170 |
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1; |
15703 | 171 |
fun maybe scan = underscore >> K NONE || scan >> SOME; |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
172 |
|
14835 | 173 |
val nat = number >> (#1 o Library.read_int o Symbol.explode); |
5826 | 174 |
|
17070 | 175 |
val tag_name = group "tag name" (short_ident || string); |
176 |
val tags = Scan.repeat ($$$ "%" |-- !!! tag_name); |
|
177 |
||
7930 | 178 |
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) (); |
14646 | 179 |
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false; |
180 |
||
20983 | 181 |
val begin = $$$ "begin"; |
182 |
val opt_begin = Scan.optional (begin >> K true) false; |
|
20961 | 183 |
|
5826 | 184 |
|
185 |
(* enumerations *) |
|
186 |
||
25999 | 187 |
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan); |
5826 | 188 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
189 |
||
190 |
fun list1 scan = enum1 "," scan; |
|
191 |
fun list scan = enum "," scan; |
|
192 |
||
6013 | 193 |
fun and_list1 scan = enum1 "and" scan; |
194 |
fun and_list scan = enum "and" scan; |
|
195 |
||
25795 | 196 |
val properties = $$$ "(" |-- !!! (list1 (string -- ($$$ "=" |-- string)) --| $$$ ")"); |
197 |
||
5826 | 198 |
|
5960 | 199 |
(* names and text *) |
5826 | 200 |
|
8146 | 201 |
val name = group "name declaration" (short_ident || sym_ident || string || number); |
202 |
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number); |
|
203 |
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim); |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21609
diff
changeset
|
204 |
val path = group "file name/path specification" name >> Path.explode; |
6553 | 205 |
|
18898 | 206 |
val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") ""; |
207 |
||
6553 | 208 |
|
6372 | 209 |
(* sorts and arities *) |
5826 | 210 |
|
8897 | 211 |
val sort = group "sort" xname; |
5826 | 212 |
|
22331 | 213 |
val arity = xname -- ($$$ "::" |-- !!! |
214 |
(Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2; |
|
5826 | 215 |
|
25541 | 216 |
val multi_arity = and_list1 xname -- ($$$ "::" |-- !!! |
217 |
(Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2; |
|
218 |
||
5826 | 219 |
|
220 |
(* types *) |
|
221 |
||
8146 | 222 |
val typ = group "type" |
223 |
(short_ident || long_ident || sym_ident || type_ident || type_var || string || number); |
|
5826 | 224 |
|
225 |
val type_args = |
|
226 |
type_ident >> single || |
|
227 |
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") || |
|
228 |
Scan.succeed []; |
|
229 |
||
230 |
||
231 |
(* mixfix annotations *) |
|
232 |
||
18669 | 233 |
val mfix = string -- |
234 |
!!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] -- |
|
235 |
Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2); |
|
236 |
||
237 |
val infx = $$$ "infix" |-- !!! (nat >> Infix || string -- nat >> InfixName); |
|
238 |
val infxl = $$$ "infixl" |-- !!! (nat >> Infixl || string -- nat >> InfixlName); |
|
239 |
val infxr = $$$ "infixr" |-- !!! (nat >> Infixr || string -- nat >> InfixrName); |
|
5826 | 240 |
|
18669 | 241 |
val binder = $$$ "binder" |-- |
242 |
!!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n)))) |
|
243 |
>> (Binder o triple2); |
|
244 |
||
245 |
fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")"); |
|
246 |
fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn; |
|
247 |
||
248 |
val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx); |
|
21609 | 249 |
val mixfix' = annotation I (mfix || binder || infxl || infxr || infx); |
18669 | 250 |
val opt_infix = opt_annotation !!! (infxl || infxr || infx); |
21609 | 251 |
val opt_infix' = opt_annotation I (infxl || infxr || infx); |
18669 | 252 |
val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx); |
253 |
val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx); |
|
5826 | 254 |
|
255 |
||
18669 | 256 |
(* fixes *) |
5826 | 257 |
|
21400 | 258 |
val where_ = $$$ "where"; |
259 |
||
18618 | 260 |
val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1; |
18669 | 261 |
|
262 |
val params = Scan.repeat1 name -- Scan.option ($$$ "::" |-- !!! typ) |
|
263 |
>> (fn (xs, T) => map (rpair T) xs); |
|
264 |
||
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19284
diff
changeset
|
265 |
val simple_fixes = and_list1 params >> flat; |
18669 | 266 |
|
267 |
val fixes = |
|
268 |
and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) || |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19284
diff
changeset
|
269 |
params >> map Syntax.no_syn) >> flat; |
5826 | 270 |
|
19845 | 271 |
val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) []; |
21371 | 272 |
val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) []; |
19845 | 273 |
|
5826 | 274 |
|
275 |
(* terms *) |
|
276 |
||
7477 | 277 |
val trm = short_ident || long_ident || sym_ident || term_var || number || string; |
5826 | 278 |
|
279 |
val term = group "term" trm; |
|
280 |
val prop = group "proposition" trm; |
|
281 |
||
282 |
||
6949 | 283 |
(* patterns *) |
6935 | 284 |
|
6949 | 285 |
val is_terms = Scan.repeat1 ($$$ "is" |-- term); |
6935 | 286 |
val is_props = Scan.repeat1 ($$$ "is" |-- prop); |
287 |
||
19585 | 288 |
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) []; |
6949 | 289 |
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) []; |
6935 | 290 |
|
291 |
||
5826 | 292 |
(* arguments *) |
293 |
||
9131 | 294 |
fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of; |
295 |
val keyword_sid = keyword_symid T.is_sid; |
|
5826 | 296 |
|
27384 | 297 |
fun parse_args is_symid = |
298 |
let |
|
299 |
fun atom blk = |
|
300 |
group "argument" |
|
301 |
(position (short_ident || long_ident || sym_ident || term_var || |
|
302 |
type_ident || type_var || number) >> Args.mk_ident || |
|
303 |
position (keyword_symid is_symid) >> Args.mk_keyword || |
|
304 |
position (string || verbatim) >> Args.mk_string || |
|
305 |
position alt_string >> Args.mk_alt_string || |
|
306 |
position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword); |
|
5826 | 307 |
|
27384 | 308 |
fun args blk x = Scan.optional (args1 blk) [] x |
309 |
and args1 blk x = |
|
310 |
((Scan.repeat1 |
|
311 |
(Scan.repeat1 (atom blk) || |
|
312 |
argsp "(" ")" || |
|
313 |
argsp "[" "]")) >> flat) x |
|
314 |
and argsp l r x = |
|
315 |
(position ($$$ l) -- !!! (args true -- position ($$$ r)) |
|
316 |
>> (fn (a, (bs, c)) => Args.mk_keyword a :: bs @ [Args.mk_keyword c])) x; |
|
317 |
in (args, args1) end; |
|
5826 | 318 |
|
27384 | 319 |
fun generic_args1 is_symid = #2 (parse_args is_symid) false; |
320 |
val arguments = #1 (parse_args T.is_sid) false; |
|
9131 | 321 |
|
5826 | 322 |
|
22119 | 323 |
(* targets *) |
19811 | 324 |
|
22119 | 325 |
val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")"); |
326 |
val opt_target = Scan.option target; |
|
12272 | 327 |
|
328 |
end; |