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