author | wenzelm |
Tue, 16 Oct 2001 00:33:22 +0200 | |
changeset 11792 | 311eee3d63b6 |
parent 11651 | 201b3f76c7b7 |
child 12047 | e151e66da2d6 |
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 |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
17 |
val underscore: token list -> string * 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 |
|
24 |
val term_var: token list -> string * token list |
|
25 |
val type_ident: token list -> string * token list |
|
26 |
val type_var: token list -> string * token list |
|
27 |
val number: token list -> string * token list |
|
28 |
val string: token list -> string * token list |
|
29 |
val verbatim: token list -> string * token list |
|
6860 | 30 |
val sync: token list -> string * token list |
5826 | 31 |
val eof: token list -> string * token list |
32 |
val not_eof: token list -> token * token list |
|
7930 | 33 |
val opt_unit: token list -> unit * token list |
5826 | 34 |
val nat: token list -> int * token list |
35 |
val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
36 |
val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
37 |
val list: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
38 |
val list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
6013 | 39 |
val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list |
40 |
val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
5826 | 41 |
val name: token list -> bstring * token list |
42 |
val xname: token list -> xstring * token list |
|
43 |
val text: token list -> string * token list |
|
6553 | 44 |
val comment: token list -> Comment.text * token list |
45 |
val marg_comment: token list -> Comment.text * token list |
|
46 |
val interest: token list -> Comment.interest * token list |
|
8897 | 47 |
val sort: token list -> string * token list |
48 |
val arity: token list -> (string list * string) * token list |
|
49 |
val simple_arity: token list -> (string list * xclass) * token list |
|
5826 | 50 |
val type_args: token list -> string list * token list |
51 |
val typ: token list -> string * token list |
|
52 |
val opt_infix: token list -> Syntax.mixfix * token list |
|
53 |
val opt_mixfix: token list -> Syntax.mixfix * token list |
|
9037 | 54 |
val opt_infix': token list -> Syntax.mixfix * token list |
55 |
val opt_mixfix': token list -> Syntax.mixfix * token list |
|
5826 | 56 |
val const: token list -> (bstring * string * Syntax.mixfix) * token list |
57 |
val term: token list -> string * token list |
|
58 |
val prop: token list -> string * token list |
|
6935 | 59 |
val propp: token list -> (string * (string list * string list)) * token list |
6949 | 60 |
val termp: token list -> (string * string list) * token list |
9131 | 61 |
val arguments: token list -> Args.T list * token list |
5826 | 62 |
val attribs: token list -> Args.src list * token list |
63 |
val opt_attribs: token list -> Args.src list * token list |
|
5917 | 64 |
val thm_name: string -> token list -> (bstring * Args.src list) * token list |
65 |
val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list |
|
6372 | 66 |
val spec_name: token list -> ((bstring * string) * Args.src list) * token list |
67 |
val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list |
|
5917 | 68 |
val xthm: token list -> (xstring * Args.src list) * token list |
69 |
val xthms1: token list -> (xstring * Args.src list) list * token list |
|
5826 | 70 |
val method: token list -> Method.text * token list |
71 |
val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c |
|
72 |
val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c |
|
6430 | 73 |
val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b |
5826 | 74 |
end; |
75 |
||
76 |
structure OuterParse: OUTER_PARSE = |
|
77 |
struct |
|
78 |
||
9131 | 79 |
structure T = OuterLex; |
80 |
type token = T.token; |
|
5826 | 81 |
|
82 |
||
83 |
(** error handling **) |
|
84 |
||
85 |
(* group atomic parsers (no cuts!) *) |
|
86 |
||
87 |
fun fail_with s = Scan.fail_with |
|
88 |
(fn [] => s ^ " expected (past end-of-file!)" |
|
9155 | 89 |
| (tok :: _) => s ^ " expected,\nbut " ^ T.name_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 |
|
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
147 |
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1; |
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
148 |
|
5877 | 149 |
val nat = number >> (fst o Term.read_int o Symbol.explode); |
5826 | 150 |
|
9131 | 151 |
val not_eof = Scan.one T.not_eof; |
5826 | 152 |
|
7930 | 153 |
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) (); |
154 |
||
5826 | 155 |
|
156 |
(* enumerations *) |
|
157 |
||
6003 | 158 |
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::; |
5826 | 159 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
160 |
||
161 |
fun list1 scan = enum1 "," scan; |
|
162 |
fun list scan = enum "," scan; |
|
163 |
||
6013 | 164 |
fun and_list1 scan = enum1 "and" scan; |
165 |
fun and_list scan = enum "and" scan; |
|
166 |
||
5826 | 167 |
|
5960 | 168 |
(* names and text *) |
5826 | 169 |
|
8146 | 170 |
val name = group "name declaration" (short_ident || sym_ident || string || number); |
171 |
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number); |
|
172 |
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim); |
|
6553 | 173 |
|
174 |
||
175 |
(* formal comments *) |
|
176 |
||
8077 | 177 |
val comment = text >> (Comment.plain o Library.single); |
178 |
val marg_comment = Scan.repeat ($$$ "--" |-- text) >> Comment.plain; |
|
7171 | 179 |
|
180 |
val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest || |
|
181 |
$$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest; |
|
5826 | 182 |
|
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; |
|
8897 | 192 |
val simple_arity = gen_arity xname; |
5826 | 193 |
|
194 |
||
195 |
(* types *) |
|
196 |
||
8146 | 197 |
val typ = group "type" |
198 |
(short_ident || long_ident || sym_ident || type_ident || type_var || string || number); |
|
5826 | 199 |
|
200 |
val type_args = |
|
201 |
type_ident >> single || |
|
202 |
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") || |
|
203 |
Scan.succeed []; |
|
204 |
||
205 |
||
206 |
(* mixfix annotations *) |
|
207 |
||
11651 | 208 |
val infx = $$$ "infix" |-- !!! (nat >> Syntax.Infix || string -- nat >> Syntax.InfixName); |
5826 | 209 |
val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName); |
210 |
val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName); |
|
211 |
||
212 |
val binder = |
|
213 |
$$$ "binder" |-- |
|
214 |
!!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n)))) |
|
215 |
>> (Syntax.Binder o triple2); |
|
216 |
||
217 |
||
218 |
val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) []; |
|
219 |
||
220 |
val mixfix = |
|
8648 | 221 |
(string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri)) |
222 |
>> (Syntax.Mixfix o triple2); |
|
5826 | 223 |
|
9037 | 224 |
fun opt_fix guard fix = |
225 |
Scan.optional ($$$ "(" |-- guard (fix --| $$$ ")")) Syntax.NoSyn; |
|
5826 | 226 |
|
11651 | 227 |
val opt_infix = opt_fix !!! (infxl || infxr || infx); |
228 |
val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr || infx); |
|
9037 | 229 |
|
11651 | 230 |
val opt_infix' = opt_fix I (infxl || infxr || infx); |
231 |
val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr || infx); |
|
5826 | 232 |
|
233 |
||
234 |
(* consts *) |
|
235 |
||
236 |
val const = |
|
237 |
name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2; |
|
238 |
||
239 |
||
240 |
(* terms *) |
|
241 |
||
7477 | 242 |
val trm = short_ident || long_ident || sym_ident || term_var || number || string; |
5826 | 243 |
|
244 |
val term = group "term" trm; |
|
245 |
val prop = group "proposition" trm; |
|
246 |
||
247 |
||
6949 | 248 |
(* patterns *) |
6935 | 249 |
|
6949 | 250 |
val is_terms = Scan.repeat1 ($$$ "is" |-- term); |
6935 | 251 |
val is_props = Scan.repeat1 ($$$ "is" |-- prop); |
252 |
val concl_props = $$$ "concl" |-- !!! is_props; |
|
7418 | 253 |
val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props []; |
6935 | 254 |
|
255 |
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_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 |
||
302 |
(* proof methods *) |
|
303 |
||
6983 | 304 |
fun is_symid_meth s = |
9131 | 305 |
s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s; |
6983 | 306 |
|
5826 | 307 |
fun meth4 x = |
5877 | 308 |
(position (xname >> rpair []) >> (Method.Source o Args.src) || |
6983 | 309 |
$$$ "(" |-- !!! (meth0 --| $$$ ")")) x |
5826 | 310 |
and meth3 x = |
311 |
(meth4 --| $$$ "?" >> Method.Try || |
|
312 |
meth4 --| $$$ "+" >> Method.Repeat1 || |
|
5940 | 313 |
meth4) x |
314 |
and meth2 x = |
|
6983 | 315 |
(position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) || |
5826 | 316 |
meth3) x |
317 |
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x |
|
318 |
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x; |
|
319 |
||
6558 | 320 |
val method = meth3; |
5826 | 321 |
|
322 |
||
323 |
end; |