src/Pure/Isar/outer_parse.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 35838 c8bd075c4de8
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
     1 (*  Title:      Pure/Isar/outer_parse.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Generic parsers for Isabelle/Isar outer syntax.
     5 *)
     6 
     7 signature OUTER_PARSE =
     8 sig
     9   type token = OuterLex.token
    10   type 'a parser = token list -> 'a * token list
    11   type 'a context_parser = Context.generic * token list -> 'a * (Context.generic * token list)
    12   val group: string -> (token list -> 'a) -> token list -> 'a
    13   val !!! : (token list -> 'a) -> token list -> 'a
    14   val !!!! : (token list -> 'a) -> token list -> 'a
    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
    18   val not_eof: token parser
    19   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    20   val command: string parser
    21   val keyword: string parser
    22   val short_ident: string parser
    23   val long_ident: string parser
    24   val sym_ident: string parser
    25   val minus: string parser
    26   val term_var: string parser
    27   val type_ident: string parser
    28   val type_var: string parser
    29   val number: string parser
    30   val string: string parser
    31   val alt_string: string parser
    32   val verbatim: string parser
    33   val sync: string parser
    34   val eof: string parser
    35   val keyword_with: (string -> bool) -> string parser
    36   val keyword_ident_or_symbolic: string parser
    37   val $$$ : string -> string parser
    38   val reserved: string -> string parser
    39   val semicolon: string parser
    40   val underscore: string parser
    41   val maybe: 'a parser -> 'a option parser
    42   val tag_name: string parser
    43   val tags: string list parser
    44   val opt_unit: unit parser
    45   val opt_keyword: string -> bool parser
    46   val begin: string parser
    47   val opt_begin: bool parser
    48   val nat: int parser
    49   val int: int parser
    50   val enum: string -> 'a parser -> 'a list parser
    51   val enum1: string -> 'a parser -> 'a list parser
    52   val and_list: 'a parser -> 'a list parser
    53   val and_list1: 'a parser -> 'a list parser
    54   val enum': string -> 'a context_parser -> 'a list context_parser
    55   val enum1': string -> 'a context_parser -> 'a list context_parser
    56   val and_list': 'a context_parser -> 'a list context_parser
    57   val and_list1': 'a context_parser -> 'a list context_parser
    58   val list: 'a parser -> 'a list parser
    59   val list1: 'a parser -> 'a list parser
    60   val name: bstring parser
    61   val binding: binding parser
    62   val xname: xstring parser
    63   val text: string parser
    64   val path: Path.T parser
    65   val parname: string parser
    66   val parbinding: binding parser
    67   val sort: string parser
    68   val arity: (string * string list * string) parser
    69   val multi_arity: (string list * string list * string) parser
    70   val type_args: string list parser
    71   val type_args_constrained: (string * string option) list parser
    72   val typ_group: string parser
    73   val typ: string parser
    74   val mixfix: mixfix parser
    75   val mixfix': mixfix parser
    76   val opt_mixfix: mixfix parser
    77   val opt_mixfix': mixfix parser
    78   val where_: string parser
    79   val const: (string * string * mixfix) parser
    80   val const_binding: (binding * string * mixfix) parser
    81   val params: (binding * string option) list parser
    82   val simple_fixes: (binding * string option) list parser
    83   val fixes: (binding * string option * mixfix) list parser
    84   val for_fixes: (binding * string option * mixfix) list parser
    85   val for_simple_fixes: (binding * string option) list parser
    86   val ML_source: (Symbol_Pos.text * Position.T) parser
    87   val doc_source: (Symbol_Pos.text * Position.T) parser
    88   val term_group: string parser
    89   val prop_group: string parser
    90   val term: string parser
    91   val prop: string parser
    92   val propp: (string * string list) parser
    93   val termp: (string * string list) parser
    94   val target: xstring parser
    95   val opt_target: xstring option parser
    96 end;
    97 
    98 structure OuterParse: OUTER_PARSE =
    99 struct
   100 
   101 structure T = OuterLex;
   102 type token = T.token;
   103 
   104 type 'a parser = token list -> 'a * token list;
   105 type 'a context_parser = Context.generic * token list -> 'a * (Context.generic * token list);
   106 
   107 
   108 (** error handling **)
   109 
   110 (* group atomic parsers (no cuts!) *)
   111 
   112 fun fail_with s = Scan.fail_with
   113   (fn [] => s ^ " expected (past end-of-file!)"
   114     | (tok :: _) =>
   115         (case T.text_of tok of
   116           (txt, "") => s ^ " expected,\nbut " ^ txt ^ T.pos_of tok ^ " was found"
   117         | (txt1, txt2) => s ^ " expected,\nbut " ^ txt1 ^ T.pos_of tok ^ " was found:\n" ^ txt2));
   118 
   119 fun group s scan = scan || fail_with s;
   120 
   121 
   122 (* cut *)
   123 
   124 fun cut kind scan =
   125   let
   126     fun get_pos [] = " (past end-of-file!)"
   127       | get_pos (tok :: _) = T.pos_of tok;
   128 
   129     fun err (toks, NONE) = kind ^ get_pos toks
   130       | err (toks, SOME msg) =
   131           if String.isPrefix kind msg then msg
   132           else kind ^ get_pos toks ^ ": " ^ msg;
   133   in Scan.!! err scan end;
   134 
   135 fun !!! scan = cut "Outer syntax error" scan;
   136 fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   137 
   138 
   139 
   140 (** basic parsers **)
   141 
   142 (* utils *)
   143 
   144 fun triple1 ((x, y), z) = (x, y, z);
   145 fun triple2 (x, (y, z)) = (x, y, z);
   146 fun triple_swap ((x, y), z) = ((x, z), y);
   147 
   148 
   149 (* tokens *)
   150 
   151 fun RESET_VALUE atom = (*required for all primitive parsers*)
   152   Scan.ahead (Scan.one (K true)) -- atom >> (fn (arg, x) => (T.assign NONE arg; x));
   153 
   154 
   155 val not_eof = RESET_VALUE (Scan.one T.not_eof);
   156 
   157 fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
   158 fun source_position atom = Scan.ahead atom |-- not_eof >> T.source_position_of;
   159 fun inner_syntax atom = Scan.ahead atom |-- not_eof >> T.source_of;
   160 
   161 fun kind k =
   162   group (T.str_of_kind k) (RESET_VALUE (Scan.one (T.is_kind k) >> T.content_of));
   163 
   164 val command = kind T.Command;
   165 val keyword = kind T.Keyword;
   166 val short_ident = kind T.Ident;
   167 val long_ident = kind T.LongIdent;
   168 val sym_ident = kind T.SymIdent;
   169 val term_var = kind T.Var;
   170 val type_ident = kind T.TypeIdent;
   171 val type_var = kind T.TypeVar;
   172 val number = kind T.Nat;
   173 val string = kind T.String;
   174 val alt_string = kind T.AltString;
   175 val verbatim = kind T.Verbatim;
   176 val sync = kind T.Sync;
   177 val eof = kind T.EOF;
   178 
   179 fun keyword_with pred = RESET_VALUE (Scan.one (T.keyword_with pred) >> T.content_of);
   180 val keyword_ident_or_symbolic = keyword_with T.ident_or_symbolic;
   181 
   182 fun $$$ x =
   183   group (T.str_of_kind T.Keyword ^ " " ^ quote x) (keyword_with (fn y => x = y));
   184 
   185 fun reserved x =
   186   group ("reserved identifier " ^ quote x)
   187     (RESET_VALUE (Scan.one (T.ident_with (fn y => x = y)) >> T.content_of));
   188 
   189 val semicolon = $$$ ";";
   190 
   191 val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
   192 val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   193 fun maybe scan = underscore >> K NONE || scan >> SOME;
   194 
   195 val nat = number >> (#1 o Library.read_int o Symbol.explode);
   196 val int = Scan.optional (minus >> K ~1) 1 -- nat >> op *;
   197 
   198 val tag_name = group "tag name" (short_ident || string);
   199 val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
   200 
   201 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   202 fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   203 
   204 val begin = $$$ "begin";
   205 val opt_begin = Scan.optional (begin >> K true) false;
   206 
   207 
   208 (* enumerations *)
   209 
   210 fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
   211 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   212 
   213 fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
   214 fun enum' sep scan = enum1' sep scan || Scan.succeed [];
   215 
   216 fun and_list1 scan = enum1 "and" scan;
   217 fun and_list scan = enum "and" scan;
   218 
   219 fun and_list1' scan = enum1' "and" scan;
   220 fun and_list' scan = enum' "and" scan;
   221 
   222 fun list1 scan = enum1 "," scan;
   223 fun list scan = enum "," scan;
   224 
   225 
   226 (* names and text *)
   227 
   228 val name = group "name declaration" (short_ident || sym_ident || string || number);
   229 val binding = position name >> Binding.make;
   230 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   231 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   232 val path = group "file name/path specification" name >> Path.explode;
   233 
   234 val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
   235 val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty;
   236 
   237 
   238 (* sorts and arities *)
   239 
   240 val sort = group "sort" (inner_syntax xname);
   241 
   242 val arity = xname -- ($$$ "::" |-- !!!
   243   (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
   244 
   245 val multi_arity = and_list1 xname -- ($$$ "::" |-- !!!
   246   (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
   247 
   248 
   249 (* types *)
   250 
   251 val typ_group = group "type"
   252   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   253 
   254 val typ = inner_syntax typ_group;
   255 
   256 fun type_arguments arg =
   257   arg >> single ||
   258   $$$ "(" |-- !!! (list1 arg --| $$$ ")") ||
   259   Scan.succeed [];
   260 
   261 val type_args = type_arguments type_ident;
   262 val type_args_constrained = type_arguments (type_ident -- Scan.option ($$$ "::" |-- !!! sort));
   263 
   264 
   265 (* mixfix annotations *)
   266 
   267 val mfix = string --
   268   !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
   269     Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
   270 
   271 val infx = $$$ "infix" |-- !!! (string -- nat >> Infix);
   272 val infxl = $$$ "infixl" |-- !!! (string -- nat >> Infixl);
   273 val infxr = $$$ "infixr" |-- !!! (string -- nat >> Infixr);
   274 
   275 val binder = $$$ "binder" |--
   276   !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   277   >> (Binder o triple2);
   278 
   279 fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   280 fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
   281 
   282 val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
   283 val mixfix' = annotation I (mfix || binder || infxl || infxr || infx);
   284 val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
   285 val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
   286 
   287 
   288 (* fixes *)
   289 
   290 val where_ = $$$ "where";
   291 
   292 val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   293 val const_binding = binding -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   294 
   295 val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ)
   296   >> (fn (xs, T) => map (rpair T) xs);
   297 
   298 val simple_fixes = and_list1 params >> flat;
   299 
   300 val fixes =
   301   and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
   302     params >> map Syntax.no_syn) >> flat;
   303 
   304 val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
   305 val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) [];
   306 
   307 
   308 (* embedded source text *)
   309 
   310 val ML_source = source_position (group "ML source" text);
   311 val doc_source = source_position (group "document source" text);
   312 
   313 
   314 (* terms *)
   315 
   316 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   317 
   318 val term_group = group "term" trm;
   319 val prop_group = group "proposition" trm;
   320 
   321 val term = inner_syntax term_group;
   322 val prop = inner_syntax prop_group;
   323 
   324 
   325 (* patterns *)
   326 
   327 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   328 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   329 
   330 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   331 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   332 
   333 
   334 (* targets *)
   335 
   336 val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
   337 val opt_target = Scan.option target;
   338 
   339 end;
   340 
   341 type 'a parser = 'a OuterParse.parser;
   342 type 'a context_parser = 'a OuterParse.context_parser;
   343