src/Pure/Isar/outer_parse.ML
author wenzelm
Fri May 05 22:09:41 2000 +0200 (2000-05-05)
changeset 8807 0046be1769f9
parent 8648 7461dc59a818
child 8897 fb1436ca3b2e
permissions -rw-r--r--
GPLed;
     1 (*  Title:      Pure/Isar/outer_parse.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Generic parsers for Isabelle/Isar outer syntax.
     7 *)
     8 
     9 signature OUTER_PARSE =
    10 sig
    11   type token
    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 $$$ : string -> token list -> string * token list
    16   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    17   val command: token list -> string * token list
    18   val keyword: token list -> string * token list
    19   val short_ident: token list -> string * token list
    20   val long_ident: token list -> string * token list
    21   val sym_ident: token list -> string * token list
    22   val term_var: token list -> string * token list
    23   val type_ident: token list -> string * token list
    24   val type_var: token list -> string * token list
    25   val number: token list -> string * token list
    26   val string: token list -> string * token list
    27   val verbatim: token list -> string * token list
    28   val sync: token list -> string * token list
    29   val eof: token list -> string * token list
    30   val not_eof: token list -> token * token list
    31   val opt_unit: token list -> unit * token list
    32   val nat: token list -> int * token list
    33   val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    34   val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    35   val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    36   val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    37   val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
    38   val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    39   val name: token list -> bstring * token list
    40   val xname: token list -> xstring * token list
    41   val text: token list -> string * token list
    42   val comment: token list -> Comment.text * token list
    43   val marg_comment: token list -> Comment.text * token list
    44   val interest: token list -> Comment.interest * token list
    45   val sort: token list -> xsort * token list
    46   val arity: token list -> (xsort list * xsort) * token list
    47   val simple_arity: token list -> (xsort list * xclass) * token list
    48   val type_args: token list -> string list * token list
    49   val typ: token list -> string * token list
    50   val opt_infix: token list -> Syntax.mixfix * token list
    51   val opt_mixfix: token list -> Syntax.mixfix * token list
    52   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    53   val term: token list -> string * token list
    54   val prop: token list -> string * token list
    55   val propp: token list -> (string * (string list * string list)) * token list
    56   val termp: token list -> (string * string list) * token list
    57   val attribs: token list -> Args.src list * token list
    58   val opt_attribs: token list -> Args.src list * token list
    59   val thm_name: string -> token list -> (bstring * Args.src list) * token list
    60   val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
    61   val spec_name: token list -> ((bstring * string) * Args.src list) * token list
    62   val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
    63   val xthm: token list -> (xstring * Args.src list) * token list
    64   val xthms1: token list -> (xstring * Args.src list) list * token list
    65   val method: token list -> Method.text * token list
    66   val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    67   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    68   val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
    69 end;
    70 
    71 structure OuterParse: OUTER_PARSE =
    72 struct
    73 
    74 type token = OuterLex.token;
    75 
    76 
    77 (** error handling **)
    78 
    79 (* group atomic parsers (no cuts!) *)
    80 
    81 fun fail_with s = Scan.fail_with
    82   (fn [] => s ^ " expected (past end-of-file!)"
    83     | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
    84       quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
    85 
    86 fun group s scan = scan || fail_with s;
    87 
    88 
    89 (* cut *)
    90 
    91 fun cut kind scan =
    92   let
    93     fun get_pos [] = " (past end-of-file!)"
    94       | get_pos (tok :: _) = OuterLex.pos_of tok;
    95 
    96     fun err (toks, None) = kind ^ get_pos toks
    97       | err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg;
    98   in Scan.!! err scan end;
    99 
   100 fun !!! scan = cut "Outer syntax error" scan;
   101 fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   102 
   103 
   104 
   105 (** basic parsers **)
   106 
   107 (* utils *)
   108 
   109 fun triple1 ((x, y), z) = (x, y, z);
   110 fun triple2 (x, (y, z)) = (x, y, z);
   111 fun triple_swap ((x, y), z) = ((x, z), y);
   112 
   113 
   114 (* tokens *)
   115 
   116 fun position scan =
   117   (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
   118 
   119 fun kind k =
   120   group (OuterLex.str_of_kind k)
   121     (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
   122 
   123 val command = kind OuterLex.Command;
   124 val keyword = kind OuterLex.Keyword;
   125 val short_ident = kind OuterLex.Ident;
   126 val long_ident = kind OuterLex.LongIdent;
   127 val sym_ident = kind OuterLex.SymIdent;
   128 val term_var = kind OuterLex.Var;
   129 val type_ident = kind OuterLex.TypeIdent;
   130 val type_var = kind OuterLex.TypeVar;
   131 val number = kind OuterLex.Nat;
   132 val string = kind OuterLex.String;
   133 val verbatim = kind OuterLex.Verbatim;
   134 val sync = kind OuterLex.Sync;
   135 val eof = kind OuterLex.EOF;
   136 
   137 fun $$$ x =
   138   group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
   139     (Scan.one (OuterLex.keyword_with (equal x)) >> OuterLex.val_of);
   140 
   141 val nat = number >> (fst o Term.read_int o Symbol.explode);
   142 
   143 val not_eof = Scan.one OuterLex.not_eof;
   144 
   145 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   146 
   147 
   148 (* enumerations *)
   149 
   150 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   151 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   152 
   153 fun list1 scan = enum1 "," scan;
   154 fun list scan = enum "," scan;
   155 
   156 fun and_list1 scan = enum1 "and" scan;
   157 fun and_list scan = enum "and" scan;
   158 
   159 
   160 (* names and text *)
   161 
   162 val name = group "name declaration" (short_ident || sym_ident || string || number);
   163 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   164 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   165 
   166 
   167 (* formal comments *)
   168 
   169 val comment = text >> (Comment.plain o Library.single);
   170 val marg_comment = Scan.repeat ($$$ "--" |-- text) >> Comment.plain;
   171 
   172 val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest ||
   173   $$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest;
   174 
   175 
   176 (* sorts and arities *)
   177 
   178 val sort =
   179   xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
   180 
   181 fun gen_arity cod =
   182   Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
   183 
   184 val arity = gen_arity sort;
   185 val simple_arity = gen_arity name;
   186 
   187 
   188 (* types *)
   189 
   190 val typ = group "type"
   191   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   192 
   193 val type_args =
   194   type_ident >> single ||
   195   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   196   Scan.succeed [];
   197 
   198 
   199 (* mixfix annotations *)
   200 
   201 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   202 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   203 
   204 val binder =
   205   $$$ "binder" |--
   206     !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   207   >> (Syntax.Binder o triple2);
   208 
   209 
   210 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   211 
   212 val mixfix =
   213   (string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri))
   214   >> (Syntax.Mixfix o triple2);
   215 
   216 fun opt_fix fix =
   217   Scan.optional ($$$ "(" |-- fix --| $$$ ")") Syntax.NoSyn;
   218 
   219 val opt_infix = opt_fix (infxl || infxr);
   220 val opt_mixfix = opt_fix (mixfix || binder || infxl || infxr);
   221 
   222 
   223 (* consts *)
   224 
   225 val const =
   226   name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   227 
   228 
   229 (* terms *)
   230 
   231 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   232 
   233 val term = group "term" trm;
   234 val prop = group "proposition" trm;
   235 
   236 
   237 (* patterns *)
   238 
   239 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   240 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   241 val concl_props = $$$ "concl" |-- !!! is_props;
   242 val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
   243 
   244 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
   245 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   246 
   247 
   248 (* arguments *)
   249 
   250 fun keyword_symid is_symid = Scan.one (OuterLex.keyword_with is_symid) >> OuterLex.val_of;
   251 val keyword_sid = keyword_symid OuterLex.is_sid;
   252 
   253 fun atom_arg is_symid blk =
   254   group "argument"
   255     (position (short_ident || long_ident || sym_ident || term_var ||
   256         type_ident || type_var || number) >> Args.ident ||
   257       position (keyword_symid is_symid) >> Args.keyword ||
   258       position (string || verbatim) >> Args.string ||
   259       position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
   260 
   261 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   262   >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
   263 
   264 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
   265 and args1 is_symid blk x =
   266   ((Scan.repeat1
   267     (Scan.repeat1 (atom_arg is_symid blk) ||
   268       paren_args "(" ")" (args is_symid) ||
   269       paren_args "{" "}" (args is_symid) ||
   270       paren_args "[" "]" (args is_symid))) >> flat) x;
   271 
   272 
   273 (* theorem specifications *)
   274 
   275 val attrib = position ((keyword_sid || xname) -- !!! (args OuterLex.is_sid false)) >> Args.src;
   276 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   277 val opt_attribs = Scan.optional attribs [];
   278 
   279 fun thm_name s = name -- opt_attribs --| $$$ s;
   280 fun opt_thm_name s =
   281   Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
   282 
   283 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   284 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   285 
   286 val xthm = xname -- opt_attribs;
   287 val xthms1 = Scan.repeat1 xthm;
   288 
   289 
   290 (* proof methods *)
   291 
   292 fun is_symid_meth s =
   293   s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.is_sid s;
   294 
   295 fun meth4 x =
   296  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   297   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   298 and meth3 x =
   299  (meth4 --| $$$ "?" >> Method.Try ||
   300   meth4 --| $$$ "+" >> Method.Repeat1 ||
   301   meth4) x
   302 and meth2 x =
   303  (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
   304   meth3) x
   305 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   306 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   307 
   308 val method = meth3;
   309 
   310 
   311 end;