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