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