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