src/Pure/Isar/outer_parse.ML
author wenzelm
Sun Jun 11 21:59:25 2006 +0200 (2006-06-11)
changeset 19845 b8985bf2ce8b
parent 19811 46abcbb2da9d
child 20273 ea313e02fd13
permissions -rw-r--r--
added for_fixes;
     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 minus: token list -> string * token list
    25   val term_var: token list -> string * token list
    26   val type_ident: token list -> string * token list
    27   val type_var: token list -> string * token list
    28   val number: token list -> string * token list
    29   val string: token list -> string * token list
    30   val verbatim: token list -> string * token list
    31   val sync: token list -> string * token list
    32   val eof: token list -> string * token list
    33   val $$$ : string -> token list -> string * token list
    34   val reserved : string -> token list -> string * token list
    35   val semicolon: token list -> string * token list
    36   val underscore: token list -> string * token list
    37   val maybe: (token list -> 'a * token list) -> token list -> 'a option * token list
    38   val tag_name: token list -> string * token list
    39   val tags: token list -> string list * token list
    40   val opt_unit: token list -> unit * token list
    41   val opt_keyword: string -> token list -> bool * token list
    42   val nat: token list -> int * token list
    43   val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    44   val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    45   val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    46   val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    47   val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
    48   val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    49   val name: token list -> bstring * token list
    50   val xname: token list -> xstring * token list
    51   val text: token list -> string * token list
    52   val path: token list -> Path.T * token list
    53   val parname: token list -> string * token list
    54   val sort: token list -> string * token list
    55   val arity: token list -> (string list * string) * token list
    56   val type_args: token list -> string list * token list
    57   val typ: token list -> string * token list
    58   val mixfix: token list -> mixfix * token list
    59   val opt_infix: token list -> mixfix * token list
    60   val opt_mixfix: token list -> mixfix * token list
    61   val opt_infix': token list -> mixfix * token list
    62   val opt_mixfix': token list -> mixfix * token list
    63   val const: token list -> (string * string * mixfix) * token list
    64   val simple_fixes: token list -> (string * string option) list * token list
    65   val fixes: token list -> (string * string option * mixfix) list * token list
    66   val for_fixes: token list -> (string * string option * mixfix) list * token list
    67   val term: token list -> string * token list
    68   val prop: token list -> string * token list
    69   val propp: token list -> (string * string list) * token list
    70   val termp: token list -> (string * string list) * token list
    71   val arguments: token list -> Args.T list * token list
    72   val attribs: token list -> Attrib.src list * token list
    73   val opt_attribs: token list -> Attrib.src list * token list
    74   val thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    75   val opt_thm_name: string -> token list -> (bstring * Attrib.src list) * token list
    76   val spec: token list -> ((bstring * Attrib.src list) * string list) * token list
    77   val named_spec: token list -> ((bstring * Attrib.src list) * string list) * token list
    78   val spec_name: token list -> ((bstring * string) * Attrib.src list) * token list
    79   val spec_opt_name: token list -> ((bstring * string) * Attrib.src list) * token list
    80   val xthm: token list -> (thmref * Attrib.src list) * token list
    81   val xthms1: token list -> (thmref * Attrib.src list) list * token list
    82   val name_facts: token list ->
    83     ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list * token list
    84   val locale_mixfix: token list -> mixfix * token list
    85   val locale_fixes: token list -> (string * string option * mixfix) list * token list
    86   val locale_insts: token list -> string option list * token list
    87   val locale_target: token list -> xstring * token list
    88   val opt_locale_target: token list -> xstring option * token list
    89   val locale_expr: token list -> Locale.expr * token list
    90   val locale_expr_unless: (token list -> 'a * token list) -> 
    91          token list -> Locale.expr * token list 
    92   val locale_keyword: token list -> string * token list
    93   val locale_element: token list -> Element.context Locale.element * token list
    94   val context_element: token list -> Element.context * token list
    95   val statement: token list ->
    96     ((bstring * Attrib.src list) * (string * string list) list) list * token list
    97   val general_statement: token list ->
    98     (Element.context Locale.element list * Element.statement) * OuterLex.token list
    99   val statement_keyword: token list -> string * token list
   100   val method: token list -> Method.text * token list
   101 end;
   102 
   103 structure OuterParse: OUTER_PARSE =
   104 struct
   105 
   106 structure T = OuterLex;
   107 type token = T.token;
   108 
   109 
   110 (** error handling **)
   111 
   112 (* group atomic parsers (no cuts!) *)
   113 
   114 fun fail_with s = Scan.fail_with
   115   (fn [] => s ^ " expected (past end-of-file!)"
   116     | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found");
   117 
   118 fun group s scan = scan || fail_with s;
   119 
   120 
   121 (* cut *)
   122 
   123 fun cut kind scan =
   124   let
   125     fun get_pos [] = " (past end-of-file!)"
   126       | get_pos (tok :: _) = T.pos_of tok;
   127 
   128     fun err (toks, NONE) = kind ^ get_pos toks
   129       | err (toks, SOME msg) = kind ^ get_pos toks ^ ": " ^ msg;
   130   in Scan.!! err scan end;
   131 
   132 fun !!! scan = cut "Outer syntax error" scan;
   133 fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   134 
   135 
   136 
   137 (** basic parsers **)
   138 
   139 (* utils *)
   140 
   141 fun triple1 ((x, y), z) = (x, y, z);
   142 fun triple2 (x, (y, z)) = (x, y, z);
   143 fun triple_swap ((x, y), z) = ((x, z), y);
   144 
   145 
   146 (* tokens *)
   147 
   148 val not_eof = Scan.one T.not_eof;
   149 
   150 fun position scan = (Scan.ahead not_eof >> T.position_of) -- scan >> Library.swap;
   151 
   152 fun kind k =
   153   group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
   154 
   155 val command = kind T.Command;
   156 val keyword = kind T.Keyword;
   157 val short_ident = kind T.Ident;
   158 val long_ident = kind T.LongIdent;
   159 val sym_ident = kind T.SymIdent;
   160 val term_var = kind T.Var;
   161 val type_ident = kind T.TypeIdent;
   162 val type_var = kind T.TypeVar;
   163 val number = kind T.Nat;
   164 val string = kind T.String;
   165 val alt_string = kind T.AltString;
   166 val verbatim = kind T.Verbatim;
   167 val sync = kind T.Sync;
   168 val eof = kind T.EOF;
   169 
   170 fun $$$ x =
   171   group (T.str_of_kind T.Keyword ^ " " ^ quote x)
   172     (Scan.one (T.keyword_with (equal x)) >> T.val_of);
   173 
   174 fun reserved x =
   175   group ("Reserved identifier " ^ quote x)
   176     (Scan.one (T.ident_with (equal x)) >> T.val_of);
   177 
   178 val semicolon = $$$ ";";
   179 
   180 val minus = sym_ident :-- (fn "-" => Scan.succeed () | _ => Scan.fail) >> #1;
   181 val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   182 fun maybe scan = underscore >> K NONE || scan >> SOME;
   183 
   184 val nat = number >> (#1 o Library.read_int o Symbol.explode);
   185 
   186 val tag_name = group "tag name" (short_ident || string);
   187 val tags = Scan.repeat ($$$ "%" |-- !!! tag_name);
   188 
   189 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   190 fun opt_keyword s = Scan.optional ($$$ "(" |-- !!! (($$$ s >> K true) --| $$$ ")")) false;
   191 
   192 
   193 (* enumerations *)
   194 
   195 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   196 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   197 
   198 fun list1 scan = enum1 "," scan;
   199 fun list scan = enum "," scan;
   200 
   201 fun and_list1 scan = enum1 "and" scan;
   202 fun and_list scan = enum "and" scan;
   203 
   204 
   205 (* names and text *)
   206 
   207 val name = group "name declaration" (short_ident || sym_ident || string || number);
   208 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   209 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   210 val path = group "file name/path specification" name >> Path.unpack;
   211 
   212 val parname = Scan.optional ($$$ "(" |-- name --| $$$ ")") "";
   213 
   214 
   215 (* sorts and arities *)
   216 
   217 val sort = group "sort" xname;
   218 
   219 fun gen_arity cod =
   220   Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
   221 
   222 val arity = gen_arity sort;
   223 
   224 
   225 (* types *)
   226 
   227 val typ = group "type"
   228   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   229 
   230 val type_args =
   231   type_ident >> single ||
   232   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   233   Scan.succeed [];
   234 
   235 
   236 (* mixfix annotations *)
   237 
   238 val mfix = string --
   239   !!! (Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [] --
   240     Scan.optional nat Syntax.max_pri) >> (Mixfix o triple2);
   241 
   242 val infx = $$$ "infix" |-- !!! (nat >> Infix || string -- nat >> InfixName);
   243 val infxl = $$$ "infixl" |-- !!! (nat >> Infixl || string -- nat >> InfixlName);
   244 val infxr = $$$ "infixr" |-- !!! (nat >> Infixr || string -- nat >> InfixrName);
   245 
   246 val binder = $$$ "binder" |--
   247   !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   248   >> (Binder o triple2);
   249 
   250 fun annotation guard fix = $$$ "(" |-- guard (fix --| $$$ ")");
   251 fun opt_annotation guard fix = Scan.optional (annotation guard fix) NoSyn;
   252 
   253 val mixfix = annotation !!! (mfix || binder || infxl || infxr || infx);
   254 val opt_infix = opt_annotation !!! (infxl || infxr || infx);
   255 val opt_mixfix = opt_annotation !!! (mfix || binder || infxl || infxr || infx);
   256 val opt_infix' = opt_annotation I (infxl || infxr || infx);
   257 val opt_mixfix' = opt_annotation I (mfix || binder || infxl || infxr || infx);
   258 
   259 
   260 (* fixes *)
   261 
   262 val const = name -- ($$$ "::" |-- !!! typ) -- opt_mixfix >> triple1;
   263 
   264 val params = Scan.repeat1 name -- Scan.option ($$$ "::" |-- !!! typ)
   265   >> (fn (xs, T) => map (rpair T) xs);
   266 
   267 val simple_fixes = and_list1 params >> flat;
   268 
   269 val fixes =
   270   and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- mixfix >> (single o triple1) ||
   271     params >> map Syntax.no_syn) >> flat;
   272 
   273 val for_fixes = Scan.optional ($$$ "for" |-- !!! fixes) [];
   274 
   275 
   276 (* terms *)
   277 
   278 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   279 
   280 val term = group "term" trm;
   281 val prop = group "proposition" trm;
   282 
   283 
   284 (* patterns *)
   285 
   286 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   287 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   288 
   289 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   290 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   291 
   292 
   293 (* arguments *)
   294 
   295 fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
   296 val keyword_sid = keyword_symid T.is_sid;
   297 
   298 fun atom_arg is_symid blk =
   299   group "argument"
   300     (position (short_ident || long_ident || sym_ident || term_var ||
   301         type_ident || type_var || number) >> Args.mk_ident ||
   302       position (keyword_symid is_symid) >> Args.mk_keyword ||
   303       position (string || verbatim) >> Args.mk_string ||
   304       position alt_string >> Args.mk_alt_string ||
   305       position (if blk then $$$ "," else Scan.fail) >> Args.mk_keyword);
   306 
   307 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   308   >> (fn (x, (ys, z)) => Args.mk_keyword x :: ys @ [Args.mk_keyword z]);
   309 
   310 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
   311 and args1 is_symid blk x =
   312   ((Scan.repeat1
   313     (Scan.repeat1 (atom_arg is_symid blk) ||
   314       paren_args "(" ")" (args is_symid) ||
   315       paren_args "[" "]" (args is_symid))) >> flat) x;
   316 
   317 val arguments = args T.is_sid false;
   318 
   319 
   320 (* theorem specifications *)
   321 
   322 val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
   323 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   324 val opt_attribs = Scan.optional attribs [];
   325 
   326 fun thm_name s = name -- opt_attribs --| $$$ s;
   327 fun opt_thm_name s =
   328   Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);
   329 
   330 val spec = opt_thm_name ":" -- Scan.repeat1 prop;
   331 val named_spec = thm_name ":" -- Scan.repeat1 prop;
   332 
   333 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   334 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   335 
   336 val thm_sel = $$$ "(" |-- list1
   337  (nat --| minus -- nat >> PureThy.FromTo ||
   338   nat --| minus >> PureThy.From ||
   339   nat >> PureThy.Single) --| $$$ ")";
   340 
   341 val xthm = (alt_string >> Fact || xname -- thm_sel >> NameSelection || xname >> Name)
   342   -- opt_attribs;
   343 val xthms1 = Scan.repeat1 xthm;
   344 
   345 val name_facts = and_list1 (opt_thm_name "=" -- xthms1);
   346 
   347 
   348 (* locale and context elements *)
   349 
   350 val locale_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K Structure || mixfix;
   351 
   352 val locale_fixes =
   353   and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- locale_mixfix >> (single o triple1) ||
   354     params >> map Syntax.no_syn) >> flat;
   355 
   356 val locale_insts =
   357   Scan.optional ($$$ "[" |-- !!! (Scan.repeat1 (maybe term) --| $$$ "]")) [];
   358 
   359 local
   360 
   361 val loc_keyword = $$$ "fixes" || $$$ "constrains" || $$$ "assumes" ||
   362    $$$ "defines" || $$$ "notes" || $$$ "includes";
   363 
   364 val loc_element =
   365   $$$ "fixes" |-- !!! locale_fixes >> Element.Fixes ||
   366   $$$ "constrains" |-- !!! (and_list1 (name -- ($$$ "::" |-- typ)))
   367     >> Element.Constrains ||
   368   $$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp))
   369     >> Element.Assumes ||
   370   $$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp))
   371     >> Element.Defines ||
   372   $$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1))
   373     >> Element.Notes;
   374 
   375 fun plus1 test scan =
   376   scan -- Scan.repeat ($$$ "+" |-- Scan.unless test (!!! scan)) >> op ::;
   377 
   378 val rename = name -- Scan.option mixfix;
   379 
   380 fun expr test =
   381   let 
   382     fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x
   383     and expr1 x = (expr2 -- Scan.repeat1 (maybe rename) >> Locale.Rename || expr2) x
   384     and expr0 x = (plus1 test expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   385   in expr0 end;
   386 in
   387 
   388 val locale_target = ($$$ "(" -- $$$ "in") |-- !!! (xname --| $$$ ")");
   389 val opt_locale_target = Scan.option locale_target;
   390 val locale_expr_unless = expr
   391 val locale_expr = expr loc_keyword;
   392 val locale_keyword = loc_keyword;
   393 
   394 val locale_element = group "locale element"
   395   (loc_element >> Locale.Elem || $$$ "includes" |-- !!! locale_expr >> Locale.Expr);
   396 
   397 val context_element = group "context element" loc_element;
   398 
   399 end;
   400 
   401 
   402 (* statements *)
   403 
   404 val statement = and_list1 (opt_thm_name ":" -- Scan.repeat1 propp);
   405 
   406 val obtain_case =
   407   parname -- (Scan.optional (simple_fixes --| $$$ "where") [] --
   408     (and_list1 (Scan.repeat1 prop) >> flat));
   409 
   410 val general_statement =
   411   statement >> (fn x => ([], Element.Shows x)) ||
   412   Scan.repeat locale_element --
   413    ($$$ "obtains" |-- !!! (enum1 "|" obtain_case) >> Element.Obtains ||
   414     $$$ "shows" |-- !!! statement >> Element.Shows);
   415 
   416 val statement_keyword =  $$$ "obtains" || $$$ "shows";
   417 
   418 
   419 (* proof methods *)
   420 
   421 fun is_symid_meth s =
   422   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   423 
   424 fun meth4 x =
   425  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   426   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   427 and meth3 x =
   428  (meth4 --| $$$ "?" >> Method.Try ||
   429   meth4 --| $$$ "+" >> Method.Repeat1 ||
   430   meth4 -- ($$$ "[" |-- Scan.optional nat 1 --| $$$ "]") >> (Method.SelectGoals o swap) ||
   431   meth4) x
   432 and meth2 x =
   433  (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
   434   meth3) x
   435 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   436 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   437 
   438 val method = meth3;
   439 
   440 
   441 end;