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