src/Pure/Isar/parse.ML
changeset 36949 080e85d46108
parent 35838 c8bd075c4de8
child 36955 226fb165833e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Isar/parse.ML	Sat May 15 22:24:25 2010 +0200
     1.3 @@ -0,0 +1,346 @@
     1.4 +(*  Title:      Pure/Isar/parse.ML
     1.5 +    Author:     Markus Wenzel, TU Muenchen
     1.6 +
     1.7 +Generic parsers for Isabelle/Isar outer syntax.
     1.8 +*)
     1.9 +
    1.10 +signature PARSE =
    1.11 +sig
    1.12 +  type token = OuterLex.token
    1.13 +  type 'a parser = token list -> 'a * token list
    1.14 +  type 'a context_parser = Context.generic * token list -> 'a * (Context.generic * token list)
    1.15 +  val group: string -> (token list -> 'a) -> token list -> 'a
    1.16 +  val !!! : (token list -> 'a) -> token list -> 'a
    1.17 +  val !!!! : (token list -> 'a) -> token list -> 'a
    1.18 +  val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    1.19 +  val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    1.20 +  val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
    1.21 +  val not_eof: token parser
    1.22 +  val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    1.23 +  val command: string parser
    1.24 +  val keyword: string parser
    1.25 +  val short_ident: string parser
    1.26 +  val long_ident: string parser
    1.27 +  val sym_ident: string parser
    1.28 +  val minus: string parser
    1.29 +  val term_var: string parser
    1.30 +  val type_ident: string parser
    1.31 +  val type_var: string parser
    1.32 +  val number: string parser
    1.33 +  val string: string parser
    1.34 +  val alt_string: string parser
    1.35 +  val verbatim: string parser
    1.36 +  val sync: string parser
    1.37 +  val eof: string parser
    1.38 +  val keyword_with: (string -> bool) -> string parser
    1.39 +  val keyword_ident_or_symbolic: string parser
    1.40 +  val $$$ : string -> string parser
    1.41 +  val reserved: string -> string parser
    1.42 +  val semicolon: string parser
    1.43 +  val underscore: string parser
    1.44 +  val maybe: 'a parser -> 'a option parser
    1.45 +  val tag_name: string parser
    1.46 +  val tags: string list parser
    1.47 +  val opt_unit: unit parser
    1.48 +  val opt_keyword: string -> bool parser
    1.49 +  val begin: string parser
    1.50 +  val opt_begin: bool parser
    1.51 +  val nat: int parser
    1.52 +  val int: int parser
    1.53 +  val enum: string -> 'a parser -> 'a list parser
    1.54 +  val enum1: string -> 'a parser -> 'a list parser
    1.55 +  val and_list: 'a parser -> 'a list parser
    1.56 +  val and_list1: 'a parser -> 'a list parser
    1.57 +  val enum': string -> 'a context_parser -> 'a list context_parser
    1.58 +  val enum1': string -> 'a context_parser -> 'a list context_parser
    1.59 +  val and_list': 'a context_parser -> 'a list context_parser
    1.60 +  val and_list1': 'a context_parser -> 'a list context_parser
    1.61 +  val list: 'a parser -> 'a list parser
    1.62 +  val list1: 'a parser -> 'a list parser
    1.63 +  val name: bstring parser
    1.64 +  val binding: binding parser
    1.65 +  val xname: xstring parser
    1.66 +  val text: string parser
    1.67 +  val path: Path.T parser
    1.68 +  val parname: string parser
    1.69 +  val parbinding: binding parser
    1.70 +  val sort: string parser
    1.71 +  val arity: (string * string list * string) parser
    1.72 +  val multi_arity: (string list * string list * string) parser
    1.73 +  val type_args: string list parser
    1.74 +  val type_args_constrained: (string * string option) list parser
    1.75 +  val typ_group: string parser
    1.76 +  val typ: string parser
    1.77 +  val mixfix: mixfix parser
    1.78 +  val mixfix': mixfix parser
    1.79 +  val opt_mixfix: mixfix parser
    1.80 +  val opt_mixfix': mixfix parser
    1.81 +  val where_: string parser
    1.82 +  val const: (string * string * mixfix) parser
    1.83 +  val const_binding: (binding * string * mixfix) parser
    1.84 +  val params: (binding * string option) list parser
    1.85 +  val simple_fixes: (binding * string option) list parser
    1.86 +  val fixes: (binding * string option * mixfix) list parser
    1.87 +  val for_fixes: (binding * string option * mixfix) list parser
    1.88 +  val for_simple_fixes: (binding * string option) list parser
    1.89 +  val ML_source: (Symbol_Pos.text * Position.T) parser
    1.90 +  val doc_source: (Symbol_Pos.text * Position.T) parser
    1.91 +  val term_group: string parser
    1.92 +  val prop_group: string parser
    1.93 +  val term: string parser
    1.94 +  val prop: string parser
    1.95 +  val propp: (string * string list) parser
    1.96 +  val termp: (string * string list) parser
    1.97 +  val target: xstring parser
    1.98 +  val opt_target: xstring option parser
    1.99 +end;
   1.100 +
   1.101 +structure Parse: PARSE =
   1.102 +struct
   1.103 +
   1.104 +structure T = OuterLex;
   1.105 +type token = T.token;
   1.106 +
   1.107 +type 'a parser = token list -> 'a * token list;
   1.108 +type 'a context_parser = Context.generic * token list -> 'a * (Context.generic * token list);
   1.109 +
   1.110 +
   1.111 +(** error handling **)
   1.112 +
   1.113 +(* group atomic parsers (no cuts!) *)
   1.114 +
   1.115 +fun fail_with s = Scan.fail_with
   1.116 +  (fn [] => s ^ " expected (past end-of-file!)"
   1.117 +    | (tok :: _) =>
   1.118 +        (case T.text_of tok of
   1.119 +          (txt, "") => s ^ " expected,\nbut " ^ txt ^ T.pos_of tok ^ " was found"
   1.120 +        | (txt1, txt2) => s ^ " expected,\nbut " ^ txt1 ^ T.pos_of tok ^ " was found:\n" ^ txt2));
   1.121 +
   1.122 +fun group s scan = scan || fail_with s;
   1.123 +
   1.124 +
   1.125 +(* cut *)
   1.126 +
   1.127 +fun cut kind scan =
   1.128 +  let
   1.129 +    fun get_pos [] = " (past end-of-file!)"
   1.130 +      | get_pos (tok :: _) = T.pos_of tok;
   1.131 +
   1.132 +    fun err (toks, NONE) = kind ^ get_pos toks
   1.133 +      | err (toks, SOME msg) =
   1.134 +          if String.isPrefix kind msg then msg
   1.135 +          else kind ^ get_pos toks ^ ": " ^ msg;
   1.136 +  in Scan.!! err scan end;
   1.137 +
   1.138 +fun !!! scan = cut "Outer syntax error" scan;
   1.139 +fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   1.140 +
   1.141 +
   1.142 +
   1.143 +(** basic parsers **)
   1.144 +
   1.145 +(* utils *)
   1.146 +
   1.147 +fun triple1 ((x, y), z) = (x, y, z);
   1.148 +fun triple2 (x, (y, z)) = (x, y, z);
   1.149 +fun triple_swap ((x, y), z) = ((x, z), y);
   1.150 +
   1.151 +
   1.152 +(* tokens *)
   1.153 +
   1.154 +fun RESET_VALUE atom = (*required for all primitive parsers*)
   1.155 +  Scan.ahead (Scan.one (K true)) -- atom >> (fn (arg, x) => (T.assign NONE arg; x));
   1.156 +
   1.157 +
   1.158 +val not_eof = RESET_VALUE (Scan.one T.not_eof);
   1.159 +
   1.160 +fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
   1.161 +fun source_position atom = Scan.ahead atom |-- not_eof >> T.source_position_of;
   1.162 +fun inner_syntax atom = Scan.ahead atom |-- not_eof >> T.source_of;
   1.163 +
   1.164 +fun kind k =
   1.165 +  group (T.str_of_kind k) (RESET_VALUE (Scan.one (T.is_kind k) >> T.content_of));
   1.166 +
   1.167 +val command = kind T.Command;
   1.168 +val keyword = kind T.Keyword;
   1.169 +val short_ident = kind T.Ident;
   1.170 +val long_ident = kind T.LongIdent;
   1.171 +val sym_ident = kind T.SymIdent;
   1.172 +val term_var = kind T.Var;
   1.173 +val type_ident = kind T.TypeIdent;
   1.174 +val type_var = kind T.TypeVar;
   1.175 +val number = kind T.Nat;
   1.176 +val string = kind T.String;
   1.177 +val alt_string = kind T.AltString;
   1.178 +val verbatim = kind T.Verbatim;
   1.179 +val sync = kind T.Sync;
   1.180 +val eof = kind T.EOF;
   1.181 +
   1.182 +fun keyword_with pred = RESET_VALUE (Scan.one (T.keyword_with pred) >> T.content_of);
   1.183 +val keyword_ident_or_symbolic = keyword_with T.ident_or_symbolic;
   1.184 +
   1.185 +fun $$$ x =
   1.186 +  group (T.str_of_kind T.Keyword ^ " " ^ quote x) (keyword_with (fn y => x = y));
   1.187 +
   1.188 +fun reserved x =
   1.189 +  group ("reserved identifier " ^ quote x)
   1.190 +    (RESET_VALUE (Scan.one (T.ident_with (fn y => x = y)) >> T.content_of));
   1.191 +
   1.192 +val semicolon = $$$ ";";
   1.193 +
   1.194 +val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
   1.195 +val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   1.196 +fun maybe scan = underscore >> K NONE || scan >> SOME;
   1.197 +
   1.198 +val nat = number >> (#1 o Library.read_int o Symbol.explode);
   1.199 +val int = Scan.optional (minus >> K ~1) 1 -- nat >> op *;
   1.200 +
   1.201 +val tag_name = group "tag name" (short_ident || string);
   1.202 +val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
   1.203 +
   1.204 +val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   1.205 +fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   1.206 +
   1.207 +val begin = $$$ "begin";
   1.208 +val opt_begin = Scan.optional (begin >> K true) false;
   1.209 +
   1.210 +
   1.211 +(* enumerations *)
   1.212 +
   1.213 +fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
   1.214 +fun enum sep scan = enum1 sep scan || Scan.succeed [];
   1.215 +
   1.216 +fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
   1.217 +fun enum' sep scan = enum1' sep scan || Scan.succeed [];
   1.218 +
   1.219 +fun and_list1 scan = enum1 "and" scan;
   1.220 +fun and_list scan = enum "and" scan;
   1.221 +
   1.222 +fun and_list1' scan = enum1' "and" scan;
   1.223 +fun and_list' scan = enum' "and" scan;
   1.224 +
   1.225 +fun list1 scan = enum1 "," scan;
   1.226 +fun list scan = enum "," scan;
   1.227 +
   1.228 +
   1.229 +(* names and text *)
   1.230 +
   1.231 +val name = group "name declaration" (short_ident || sym_ident || string || number);
   1.232 +val binding = position name >> Binding.make;
   1.233 +val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   1.234 +val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   1.235 +val path = group "file name/path specification" name >> Path.explode;
   1.236 +
   1.237 +val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
   1.238 +val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty;
   1.239 +
   1.240 +
   1.241 +(* sorts and arities *)
   1.242 +
   1.243 +val sort = group "sort" (inner_syntax xname);
   1.244 +
   1.245 +val arity = xname -- ($$$ "::" |-- !!!
   1.246 +  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
   1.247 +
   1.248 +val multi_arity = and_list1 xname -- ($$$ "::" |-- !!!
   1.249 +  (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
   1.250 +
   1.251 +
   1.252 +(* types *)
   1.253 +
   1.254 +val typ_group = group "type"
   1.255 +  (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   1.256 +
   1.257 +val typ = inner_syntax typ_group;
   1.258 +
   1.259 +fun type_arguments arg =
   1.260 +  arg >> single ||
   1.261 +  $$$ "(" |-- !!! (list1 arg --| $$$ ")") ||
   1.262 +  Scan.succeed [];
   1.263 +
   1.264 +val type_args = type_arguments type_ident;
   1.265 +val type_args_constrained = type_arguments (type_ident -- Scan.option ($$$ "::" |-- !!! sort));
   1.266 +
   1.267 +
   1.268 +(* mixfix annotations *)
   1.269 +
   1.270 +val mfix = string --
   1.271 +  !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
   1.272 +    Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
   1.273 +
   1.274 +val infx = $$$ "infix" |-- !!! (string -- nat >> Infix);
   1.275 +val infxl = $$$ "infixl" |-- !!! (string -- nat >> Infixl);
   1.276 +val infxr = $$$ "infixr" |-- !!! (string -- nat >> Infixr);
   1.277 +
   1.278 +val binder = $$$ "binder" |--
   1.279 +  !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   1.280 +  >> (Binder o triple2);
   1.281 +
   1.282 +fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   1.283 +fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
   1.284 +
   1.285 +val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
   1.286 +val mixfix' = annotation I (mfix || binder || infxl || infxr || infx);
   1.287 +val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
   1.288 +val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
   1.289 +
   1.290 +
   1.291 +(* fixes *)
   1.292 +
   1.293 +val where_ = $$$ "where";
   1.294 +
   1.295 +val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   1.296 +val const_binding = binding -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   1.297 +
   1.298 +val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ)
   1.299 +  >> (fn (xs, T) => map (rpair T) xs);
   1.300 +
   1.301 +val simple_fixes = and_list1 params >> flat;
   1.302 +
   1.303 +val fixes =
   1.304 +  and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
   1.305 +    params >> map Syntax.no_syn) >> flat;
   1.306 +
   1.307 +val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
   1.308 +val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) [];
   1.309 +
   1.310 +
   1.311 +(* embedded source text *)
   1.312 +
   1.313 +val ML_source = source_position (group "ML source" text);
   1.314 +val doc_source = source_position (group "document source" text);
   1.315 +
   1.316 +
   1.317 +(* terms *)
   1.318 +
   1.319 +val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   1.320 +
   1.321 +val term_group = group "term" trm;
   1.322 +val prop_group = group "proposition" trm;
   1.323 +
   1.324 +val term = inner_syntax term_group;
   1.325 +val prop = inner_syntax prop_group;
   1.326 +
   1.327 +
   1.328 +(* patterns *)
   1.329 +
   1.330 +val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   1.331 +val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   1.332 +
   1.333 +val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   1.334 +val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   1.335 +
   1.336 +
   1.337 +(* targets *)
   1.338 +
   1.339 +val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
   1.340 +val opt_target = Scan.option target;
   1.341 +
   1.342 +end;
   1.343 +
   1.344 +type 'a parser = 'a Parse.parser;
   1.345 +type 'a context_parser = 'a Parse.context_parser;
   1.346 +
   1.347 +(*legacy alias*)
   1.348 +structure OuterParse = Parse;
   1.349 +