Generic parsers for Isabelle/Isar outer syntax.
authorwenzelm
Mon Nov 09 15:33:12 1998 +0100 (1998-11-09)
changeset 5826977f789566b7
parent 5825 24e4b1780d33
child 5827 77071ac7c7b5
Generic parsers for Isabelle/Isar outer syntax.
src/Pure/Isar/outer_parse.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Isar/outer_parse.ML	Mon Nov 09 15:33:12 1998 +0100
     1.3 @@ -0,0 +1,255 @@
     1.4 +(*  Title:      Pure/Isar/outer_parse.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Markus Wenzel, TU Muenchen
     1.7 +
     1.8 +Generic parsers for Isabelle/Isar outer syntax.
     1.9 +*)
    1.10 +
    1.11 +signature OUTER_PARSE =
    1.12 +sig
    1.13 +  type token
    1.14 +  val group: string -> (token list -> 'a) -> token list -> 'a
    1.15 +  val !!! : (token list -> 'a) -> token list -> 'a
    1.16 +  val $$$ : string -> token list -> string * token list
    1.17 +  val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    1.18 +  val keyword: token list -> string * token list
    1.19 +  val short_ident: token list -> string * token list
    1.20 +  val long_ident: token list -> string * token list
    1.21 +  val sym_ident: token list -> string * token list
    1.22 +  val term_var: token list -> string * token list
    1.23 +  val text_var: token list -> string * token list
    1.24 +  val type_ident: token list -> string * token list
    1.25 +  val type_var: token list -> string * token list
    1.26 +  val number: token list -> string * token list
    1.27 +  val string: token list -> string * token list
    1.28 +  val verbatim: token list -> string * token list
    1.29 +  val eof: token list -> string * token list
    1.30 +  val not_eof: token list -> token * token list
    1.31 +  val nat: token list -> int * token list
    1.32 +  val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    1.33 +  val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    1.34 +  val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    1.35 +  val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    1.36 +  val name: token list -> bstring * token list
    1.37 +  val xname: token list -> xstring * token list
    1.38 +  val text: token list -> string * token list
    1.39 +  val sort: token list -> xsort * token list
    1.40 +  val arity: token list -> (xsort list * xsort) * token list
    1.41 +  val type_args: token list -> string list * token list
    1.42 +  val typ: token list -> string * token list
    1.43 +  val opt_infix: token list -> Syntax.mixfix * token list
    1.44 +  val opt_mixfix: token list -> Syntax.mixfix * token list
    1.45 +  val const: token list -> (bstring * string * Syntax.mixfix) * token list
    1.46 +  val term: token list -> string * token list
    1.47 +  val prop: token list -> string * token list
    1.48 +  val args: token list -> Args.T list * token list
    1.49 +  val args1: token list -> Args.T list * token list
    1.50 +  val attribs: token list -> Args.src list * token list
    1.51 +  val opt_attribs: token list -> Args.src list * token list
    1.52 +  val thm_name: token list -> (bstring * Args.src list) * token list
    1.53 +  val opt_thm_name: token list -> (bstring * Args.src list) * token list
    1.54 +  val thm_xname: token list -> (xstring * Args.src list) * token list
    1.55 +  val method: token list -> Method.text * token list
    1.56 +  val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    1.57 +  val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    1.58 +end;
    1.59 +
    1.60 +structure OuterParse: OUTER_PARSE =
    1.61 +struct
    1.62 +
    1.63 +type token = OuterLex.token;
    1.64 +
    1.65 +
    1.66 +(** error handling **)
    1.67 +
    1.68 +(* group atomic parsers (no cuts!) *)
    1.69 +
    1.70 +fun fail_with s = Scan.fail_with
    1.71 +  (fn [] => s ^ " expected (past end-of-file!)"
    1.72 +    | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
    1.73 +      quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
    1.74 +
    1.75 +fun group s scan = scan || fail_with s;
    1.76 +
    1.77 +
    1.78 +(* cut alternatives *)
    1.79 +
    1.80 +fun !!! scan =
    1.81 +  let
    1.82 +    fun get_pos [] = " (past end-of-file!)"
    1.83 +      | get_pos (tok :: _) = OuterLex.pos_of tok;
    1.84 +
    1.85 +    fun err (toks, None) = "Outer syntax error" ^ get_pos toks
    1.86 +      | err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
    1.87 +  in Scan.!! err scan end;
    1.88 +
    1.89 +
    1.90 +
    1.91 +(** basic parsers **)
    1.92 +
    1.93 +(* utils *)
    1.94 +
    1.95 +fun triple1 ((x, y), z) = (x, y, z);
    1.96 +fun triple2 (x, (y, z)) = (x, y, z);
    1.97 +
    1.98 +
    1.99 +(* tokens *)
   1.100 +
   1.101 +fun position scan =
   1.102 +  (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
   1.103 +
   1.104 +fun kind k =
   1.105 +  group (OuterLex.str_of_kind k)
   1.106 +    (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
   1.107 +
   1.108 +val keyword = kind OuterLex.Keyword;
   1.109 +val short_ident = kind OuterLex.Ident;
   1.110 +val long_ident = kind OuterLex.LongIdent;
   1.111 +val sym_ident = kind OuterLex.SymIdent;
   1.112 +val term_var = kind OuterLex.Var;
   1.113 +val text_var = kind OuterLex.TextVar;
   1.114 +val type_ident = kind OuterLex.TypeIdent;
   1.115 +val type_var = kind OuterLex.TypeVar;
   1.116 +val number = kind OuterLex.Nat;
   1.117 +val string = kind OuterLex.String;
   1.118 +val verbatim = kind OuterLex.Verbatim;
   1.119 +val eof = kind OuterLex.EOF;
   1.120 +
   1.121 +fun $$$ x =
   1.122 +  group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
   1.123 +    (Scan.one (OuterLex.is_kind OuterLex.Keyword andf (equal x o OuterLex.val_of))
   1.124 +      >> OuterLex.val_of);
   1.125 +
   1.126 +val nat = number >> (fst o Term.read_int o explode);
   1.127 +
   1.128 +val not_eof = Scan.one OuterLex.not_eof;
   1.129 +
   1.130 +
   1.131 +(* enumerations *)
   1.132 +
   1.133 +fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- scan) >> op ::;
   1.134 +fun enum sep scan = enum1 sep scan || Scan.succeed [];
   1.135 +
   1.136 +fun list1 scan = enum1 "," scan;
   1.137 +fun list scan = enum "," scan;
   1.138 +
   1.139 +
   1.140 +(* names *)
   1.141 +
   1.142 +val name = group "name declaration" (short_ident || string);
   1.143 +val xname = group "name reference" (short_ident || long_ident || string);
   1.144 +val text = group "text" (short_ident || long_ident || string || verbatim);
   1.145 +
   1.146 +
   1.147 +(* sorts *)
   1.148 +
   1.149 +val sort =
   1.150 +  xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
   1.151 +
   1.152 +val arity =
   1.153 +  Scan.optional ($$$ "(" |-- !!! (Scan.repeat1 sort --| $$$ ")")) [] -- sort;
   1.154 +
   1.155 +
   1.156 +(* types *)
   1.157 +
   1.158 +val typ =
   1.159 +  group "type" (short_ident || long_ident || type_ident || type_var || string);
   1.160 +
   1.161 +val type_args =
   1.162 +  type_ident >> single ||
   1.163 +  $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   1.164 +  Scan.succeed [];
   1.165 +
   1.166 +
   1.167 +(* mixfix annotations *)
   1.168 +
   1.169 +val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   1.170 +val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   1.171 +
   1.172 +val binder =
   1.173 +  $$$ "binder" |--
   1.174 +    !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   1.175 +  >> (Syntax.Binder o triple2);
   1.176 +
   1.177 +
   1.178 +val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   1.179 +
   1.180 +val mixfix =
   1.181 +  string -- opt_pris -- Scan.optional nat Syntax.max_pri
   1.182 +  >> (Syntax.Mixfix o triple1);
   1.183 +
   1.184 +fun opt_fix fix =
   1.185 +  Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
   1.186 +
   1.187 +val opt_infix = opt_fix (infxl || infxr);
   1.188 +val opt_mixfix = opt_fix (mixfix || infxl || infxr || binder);
   1.189 +
   1.190 +
   1.191 +(* consts *)
   1.192 +
   1.193 +val const =
   1.194 +  name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   1.195 +
   1.196 +
   1.197 +(* terms *)
   1.198 +
   1.199 +val trm = short_ident || long_ident || term_var || text_var || string;
   1.200 +
   1.201 +val term = group "term" trm;
   1.202 +val prop = group "proposition" trm;
   1.203 +
   1.204 +
   1.205 +(* arguments *)
   1.206 +
   1.207 +val keyword_symid = Scan.one (OuterLex.keyword_pred OuterLex.is_symid) >> OuterLex.val_of;
   1.208 +
   1.209 +val atom_arg =
   1.210 +  group "argument"
   1.211 +    ((short_ident || long_ident || sym_ident || number) >> Args.ident ||
   1.212 +      keyword_symid >> Args.keyword ||
   1.213 +      string >> Args.string);
   1.214 +
   1.215 +fun paren_args l r scan = $$$ l -- !!! (scan -- $$$ r)
   1.216 +  >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
   1.217 +
   1.218 +fun args x = Scan.optional args1 [] x
   1.219 +and args1 x =
   1.220 +  ((Scan.repeat1
   1.221 +    (Scan.repeat1 atom_arg ||
   1.222 +      paren_args "(" ")" args ||
   1.223 +      paren_args "{" "}" args ||
   1.224 +      paren_args "[" "]" args)) >> flat) x;
   1.225 +
   1.226 +
   1.227 +(* theorem names *)
   1.228 +
   1.229 +val attrib = position (xname -- !!! args);
   1.230 +
   1.231 +val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   1.232 +val opt_attribs = Scan.optional attribs [];
   1.233 +
   1.234 +val thm_name = name -- opt_attribs --| $$$ ":";
   1.235 +val opt_thm_name = Scan.optional thm_name ("", []);
   1.236 +val thm_xname = xname -- opt_attribs --| $$$ ":";
   1.237 +
   1.238 +
   1.239 +(* proof methods *)
   1.240 +
   1.241 +fun meth4 x =
   1.242 + (position (xname >> rpair []) >> Method.Source ||
   1.243 +  $$$ "(" |-- meth0 --| $$$ ")") x
   1.244 +and meth3 x =
   1.245 + (position (xname -- args1) >> Method.Source ||
   1.246 +  meth4) x
   1.247 +and meth2 x =
   1.248 + (meth4 --| $$$ "?" >> Method.Try ||
   1.249 +  meth4 --| $$$ "*" >> Method.Repeat ||
   1.250 +  meth4 --| $$$ "+" >> Method.Repeat1 ||
   1.251 +  meth3) x
   1.252 +and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   1.253 +and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   1.254 +
   1.255 +val method = meth2;
   1.256 +
   1.257 +
   1.258 +end;