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