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