src/Pure/Isar/outer_parse.ML
author wenzelm
Tue Jun 15 13:23:39 2004 +0200 (2004-06-15)
changeset 14949 5f5605361aad
parent 14835 695ee8ad0bb6
child 14981 e73f8140af78
permissions -rw-r--r--
added path;
     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 triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    16   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    17   val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
    18   val $$$ : string -> token list -> string * token list
    19   val semicolon: token list -> string * token list
    20   val underscore: token list -> string * token list
    21   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    22   val command: token list -> string * token list
    23   val keyword: token list -> string * token list
    24   val short_ident: token list -> string * token list
    25   val long_ident: token list -> string * token list
    26   val sym_ident: token list -> string * token list
    27   val term_var: token list -> string * token list
    28   val type_ident: token list -> string * token list
    29   val type_var: token list -> string * token list
    30   val number: token list -> string * token list
    31   val string: token list -> string * token list
    32   val verbatim: token list -> string * token list
    33   val sync: token list -> string * token list
    34   val eof: token list -> string * token list
    35   val not_eof: token list -> token * token list
    36   val opt_unit: token list -> unit * token list
    37   val opt_keyword: string -> token list -> bool * token list
    38   val nat: token list -> int * token list
    39   val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    40   val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    41   val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    42   val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    43   val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
    44   val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    45   val name: token list -> bstring * token list
    46   val xname: token list -> xstring * token list
    47   val text: token list -> string * token list
    48   val path: token list -> Path.T * token list
    49   val uname: token list -> string option * token list
    50   val sort: token list -> string * token list
    51   val arity: token list -> (string list * string) * token list
    52   val type_args: token list -> string list * token list
    53   val typ: token list -> string * token list
    54   val opt_infix: token list -> Syntax.mixfix * token list
    55   val opt_mixfix: token list -> Syntax.mixfix * token list
    56   val opt_infix': token list -> Syntax.mixfix * token list
    57   val opt_mixfix': token list -> Syntax.mixfix * token list
    58   val mixfix': token list -> Syntax.mixfix * token list
    59   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    60   val term: token list -> string * token list
    61   val prop: token list -> string * token list
    62   val propp: token list -> (string * (string list * string list)) * token list
    63   val termp: token list -> (string * string list) * token list
    64   val arguments: token list -> Args.T list * token list
    65   val attribs: token list -> Args.src list * token list
    66   val opt_attribs: token list -> Args.src list * token list
    67   val thm_name: string -> token list -> (bstring * Args.src list) * token list
    68   val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
    69   val spec_name: token list -> ((bstring * string) * Args.src list) * token list
    70   val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
    71   val xthm: token list -> (xstring * Args.src list) * token list
    72   val xthms1: token list -> (xstring * Args.src list) list * token list
    73   val locale_target: token list -> xstring option * token list
    74   val locale_expr: token list -> Locale.expr * token list
    75   val locale_keyword: token list -> string * token list
    76   val locale_element: token list -> Args.src Locale.element * token list
    77   val method: token list -> Method.text * token list
    78 end;
    79 
    80 structure OuterParse: OUTER_PARSE =
    81 struct
    82 
    83 structure T = OuterLex;
    84 type token = T.token;
    85 
    86 
    87 (** error handling **)
    88 
    89 (* group atomic parsers (no cuts!) *)
    90 
    91 fun fail_with s = Scan.fail_with
    92   (fn [] => s ^ " expected (past end-of-file!)"
    93     | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found");
    94 
    95 fun group s scan = scan || fail_with s;
    96 
    97 
    98 (* cut *)
    99 
   100 fun cut kind scan =
   101   let
   102     fun get_pos [] = " (past end-of-file!)"
   103       | get_pos (tok :: _) = T.pos_of tok;
   104 
   105     fun err (toks, None) = kind ^ get_pos toks
   106       | err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg;
   107   in Scan.!! err scan end;
   108 
   109 fun !!! scan = cut "Outer syntax error" scan;
   110 fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   111 
   112 
   113 
   114 (** basic parsers **)
   115 
   116 (* utils *)
   117 
   118 fun triple1 ((x, y), z) = (x, y, z);
   119 fun triple2 (x, (y, z)) = (x, y, z);
   120 fun triple_swap ((x, y), z) = ((x, z), y);
   121 
   122 
   123 (* tokens *)
   124 
   125 fun position scan =
   126   (Scan.ahead (Scan.one T.not_eof) >> T.position_of) -- scan >> Library.swap;
   127 
   128 fun kind k =
   129   group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
   130 
   131 val command = kind T.Command;
   132 val keyword = kind T.Keyword;
   133 val short_ident = kind T.Ident;
   134 val long_ident = kind T.LongIdent;
   135 val sym_ident = kind T.SymIdent;
   136 val term_var = kind T.Var;
   137 val type_ident = kind T.TypeIdent;
   138 val type_var = kind T.TypeVar;
   139 val number = kind T.Nat;
   140 val string = kind T.String;
   141 val verbatim = kind T.Verbatim;
   142 val sync = kind T.Sync;
   143 val eof = kind T.EOF;
   144 
   145 fun $$$ x =
   146   group (T.str_of_kind T.Keyword ^ " " ^ quote x)
   147     (Scan.one (T.keyword_with (equal x)) >> T.val_of);
   148 
   149 val semicolon = $$$ ";";
   150 
   151 val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   152 
   153 val nat = number >> (#1 o Library.read_int o Symbol.explode);
   154 
   155 val not_eof = Scan.one T.not_eof;
   156 
   157 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   158 
   159 fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   160 
   161 
   162 (* enumerations *)
   163 
   164 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   165 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   166 
   167 fun list1 scan = enum1 "," scan;
   168 fun list scan = enum "," scan;
   169 
   170 fun and_list1 scan = enum1 "and" scan;
   171 fun and_list scan = enum "and" scan;
   172 
   173 
   174 (* names and text *)
   175 
   176 val name = group "name declaration" (short_ident || sym_ident || string || number);
   177 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   178 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   179 val path = group "file name/path specification" name >> Path.unpack;
   180 
   181 val uname = underscore >> K None || name >> Some;
   182 
   183 
   184 (* sorts and arities *)
   185 
   186 val sort = group "sort" xname;
   187 
   188 fun gen_arity cod =
   189   Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
   190 
   191 val arity = gen_arity sort;
   192 
   193 
   194 (* types *)
   195 
   196 val typ = group "type"
   197   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   198 
   199 val type_args =
   200   type_ident >> single ||
   201   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   202   Scan.succeed [];
   203 
   204 
   205 (* mixfix annotations *)
   206 
   207 val infx = $$$ "infix" |-- !!! (nat >> Syntax.Infix || string -- nat >> Syntax.InfixName);
   208 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   209 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   210 
   211 val binder =
   212   $$$ "binder" |--
   213     !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   214   >> (Syntax.Binder o triple2);
   215 
   216 
   217 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   218 
   219 val mixfix =
   220   (string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri))
   221   >> (Syntax.Mixfix o triple2);
   222 
   223 fun fix_decl guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   224 fun opt_fix guard fix = Scan.optional (fix_decl guard fix) Syntax.NoSyn;
   225 
   226 val opt_infix = opt_fix !!! (infxl || infxr || infx);
   227 val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr || infx);
   228 val opt_infix' = opt_fix I (infxl || infxr || infx);
   229 val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr || infx);
   230 val mixfix' = fix_decl !!! (mixfix || binder || infxl || infxr || infx);
   231 
   232 
   233 (* consts *)
   234 
   235 val const =
   236   name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   237 
   238 
   239 (* terms *)
   240 
   241 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   242 
   243 val term = group "term" trm;
   244 val prop = group "proposition" trm;
   245 
   246 
   247 (* patterns *)
   248 
   249 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   250 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   251 val concl_props = $$$ "concl" |-- !!! is_props;
   252 val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
   253 
   254 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
   255 val propp' = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   256 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   257 
   258 
   259 (* arguments *)
   260 
   261 fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
   262 val keyword_sid = keyword_symid T.is_sid;
   263 
   264 fun atom_arg is_symid blk =
   265   group "argument"
   266     (position (short_ident || long_ident || sym_ident || term_var ||
   267         type_ident || type_var || number) >> Args.ident ||
   268       position (keyword_symid is_symid) >> Args.keyword ||
   269       position (string || verbatim) >> Args.string ||
   270       position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
   271 
   272 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   273   >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
   274 
   275 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
   276 and args1 is_symid blk x =
   277   ((Scan.repeat1
   278     (Scan.repeat1 (atom_arg is_symid blk) ||
   279       paren_args "(" ")" (args is_symid) ||
   280       paren_args "[" "]" (args is_symid))) >> flat) x;
   281 
   282 val arguments = args T.is_sid false;
   283 
   284 
   285 (* theorem specifications *)
   286 
   287 val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
   288 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   289 val opt_attribs = Scan.optional attribs [];
   290 
   291 fun thm_name s = name -- opt_attribs --| $$$ s;
   292 fun opt_thm_name s =
   293   Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
   294 
   295 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   296 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   297 
   298 val xthm = xname -- opt_attribs;
   299 val xthms1 = Scan.repeat1 xthm;
   300 
   301 
   302 (* locale elements *)
   303 
   304 local
   305 
   306 val loc_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K None || opt_mixfix >> Some;
   307 val loc_keyword = $$$ "fixes" || $$$ "assumes" || $$$ "defines" || $$$ "notes" || $$$ "includes";
   308 
   309 fun plus1 scan =
   310   scan -- Scan.repeat ($$$ "+" |-- Scan.unless loc_keyword (!!! scan)) >> op ::;
   311 
   312 fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x
   313 and expr1 x = (expr2 -- Scan.repeat1 uname >> Locale.Rename || expr2) x
   314 and expr0 x = (plus1 expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   315 
   316 in
   317 
   318 val locale_target = Scan.option (($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")"));
   319 val locale_expr = expr0;
   320 val locale_keyword = loc_keyword;
   321 
   322 val locale_element = group "locale element"
   323   ($$$ "fixes" |-- !!! (and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- loc_mixfix
   324     >> triple1)) >> (Locale.Elem o Locale.Fixes) ||
   325   $$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp))
   326     >> (Locale.Elem o Locale.Assumes) ||
   327   $$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp'))
   328     >> (Locale.Elem o Locale.Defines) ||
   329   $$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1))
   330     >> (Locale.Elem o Locale.Notes) ||
   331   $$$ "includes" |-- !!! locale_expr >> Locale.Expr);
   332 
   333 end;
   334 
   335 
   336 (* proof methods *)
   337 
   338 fun is_symid_meth s =
   339   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   340 
   341 fun meth4 x =
   342  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   343   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   344 and meth3 x =
   345  (meth4 --| $$$ "?" >> Method.Try ||
   346   meth4 --| $$$ "+" >> Method.Repeat1 ||
   347   meth4) x
   348 and meth2 x =
   349  (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
   350   meth3) x
   351 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   352 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   353 
   354 val method = meth3;
   355 
   356 
   357 end;