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