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