author | wenzelm |
Sun, 25 Jun 2000 23:52:59 +0200 | |
changeset 9131 | cd17637c917f |
parent 9037 | 91cbae314c84 |
child 9155 | adfa40218e06 |
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 |
5826 | 15 |
val $$$ : string -> token list -> string * token list |
9131 | 16 |
val semicolon: token list -> string * token list |
5826 | 17 |
val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b |
7026 | 18 |
val command: token list -> string * token list |
5826 | 19 |
val keyword: token list -> string * token list |
20 |
val short_ident: token list -> string * token list |
|
21 |
val long_ident: token list -> string * token list |
|
22 |
val sym_ident: token list -> string * token list |
|
23 |
val term_var: token list -> string * token list |
|
24 |
val type_ident: token list -> string * token list |
|
25 |
val type_var: token list -> string * token list |
|
26 |
val number: token list -> string * token list |
|
27 |
val string: token list -> string * token list |
|
28 |
val verbatim: token list -> string * token list |
|
6860 | 29 |
val sync: token list -> string * token list |
5826 | 30 |
val eof: token list -> string * token list |
31 |
val not_eof: token list -> token * token list |
|
7930 | 32 |
val opt_unit: token list -> unit * token list |
5826 | 33 |
val nat: token list -> int * token list |
34 |
val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
35 |
val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
36 |
val list: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
37 |
val list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
6013 | 38 |
val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list |
39 |
val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
5826 | 40 |
val name: token list -> bstring * token list |
41 |
val xname: token list -> xstring * token list |
|
42 |
val text: token list -> string * token list |
|
6553 | 43 |
val comment: token list -> Comment.text * token list |
44 |
val marg_comment: token list -> Comment.text * token list |
|
45 |
val interest: token list -> Comment.interest * token list |
|
8897 | 46 |
val sort: token list -> string * token list |
47 |
val arity: token list -> (string list * string) * token list |
|
48 |
val simple_arity: token list -> (string list * xclass) * token list |
|
5826 | 49 |
val type_args: token list -> string list * token list |
50 |
val typ: token list -> string * token list |
|
51 |
val opt_infix: token list -> Syntax.mixfix * token list |
|
52 |
val opt_mixfix: token list -> Syntax.mixfix * token list |
|
9037 | 53 |
val opt_infix': token list -> Syntax.mixfix * token list |
54 |
val opt_mixfix': token list -> Syntax.mixfix * token list |
|
5826 | 55 |
val const: token list -> (bstring * string * Syntax.mixfix) * token list |
56 |
val term: token list -> string * token list |
|
57 |
val prop: token list -> string * token list |
|
6935 | 58 |
val propp: token list -> (string * (string list * string list)) * token list |
6949 | 59 |
val termp: token list -> (string * string list) * token list |
9131 | 60 |
val arguments: token list -> Args.T list * token list |
5826 | 61 |
val attribs: token list -> Args.src list * token list |
62 |
val opt_attribs: token list -> Args.src list * token list |
|
5917 | 63 |
val thm_name: string -> token list -> (bstring * Args.src list) * token list |
64 |
val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list |
|
6372 | 65 |
val spec_name: token list -> ((bstring * string) * Args.src list) * token list |
66 |
val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list |
|
5917 | 67 |
val xthm: token list -> (xstring * Args.src list) * token list |
68 |
val xthms1: token list -> (xstring * Args.src list) list * token list |
|
5826 | 69 |
val method: token list -> Method.text * token list |
70 |
val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c |
|
71 |
val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c |
|
6430 | 72 |
val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b |
5826 | 73 |
end; |
74 |
||
75 |
structure OuterParse: OUTER_PARSE = |
|
76 |
struct |
|
77 |
||
9131 | 78 |
structure T = OuterLex; |
79 |
type token = T.token; |
|
5826 | 80 |
|
81 |
||
82 |
(** error handling **) |
|
83 |
||
84 |
(* group atomic parsers (no cuts!) *) |
|
85 |
||
86 |
fun fail_with s = Scan.fail_with |
|
87 |
(fn [] => s ^ " expected (past end-of-file!)" |
|
9131 | 88 |
| (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ " " ^ |
89 |
quote (T.val_of tok) ^ T.pos_of tok ^ " was found"); |
|
5826 | 90 |
|
91 |
fun group s scan = scan || fail_with s; |
|
92 |
||
93 |
||
5877 | 94 |
(* cut *) |
5826 | 95 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
96 |
fun cut kind scan = |
5826 | 97 |
let |
98 |
fun get_pos [] = " (past end-of-file!)" |
|
9131 | 99 |
| get_pos (tok :: _) = T.pos_of tok; |
5826 | 100 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
101 |
fun err (toks, None) = kind ^ get_pos toks |
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
102 |
| err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg; |
5826 | 103 |
in Scan.!! err scan end; |
104 |
||
8586 | 105 |
fun !!! scan = cut "Outer syntax error" scan; |
106 |
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan; |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
107 |
|
5826 | 108 |
|
109 |
||
110 |
(** basic parsers **) |
|
111 |
||
112 |
(* utils *) |
|
113 |
||
114 |
fun triple1 ((x, y), z) = (x, y, z); |
|
115 |
fun triple2 (x, (y, z)) = (x, y, z); |
|
6430 | 116 |
fun triple_swap ((x, y), z) = ((x, z), y); |
5826 | 117 |
|
118 |
||
119 |
(* tokens *) |
|
120 |
||
121 |
fun position scan = |
|
9131 | 122 |
(Scan.ahead (Scan.one T.not_eof) >> T.position_of) -- scan >> Library.swap; |
5826 | 123 |
|
124 |
fun kind k = |
|
9131 | 125 |
group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of); |
5826 | 126 |
|
9131 | 127 |
val command = kind T.Command; |
128 |
val keyword = kind T.Keyword; |
|
129 |
val short_ident = kind T.Ident; |
|
130 |
val long_ident = kind T.LongIdent; |
|
131 |
val sym_ident = kind T.SymIdent; |
|
132 |
val term_var = kind T.Var; |
|
133 |
val type_ident = kind T.TypeIdent; |
|
134 |
val type_var = kind T.TypeVar; |
|
135 |
val number = kind T.Nat; |
|
136 |
val string = kind T.String; |
|
137 |
val verbatim = kind T.Verbatim; |
|
138 |
val sync = kind T.Sync; |
|
139 |
val eof = kind T.EOF; |
|
5826 | 140 |
|
141 |
fun $$$ x = |
|
9131 | 142 |
group (T.str_of_kind T.Keyword ^ " " ^ quote x) |
143 |
(Scan.one (T.keyword_with (equal x)) >> T.val_of); |
|
144 |
||
145 |
val semicolon = $$$ ";"; |
|
5826 | 146 |
|
5877 | 147 |
val nat = number >> (fst o Term.read_int o Symbol.explode); |
5826 | 148 |
|
9131 | 149 |
val not_eof = Scan.one T.not_eof; |
5826 | 150 |
|
7930 | 151 |
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) (); |
152 |
||
5826 | 153 |
|
154 |
(* enumerations *) |
|
155 |
||
6003 | 156 |
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::; |
5826 | 157 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
158 |
||
159 |
fun list1 scan = enum1 "," scan; |
|
160 |
fun list scan = enum "," scan; |
|
161 |
||
6013 | 162 |
fun and_list1 scan = enum1 "and" scan; |
163 |
fun and_list scan = enum "and" scan; |
|
164 |
||
5826 | 165 |
|
5960 | 166 |
(* names and text *) |
5826 | 167 |
|
8146 | 168 |
val name = group "name declaration" (short_ident || sym_ident || string || number); |
169 |
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number); |
|
170 |
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim); |
|
6553 | 171 |
|
172 |
||
173 |
(* formal comments *) |
|
174 |
||
8077 | 175 |
val comment = text >> (Comment.plain o Library.single); |
176 |
val marg_comment = Scan.repeat ($$$ "--" |-- text) >> Comment.plain; |
|
7171 | 177 |
|
178 |
val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest || |
|
179 |
$$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest; |
|
5826 | 180 |
|
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; |
|
8897 | 190 |
val simple_arity = gen_arity xname; |
5826 | 191 |
|
192 |
||
193 |
(* types *) |
|
194 |
||
8146 | 195 |
val typ = group "type" |
196 |
(short_ident || long_ident || sym_ident || type_ident || type_var || string || number); |
|
5826 | 197 |
|
198 |
val type_args = |
|
199 |
type_ident >> single || |
|
200 |
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") || |
|
201 |
Scan.succeed []; |
|
202 |
||
203 |
||
204 |
(* mixfix annotations *) |
|
205 |
||
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 |
|
9037 | 221 |
fun opt_fix guard fix = |
222 |
Scan.optional ($$$ "(" |-- guard (fix --| $$$ ")")) Syntax.NoSyn; |
|
5826 | 223 |
|
9037 | 224 |
val opt_infix = opt_fix !!! (infxl || infxr); |
225 |
val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr); |
|
226 |
||
227 |
val opt_infix' = opt_fix I (infxl || infxr); |
|
228 |
val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr); |
|
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 --| $$$ ")")) ([], []); |
|
6949 | 253 |
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) []; |
6935 | 254 |
|
255 |
||
5826 | 256 |
(* arguments *) |
257 |
||
9131 | 258 |
fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of; |
259 |
val keyword_sid = keyword_symid T.is_sid; |
|
5826 | 260 |
|
6983 | 261 |
fun atom_arg is_symid blk = |
5826 | 262 |
group "argument" |
7477 | 263 |
(position (short_ident || long_ident || sym_ident || term_var || |
5877 | 264 |
type_ident || type_var || number) >> Args.ident || |
6983 | 265 |
position (keyword_symid is_symid) >> Args.keyword || |
8350 | 266 |
position (string || verbatim) >> Args.string || |
5877 | 267 |
position (if blk then $$$ "," else Scan.fail) >> Args.keyword); |
5826 | 268 |
|
5877 | 269 |
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r)) |
5826 | 270 |
>> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]); |
271 |
||
6983 | 272 |
fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x |
273 |
and args1 is_symid blk x = |
|
5826 | 274 |
((Scan.repeat1 |
6983 | 275 |
(Scan.repeat1 (atom_arg is_symid blk) || |
276 |
paren_args "(" ")" (args is_symid) || |
|
277 |
paren_args "[" "]" (args is_symid))) >> flat) x; |
|
5826 | 278 |
|
9131 | 279 |
val arguments = args T.is_sid false; |
280 |
||
5826 | 281 |
|
6372 | 282 |
(* theorem specifications *) |
5826 | 283 |
|
9131 | 284 |
val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src; |
5826 | 285 |
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]"); |
286 |
val opt_attribs = Scan.optional attribs []; |
|
287 |
||
6398 | 288 |
fun thm_name s = name -- opt_attribs --| $$$ s; |
6511 | 289 |
fun opt_thm_name s = |
290 |
Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);; |
|
5917 | 291 |
|
6372 | 292 |
val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y)); |
293 |
val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y)); |
|
294 |
||
5917 | 295 |
val xthm = xname -- opt_attribs; |
6372 | 296 |
val xthms1 = Scan.repeat1 xthm; |
5826 | 297 |
|
298 |
||
299 |
(* proof methods *) |
|
300 |
||
6983 | 301 |
fun is_symid_meth s = |
9131 | 302 |
s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s; |
6983 | 303 |
|
5826 | 304 |
fun meth4 x = |
5877 | 305 |
(position (xname >> rpair []) >> (Method.Source o Args.src) || |
6983 | 306 |
$$$ "(" |-- !!! (meth0 --| $$$ ")")) x |
5826 | 307 |
and meth3 x = |
308 |
(meth4 --| $$$ "?" >> Method.Try || |
|
309 |
meth4 --| $$$ "+" >> Method.Repeat1 || |
|
5940 | 310 |
meth4) x |
311 |
and meth2 x = |
|
6983 | 312 |
(position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) || |
5826 | 313 |
meth3) x |
314 |
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x |
|
315 |
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x; |
|
316 |
||
6558 | 317 |
val method = meth3; |
5826 | 318 |
|
319 |
||
320 |
end; |