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