1 (* Title: Pure/Isar/outer_parse.ML
3 Author: Markus Wenzel, TU Muenchen
5 Generic parsers for Isabelle/Isar outer syntax.
8 signature OUTER_PARSE =
11 val group: string -> (token list -> 'a) -> token list -> 'a
12 val !!! : (token list -> 'a) -> token list -> 'a
13 val $$$ : string -> token list -> string * token list
14 val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
15 val command: token list -> string * token list
16 val keyword: token list -> string * token list
17 val short_ident: token list -> string * token list
18 val long_ident: token list -> string * token list
19 val sym_ident: token list -> string * token list
20 val term_var: token list -> string * token list
21 val text_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
27 val sync: token list -> string * token list
28 val eof: token list -> string * token list
29 val not_eof: token list -> token * token list
30 val nat: token list -> int * token list
31 val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
32 val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
33 val list: (token list -> 'a * token list) -> token list -> 'a list * token list
34 val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
35 val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
36 val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
37 val name: token list -> bstring * token list
38 val xname: token list -> xstring * token list
39 val text: token list -> string * token list
40 val comment: token list -> Comment.text * token list
41 val marg_comment: token list -> Comment.text * token list
42 val interest: token list -> Comment.interest * token list
43 val sort: token list -> xsort * token list
44 val arity: token list -> (xsort list * xsort) * token list
45 val simple_arity: token list -> (xsort list * xclass) * token list
46 val type_args: token list -> string list * token list
47 val typ: token list -> string * token list
48 val opt_infix: token list -> Syntax.mixfix * token list
49 val opt_mixfix: token list -> Syntax.mixfix * token list
50 val const: token list -> (bstring * string * Syntax.mixfix) * token list
51 val term: token list -> string * token list
52 val prop: token list -> string * token list
53 val propp: token list -> (string * (string list * string list)) * token list
54 val termp: token list -> (string * string list) * token list
55 val attribs: token list -> Args.src list * token list
56 val opt_attribs: token list -> Args.src list * token list
57 val thm_name: string -> token list -> (bstring * Args.src list) * token list
58 val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
59 val spec_name: token list -> ((bstring * string) * Args.src list) * token list
60 val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
61 val xthm: token list -> (xstring * Args.src list) * token list
62 val xthms1: token list -> (xstring * Args.src list) list * token list
63 val method: token list -> Method.text * token list
64 val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
65 val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
66 val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
69 structure OuterParse: OUTER_PARSE =
72 type token = OuterLex.token;
75 (** error handling **)
77 (* group atomic parsers (no cuts!) *)
79 fun fail_with s = Scan.fail_with
80 (fn [] => s ^ " expected (past end-of-file!)"
81 | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
82 quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
84 fun group s scan = scan || fail_with s;
91 fun get_pos [] = " (past end-of-file!)"
92 | get_pos (tok :: _) = OuterLex.pos_of tok;
94 fun err (toks, None) = "Outer syntax error" ^ get_pos toks
95 | err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
96 in Scan.!! err scan end;
100 (** basic parsers **)
104 fun triple1 ((x, y), z) = (x, y, z);
105 fun triple2 (x, (y, z)) = (x, y, z);
106 fun triple_swap ((x, y), z) = ((x, z), y);
112 (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
115 group (OuterLex.str_of_kind k)
116 (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
118 val command = kind OuterLex.Command;
119 val keyword = kind OuterLex.Keyword;
120 val short_ident = kind OuterLex.Ident;
121 val long_ident = kind OuterLex.LongIdent;
122 val sym_ident = kind OuterLex.SymIdent;
123 val term_var = kind OuterLex.Var;
124 val text_var = kind OuterLex.TextVar;
125 val type_ident = kind OuterLex.TypeIdent;
126 val type_var = kind OuterLex.TypeVar;
127 val number = kind OuterLex.Nat;
128 val string = kind OuterLex.String;
129 val verbatim = kind OuterLex.Verbatim;
130 val sync = kind OuterLex.Sync;
131 val eof = kind OuterLex.EOF;
134 group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
135 (Scan.one (OuterLex.keyword_with (equal x)) >> OuterLex.val_of);
137 val nat = number >> (fst o Term.read_int o Symbol.explode);
139 val not_eof = Scan.one OuterLex.not_eof;
144 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
145 fun enum sep scan = enum1 sep scan || Scan.succeed [];
147 fun list1 scan = enum1 "," scan;
148 fun list scan = enum "," scan;
150 fun and_list1 scan = enum1 "and" scan;
151 fun and_list scan = enum "and" scan;
156 val name = group "name declaration" (short_ident || sym_ident || string);
157 val xname = group "name reference" (short_ident || long_ident || sym_ident || string);
158 val text = group "text" (short_ident || long_ident || sym_ident || string || verbatim);
161 (* formal comments *)
163 val comment = text >> Comment.plain;
164 val marg_comment = Scan.optional ($$$ "--" |-- comment) Comment.none;
166 val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest ||
167 $$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest;
170 (* sorts and arities *)
173 xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
176 Scan.optional ($$$ "(" |-- !!! (Scan.repeat1 sort --| $$$ ")")) [] -- cod;
178 val arity = gen_arity sort;
179 val simple_arity = gen_arity name;
185 group "type" (short_ident || long_ident || sym_ident || type_ident || type_var || string);
188 type_ident >> single ||
189 $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
193 (* mixfix annotations *)
195 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
196 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
200 !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
201 >> (Syntax.Binder o triple2);
204 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
207 string -- opt_pris -- Scan.optional nat Syntax.max_pri
208 >> (Syntax.Mixfix o triple1);
211 Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
213 val opt_infix = opt_fix (infxl || infxr);
214 val opt_mixfix = opt_fix (mixfix || binder || infxl || infxr);
220 name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
225 val trm = short_ident || long_ident || sym_ident || term_var || text_var || number || string;
227 val term = group "term" trm;
228 val prop = group "proposition" trm;
233 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
234 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
235 val concl_props = $$$ "concl" |-- !!! is_props;
236 val any_props = is_props -- Scan.optional concl_props [] || concl_props >> pair [];
238 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
239 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
244 fun keyword_symid is_symid = Scan.one (OuterLex.keyword_with is_symid) >> OuterLex.val_of;
246 fun atom_arg is_symid blk =
248 (position (short_ident || long_ident || sym_ident || term_var || text_var ||
249 type_ident || type_var || number) >> Args.ident ||
250 position (keyword_symid is_symid) >> Args.keyword ||
251 position string >> Args.string ||
252 position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
254 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
255 >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
257 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
258 and args1 is_symid blk x =
260 (Scan.repeat1 (atom_arg is_symid blk) ||
261 paren_args "(" ")" (args is_symid) ||
262 paren_args "{" "}" (args is_symid) ||
263 paren_args "[" "]" (args is_symid))) >> flat) x;
266 (* theorem specifications *)
268 val attrib = position (xname -- !!! (args OuterLex.is_sid false)) >> Args.src;
269 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
270 val opt_attribs = Scan.optional attribs [];
272 fun thm_name s = name -- opt_attribs --| $$$ s;
274 Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
276 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
277 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
279 val xthm = xname -- opt_attribs;
280 val xthms1 = Scan.repeat1 xthm;
285 fun is_symid_meth s =
286 s <> "|" andalso s <> "?" andalso s <> "*" andalso s <> "+" andalso OuterLex.is_sid s;
289 (position (xname >> rpair []) >> (Method.Source o Args.src) ||
290 $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
292 (meth4 --| $$$ "?" >> Method.Try ||
293 meth4 --| $$$ "*" >> Method.Repeat ||
294 meth4 --| $$$ "+" >> Method.Repeat1 ||
297 (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
299 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
300 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;