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