author | ballarin |
Fri, 02 Apr 2004 14:08:30 +0200 | |
changeset 14508 | 859b11514537 |
parent 12955 | f4d60f358cb6 |
child 14605 | 9de4d64eee3b |
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 |
12047 | 15 |
val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c |
16 |
val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c |
|
17 |
val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b |
|
5826 | 18 |
val $$$ : string -> token list -> string * token list |
9131 | 19 |
val semicolon: token list -> string * token list |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
20 |
val underscore: token list -> string * token list |
5826 | 21 |
val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b |
7026 | 22 |
val command: token list -> string * token list |
5826 | 23 |
val keyword: token list -> string * token list |
24 |
val short_ident: token list -> string * token list |
|
25 |
val long_ident: token list -> string * token list |
|
26 |
val sym_ident: token list -> string * token list |
|
27 |
val term_var: token list -> string * token list |
|
28 |
val type_ident: token list -> string * token list |
|
29 |
val type_var: token list -> string * token list |
|
30 |
val number: token list -> string * token list |
|
31 |
val string: token list -> string * token list |
|
32 |
val verbatim: token list -> string * token list |
|
6860 | 33 |
val sync: token list -> string * token list |
5826 | 34 |
val eof: token list -> string * token list |
35 |
val not_eof: token list -> token * token list |
|
7930 | 36 |
val opt_unit: token list -> unit * token list |
5826 | 37 |
val nat: token list -> int * token list |
38 |
val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
39 |
val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list |
|
40 |
val list: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
41 |
val list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
6013 | 42 |
val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list |
43 |
val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list |
|
5826 | 44 |
val name: token list -> bstring * token list |
45 |
val xname: token list -> xstring * token list |
|
46 |
val text: token list -> string * token list |
|
12268 | 47 |
val uname: token list -> string option * token list |
8897 | 48 |
val sort: token list -> string * token list |
49 |
val arity: token list -> (string list * string) * token list |
|
50 |
val simple_arity: token list -> (string list * xclass) * token list |
|
5826 | 51 |
val type_args: token list -> string list * token list |
52 |
val typ: token list -> string * token list |
|
53 |
val opt_infix: token list -> Syntax.mixfix * token list |
|
54 |
val opt_mixfix: token list -> Syntax.mixfix * token list |
|
9037 | 55 |
val opt_infix': token list -> Syntax.mixfix * token list |
56 |
val opt_mixfix': token list -> Syntax.mixfix * token list |
|
5826 | 57 |
val const: token list -> (bstring * string * Syntax.mixfix) * token list |
58 |
val term: token list -> string * token list |
|
59 |
val prop: token list -> string * token list |
|
6935 | 60 |
val propp: token list -> (string * (string list * string list)) * token list |
6949 | 61 |
val termp: token list -> (string * string list) * token list |
9131 | 62 |
val arguments: token list -> Args.T list * token list |
5826 | 63 |
val attribs: token list -> Args.src list * token list |
64 |
val opt_attribs: token list -> Args.src list * token list |
|
5917 | 65 |
val thm_name: string -> token list -> (bstring * Args.src list) * token list |
66 |
val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list |
|
6372 | 67 |
val spec_name: token list -> ((bstring * string) * Args.src list) * token list |
68 |
val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list |
|
5917 | 69 |
val xthm: token list -> (xstring * Args.src list) * token list |
70 |
val xthms1: token list -> (xstring * Args.src list) list * token list |
|
12942 | 71 |
val locale_target: token list -> xstring option * token list |
12272 | 72 |
val locale_expr: token list -> Locale.expr * token list |
12942 | 73 |
val locale_keyword: token list -> string * token list |
12047 | 74 |
val locale_element: token list -> Args.src Locale.element * token list |
5826 | 75 |
val method: token list -> Method.text * token list |
76 |
end; |
|
77 |
||
78 |
structure OuterParse: OUTER_PARSE = |
|
79 |
struct |
|
80 |
||
9131 | 81 |
structure T = OuterLex; |
82 |
type token = T.token; |
|
5826 | 83 |
|
84 |
||
85 |
(** error handling **) |
|
86 |
||
87 |
(* group atomic parsers (no cuts!) *) |
|
88 |
||
89 |
fun fail_with s = Scan.fail_with |
|
90 |
(fn [] => s ^ " expected (past end-of-file!)" |
|
9155 | 91 |
| (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found"); |
5826 | 92 |
|
93 |
fun group s scan = scan || fail_with s; |
|
94 |
||
95 |
||
5877 | 96 |
(* cut *) |
5826 | 97 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
98 |
fun cut kind scan = |
5826 | 99 |
let |
100 |
fun get_pos [] = " (past end-of-file!)" |
|
9131 | 101 |
| get_pos (tok :: _) = T.pos_of tok; |
5826 | 102 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
103 |
fun err (toks, None) = kind ^ get_pos toks |
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
104 |
| err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg; |
5826 | 105 |
in Scan.!! err scan end; |
106 |
||
8586 | 107 |
fun !!! scan = cut "Outer syntax error" scan; |
108 |
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan; |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
109 |
|
5826 | 110 |
|
111 |
||
112 |
(** basic parsers **) |
|
113 |
||
114 |
(* utils *) |
|
115 |
||
116 |
fun triple1 ((x, y), z) = (x, y, z); |
|
117 |
fun triple2 (x, (y, z)) = (x, y, z); |
|
6430 | 118 |
fun triple_swap ((x, y), z) = ((x, z), y); |
5826 | 119 |
|
120 |
||
121 |
(* tokens *) |
|
122 |
||
123 |
fun position scan = |
|
9131 | 124 |
(Scan.ahead (Scan.one T.not_eof) >> T.position_of) -- scan >> Library.swap; |
5826 | 125 |
|
126 |
fun kind k = |
|
9131 | 127 |
group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of); |
5826 | 128 |
|
9131 | 129 |
val command = kind T.Command; |
130 |
val keyword = kind T.Keyword; |
|
131 |
val short_ident = kind T.Ident; |
|
132 |
val long_ident = kind T.LongIdent; |
|
133 |
val sym_ident = kind T.SymIdent; |
|
134 |
val term_var = kind T.Var; |
|
135 |
val type_ident = kind T.TypeIdent; |
|
136 |
val type_var = kind T.TypeVar; |
|
137 |
val number = kind T.Nat; |
|
138 |
val string = kind T.String; |
|
139 |
val verbatim = kind T.Verbatim; |
|
140 |
val sync = kind T.Sync; |
|
141 |
val eof = kind T.EOF; |
|
5826 | 142 |
|
143 |
fun $$$ x = |
|
9131 | 144 |
group (T.str_of_kind T.Keyword ^ " " ^ quote x) |
145 |
(Scan.one (T.keyword_with (equal x)) >> T.val_of); |
|
146 |
||
147 |
val semicolon = $$$ ";"; |
|
5826 | 148 |
|
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
149 |
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1; |
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
150 |
|
5877 | 151 |
val nat = number >> (fst o Term.read_int o Symbol.explode); |
5826 | 152 |
|
9131 | 153 |
val not_eof = Scan.one T.not_eof; |
5826 | 154 |
|
7930 | 155 |
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) (); |
156 |
||
5826 | 157 |
|
158 |
(* enumerations *) |
|
159 |
||
6003 | 160 |
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::; |
5826 | 161 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
162 |
||
163 |
fun list1 scan = enum1 "," scan; |
|
164 |
fun list scan = enum "," scan; |
|
165 |
||
6013 | 166 |
fun and_list1 scan = enum1 "and" scan; |
167 |
fun and_list scan = enum "and" scan; |
|
168 |
||
5826 | 169 |
|
5960 | 170 |
(* names and text *) |
5826 | 171 |
|
8146 | 172 |
val name = group "name declaration" (short_ident || sym_ident || string || number); |
173 |
val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number); |
|
174 |
val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim); |
|
6553 | 175 |
|
12268 | 176 |
val uname = underscore >> K None || name >> Some; |
14508
859b11514537
Experimental command for instantiation of locales in proof contexts:
ballarin
parents:
12955
diff
changeset
|
177 |
(* CB: underscore yields None, any other name Some with that string. |
859b11514537
Experimental command for instantiation of locales in proof contexts:
ballarin
parents:
12955
diff
changeset
|
178 |
Used, for example, in the renaming of locale parameters. *) |
6553 | 179 |
|
6372 | 180 |
(* sorts and arities *) |
5826 | 181 |
|
8897 | 182 |
val sort = group "sort" xname; |
5826 | 183 |
|
6372 | 184 |
fun gen_arity cod = |
7352 | 185 |
Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod; |
6372 | 186 |
|
187 |
val arity = gen_arity sort; |
|
8897 | 188 |
val simple_arity = gen_arity xname; |
5826 | 189 |
|
190 |
||
191 |
(* types *) |
|
192 |
||
8146 | 193 |
val typ = group "type" |
194 |
(short_ident || long_ident || sym_ident || type_ident || type_var || string || number); |
|
5826 | 195 |
|
196 |
val type_args = |
|
197 |
type_ident >> single || |
|
198 |
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") || |
|
199 |
Scan.succeed []; |
|
200 |
||
201 |
||
202 |
(* mixfix annotations *) |
|
203 |
||
11651 | 204 |
val infx = $$$ "infix" |-- !!! (nat >> Syntax.Infix || string -- nat >> Syntax.InfixName); |
5826 | 205 |
val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName); |
206 |
val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName); |
|
207 |
||
208 |
val binder = |
|
209 |
$$$ "binder" |-- |
|
210 |
!!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n)))) |
|
211 |
>> (Syntax.Binder o triple2); |
|
212 |
||
213 |
||
214 |
val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) []; |
|
215 |
||
216 |
val mixfix = |
|
8648 | 217 |
(string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri)) |
218 |
>> (Syntax.Mixfix o triple2); |
|
5826 | 219 |
|
9037 | 220 |
fun opt_fix guard fix = |
221 |
Scan.optional ($$$ "(" |-- guard (fix --| $$$ ")")) Syntax.NoSyn; |
|
5826 | 222 |
|
11651 | 223 |
val opt_infix = opt_fix !!! (infxl || infxr || infx); |
224 |
val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr || infx); |
|
9037 | 225 |
|
11651 | 226 |
val opt_infix' = opt_fix I (infxl || infxr || infx); |
227 |
val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr || infx); |
|
5826 | 228 |
|
229 |
||
230 |
(* consts *) |
|
231 |
||
232 |
val const = |
|
233 |
name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2; |
|
234 |
||
235 |
||
236 |
(* terms *) |
|
237 |
||
7477 | 238 |
val trm = short_ident || long_ident || sym_ident || term_var || number || string; |
5826 | 239 |
|
240 |
val term = group "term" trm; |
|
241 |
val prop = group "proposition" trm; |
|
242 |
||
243 |
||
6949 | 244 |
(* patterns *) |
6935 | 245 |
|
6949 | 246 |
val is_terms = Scan.repeat1 ($$$ "is" |-- term); |
6935 | 247 |
val is_props = Scan.repeat1 ($$$ "is" |-- prop); |
248 |
val concl_props = $$$ "concl" |-- !!! is_props; |
|
7418 | 249 |
val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props []; |
6935 | 250 |
|
251 |
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []); |
|
12047 | 252 |
val propp' = prop -- Scan.optional ($$$ "(" |-- !!! (is_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 |
||
12047 | 299 |
(* locale elements *) |
300 |
||
12272 | 301 |
local |
302 |
||
303 |
val loc_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K None || opt_mixfix >> Some; |
|
12955 | 304 |
val loc_keyword = $$$ "fixes" || $$$ "assumes" || $$$ "defines" || $$$ "notes" || $$$ "includes"; |
12272 | 305 |
|
306 |
fun plus1 scan = |
|
307 |
scan -- Scan.repeat ($$$ "+" |-- Scan.unless loc_keyword (!!! scan)) >> op ::; |
|
308 |
||
12268 | 309 |
fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x |
310 |
and expr1 x = (expr2 -- Scan.repeat1 uname >> Locale.Rename || expr2) x |
|
12272 | 311 |
and expr0 x = (plus1 expr1 >> (fn [e] => e | es => Locale.Merge es)) x; |
12268 | 312 |
|
12272 | 313 |
in |
314 |
||
12942 | 315 |
val locale_target = Scan.option (($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")")); |
12272 | 316 |
val locale_expr = expr0; |
12942 | 317 |
val locale_keyword = loc_keyword; |
12047 | 318 |
|
12064 | 319 |
val locale_element = group "locale element" |
320 |
($$$ "fixes" |-- !!! (and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- loc_mixfix |
|
12272 | 321 |
>> triple1)) >> (Locale.Elem o Locale.Fixes) || |
12047 | 322 |
$$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp)) |
12272 | 323 |
>> (Locale.Elem o Locale.Assumes) || |
12047 | 324 |
$$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp')) |
12272 | 325 |
>> (Locale.Elem o Locale.Defines) || |
326 |
$$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1)) |
|
327 |
>> (Locale.Elem o Locale.Notes) || |
|
12955 | 328 |
$$$ "includes" |-- !!! locale_expr >> Locale.Expr); |
12272 | 329 |
|
330 |
end; |
|
12047 | 331 |
|
332 |
||
5826 | 333 |
(* proof methods *) |
334 |
||
6983 | 335 |
fun is_symid_meth s = |
9131 | 336 |
s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s; |
6983 | 337 |
|
5826 | 338 |
fun meth4 x = |
5877 | 339 |
(position (xname >> rpair []) >> (Method.Source o Args.src) || |
6983 | 340 |
$$$ "(" |-- !!! (meth0 --| $$$ ")")) x |
5826 | 341 |
and meth3 x = |
342 |
(meth4 --| $$$ "?" >> Method.Try || |
|
343 |
meth4 --| $$$ "+" >> Method.Repeat1 || |
|
5940 | 344 |
meth4) x |
345 |
and meth2 x = |
|
6983 | 346 |
(position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) || |
5826 | 347 |
meth3) x |
348 |
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x |
|
349 |
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x; |
|
350 |
||
6558 | 351 |
val method = meth3; |
5826 | 352 |
|
353 |
||
354 |
end; |