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