author | wenzelm |
Tue, 18 Mar 2014 12:25:17 +0100 | |
changeset 56202 | 0a11d17eeeff |
parent 56201 | dd2df97b379b |
child 56499 | 7e0178c84994 |
permissions | -rw-r--r-- |
36949
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
wenzelm
parents:
35838
diff
changeset
|
1 |
(* Title: Pure/Isar/parse.ML |
5826 | 2 |
Author: Markus Wenzel, TU Muenchen |
3 |
||
4 |
Generic parsers for Isabelle/Isar outer syntax. |
|
5 |
*) |
|
6 |
||
36949
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
wenzelm
parents:
35838
diff
changeset
|
7 |
signature PARSE = |
5826 | 8 |
sig |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
9 |
type 'a parser = Token.T list -> 'a * Token.T list |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
10 |
type 'a context_parser = Context.generic * Token.T list -> 'a * (Context.generic * Token.T list) |
44357 | 11 |
val group: (unit -> string) -> (Token.T list -> 'a) -> Token.T list -> 'a |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
12 |
val !!! : (Token.T list -> 'a) -> Token.T list -> 'a |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
13 |
val !!!! : (Token.T list -> 'a) -> Token.T list -> 'a |
12047 | 14 |
val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c |
15 |
val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c |
|
16 |
val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
17 |
val not_eof: Token.T parser |
56201 | 18 |
val token: 'a parser -> Token.T parser |
42326 | 19 |
val position: 'a parser -> ('a * Position.T) parser |
55828
42ac3cfb89f6
clarified language markup: added "delimited" property;
wenzelm
parents:
55764
diff
changeset
|
20 |
val source_position: 'a parser -> Symbol_Pos.source parser |
42326 | 21 |
val inner_syntax: 'a parser -> string parser |
29310 | 22 |
val command: string parser |
23 |
val keyword: string parser |
|
24 |
val short_ident: string parser |
|
25 |
val long_ident: string parser |
|
26 |
val sym_ident: string parser |
|
27 |
val minus: string parser |
|
28 |
val term_var: string parser |
|
29 |
val type_ident: string parser |
|
30 |
val type_var: string parser |
|
31 |
val number: string parser |
|
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
36959
diff
changeset
|
32 |
val float_number: string parser |
29310 | 33 |
val string: string parser |
34 |
val alt_string: string parser |
|
35 |
val verbatim: string parser |
|
55033 | 36 |
val cartouche: string parser |
29310 | 37 |
val sync: string parser |
38 |
val eof: string parser |
|
48927
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents:
48911
diff
changeset
|
39 |
val command_name: string -> string parser |
29310 | 40 |
val keyword_with: (string -> bool) -> string parser |
56202 | 41 |
val keyword_markup: bool * Markup.T -> string -> string parser |
42 |
val keyword_improper: string -> string parser |
|
29310 | 43 |
val $$$ : string -> string parser |
44 |
val reserved: string -> string parser |
|
45 |
val semicolon: string parser |
|
46 |
val underscore: string parser |
|
47 |
val maybe: 'a parser -> 'a option parser |
|
48 |
val tag_name: string parser |
|
49 |
val tags: string list parser |
|
50 |
val opt_unit: unit parser |
|
51 |
val opt_keyword: string -> bool parser |
|
52 |
val begin: string parser |
|
53 |
val opt_begin: bool parser |
|
54 |
val nat: int parser |
|
55 |
val int: int parser |
|
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
36959
diff
changeset
|
56 |
val real: real parser |
55764 | 57 |
val enum_positions: string -> 'a parser -> ('a list * Position.T list) parser |
58 |
val enum1_positions: string -> 'a parser -> ('a list * Position.T list) parser |
|
29310 | 59 |
val enum: string -> 'a parser -> 'a list parser |
60 |
val enum1: string -> 'a parser -> 'a list parser |
|
61 |
val and_list: 'a parser -> 'a list parser |
|
62 |
val and_list1: 'a parser -> 'a list parser |
|
30511 | 63 |
val enum': string -> 'a context_parser -> 'a list context_parser |
64 |
val enum1': string -> 'a context_parser -> 'a list context_parser |
|
65 |
val and_list': 'a context_parser -> 'a list context_parser |
|
66 |
val and_list1': 'a context_parser -> 'a list context_parser |
|
29310 | 67 |
val list: 'a parser -> 'a list parser |
68 |
val list1: 'a parser -> 'a list parser |
|
43775
b361c7d184e7
added Parse.properties (again) -- allow empty list like Parse_Value.properties but unlike Parse.properties of ef86de9c98aa;
wenzelm
parents:
42657
diff
changeset
|
69 |
val properties: Properties.T parser |
29310 | 70 |
val name: bstring parser |
29581 | 71 |
val binding: binding parser |
29310 | 72 |
val xname: xstring parser |
73 |
val text: string parser |
|
48881
46e053eda5dd
clarified Parse.path vs. Parse.explode -- prefer errors in proper transaction context;
wenzelm
parents:
46922
diff
changeset
|
74 |
val path: string parser |
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40793
diff
changeset
|
75 |
val liberal_name: xstring parser |
29310 | 76 |
val parname: string parser |
29581 | 77 |
val parbinding: binding parser |
46922
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
78 |
val class: string parser |
29310 | 79 |
val sort: string parser |
46922
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
80 |
val type_const: string parser |
29310 | 81 |
val arity: (string * string list * string) parser |
82 |
val multi_arity: (string list * string list * string) parser |
|
83 |
val type_args: string list parser |
|
35838 | 84 |
val type_args_constrained: (string * string option) list parser |
29310 | 85 |
val typ_group: string parser |
86 |
val typ: string parser |
|
87 |
val mixfix: mixfix parser |
|
88 |
val mixfix': mixfix parser |
|
89 |
val opt_mixfix: mixfix parser |
|
90 |
val opt_mixfix': mixfix parser |
|
91 |
val where_: string parser |
|
42299 | 92 |
val const_decl: (string * string * mixfix) parser |
30339 | 93 |
val const_binding: (binding * string * mixfix) parser |
29581 | 94 |
val params: (binding * string option) list parser |
95 |
val simple_fixes: (binding * string option) list parser |
|
96 |
val fixes: (binding * string option * mixfix) list parser |
|
97 |
val for_fixes: (binding * string option * mixfix) list parser |
|
55828
42ac3cfb89f6
clarified language markup: added "delimited" property;
wenzelm
parents:
55764
diff
changeset
|
98 |
val ML_source: Symbol_Pos.source parser |
42ac3cfb89f6
clarified language markup: added "delimited" property;
wenzelm
parents:
55764
diff
changeset
|
99 |
val document_source: Symbol_Pos.source parser |
29310 | 100 |
val term_group: string parser |
101 |
val prop_group: string parser |
|
102 |
val term: string parser |
|
103 |
val prop: string parser |
|
42300
0d1cbc1fe579
notation: proper markup for type constructor / constant;
wenzelm
parents:
42299
diff
changeset
|
104 |
val const: string parser |
40793
d21aedaa91e7
added Parse.literal_fact with proper inner_syntax markup (source position);
wenzelm
parents:
40296
diff
changeset
|
105 |
val literal_fact: string parser |
29310 | 106 |
val propp: (string * string list) parser |
107 |
val termp: (string * string list) parser |
|
45488
6d71d9e52369
pass positions for named targets, for formal links in the document model;
wenzelm
parents:
45331
diff
changeset
|
108 |
val target: (xstring * Position.T) parser |
6d71d9e52369
pass positions for named targets, for formal links in the document model;
wenzelm
parents:
45331
diff
changeset
|
109 |
val opt_target: (xstring * Position.T) option parser |
56201 | 110 |
val args: Token.T list parser |
111 |
val args1: (string -> bool) -> Token.T list parser |
|
5826 | 112 |
end; |
113 |
||
36949
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
wenzelm
parents:
35838
diff
changeset
|
114 |
structure Parse: PARSE = |
5826 | 115 |
struct |
116 |
||
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
117 |
type 'a parser = Token.T list -> 'a * Token.T list; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
118 |
type 'a context_parser = Context.generic * Token.T list -> 'a * (Context.generic * Token.T list); |
29310 | 119 |
|
5826 | 120 |
|
121 |
(** error handling **) |
|
122 |
||
123 |
(* group atomic parsers (no cuts!) *) |
|
124 |
||
44357 | 125 |
fun group s scan = scan || Scan.fail_with |
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48881
diff
changeset
|
126 |
(fn [] => (fn () => s () ^ " expected,\nbut end-of-input was found") |
42519 | 127 |
| tok :: _ => |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
128 |
(fn () => |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
129 |
(case Token.text_of tok of |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
130 |
(txt, "") => |
55708 | 131 |
s () ^ " expected,\nbut " ^ txt ^ Position.here (Token.pos_of tok) ^ |
132 |
" was found" |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
133 |
| (txt1, txt2) => |
55708 | 134 |
s () ^ " expected,\nbut " ^ txt1 ^ Position.here (Token.pos_of tok) ^ |
135 |
" was found:\n" ^ txt2))); |
|
5826 | 136 |
|
137 |
||
5877 | 138 |
(* cut *) |
5826 | 139 |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
140 |
fun cut kind scan = |
5826 | 141 |
let |
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48881
diff
changeset
|
142 |
fun get_pos [] = " (end-of-input)" |
55708 | 143 |
| get_pos (tok :: _) = Position.here (Token.pos_of tok); |
5826 | 144 |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
145 |
fun err (toks, NONE) = (fn () => kind ^ get_pos toks) |
25625 | 146 |
| err (toks, SOME msg) = |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
147 |
(fn () => |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
148 |
let val s = msg () in |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
149 |
if String.isPrefix kind s then s |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
150 |
else kind ^ get_pos toks ^ ": " ^ s |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43775
diff
changeset
|
151 |
end); |
5826 | 152 |
in Scan.!! err scan end; |
153 |
||
8586 | 154 |
fun !!! scan = cut "Outer syntax error" scan; |
155 |
fun !!!! scan = cut "Corrupted outer syntax in presentation" scan; |
|
8581
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
wenzelm
parents:
8350
diff
changeset
|
156 |
|
5826 | 157 |
|
158 |
||
159 |
(** basic parsers **) |
|
160 |
||
161 |
(* utils *) |
|
162 |
||
163 |
fun triple1 ((x, y), z) = (x, y, z); |
|
164 |
fun triple2 (x, (y, z)) = (x, y, z); |
|
6430 | 165 |
fun triple_swap ((x, y), z) = ((x, z), y); |
5826 | 166 |
|
167 |
||
168 |
(* tokens *) |
|
169 |
||
27815 | 170 |
fun RESET_VALUE atom = (*required for all primitive parsers*) |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
171 |
Scan.ahead (Scan.one (K true)) -- atom >> (fn (arg, x) => (Token.assign NONE arg; x)); |
27815 | 172 |
|
173 |
||
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
174 |
val not_eof = RESET_VALUE (Scan.one Token.not_eof); |
15703 | 175 |
|
56201 | 176 |
fun token atom = Scan.ahead not_eof --| atom; |
177 |
||
55708 | 178 |
fun position scan = (Scan.ahead not_eof >> Token.pos_of) -- scan >> Library.swap; |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
179 |
fun source_position atom = Scan.ahead atom |-- not_eof >> Token.source_position_of; |
55111 | 180 |
fun inner_syntax atom = Scan.ahead atom |-- not_eof >> Token.inner_syntax_of; |
5826 | 181 |
|
182 |
fun kind k = |
|
44357 | 183 |
group (fn () => Token.str_of_kind k) |
184 |
(RESET_VALUE (Scan.one (Token.is_kind k) >> Token.content_of)); |
|
5826 | 185 |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
186 |
val command = kind Token.Command; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
187 |
val keyword = kind Token.Keyword; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
188 |
val short_ident = kind Token.Ident; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
189 |
val long_ident = kind Token.LongIdent; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
190 |
val sym_ident = kind Token.SymIdent; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
191 |
val term_var = kind Token.Var; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
192 |
val type_ident = kind Token.TypeIdent; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
193 |
val type_var = kind Token.TypeVar; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
194 |
val number = kind Token.Nat; |
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
36959
diff
changeset
|
195 |
val float_number = kind Token.Float; |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
196 |
val string = kind Token.String; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
197 |
val alt_string = kind Token.AltString; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
198 |
val verbatim = kind Token.Verbatim; |
55033 | 199 |
val cartouche = kind Token.Cartouche; |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
200 |
val sync = kind Token.Sync; |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
201 |
val eof = kind Token.EOF; |
5826 | 202 |
|
48927
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents:
48911
diff
changeset
|
203 |
fun command_name x = |
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents:
48911
diff
changeset
|
204 |
group (fn () => Token.str_of_kind Token.Command ^ " " ^ quote x) |
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents:
48911
diff
changeset
|
205 |
(RESET_VALUE (Scan.one (fn tok => Token.is_command tok andalso Token.content_of tok = x))) |
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents:
48911
diff
changeset
|
206 |
>> Token.content_of; |
ef462b5558eb
theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents:
48911
diff
changeset
|
207 |
|
56202 | 208 |
fun keyword_with pred = RESET_VALUE (Scan.one (Token.keyword_with pred) >> Token.content_of); |
209 |
||
210 |
fun keyword_markup markup x = |
|
44357 | 211 |
group (fn () => Token.str_of_kind Token.Keyword ^ " " ^ quote x) |
56063 | 212 |
(Scan.ahead not_eof -- keyword_with (fn y => x = y)) |
56202 | 213 |
>> (fn (tok, x) => (Token.assign (SOME (Token.Literal markup)) tok; x)); |
214 |
||
215 |
val keyword_improper = keyword_markup (true, Markup.improper); |
|
216 |
val $$$ = keyword_markup (false, Markup.quasi_keyword); |
|
9131 | 217 |
|
16030 | 218 |
fun reserved x = |
44357 | 219 |
group (fn () => "reserved identifier " ^ quote x) |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
36955
diff
changeset
|
220 |
(RESET_VALUE (Scan.one (Token.ident_with (fn y => x = y)) >> Token.content_of)); |
16030 | 221 |
|
9131 | 222 |
val semicolon = $$$ ";"; |
5826 | 223 |
|
15703 | 224 |
val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1; |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
225 |
val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1; |
15703 | 226 |
fun maybe scan = underscore >> K NONE || scan >> SOME; |
11792
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
wenzelm
parents:
11651
diff
changeset
|
227 |
|
14835 | 228 |
val nat = number >> (#1 o Library.read_int o Symbol.explode); |
27815 | 229 |
val int = Scan.optional (minus >> K ~1) 1 -- nat >> op *; |
51988 | 230 |
val real = float_number >> Markup.parse_real || int >> Real.fromInt; |
5826 | 231 |
|
44357 | 232 |
val tag_name = group (fn () => "tag name") (short_ident || string); |
17070 | 233 |
val tags = Scan.repeat ($$$ "%" |-- !!! tag_name); |
234 |
||
7930 | 235 |
val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) (); |
14646 | 236 |
fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false; |
237 |
||
20983 | 238 |
val begin = $$$ "begin"; |
239 |
val opt_begin = Scan.optional (begin >> K true) false; |
|
20961 | 240 |
|
5826 | 241 |
|
242 |
(* enumerations *) |
|
243 |
||
55764 | 244 |
fun enum1_positions sep scan = |
245 |
scan -- Scan.repeat (position ($$$ sep) -- !!! scan) >> |
|
246 |
(fn (x, ys) => (x :: map #2 ys, map (#2 o #1) ys)); |
|
247 |
fun enum_positions sep scan = |
|
248 |
enum1_positions sep scan || Scan.succeed ([], []); |
|
249 |
||
25999 | 250 |
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan); |
5826 | 251 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
252 |
||
27815 | 253 |
fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan); |
254 |
fun enum' sep scan = enum1' sep scan || Scan.succeed []; |
|
5826 | 255 |
|
6013 | 256 |
fun and_list1 scan = enum1 "and" scan; |
257 |
fun and_list scan = enum "and" scan; |
|
258 |
||
27815 | 259 |
fun and_list1' scan = enum1' "and" scan; |
260 |
fun and_list' scan = enum' "and" scan; |
|
261 |
||
262 |
fun list1 scan = enum1 "," scan; |
|
263 |
fun list scan = enum "," scan; |
|
264 |
||
43775
b361c7d184e7
added Parse.properties (again) -- allow empty list like Parse_Value.properties but unlike Parse.properties of ef86de9c98aa;
wenzelm
parents:
42657
diff
changeset
|
265 |
val properties = $$$ "(" |-- !!! (list (string -- ($$$ "=" |-- string)) --| $$$ ")"); |
b361c7d184e7
added Parse.properties (again) -- allow empty list like Parse_Value.properties but unlike Parse.properties of ef86de9c98aa;
wenzelm
parents:
42657
diff
changeset
|
266 |
|
5826 | 267 |
|
5960 | 268 |
(* names and text *) |
5826 | 269 |
|
44357 | 270 |
val name = group (fn () => "name declaration") (short_ident || sym_ident || string || number); |
271 |
||
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
29581
diff
changeset
|
272 |
val binding = position name >> Binding.make; |
44357 | 273 |
|
274 |
val xname = group (fn () => "name reference") |
|
275 |
(short_ident || long_ident || sym_ident || string || number); |
|
276 |
||
277 |
val text = group (fn () => "text") |
|
278 |
(short_ident || long_ident || sym_ident || string || number || verbatim); |
|
279 |
||
48881
46e053eda5dd
clarified Parse.path vs. Parse.explode -- prefer errors in proper transaction context;
wenzelm
parents:
46922
diff
changeset
|
280 |
val path = group (fn () => "file name/path specification") name; |
6553 | 281 |
|
56063 | 282 |
val liberal_name = keyword_with Token.ident_or_symbolic || xname; |
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40793
diff
changeset
|
283 |
|
18898 | 284 |
val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") ""; |
28965 | 285 |
val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty; |
18898 | 286 |
|
6553 | 287 |
|
46922
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
288 |
(* type classes *) |
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
289 |
|
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
290 |
val class = group (fn () => "type class") (inner_syntax xname); |
5826 | 291 |
|
44357 | 292 |
val sort = group (fn () => "sort") (inner_syntax xname); |
5826 | 293 |
|
46922
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
294 |
val type_const = inner_syntax (group (fn () => "type constructor") xname); |
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
295 |
|
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
296 |
val arity = type_const -- ($$$ "::" |-- !!! |
22331 | 297 |
(Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2; |
5826 | 298 |
|
46922
3717f3878714
source positions for locale and class expressions;
wenzelm
parents:
45596
diff
changeset
|
299 |
val multi_arity = and_list1 type_const -- ($$$ "::" |-- !!! |
25541 | 300 |
(Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2; |
301 |
||
5826 | 302 |
|
303 |
(* types *) |
|
304 |
||
44357 | 305 |
val typ_group = |
306 |
group (fn () => "type") |
|
307 |
(short_ident || long_ident || sym_ident || type_ident || type_var || string || number); |
|
5826 | 308 |
|
27753
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
wenzelm
parents:
27737
diff
changeset
|
309 |
val typ = inner_syntax typ_group; |
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
wenzelm
parents:
27737
diff
changeset
|
310 |
|
35838 | 311 |
fun type_arguments arg = |
312 |
arg >> single || |
|
313 |
$$$ "(" |-- !!! (list1 arg --| $$$ ")") || |
|
5826 | 314 |
Scan.succeed []; |
315 |
||
35838 | 316 |
val type_args = type_arguments type_ident; |
317 |
val type_args_constrained = type_arguments (type_ident -- Scan.option ($$$ "::" |-- !!! sort)); |
|
318 |
||
5826 | 319 |
|
320 |
(* mixfix annotations *) |
|
321 |
||
51654
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
322 |
local |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
323 |
|
18669 | 324 |
val mfix = string -- |
325 |
!!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] -- |
|
42297
140f283266b7
discontinued Syntax.max_pri, which is not really a symbolic parameter;
wenzelm
parents:
42287
diff
changeset
|
326 |
Scan.optional nat 1000) >> (Mixfix o triple2); |
18669 | 327 |
|
35130 | 328 |
val infx = $$$ "infix" |-- !!! (string -- nat >> Infix); |
329 |
val infxl = $$$ "infixl" |-- !!! (string -- nat >> Infixl); |
|
330 |
val infxr = $$$ "infixr" |-- !!! (string -- nat >> Infixr); |
|
51654
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
331 |
val strcture = $$$ "structure" >> K Structure; |
5826 | 332 |
|
18669 | 333 |
val binder = $$$ "binder" |-- |
334 |
!!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n)))) |
|
335 |
>> (Binder o triple2); |
|
336 |
||
51654
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
337 |
val mixfix_body = mfix || strcture || binder || infxl || infxr || infx; |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
338 |
|
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
339 |
fun annotation guard body = $$$ "(" |-- guard (body --| $$$ ")"); |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
340 |
fun opt_annotation guard body = Scan.optional (annotation guard body) NoSyn; |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
341 |
|
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
342 |
in |
18669 | 343 |
|
51654
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
344 |
val mixfix = annotation !!! mixfix_body; |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
345 |
val mixfix' = annotation I mixfix_body; |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
346 |
val opt_mixfix = opt_annotation !!! mixfix_body; |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
347 |
val opt_mixfix' = opt_annotation I mixfix_body; |
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
348 |
|
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
51627
diff
changeset
|
349 |
end; |
5826 | 350 |
|
351 |
||
18669 | 352 |
(* fixes *) |
5826 | 353 |
|
21400 | 354 |
val where_ = $$$ "where"; |
355 |
||
42299 | 356 |
val const_decl = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1; |
30339 | 357 |
val const_binding = binding -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1; |
18669 | 358 |
|
28081
d664b2c1dfe6
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
28017
diff
changeset
|
359 |
val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ) |
18669 | 360 |
>> (fn (xs, T) => map (rpair T) xs); |
361 |
||
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19284
diff
changeset
|
362 |
val simple_fixes = and_list1 params >> flat; |
18669 | 363 |
|
364 |
val fixes = |
|
45331
6e0a8aba99ec
more liberal Parse.fixes, to avoid overlap of mixfix with is-pattern (notably in 'obtain' syntax);
wenzelm
parents:
44357
diff
changeset
|
365 |
and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix' >> (single o triple1) || |
42287
d98eb048a2e4
discontinued special treatment of structure Mixfix;
wenzelm
parents:
40800
diff
changeset
|
366 |
params >> map (fn (x, y) => (x, y, NoSyn))) >> flat; |
5826 | 367 |
|
19845 | 368 |
val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) []; |
369 |
||
5826 | 370 |
|
27877 | 371 |
(* embedded source text *) |
27872
631371a02b8c
P.doc_source and P.ml_sorce for proper SymbolPos.text;
wenzelm
parents:
27815
diff
changeset
|
372 |
|
44357 | 373 |
val ML_source = source_position (group (fn () => "ML source") text); |
51627
589daaf48dba
tuned signature -- agree with markup terminology;
wenzelm
parents:
48927
diff
changeset
|
374 |
val document_source = source_position (group (fn () => "document source") text); |
27872
631371a02b8c
P.doc_source and P.ml_sorce for proper SymbolPos.text;
wenzelm
parents:
27815
diff
changeset
|
375 |
|
631371a02b8c
P.doc_source and P.ml_sorce for proper SymbolPos.text;
wenzelm
parents:
27815
diff
changeset
|
376 |
|
5826 | 377 |
(* terms *) |
378 |
||
40793
d21aedaa91e7
added Parse.literal_fact with proper inner_syntax markup (source position);
wenzelm
parents:
40296
diff
changeset
|
379 |
val tm = short_ident || long_ident || sym_ident || term_var || number || string; |
5826 | 380 |
|
44357 | 381 |
val term_group = group (fn () => "term") tm; |
382 |
val prop_group = group (fn () => "proposition") tm; |
|
27753
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
wenzelm
parents:
27737
diff
changeset
|
383 |
|
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
wenzelm
parents:
27737
diff
changeset
|
384 |
val term = inner_syntax term_group; |
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
wenzelm
parents:
27737
diff
changeset
|
385 |
val prop = inner_syntax prop_group; |
5826 | 386 |
|
44357 | 387 |
val const = inner_syntax (group (fn () => "constant") xname); |
42300
0d1cbc1fe579
notation: proper markup for type constructor / constant;
wenzelm
parents:
42299
diff
changeset
|
388 |
|
44357 | 389 |
val literal_fact = inner_syntax (group (fn () => "literal fact") alt_string); |
40793
d21aedaa91e7
added Parse.literal_fact with proper inner_syntax markup (source position);
wenzelm
parents:
40296
diff
changeset
|
390 |
|
5826 | 391 |
|
6949 | 392 |
(* patterns *) |
6935 | 393 |
|
6949 | 394 |
val is_terms = Scan.repeat1 ($$$ "is" |-- term); |
6935 | 395 |
val is_props = Scan.repeat1 ($$$ "is" |-- prop); |
396 |
||
19585 | 397 |
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) []; |
6949 | 398 |
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) []; |
6935 | 399 |
|
400 |
||
22119 | 401 |
(* targets *) |
19811 | 402 |
|
45488
6d71d9e52369
pass positions for named targets, for formal links in the document model;
wenzelm
parents:
45331
diff
changeset
|
403 |
val target = ($$$ "(" -- $$$ "in") |-- !!! (position xname --| $$$ ")"); |
22119 | 404 |
val opt_target = Scan.option target; |
12272 | 405 |
|
56201 | 406 |
|
407 |
(* arguments within outer syntax *) |
|
408 |
||
409 |
local |
|
410 |
||
411 |
val argument_kinds = |
|
412 |
[Token.Ident, Token.LongIdent, Token.SymIdent, Token.Var, Token.TypeIdent, Token.TypeVar, |
|
413 |
Token.Nat, Token.Float, Token.String, Token.AltString, Token.Cartouche, Token.Verbatim]; |
|
414 |
||
415 |
fun arguments is_symid = |
|
416 |
let |
|
417 |
fun argument blk = |
|
418 |
group (fn () => "argument") |
|
419 |
(Scan.one (fn tok => |
|
420 |
let val kind = Token.kind_of tok in |
|
421 |
member (op =) argument_kinds kind orelse |
|
422 |
Token.keyword_with is_symid tok orelse |
|
423 |
(blk andalso Token.keyword_with (fn s => s = ",") tok) |
|
424 |
end)); |
|
425 |
||
426 |
fun args blk x = Scan.optional (args1 blk) [] x |
|
427 |
and args1 blk x = |
|
428 |
((Scan.repeat1 |
|
429 |
(Scan.repeat1 (argument blk) || |
|
430 |
argsp "(" ")" || |
|
431 |
argsp "[" "]")) >> flat) x |
|
432 |
and argsp l r x = (token ($$$ l) ::: !!! (args true @@@ (token ($$$ r) >> single))) x; |
|
433 |
in (args, args1) end; |
|
434 |
||
435 |
in |
|
436 |
||
437 |
val args = #1 (arguments Token.ident_or_symbolic) false; |
|
438 |
fun args1 is_symid = #2 (arguments is_symid) false; |
|
439 |
||
440 |
end; |
|
441 |
||
12272 | 442 |
end; |
30511 | 443 |
|
36949
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
wenzelm
parents:
35838
diff
changeset
|
444 |
type 'a parser = 'a Parse.parser; |
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
wenzelm
parents:
35838
diff
changeset
|
445 |
type 'a context_parser = 'a Parse.context_parser; |
30511 | 446 |