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