| author | wenzelm | 
| Sun, 01 Apr 2012 15:23:43 +0200 | |
| changeset 47246 | 2bbab021c0e6 | 
| parent 46922 | 3717f3878714 | 
| child 48881 | 46e053eda5dd | 
| 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: 
35838diff
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: 
35838diff
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: 
36955diff
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: 
36955diff
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: 
36955diff
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: 
36955diff
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: 
36955diff
changeset | 17 | val not_eof: Token.T parser | 
| 42326 | 18 |   val position: 'a parser -> ('a * Position.T) parser
 | 
| 42657 | 19 | val source_position: 'a parser -> (Symbol_Pos.text * Position.T) parser | 
| 42326 | 20 | val inner_syntax: 'a parser -> string parser | 
| 29310 | 21 | val command: string parser | 
| 22 | val keyword: string parser | |
| 23 | val short_ident: string parser | |
| 24 | val long_ident: string parser | |
| 25 | val sym_ident: string parser | |
| 26 | val minus: string parser | |
| 27 | val term_var: string parser | |
| 28 | val type_ident: string parser | |
| 29 | val type_var: string parser | |
| 30 | val number: string parser | |
| 40290 
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
 wenzelm parents: 
36959diff
changeset | 31 | val float_number: string parser | 
| 29310 | 32 | val string: string parser | 
| 33 | val alt_string: string parser | |
| 34 | val verbatim: string parser | |
| 35 | val sync: string parser | |
| 36 | val eof: string parser | |
| 37 | val keyword_with: (string -> bool) -> string parser | |
| 38 | val keyword_ident_or_symbolic: string parser | |
| 39 | val $$$ : string -> string parser | |
| 40 | val reserved: string -> string parser | |
| 41 | val semicolon: string parser | |
| 42 | val underscore: string parser | |
| 43 | val maybe: 'a parser -> 'a option parser | |
| 44 | val tag_name: string parser | |
| 45 | val tags: string list parser | |
| 46 | val opt_unit: unit parser | |
| 47 | val opt_keyword: string -> bool parser | |
| 48 | val begin: string parser | |
| 49 | val opt_begin: bool parser | |
| 50 | val nat: int parser | |
| 51 | val int: int parser | |
| 40290 
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
 wenzelm parents: 
36959diff
changeset | 52 | val real: real parser | 
| 29310 | 53 | val enum: string -> 'a parser -> 'a list parser | 
| 54 | val enum1: string -> 'a parser -> 'a list parser | |
| 55 | val and_list: 'a parser -> 'a list parser | |
| 56 | val and_list1: 'a parser -> 'a list parser | |
| 30511 | 57 | val enum': string -> 'a context_parser -> 'a list context_parser | 
| 58 | val enum1': string -> 'a context_parser -> 'a list context_parser | |
| 59 | val and_list': 'a context_parser -> 'a list context_parser | |
| 60 | val and_list1': 'a context_parser -> 'a list context_parser | |
| 29310 | 61 | val list: 'a parser -> 'a list parser | 
| 62 | 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: 
42657diff
changeset | 63 | val properties: Properties.T parser | 
| 29310 | 64 | val name: bstring parser | 
| 29581 | 65 | val binding: binding parser | 
| 29310 | 66 | val xname: xstring parser | 
| 67 | val text: string parser | |
| 68 | val path: Path.T parser | |
| 40800 
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
 wenzelm parents: 
40793diff
changeset | 69 | val liberal_name: xstring parser | 
| 29310 | 70 | val parname: string parser | 
| 29581 | 71 | val parbinding: binding parser | 
| 46922 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 72 | val class: string parser | 
| 29310 | 73 | val sort: string parser | 
| 46922 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 74 | val type_const: string parser | 
| 29310 | 75 | val arity: (string * string list * string) parser | 
| 76 | val multi_arity: (string list * string list * string) parser | |
| 77 | val type_args: string list parser | |
| 35838 | 78 | val type_args_constrained: (string * string option) list parser | 
| 29310 | 79 | val typ_group: string parser | 
| 80 | val typ: string parser | |
| 81 | val mixfix: mixfix parser | |
| 82 | val mixfix': mixfix parser | |
| 83 | val opt_mixfix: mixfix parser | |
| 84 | val opt_mixfix': mixfix parser | |
| 85 | val where_: string parser | |
| 42299 | 86 | val const_decl: (string * string * mixfix) parser | 
| 30339 | 87 | val const_binding: (binding * string * mixfix) parser | 
| 29581 | 88 | val params: (binding * string option) list parser | 
| 89 | val simple_fixes: (binding * string option) list parser | |
| 90 | val fixes: (binding * string option * mixfix) list parser | |
| 91 | val for_fixes: (binding * string option * mixfix) list parser | |
| 30573 | 92 | val ML_source: (Symbol_Pos.text * Position.T) parser | 
| 93 | val doc_source: (Symbol_Pos.text * Position.T) parser | |
| 29310 | 94 | val term_group: string parser | 
| 95 | val prop_group: string parser | |
| 96 | val term: string parser | |
| 97 | val prop: string parser | |
| 42300 
0d1cbc1fe579
notation: proper markup for type constructor / constant;
 wenzelm parents: 
42299diff
changeset | 98 | val const: string parser | 
| 40793 
d21aedaa91e7
added Parse.literal_fact with proper inner_syntax markup (source position);
 wenzelm parents: 
40296diff
changeset | 99 | val literal_fact: string parser | 
| 29310 | 100 | val propp: (string * string list) parser | 
| 101 | val termp: (string * string list) parser | |
| 45488 
6d71d9e52369
pass positions for named targets, for formal links in the document model;
 wenzelm parents: 
45331diff
changeset | 102 | val target: (xstring * Position.T) parser | 
| 
6d71d9e52369
pass positions for named targets, for formal links in the document model;
 wenzelm parents: 
45331diff
changeset | 103 | val opt_target: (xstring * Position.T) option parser | 
| 5826 | 104 | end; | 
| 105 | ||
| 36949 
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
 wenzelm parents: 
35838diff
changeset | 106 | structure Parse: PARSE = | 
| 5826 | 107 | struct | 
| 108 | ||
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 109 | 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: 
36955diff
changeset | 110 | type 'a context_parser = Context.generic * Token.T list -> 'a * (Context.generic * Token.T list); | 
| 29310 | 111 | |
| 5826 | 112 | |
| 113 | (** error handling **) | |
| 114 | ||
| 115 | (* group atomic parsers (no cuts!) *) | |
| 116 | ||
| 44357 | 117 | fun group s scan = scan || Scan.fail_with | 
| 118 | (fn [] => (fn () => s () ^ " expected (past end-of-file!)") | |
| 42519 | 119 | | 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: 
43775diff
changeset | 120 | (fn () => | 
| 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
43775diff
changeset | 121 | (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: 
43775diff
changeset | 122 | (txt, "") => | 
| 44357 | 123 | s () ^ " expected,\nbut " ^ txt ^ Token.pos_of tok ^ " 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: 
43775diff
changeset | 124 | | (txt1, txt2) => | 
| 44357 | 125 | s () ^ " expected,\nbut " ^ txt1 ^ Token.pos_of tok ^ " was found:\n" ^ txt2))); | 
| 5826 | 126 | |
| 127 | ||
| 5877 | 128 | (* cut *) | 
| 5826 | 129 | |
| 8581 
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
 wenzelm parents: 
8350diff
changeset | 130 | fun cut kind scan = | 
| 5826 | 131 | let | 
| 132 | fun get_pos [] = " (past end-of-file!)" | |
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 133 | | get_pos (tok :: _) = Token.pos_of tok; | 
| 5826 | 134 | |
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
43775diff
changeset | 135 | fun err (toks, NONE) = (fn () => kind ^ get_pos toks) | 
| 25625 | 136 | | 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: 
43775diff
changeset | 137 | (fn () => | 
| 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
43775diff
changeset | 138 | 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: 
43775diff
changeset | 139 | 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: 
43775diff
changeset | 140 | 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: 
43775diff
changeset | 141 | end); | 
| 5826 | 142 | in Scan.!! err scan end; | 
| 143 | ||
| 8586 | 144 | fun !!! scan = cut "Outer syntax error" scan; | 
| 145 | fun !!!! scan = cut "Corrupted outer syntax in presentation" scan; | |
| 8581 
5c7ed2af8bfb
!!!! = cut "Corrupted outer syntax in presentation";
 wenzelm parents: 
8350diff
changeset | 146 | |
| 5826 | 147 | |
| 148 | ||
| 149 | (** basic parsers **) | |
| 150 | ||
| 151 | (* utils *) | |
| 152 | ||
| 153 | fun triple1 ((x, y), z) = (x, y, z); | |
| 154 | fun triple2 (x, (y, z)) = (x, y, z); | |
| 6430 | 155 | fun triple_swap ((x, y), z) = ((x, z), y); | 
| 5826 | 156 | |
| 157 | ||
| 158 | (* tokens *) | |
| 159 | ||
| 27815 | 160 | 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: 
36955diff
changeset | 161 | Scan.ahead (Scan.one (K true)) -- atom >> (fn (arg, x) => (Token.assign NONE arg; x)); | 
| 27815 | 162 | |
| 163 | ||
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 164 | val not_eof = RESET_VALUE (Scan.one Token.not_eof); | 
| 15703 | 165 | |
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 166 | fun position scan = (Scan.ahead not_eof >> Token.position_of) -- scan >> Library.swap; | 
| 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 167 | fun source_position atom = Scan.ahead atom |-- not_eof >> Token.source_position_of; | 
| 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 168 | fun inner_syntax atom = Scan.ahead atom |-- not_eof >> Token.source_of; | 
| 5826 | 169 | |
| 170 | fun kind k = | |
| 44357 | 171 | group (fn () => Token.str_of_kind k) | 
| 172 | (RESET_VALUE (Scan.one (Token.is_kind k) >> Token.content_of)); | |
| 5826 | 173 | |
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 174 | 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: 
36955diff
changeset | 175 | 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: 
36955diff
changeset | 176 | 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: 
36955diff
changeset | 177 | 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: 
36955diff
changeset | 178 | 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: 
36955diff
changeset | 179 | 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: 
36955diff
changeset | 180 | 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: 
36955diff
changeset | 181 | 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: 
36955diff
changeset | 182 | val number = kind Token.Nat; | 
| 40290 
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
 wenzelm parents: 
36959diff
changeset | 183 | 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: 
36955diff
changeset | 184 | 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: 
36955diff
changeset | 185 | 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: 
36955diff
changeset | 186 | val verbatim = kind Token.Verbatim; | 
| 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 187 | 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: 
36955diff
changeset | 188 | val eof = kind Token.EOF; | 
| 5826 | 189 | |
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 190 | fun keyword_with pred = RESET_VALUE (Scan.one (Token.keyword_with pred) >> Token.content_of); | 
| 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36955diff
changeset | 191 | val keyword_ident_or_symbolic = keyword_with Token.ident_or_symbolic; | 
| 27815 | 192 | |
| 5826 | 193 | fun $$$ x = | 
| 44357 | 194 | group (fn () => Token.str_of_kind Token.Keyword ^ " " ^ quote x) | 
| 195 | (keyword_with (fn y => x = y)); | |
| 9131 | 196 | |
| 16030 | 197 | fun reserved x = | 
| 44357 | 198 | 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: 
36955diff
changeset | 199 | (RESET_VALUE (Scan.one (Token.ident_with (fn y => x = y)) >> Token.content_of)); | 
| 16030 | 200 | |
| 9131 | 201 | val semicolon = $$$ ";"; | 
| 5826 | 202 | |
| 15703 | 203 | val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1; | 
| 11792 
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
 wenzelm parents: 
11651diff
changeset | 204 | val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1; | 
| 15703 | 205 | fun maybe scan = underscore >> K NONE || scan >> SOME; | 
| 11792 
311eee3d63b6
parser for underscore (actually a symbolic identifier!);
 wenzelm parents: 
11651diff
changeset | 206 | |
| 14835 | 207 | val nat = number >> (#1 o Library.read_int o Symbol.explode); | 
| 27815 | 208 | val int = Scan.optional (minus >> K ~1) 1 -- nat >> op *; | 
| 40296 | 209 | val real = float_number >> (the o Real.fromString) || int >> Real.fromInt; | 
| 5826 | 210 | |
| 44357 | 211 | val tag_name = group (fn () => "tag name") (short_ident || string); | 
| 17070 | 212 | val tags = Scan.repeat ($$$ "%" |-- !!! tag_name); | 
| 213 | ||
| 7930 | 214 | val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
 | 
| 14646 | 215 | fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
 | 
| 216 | ||
| 20983 | 217 | val begin = $$$ "begin"; | 
| 218 | val opt_begin = Scan.optional (begin >> K true) false; | |
| 20961 | 219 | |
| 5826 | 220 | |
| 221 | (* enumerations *) | |
| 222 | ||
| 25999 | 223 | fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan); | 
| 5826 | 224 | fun enum sep scan = enum1 sep scan || Scan.succeed []; | 
| 225 | ||
| 27815 | 226 | fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan); | 
| 227 | fun enum' sep scan = enum1' sep scan || Scan.succeed []; | |
| 5826 | 228 | |
| 6013 | 229 | fun and_list1 scan = enum1 "and" scan; | 
| 230 | fun and_list scan = enum "and" scan; | |
| 231 | ||
| 27815 | 232 | fun and_list1' scan = enum1' "and" scan; | 
| 233 | fun and_list' scan = enum' "and" scan; | |
| 234 | ||
| 235 | fun list1 scan = enum1 "," scan; | |
| 236 | fun list scan = enum "," scan; | |
| 237 | ||
| 43775 
b361c7d184e7
added Parse.properties (again) -- allow empty list like Parse_Value.properties but unlike Parse.properties of ef86de9c98aa;
 wenzelm parents: 
42657diff
changeset | 238 | 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: 
42657diff
changeset | 239 | |
| 5826 | 240 | |
| 5960 | 241 | (* names and text *) | 
| 5826 | 242 | |
| 44357 | 243 | val name = group (fn () => "name declaration") (short_ident || sym_ident || string || number); | 
| 244 | ||
| 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: 
29581diff
changeset | 245 | val binding = position name >> Binding.make; | 
| 44357 | 246 | |
| 247 | val xname = group (fn () => "name reference") | |
| 248 | (short_ident || long_ident || sym_ident || string || number); | |
| 249 | ||
| 250 | val text = group (fn () => "text") | |
| 251 | (short_ident || long_ident || sym_ident || string || number || verbatim); | |
| 252 | ||
| 253 | val path = group (fn () => "file name/path specification") name >> Path.explode; | |
| 6553 | 254 | |
| 40800 
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
 wenzelm parents: 
40793diff
changeset | 255 | val liberal_name = keyword_ident_or_symbolic || xname; | 
| 
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
 wenzelm parents: 
40793diff
changeset | 256 | |
| 18898 | 257 | val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
 | 
| 28965 | 258 | val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty;
 | 
| 18898 | 259 | |
| 6553 | 260 | |
| 46922 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 261 | (* type classes *) | 
| 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 262 | |
| 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 263 | val class = group (fn () => "type class") (inner_syntax xname); | 
| 5826 | 264 | |
| 44357 | 265 | val sort = group (fn () => "sort") (inner_syntax xname); | 
| 5826 | 266 | |
| 46922 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 267 | val type_const = inner_syntax (group (fn () => "type constructor") xname); | 
| 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 268 | |
| 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 269 | val arity = type_const -- ($$$ "::" |-- !!! | 
| 22331 | 270 |   (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
 | 
| 5826 | 271 | |
| 46922 
3717f3878714
source positions for locale and class expressions;
 wenzelm parents: 
45596diff
changeset | 272 | val multi_arity = and_list1 type_const -- ($$$ "::" |-- !!! | 
| 25541 | 273 |   (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
 | 
| 274 | ||
| 5826 | 275 | |
| 276 | (* types *) | |
| 277 | ||
| 44357 | 278 | val typ_group = | 
| 279 | group (fn () => "type") | |
| 280 | (short_ident || long_ident || sym_ident || type_ident || type_var || string || number); | |
| 5826 | 281 | |
| 27753 
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
 wenzelm parents: 
27737diff
changeset | 282 | val typ = inner_syntax typ_group; | 
| 
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
 wenzelm parents: 
27737diff
changeset | 283 | |
| 35838 | 284 | fun type_arguments arg = | 
| 285 | arg >> single || | |
| 286 |   $$$ "(" |-- !!! (list1 arg --| $$$ ")") ||
 | |
| 5826 | 287 | Scan.succeed []; | 
| 288 | ||
| 35838 | 289 | val type_args = type_arguments type_ident; | 
| 290 | val type_args_constrained = type_arguments (type_ident -- Scan.option ($$$ "::" |-- !!! sort)); | |
| 291 | ||
| 5826 | 292 | |
| 293 | (* mixfix annotations *) | |
| 294 | ||
| 18669 | 295 | val mfix = string -- | 
| 296 | !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] -- | |
| 42297 
140f283266b7
discontinued Syntax.max_pri, which is not really a symbolic parameter;
 wenzelm parents: 
42287diff
changeset | 297 | Scan.optional nat 1000) >> (Mixfix o triple2); | 
| 18669 | 298 | |
| 35130 | 299 | val infx = $$$ "infix" |-- !!! (string -- nat >> Infix); | 
| 300 | val infxl = $$$ "infixl" |-- !!! (string -- nat >> Infixl); | |
| 301 | val infxr = $$$ "infixr" |-- !!! (string -- nat >> Infixr); | |
| 5826 | 302 | |
| 18669 | 303 | val binder = $$$ "binder" |-- | 
| 304 | !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n)))) | |
| 305 | >> (Binder o triple2); | |
| 306 | ||
| 307 | fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
 | |
| 308 | fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn; | |
| 309 | ||
| 310 | val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx); | |
| 21609 | 311 | val mixfix' = annotation I (mfix || binder || infxl || infxr || infx); | 
| 18669 | 312 | val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx); | 
| 313 | val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx); | |
| 5826 | 314 | |
| 315 | ||
| 18669 | 316 | (* fixes *) | 
| 5826 | 317 | |
| 21400 | 318 | val where_ = $$$ "where"; | 
| 319 | ||
| 42299 | 320 | val const_decl = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1; | 
| 30339 | 321 | val const_binding = binding -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1; | 
| 18669 | 322 | |
| 28081 
d664b2c1dfe6
explicit type Name.binding for higher-specification elements;
 wenzelm parents: 
28017diff
changeset | 323 | val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ) | 
| 18669 | 324 | >> (fn (xs, T) => map (rpair T) xs); | 
| 325 | ||
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19284diff
changeset | 326 | val simple_fixes = and_list1 params >> flat; | 
| 18669 | 327 | |
| 328 | val fixes = | |
| 45331 
6e0a8aba99ec
more liberal Parse.fixes, to avoid overlap of mixfix with is-pattern (notably in 'obtain' syntax);
 wenzelm parents: 
44357diff
changeset | 329 | and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix' >> (single o triple1) || | 
| 42287 
d98eb048a2e4
discontinued special treatment of structure Mixfix;
 wenzelm parents: 
40800diff
changeset | 330 | params >> map (fn (x, y) => (x, y, NoSyn))) >> flat; | 
| 5826 | 331 | |
| 19845 | 332 | val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) []; | 
| 333 | ||
| 5826 | 334 | |
| 27877 | 335 | (* embedded source text *) | 
| 27872 
631371a02b8c
P.doc_source and P.ml_sorce for proper SymbolPos.text;
 wenzelm parents: 
27815diff
changeset | 336 | |
| 44357 | 337 | val ML_source = source_position (group (fn () => "ML source") text); | 
| 338 | val doc_source = source_position (group (fn () => "document source") text); | |
| 27872 
631371a02b8c
P.doc_source and P.ml_sorce for proper SymbolPos.text;
 wenzelm parents: 
27815diff
changeset | 339 | |
| 
631371a02b8c
P.doc_source and P.ml_sorce for proper SymbolPos.text;
 wenzelm parents: 
27815diff
changeset | 340 | |
| 5826 | 341 | (* terms *) | 
| 342 | ||
| 40793 
d21aedaa91e7
added Parse.literal_fact with proper inner_syntax markup (source position);
 wenzelm parents: 
40296diff
changeset | 343 | val tm = short_ident || long_ident || sym_ident || term_var || number || string; | 
| 5826 | 344 | |
| 44357 | 345 | val term_group = group (fn () => "term") tm; | 
| 346 | val prop_group = group (fn () => "proposition") tm; | |
| 27753 
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
 wenzelm parents: 
27737diff
changeset | 347 | |
| 
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
 wenzelm parents: 
27737diff
changeset | 348 | val term = inner_syntax term_group; | 
| 
94b672153b49
sort/typ/term/prop: inner_syntax markup encodes original source position;
 wenzelm parents: 
27737diff
changeset | 349 | val prop = inner_syntax prop_group; | 
| 5826 | 350 | |
| 44357 | 351 | val const = inner_syntax (group (fn () => "constant") xname); | 
| 42300 
0d1cbc1fe579
notation: proper markup for type constructor / constant;
 wenzelm parents: 
42299diff
changeset | 352 | |
| 44357 | 353 | 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: 
40296diff
changeset | 354 | |
| 5826 | 355 | |
| 6949 | 356 | (* patterns *) | 
| 6935 | 357 | |
| 6949 | 358 | val is_terms = Scan.repeat1 ($$$ "is" |-- term); | 
| 6935 | 359 | val is_props = Scan.repeat1 ($$$ "is" |-- prop); | 
| 360 | ||
| 19585 | 361 | val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
 | 
| 6949 | 362 | val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
 | 
| 6935 | 363 | |
| 364 | ||
| 22119 | 365 | (* targets *) | 
| 19811 | 366 | |
| 45488 
6d71d9e52369
pass positions for named targets, for formal links in the document model;
 wenzelm parents: 
45331diff
changeset | 367 | val target = ($$$ "(" -- $$$ "in") |-- !!! (position xname --| $$$ ")");
 | 
| 22119 | 368 | val opt_target = Scan.option target; | 
| 12272 | 369 | |
| 370 | end; | |
| 30511 | 371 | |
| 36949 
080e85d46108
renamed structure OuterKeyword to Keyword and OuterParse to Parse, keeping the old names as legacy aliases for some time;
 wenzelm parents: 
35838diff
changeset | 372 | 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: 
35838diff
changeset | 373 | type 'a context_parser = 'a Parse.context_parser; | 
| 30511 | 374 |