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