src/Pure/Isar/outer_parse.ML
author wenzelm
Fri Mar 19 11:24:00 1999 +0100 (1999-03-19)
changeset 6404 2daaf2943c79
parent 6398 e6f0c192ef88
child 6430 69400c97d3bf
permissions -rw-r--r--
common qed and end of proofs;
     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 and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
    34   val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    35   val name: token list -> bstring * token list
    36   val xname: token list -> xstring * token list
    37   val text: token list -> string * token list
    38   val comment: token list -> string * token list
    39   val sort: token list -> xsort * token list
    40   val arity: token list -> (xsort list * xsort) * token list
    41   val simple_arity: token list -> (xsort list * xclass) * token list
    42   val type_args: token list -> string list * token list
    43   val typ: token list -> string * token list
    44   val opt_infix: token list -> Syntax.mixfix * token list
    45   val opt_mixfix: token list -> Syntax.mixfix * token list
    46   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    47   val term: token list -> string * token list
    48   val prop: token list -> string * token list
    49   val attribs: token list -> Args.src list * token list
    50   val opt_attribs: token list -> Args.src list * token list
    51   val thm_name: string -> token list -> (bstring * Args.src list) * token list
    52   val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
    53   val spec_name: token list -> ((bstring * string) * Args.src list) * token list
    54   val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
    55   val xthm: token list -> (xstring * Args.src list) * token list
    56   val xthms1: token list -> (xstring * Args.src list) list * token list
    57   val method: token list -> Method.text * token list
    58   val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    59   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    60 end;
    61 
    62 structure OuterParse: OUTER_PARSE =
    63 struct
    64 
    65 type token = OuterLex.token;
    66 
    67 
    68 (** error handling **)
    69 
    70 (* group atomic parsers (no cuts!) *)
    71 
    72 fun fail_with s = Scan.fail_with
    73   (fn [] => s ^ " expected (past end-of-file!)"
    74     | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
    75       quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
    76 
    77 fun group s scan = scan || fail_with s;
    78 
    79 
    80 (* cut *)
    81 
    82 fun !!! scan =
    83   let
    84     fun get_pos [] = " (past end-of-file!)"
    85       | get_pos (tok :: _) = OuterLex.pos_of tok;
    86 
    87     fun err (toks, None) = "Outer syntax error" ^ get_pos toks
    88       | err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
    89   in Scan.!! err scan end;
    90 
    91 
    92 
    93 (** basic parsers **)
    94 
    95 (* utils *)
    96 
    97 fun triple1 ((x, y), z) = (x, y, z);
    98 fun triple2 (x, (y, z)) = (x, y, z);
    99 
   100 
   101 (* tokens *)
   102 
   103 fun position scan =
   104   (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
   105 
   106 fun kind k =
   107   group (OuterLex.str_of_kind k)
   108     (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
   109 
   110 val keyword = kind OuterLex.Keyword;
   111 val short_ident = kind OuterLex.Ident;
   112 val long_ident = kind OuterLex.LongIdent;
   113 val sym_ident = kind OuterLex.SymIdent;
   114 val term_var = kind OuterLex.Var;
   115 val text_var = kind OuterLex.TextVar;
   116 val type_ident = kind OuterLex.TypeIdent;
   117 val type_var = kind OuterLex.TypeVar;
   118 val number = kind OuterLex.Nat;
   119 val string = kind OuterLex.String;
   120 val verbatim = kind OuterLex.Verbatim;
   121 val eof = kind OuterLex.EOF;
   122 
   123 fun $$$ x =
   124   group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
   125     (Scan.one (OuterLex.is_kind OuterLex.Keyword andf (equal x o OuterLex.val_of))
   126       >> OuterLex.val_of);
   127 
   128 val nat = number >> (fst o Term.read_int o Symbol.explode);
   129 
   130 val not_eof = Scan.one OuterLex.not_eof;
   131 
   132 
   133 (* enumerations *)
   134 
   135 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   136 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   137 
   138 fun list1 scan = enum1 "," scan;
   139 fun list scan = enum "," scan;
   140 
   141 fun and_list1 scan = enum1 "and" scan;
   142 fun and_list scan = enum "and" scan;
   143 
   144 
   145 (* names and text *)
   146 
   147 val name = group "name declaration" (short_ident || sym_ident || string);
   148 val xname = group "name reference" (short_ident || long_ident || sym_ident || string);
   149 val text = group "text" (short_ident || long_ident || string || verbatim);
   150 val comment = Scan.optional ($$$ "--" |-- text) "";
   151 
   152 
   153 (* sorts and arities *)
   154 
   155 val sort =
   156   xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
   157 
   158 fun gen_arity cod =
   159   Scan.optional ($$$ "(" |-- !!! (Scan.repeat1 sort --| $$$ ")")) [] -- cod;
   160 
   161 val arity = gen_arity sort;
   162 val simple_arity = gen_arity name;
   163 
   164 
   165 (* types *)
   166 
   167 val typ =
   168   group "type" (short_ident || long_ident || type_ident || type_var || string);
   169 
   170 val type_args =
   171   type_ident >> single ||
   172   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   173   Scan.succeed [];
   174 
   175 
   176 (* mixfix annotations *)
   177 
   178 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   179 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   180 
   181 val binder =
   182   $$$ "binder" |--
   183     !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   184   >> (Syntax.Binder o triple2);
   185 
   186 
   187 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   188 
   189 val mixfix =
   190   string -- opt_pris -- Scan.optional nat Syntax.max_pri
   191   >> (Syntax.Mixfix o triple1);
   192 
   193 fun opt_fix fix =
   194   Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
   195 
   196 val opt_infix = opt_fix (infxl || infxr);
   197 val opt_mixfix = opt_fix (mixfix || binder || infxl || infxr);
   198 
   199 
   200 (* consts *)
   201 
   202 val const =
   203   name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   204 
   205 
   206 (* terms *)
   207 
   208 val trm = short_ident || long_ident || term_var || text_var || string;
   209 
   210 val term = group "term" trm;
   211 val prop = group "proposition" trm;
   212 
   213 
   214 (* arguments *)
   215 
   216 val keyword_symid = Scan.one (OuterLex.keyword_pred OuterLex.is_sid) >> OuterLex.val_of;
   217 
   218 fun atom_arg blk =
   219   group "argument"
   220     (position (short_ident || long_ident || sym_ident || term_var || text_var ||
   221         type_ident || type_var || number) >> Args.ident ||
   222       position keyword_symid >> Args.keyword ||
   223       position string >> Args.string ||
   224       position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
   225 
   226 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   227   >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
   228 
   229 fun args blk x = Scan.optional (args1 blk) [] x
   230 and args1 blk x =
   231   ((Scan.repeat1
   232     (Scan.repeat1 (atom_arg blk) ||
   233       paren_args "(" ")" args ||
   234       paren_args "{" "}" args ||
   235       paren_args "[" "]" args)) >> flat) x;
   236 
   237 
   238 (* theorem specifications *)
   239 
   240 val attrib = position (xname -- !!! (args false)) >> Args.src;
   241 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   242 val opt_attribs = Scan.optional attribs [];
   243 
   244 fun thm_name s = name -- opt_attribs --| $$$ s;
   245 fun opt_thm_name s = Scan.optional (thm_name s) ("", []);
   246 
   247 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   248 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   249 
   250 val xthm = xname -- opt_attribs;
   251 val xthms1 = Scan.repeat1 xthm;
   252 
   253 
   254 (* proof methods *)
   255 
   256 fun meth4 x =
   257  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   258   $$$ "(" |-- meth0 --| $$$ ")") x
   259 and meth3 x =
   260  (meth4 --| $$$ "?" >> Method.Try ||
   261   meth4 --| $$$ "*" >> Method.Repeat ||
   262   meth4 --| $$$ "+" >> Method.Repeat1 ||
   263   meth4) x
   264 and meth2 x =
   265  (position (xname -- args1 false) >> (Method.Source o Args.src) ||
   266   meth3) x
   267 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   268 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   269 
   270 val method = meth3;
   271 
   272 
   273 end;