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