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