src/Pure/Isar/outer_parse.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29581 b3b33e0298eb
child 30223 24d975352879
permissions -rw-r--r--
use long names for old-style fold combinators;
     1 (*  Title:      Pure/Isar/outer_parse.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Generic parsers for Isabelle/Isar outer syntax.
     5 *)
     6 
     7 signature OUTER_PARSE =
     8 sig
     9   type token = OuterLex.token
    10   type 'a parser = token list -> 'a * token list
    11   val group: string -> (token list -> 'a) -> token list -> 'a
    12   val !!! : (token list -> 'a) -> token list -> 'a
    13   val !!!! : (token list -> 'a) -> token 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 parser
    18   val position: (token list -> 'a * 'b) -> token 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 * token list -> 'b * ('a * token list)) ->
    54     'a * token list -> 'b list * ('a * token list)
    55   val enum1': string -> ('a * token list -> 'b * ('a * token list)) ->
    56     'a * token list -> 'b list * ('a * token list)
    57   val and_list': ('a * token list -> 'b * ('a * token list)) ->
    58     'a * token list -> 'b list * ('a * token list)
    59   val and_list1': ('a * token list -> 'b * ('a * token list)) ->
    60     'a * token list -> 'b list * ('a * token list)
    61   val list: 'a parser -> 'a list parser
    62   val list1: 'a parser -> 'a list parser
    63   val name: bstring parser
    64   val binding: binding parser
    65   val xname: xstring parser
    66   val text: string parser
    67   val path: Path.T parser
    68   val parname: string parser
    69   val parbinding: binding parser
    70   val sort: string parser
    71   val arity: (string * string list * string) parser
    72   val multi_arity: (string list * string list * string) parser
    73   val type_args: string 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_infix: mixfix parser
    79   val opt_infix': mixfix parser
    80   val opt_mixfix: mixfix parser
    81   val opt_mixfix': mixfix parser
    82   val where_: string parser
    83   val const: (string * string * mixfix) parser
    84   val params: (binding * string option) list parser
    85   val simple_fixes: (binding * string option) list parser
    86   val fixes: (binding * string option * mixfix) list parser
    87   val for_fixes: (binding * string option * mixfix) list parser
    88   val for_simple_fixes: (binding * string option) list parser
    89   val ML_source: (SymbolPos.text * Position.T) parser
    90   val doc_source: (SymbolPos.text * Position.T) parser
    91   val term_group: string parser
    92   val prop_group: string parser
    93   val term: string parser
    94   val prop: 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 OuterParse: OUTER_PARSE =
   102 struct
   103 
   104 structure T = OuterLex;
   105 type token = T.token;
   106 
   107 type 'a parser = token list -> 'a * token 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 T.text_of tok of
   118           (txt, "") => s ^ " expected,\nbut " ^ txt ^ T.pos_of tok ^ " was found"
   119         | (txt1, txt2) => s ^ " expected,\nbut " ^ txt1 ^ T.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 :: _) = T.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) => (T.assign NONE arg; x));
   155 
   156 
   157 val not_eof = RESET_VALUE (Scan.one T.not_eof);
   158 
   159 fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
   160 fun source_position atom = Scan.ahead atom |-- not_eof >> T.source_position_of;
   161 fun inner_syntax atom = Scan.ahead atom |-- not_eof >> T.source_of;
   162 
   163 fun kind k =
   164   group (T.str_of_kind k) (RESET_VALUE (Scan.one (T.is_kind k) >> T.content_of));
   165 
   166 val command = kind T.Command;
   167 val keyword = kind T.Keyword;
   168 val short_ident = kind T.Ident;
   169 val long_ident = kind T.LongIdent;
   170 val sym_ident = kind T.SymIdent;
   171 val term_var = kind T.Var;
   172 val type_ident = kind T.TypeIdent;
   173 val type_var = kind T.TypeVar;
   174 val number = kind T.Nat;
   175 val string = kind T.String;
   176 val alt_string = kind T.AltString;
   177 val verbatim = kind T.Verbatim;
   178 val sync = kind T.Sync;
   179 val eof = kind T.EOF;
   180 
   181 fun keyword_with pred = RESET_VALUE (Scan.one (T.keyword_with pred) >> T.content_of);
   182 val keyword_ident_or_symbolic = keyword_with T.ident_or_symbolic;
   183 
   184 fun $$$ x =
   185   group (T.str_of_kind T.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 (T.ident_with (fn y => x = y)) >> T.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 
   200 val tag_name = group "tag name" (short_ident || string);
   201 val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
   202 
   203 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   204 fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   205 
   206 val begin = $$$ "begin";
   207 val opt_begin = Scan.optional (begin >> K true) false;
   208 
   209 
   210 (* enumerations *)
   211 
   212 fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
   213 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   214 
   215 fun enum1' sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
   216 fun enum' sep scan = enum1' sep scan || Scan.succeed [];
   217 
   218 fun and_list1 scan = enum1 "and" scan;
   219 fun and_list scan = enum "and" scan;
   220 
   221 fun and_list1' scan = enum1' "and" scan;
   222 fun and_list' scan = enum' "and" scan;
   223 
   224 fun list1 scan = enum1 "," scan;
   225 fun list scan = enum "," scan;
   226 
   227 
   228 (* names and text *)
   229 
   230 val name = group "name declaration" (short_ident || sym_ident || string || number);
   231 val binding = position name >> Binding.name_pos;
   232 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   233 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   234 val path = group "file name/path specification" name >> Path.explode;
   235 
   236 val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
   237 val parbinding = Scan.optional ($$$ "(" |-- binding --| $$$ ")") Binding.empty;
   238 
   239 
   240 (* sorts and arities *)
   241 
   242 val sort = group "sort" (inner_syntax xname);
   243 
   244 val arity = xname -- ($$$ "::" |-- !!!
   245   (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
   246 
   247 val multi_arity = and_list1 xname -- ($$$ "::" |-- !!!
   248   (Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- sort)) >> triple2;
   249 
   250 
   251 (* types *)
   252 
   253 val typ_group = group "type"
   254   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   255 
   256 val typ = inner_syntax typ_group;
   257 
   258 val type_args =
   259   type_ident >> single ||
   260   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   261   Scan.succeed [];
   262 
   263 
   264 (* mixfix annotations *)
   265 
   266 val mfix = string --
   267   !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
   268     Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
   269 
   270 val infx = $$$ "infix" |-- !!! (nat >> Infix || string -- nat >> InfixName);
   271 val infxl = $$$ "infixl" |-- !!! (nat >> Infixl || string -- nat >> InfixlName);
   272 val infxr = $$$ "infixr" |-- !!! (nat >> Infixr || string -- nat >> InfixrName);
   273 
   274 val binder = $$$ "binder" |--
   275   !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   276   >> (Binder o triple2);
   277 
   278 fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   279 fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
   280 
   281 val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
   282 val mixfix' = annotation I (mfix || binder || infxl || infxr || infx);
   283 val opt_infix = opt_annotation !!! (infxl || infxr || infx);
   284 val opt_infix' = opt_annotation I (infxl || infxr || infx);
   285 val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
   286 val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
   287 
   288 
   289 (* fixes *)
   290 
   291 val where_ = $$$ "where";
   292 
   293 val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   294 
   295 val params = Scan.repeat1 binding -- Scan.option ($$$ "::" |-- !!! typ)
   296   >> (fn (xs, T) => map (rpair T) xs);
   297 
   298 val simple_fixes = and_list1 params >> flat;
   299 
   300 val fixes =
   301   and_list1 (binding -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
   302     params >> map Syntax.no_syn) >> flat;
   303 
   304 val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
   305 val for_simple_fixes = Scan.optional ($$$ "for" |-- !!! simple_fixes) [];
   306 
   307 
   308 (* embedded source text *)
   309 
   310 val ML_source = source_position (group "ML source" text);
   311 val doc_source = source_position (group "document source" text);
   312 
   313 
   314 (* terms *)
   315 
   316 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   317 
   318 val term_group = group "term" trm;
   319 val prop_group = group "proposition" trm;
   320 
   321 val term = inner_syntax term_group;
   322 val prop = inner_syntax prop_group;
   323 
   324 
   325 (* patterns *)
   326 
   327 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   328 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   329 
   330 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   331 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   332 
   333 
   334 (* targets *)
   335 
   336 val target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
   337 val opt_target = Scan.option target;
   338 
   339 end;