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