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