src/Pure/Isar/outer_parse.ML
author wenzelm
Mon Nov 09 15:33:12 1998 +0100 (1998-11-09)
changeset 5826 977f789566b7
child 5877 ee214dec5657
permissions -rw-r--r--
Generic parsers for Isabelle/Isar outer syntax.
     1 (*  Title:      Pure/Isar/outer_parse.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Generic parsers for Isabelle/Isar outer syntax.
     6 *)
     7 
     8 signature OUTER_PARSE =
     9 sig
    10   type token
    11   val group: string -> (token list -> 'a) -> token list -> 'a
    12   val !!! : (token list -> 'a) -> token list -> 'a
    13   val $$$ : string -> token list -> string * token list
    14   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    15   val keyword: token list -> string * token list
    16   val short_ident: token list -> string * token list
    17   val long_ident: token list -> string * token list
    18   val sym_ident: token list -> string * token list
    19   val term_var: token list -> string * token list
    20   val text_var: token list -> string * token list
    21   val type_ident: token list -> string * token list
    22   val type_var: token list -> string * token list
    23   val number: token list -> string * token list
    24   val string: token list -> string * token list
    25   val verbatim: token list -> string * token list
    26   val eof: token list -> string * token list
    27   val not_eof: token list -> token * token list
    28   val nat: token list -> int * token list
    29   val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    30   val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    31   val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    32   val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    33   val name: token list -> bstring * token list
    34   val xname: token list -> xstring * token list
    35   val text: token list -> string * token list
    36   val sort: token list -> xsort * token list
    37   val arity: token list -> (xsort list * xsort) * token list
    38   val type_args: token list -> string list * token list
    39   val typ: token list -> string * token list
    40   val opt_infix: token list -> Syntax.mixfix * token list
    41   val opt_mixfix: token list -> Syntax.mixfix * token list
    42   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    43   val term: token list -> string * token list
    44   val prop: token list -> string * token list
    45   val args: token list -> Args.T list * token list
    46   val args1: token list -> Args.T list * token list
    47   val attribs: token list -> Args.src list * token list
    48   val opt_attribs: token list -> Args.src list * token list
    49   val thm_name: token list -> (bstring * Args.src list) * token list
    50   val opt_thm_name: token list -> (bstring * Args.src list) * token list
    51   val thm_xname: token list -> (xstring * Args.src list) * token list
    52   val method: token list -> Method.text * token list
    53   val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    54   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    55 end;
    56 
    57 structure OuterParse: OUTER_PARSE =
    58 struct
    59 
    60 type token = OuterLex.token;
    61 
    62 
    63 (** error handling **)
    64 
    65 (* group atomic parsers (no cuts!) *)
    66 
    67 fun fail_with s = Scan.fail_with
    68   (fn [] => s ^ " expected (past end-of-file!)"
    69     | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
    70       quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
    71 
    72 fun group s scan = scan || fail_with s;
    73 
    74 
    75 (* cut alternatives *)
    76 
    77 fun !!! scan =
    78   let
    79     fun get_pos [] = " (past end-of-file!)"
    80       | get_pos (tok :: _) = OuterLex.pos_of tok;
    81 
    82     fun err (toks, None) = "Outer syntax error" ^ get_pos toks
    83       | err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
    84   in Scan.!! err scan end;
    85 
    86 
    87 
    88 (** basic parsers **)
    89 
    90 (* utils *)
    91 
    92 fun triple1 ((x, y), z) = (x, y, z);
    93 fun triple2 (x, (y, z)) = (x, y, z);
    94 
    95 
    96 (* tokens *)
    97 
    98 fun position scan =
    99   (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
   100 
   101 fun kind k =
   102   group (OuterLex.str_of_kind k)
   103     (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
   104 
   105 val keyword = kind OuterLex.Keyword;
   106 val short_ident = kind OuterLex.Ident;
   107 val long_ident = kind OuterLex.LongIdent;
   108 val sym_ident = kind OuterLex.SymIdent;
   109 val term_var = kind OuterLex.Var;
   110 val text_var = kind OuterLex.TextVar;
   111 val type_ident = kind OuterLex.TypeIdent;
   112 val type_var = kind OuterLex.TypeVar;
   113 val number = kind OuterLex.Nat;
   114 val string = kind OuterLex.String;
   115 val verbatim = kind OuterLex.Verbatim;
   116 val eof = kind OuterLex.EOF;
   117 
   118 fun $$$ x =
   119   group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
   120     (Scan.one (OuterLex.is_kind OuterLex.Keyword andf (equal x o OuterLex.val_of))
   121       >> OuterLex.val_of);
   122 
   123 val nat = number >> (fst o Term.read_int o explode);
   124 
   125 val not_eof = Scan.one OuterLex.not_eof;
   126 
   127 
   128 (* enumerations *)
   129 
   130 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- scan) >> op ::;
   131 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   132 
   133 fun list1 scan = enum1 "," scan;
   134 fun list scan = enum "," scan;
   135 
   136 
   137 (* names *)
   138 
   139 val name = group "name declaration" (short_ident || string);
   140 val xname = group "name reference" (short_ident || long_ident || string);
   141 val text = group "text" (short_ident || long_ident || string || verbatim);
   142 
   143 
   144 (* sorts *)
   145 
   146 val sort =
   147   xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
   148 
   149 val arity =
   150   Scan.optional ($$$ "(" |-- !!! (Scan.repeat1 sort --| $$$ ")")) [] -- sort;
   151 
   152 
   153 (* types *)
   154 
   155 val typ =
   156   group "type" (short_ident || long_ident || type_ident || type_var || string);
   157 
   158 val type_args =
   159   type_ident >> single ||
   160   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   161   Scan.succeed [];
   162 
   163 
   164 (* mixfix annotations *)
   165 
   166 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   167 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   168 
   169 val binder =
   170   $$$ "binder" |--
   171     !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   172   >> (Syntax.Binder o triple2);
   173 
   174 
   175 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   176 
   177 val mixfix =
   178   string -- opt_pris -- Scan.optional nat Syntax.max_pri
   179   >> (Syntax.Mixfix o triple1);
   180 
   181 fun opt_fix fix =
   182   Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
   183 
   184 val opt_infix = opt_fix (infxl || infxr);
   185 val opt_mixfix = opt_fix (mixfix || infxl || infxr || binder);
   186 
   187 
   188 (* consts *)
   189 
   190 val const =
   191   name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   192 
   193 
   194 (* terms *)
   195 
   196 val trm = short_ident || long_ident || term_var || text_var || string;
   197 
   198 val term = group "term" trm;
   199 val prop = group "proposition" trm;
   200 
   201 
   202 (* arguments *)
   203 
   204 val keyword_symid = Scan.one (OuterLex.keyword_pred OuterLex.is_symid) >> OuterLex.val_of;
   205 
   206 val atom_arg =
   207   group "argument"
   208     ((short_ident || long_ident || sym_ident || number) >> Args.ident ||
   209       keyword_symid >> Args.keyword ||
   210       string >> Args.string);
   211 
   212 fun paren_args l r scan = $$$ l -- !!! (scan -- $$$ r)
   213   >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
   214 
   215 fun args x = Scan.optional args1 [] x
   216 and args1 x =
   217   ((Scan.repeat1
   218     (Scan.repeat1 atom_arg ||
   219       paren_args "(" ")" args ||
   220       paren_args "{" "}" args ||
   221       paren_args "[" "]" args)) >> flat) x;
   222 
   223 
   224 (* theorem names *)
   225 
   226 val attrib = position (xname -- !!! args);
   227 
   228 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   229 val opt_attribs = Scan.optional attribs [];
   230 
   231 val thm_name = name -- opt_attribs --| $$$ ":";
   232 val opt_thm_name = Scan.optional thm_name ("", []);
   233 val thm_xname = xname -- opt_attribs --| $$$ ":";
   234 
   235 
   236 (* proof methods *)
   237 
   238 fun meth4 x =
   239  (position (xname >> rpair []) >> Method.Source ||
   240   $$$ "(" |-- meth0 --| $$$ ")") x
   241 and meth3 x =
   242  (position (xname -- args1) >> Method.Source ||
   243   meth4) x
   244 and meth2 x =
   245  (meth4 --| $$$ "?" >> Method.Try ||
   246   meth4 --| $$$ "*" >> Method.Repeat ||
   247   meth4 --| $$$ "+" >> Method.Repeat1 ||
   248   meth3) x
   249 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   250 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   251 
   252 val method = meth2;
   253 
   254 
   255 end;