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