src/Pure/Isar/outer_parse.ML
author wenzelm
Wed Apr 13 18:34:22 2005 +0200 (2005-04-13)
changeset 15703 727ef1b8b3ee
parent 15570 8d8c70b41bab
child 15714 9b8da47715c3
permissions -rw-r--r--
*** empty log message ***
     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 triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    15   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    16   val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
    17   val not_eof: token list -> token * token list
    18   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    19   val command: token list -> string * token list
    20   val keyword: token list -> string * token list
    21   val short_ident: token list -> string * token list
    22   val long_ident: token list -> string * token list
    23   val sym_ident: token list -> string * token list
    24   val term_var: token list -> string * token list
    25   val type_ident: token list -> string * token list
    26   val type_var: token list -> string * token list
    27   val number: token list -> string * token list
    28   val string: token list -> string * token list
    29   val verbatim: token list -> string * token list
    30   val sync: token list -> string * token list
    31   val eof: token list -> string * token list
    32   val $$$ : string -> token list -> string * token list
    33   val semicolon: token list -> string * token list
    34   val underscore: token list -> string * token list
    35   val maybe: (token list -> 'a * token list) -> token list -> 'a option * 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 sort: token list -> string * token list
    50   val arity: token list -> (string list * string) * token list
    51   val type_args: token list -> string list * token list
    52   val typ: token list -> string * token list
    53   val opt_infix: token list -> Syntax.mixfix * token list
    54   val opt_mixfix: token list -> Syntax.mixfix * token list
    55   val opt_infix': token list -> Syntax.mixfix * token list
    56   val opt_mixfix': token list -> Syntax.mixfix * token list
    57   val mixfix': token list -> Syntax.mixfix * token list
    58   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    59   val term: token list -> string * token list
    60   val prop: token list -> string * token list
    61   val propp: token list -> (string * (string list * string list)) * token list
    62   val termp: token list -> (string * string list) * token list
    63   val arguments: token list -> Args.T list * token list
    64   val attribs: token list -> Attrib.src list * token list
    65   val opt_attribs: token list -> Attrib.src list * token list
    66   val thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    67   val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    68   val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list
    69   val spec_opt_name: token list -> ((bstring * string) * Attrib.src list) * token list
    70   val xthm: token list -> (thmref * Attrib.src list) * token list
    71   val xthms1: token list -> (thmref * Attrib.src list) list * token list
    72   val locale_target: token list -> xstring option * token list
    73   val locale_expr: token list -> Locale.expr * token list
    74   val locale_keyword: token list -> string * token list
    75   val locale_element: token list -> Locale.element * token list
    76   val locale_elem_or_expr: token list -> Locale.element Locale.elem_expr * 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 val not_eof = Scan.one T.not_eof;
   126 
   127 fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
   128 
   129 fun kind k =
   130   group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
   131 
   132 val command = kind T.Command;
   133 val keyword = kind T.Keyword;
   134 val short_ident = kind T.Ident;
   135 val long_ident = kind T.LongIdent;
   136 val sym_ident = kind T.SymIdent;
   137 val term_var = kind T.Var;
   138 val type_ident = kind T.TypeIdent;
   139 val type_var = kind T.TypeVar;
   140 val number = kind T.Nat;
   141 val string = kind T.String;
   142 val verbatim = kind T.Verbatim;
   143 val sync = kind T.Sync;
   144 val eof = kind T.EOF;
   145 
   146 fun $$$ x =
   147   group (T.str_of_kind T.Keyword ^ " " ^ quote x)
   148     (Scan.one (T.keyword_with (equal x)) >> T.val_of);
   149 
   150 val semicolon = $$$ ";";
   151 
   152 val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
   153 val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   154 fun maybe scan = underscore >> K NONE || scan >> SOME;
   155 
   156 val nat = number >> (#1 o Library.read_int o Symbol.explode);
   157 
   158 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   159 
   160 fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   161 
   162 
   163 (* enumerations *)
   164 
   165 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   166 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   167 
   168 fun list1 scan = enum1 "," scan;
   169 fun list scan = enum "," scan;
   170 
   171 fun and_list1 scan = enum1 "and" scan;
   172 fun and_list scan = enum "and" scan;
   173 
   174 
   175 (* names and text *)
   176 
   177 val name = group "name declaration" (short_ident || sym_ident || string || number);
   178 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   179 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   180 val path = group "file name/path specification" name >> Path.unpack;
   181 
   182 
   183 (* sorts and arities *)
   184 
   185 val sort = group "sort" xname;
   186 
   187 fun gen_arity cod =
   188   Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
   189 
   190 val arity = gen_arity sort;
   191 
   192 
   193 (* types *)
   194 
   195 val typ = group "type"
   196   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   197 
   198 val type_args =
   199   type_ident >> single ||
   200   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   201   Scan.succeed [];
   202 
   203 
   204 (* mixfix annotations *)
   205 
   206 val infx = $$$ "infix" |-- !!! (nat >> Syntax.Infix || string -- nat >> Syntax.InfixName);
   207 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   208 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   209 
   210 val binder =
   211   $$$ "binder" |--
   212     !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   213   >> (Syntax.Binder o triple2);
   214 
   215 
   216 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   217 
   218 val mixfix =
   219   (string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri))
   220   >> (Syntax.Mixfix o triple2);
   221 
   222 fun fix_decl guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   223 fun opt_fix guard fix = Scan.optional (fix_decl guard fix) Syntax.NoSyn;
   224 
   225 val opt_infix = opt_fix !!! (infxl || infxr || infx);
   226 val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr || infx);
   227 val opt_infix' = opt_fix I (infxl || infxr || infx);
   228 val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr || infx);
   229 val mixfix' = fix_decl !!! (mixfix || binder || infxl || infxr || infx);
   230 
   231 
   232 (* consts *)
   233 
   234 val const =
   235   name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   236 
   237 
   238 (* terms *)
   239 
   240 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   241 
   242 val term = group "term" trm;
   243 val prop = group "proposition" trm;
   244 
   245 
   246 (* patterns *)
   247 
   248 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   249 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   250 val concl_props = $$$ "concl" |-- !!! is_props;
   251 val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
   252 
   253 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
   254 val propp' = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   255 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   256 
   257 
   258 (* arguments *)
   259 
   260 fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
   261 val keyword_sid = keyword_symid T.is_sid;
   262 
   263 fun atom_arg is_symid blk =
   264   group "argument"
   265     (position (short_ident || long_ident || sym_ident || term_var ||
   266         type_ident || type_var || number) >> Args.mk_ident ||
   267       position (keyword_symid is_symid) >> Args.mk_keyword ||
   268       position (string || verbatim) >> Args.mk_string ||
   269       position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword);
   270 
   271 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   272   >> (fn (x, (ys, z)) => Args.mk_keyword x :: ys @ [Args.mk_keyword z]);
   273 
   274 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
   275 and args1 is_symid blk x =
   276   ((Scan.repeat1
   277     (Scan.repeat1 (atom_arg is_symid blk) ||
   278       paren_args "(" ")" (args is_symid) ||
   279       paren_args "[" "]" (args is_symid))) >> List.concat) x;
   280 
   281 val arguments = args T.is_sid false;
   282 
   283 
   284 (* theorem specifications *)
   285 
   286 val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
   287 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   288 val opt_attribs = Scan.optional attribs [];
   289 
   290 fun thm_name s = name -- opt_attribs --| $$$ s;
   291 fun opt_thm_name s =
   292   Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
   293 
   294 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   295 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   296 
   297 val thm_sel = $$$ "(" |-- list1
   298  (nat --| minus -- nat >> PureThy.FromTo ||
   299   nat --| minus >> PureThy.From ||
   300   nat >> PureThy.Single) --| $$$ ")";
   301 
   302 val xthm = xname -- Scan.option thm_sel -- opt_attribs;
   303 val xthms1 = Scan.repeat1 xthm;
   304 
   305 
   306 (* locale elements *)
   307 
   308 local
   309 
   310 val loc_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K NONE || opt_mixfix >> SOME;
   311 val loc_keyword = $$$ "fixes" || $$$ "assumes" || $$$ "defines" || $$$ "notes" || $$$ "includes";
   312 
   313 val loc_element =
   314   $$$ "fixes" |-- !!! (and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- loc_mixfix
   315     >> triple1)) >> Locale.Fixes ||
   316   $$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp))
   317     >> Locale.Assumes ||
   318   $$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp'))
   319     >> Locale.Defines ||
   320   $$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1))
   321     >> Locale.Notes;
   322 
   323 fun plus1 scan =
   324   scan -- Scan.repeat ($$$ "+" |-- Scan.unless loc_keyword (!!! scan)) >> op ::;
   325 
   326 fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x
   327 and expr1 x = (expr2 -- Scan.repeat1 (maybe name) >> Locale.Rename || expr2) x
   328 and expr0 x = (plus1 expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   329 
   330 in
   331 
   332 val locale_target = Scan.option (($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")"));
   333 val locale_expr = expr0;
   334 val locale_keyword = loc_keyword;
   335 
   336 val locale_element = group "locale element" loc_element;
   337 
   338 val locale_elem_or_expr = group "locale element or includes"
   339   (loc_element >> Locale.Elem ||
   340   $$$ "includes" |-- !!! locale_expr >> Locale.Expr);
   341 
   342 end;
   343 
   344 
   345 (* proof methods *)
   346 
   347 fun is_symid_meth s =
   348   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   349 
   350 fun meth4 x =
   351  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   352   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   353 and meth3 x =
   354  (meth4 --| $$$ "?" >> Method.Try ||
   355   meth4 --| $$$ "+" >> Method.Repeat1 ||
   356   meth4) x
   357 and meth2 x =
   358  (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
   359   meth3) x
   360 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   361 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   362 
   363 val method = meth3;
   364 
   365 
   366 end;