src/Pure/Isar/outer_parse.ML
author wenzelm
Wed Dec 05 03:18:03 2001 +0100 (2001-12-05 ago)
changeset 12385 389d11fb62c8
parent 12272 9bc50336dab6
child 12876 a70df1e5bf10
permissions -rw-r--r--
removed unused functionality (weight etc.);
     1 (*  Title:      Pure/Isar/outer_parse.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Generic parsers for Isabelle/Isar outer syntax.
     7 *)
     8 
     9 signature OUTER_PARSE =
    10 sig
    11   type token
    12   val group: string -> (token list -> 'a) -> token list -> 'a
    13   val !!! : (token list -> 'a) -> token list -> 'a
    14   val !!!! : (token list -> 'a) -> token list -> 'a
    15   val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
    16   val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
    17   val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
    18   val $$$ : string -> token list -> string * token list
    19   val semicolon: token list -> string * token list
    20   val underscore: token list -> string * token list
    21   val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
    22   val command: token list -> string * token list
    23   val keyword: token list -> string * token list
    24   val short_ident: token list -> string * token list
    25   val long_ident: token list -> string * token list
    26   val sym_ident: token list -> string * token list
    27   val term_var: token list -> string * token list
    28   val type_ident: token list -> string * token list
    29   val type_var: token list -> string * token list
    30   val number: token list -> string * token list
    31   val string: token list -> string * token list
    32   val verbatim: token list -> string * token list
    33   val sync: token list -> string * token list
    34   val eof: token list -> string * token list
    35   val not_eof: token list -> token * token list
    36   val opt_unit: token list -> unit * token list
    37   val nat: token list -> int * token list
    38   val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    39   val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
    40   val list: (token list -> 'a * token list) -> token list -> 'a list * token list
    41   val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    42   val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
    43   val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
    44   val name: token list -> bstring * token list
    45   val xname: token list -> xstring * token list
    46   val text: token list -> string * token list
    47   val uname: token list -> string option * token list
    48   val comment: token list -> Comment.text * token list
    49   val marg_comment: token list -> Comment.text * token list
    50   val interest: token list -> Comment.interest * token list
    51   val sort: token list -> string * token list
    52   val arity: token list -> (string list * string) * token list
    53   val simple_arity: token list -> (string list * xclass) * token list
    54   val type_args: token list -> string list * token list
    55   val typ: token list -> string * token list
    56   val opt_infix: token list -> Syntax.mixfix * token list
    57   val opt_mixfix: token list -> Syntax.mixfix * token list
    58   val opt_infix': token list -> Syntax.mixfix * token list
    59   val opt_mixfix': token list -> Syntax.mixfix * token list
    60   val const: token list -> (bstring * string * Syntax.mixfix) * token list
    61   val term: token list -> string * token list
    62   val prop: token list -> string * token list
    63   val propp: token list -> (string * (string list * string list)) * token list
    64   val termp: token list -> (string * string list) * token list
    65   val arguments: token list -> Args.T list * token list
    66   val attribs: token list -> Args.src list * token list
    67   val opt_attribs: token list -> Args.src list * token list
    68   val thm_name: string -> token list -> (bstring * Args.src list) * token list
    69   val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
    70   val spec_name: token list -> ((bstring * string) * Args.src list) * token list
    71   val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
    72   val xthm: token list -> (xstring * Args.src list) * token list
    73   val xthms1: token list -> (xstring * Args.src list) list * token list
    74   val locale_expr: token list -> Locale.expr * token list
    75   val locale_element: token list -> Args.src Locale.element * token list
    76   val method: token list -> Method.text * token list
    77 end;
    78 
    79 structure OuterParse: OUTER_PARSE =
    80 struct
    81 
    82 structure T = OuterLex;
    83 type token = T.token;
    84 
    85 
    86 (** error handling **)
    87 
    88 (* group atomic parsers (no cuts!) *)
    89 
    90 fun fail_with s = Scan.fail_with
    91   (fn [] => s ^ " expected (past end-of-file!)"
    92     | (tok :: _) => s ^ " expected,\nbut " ^ T.name_of tok ^ T.pos_of tok ^ " was found");
    93 
    94 fun group s scan = scan || fail_with s;
    95 
    96 
    97 (* cut *)
    98 
    99 fun cut kind scan =
   100   let
   101     fun get_pos [] = " (past end-of-file!)"
   102       | get_pos (tok :: _) = T.pos_of tok;
   103 
   104     fun err (toks, None) = kind ^ get_pos toks
   105       | err (toks, Some msg) = kind ^ get_pos toks ^ ": " ^ msg;
   106   in Scan.!! err scan end;
   107 
   108 fun !!! scan = cut "Outer syntax error" scan;
   109 fun !!!! scan = cut "Corrupted outer syntax in presentation" scan;
   110 
   111 
   112 
   113 (** basic parsers **)
   114 
   115 (* utils *)
   116 
   117 fun triple1 ((x, y), z) = (x, y, z);
   118 fun triple2 (x, (y, z)) = (x, y, z);
   119 fun triple_swap ((x, y), z) = ((x, z), y);
   120 
   121 
   122 (* tokens *)
   123 
   124 fun position scan =
   125   (Scan.ahead (Scan.one T.not_eof) >> T.position_of) -- scan >> Library.swap;
   126 
   127 fun kind k =
   128   group (T.str_of_kind k) (Scan.one (T.is_kind k) >> T.val_of);
   129 
   130 val command = kind T.Command;
   131 val keyword = kind T.Keyword;
   132 val short_ident = kind T.Ident;
   133 val long_ident = kind T.LongIdent;
   134 val sym_ident = kind T.SymIdent;
   135 val term_var = kind T.Var;
   136 val type_ident = kind T.TypeIdent;
   137 val type_var = kind T.TypeVar;
   138 val number = kind T.Nat;
   139 val string = kind T.String;
   140 val verbatim = kind T.Verbatim;
   141 val sync = kind T.Sync;
   142 val eof = kind T.EOF;
   143 
   144 fun $$$ x =
   145   group (T.str_of_kind T.Keyword ^ " " ^ quote x)
   146     (Scan.one (T.keyword_with (equal x)) >> T.val_of);
   147 
   148 val semicolon = $$$ ";";
   149 
   150 val underscore = sym_ident :-- (fn "_" => Scan.succeed () | _ => Scan.fail) >> #1;
   151 
   152 val nat = number >> (fst o Term.read_int o Symbol.explode);
   153 
   154 val not_eof = Scan.one T.not_eof;
   155 
   156 val opt_unit = Scan.optional ($$$ "(" -- $$$ ")" >> (K ())) ();
   157 
   158 
   159 (* enumerations *)
   160 
   161 fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
   162 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   163 
   164 fun list1 scan = enum1 "," scan;
   165 fun list scan = enum "," scan;
   166 
   167 fun and_list1 scan = enum1 "and" scan;
   168 fun and_list scan = enum "and" scan;
   169 
   170 
   171 (* names and text *)
   172 
   173 val name = group "name declaration" (short_ident || sym_ident || string || number);
   174 val xname = group "name reference" (short_ident || long_ident || sym_ident || string || number);
   175 val text = group "text" (short_ident || long_ident || sym_ident || string || number || verbatim);
   176 
   177 val uname = underscore >> K None || name >> Some;
   178 
   179 
   180 (* formal comments *)
   181 
   182 val comment = text >> (Comment.plain o Library.single);
   183 val marg_comment = Scan.repeat ($$$ "--" |-- text) >> Comment.plain;
   184 
   185 val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest ||
   186   $$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest;
   187 
   188 
   189 (* sorts and arities *)
   190 
   191 val sort = group "sort" xname;
   192 
   193 fun gen_arity cod =
   194   Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
   195 
   196 val arity = gen_arity sort;
   197 val simple_arity = gen_arity xname;
   198 
   199 
   200 (* types *)
   201 
   202 val typ = group "type"
   203   (short_ident || long_ident || sym_ident || type_ident || type_var || string || number);
   204 
   205 val type_args =
   206   type_ident >> single ||
   207   $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
   208   Scan.succeed [];
   209 
   210 
   211 (* mixfix annotations *)
   212 
   213 val infx = $$$ "infix" |-- !!! (nat >> Syntax.Infix || string -- nat >> Syntax.InfixName);
   214 val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
   215 val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
   216 
   217 val binder =
   218   $$$ "binder" |--
   219     !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
   220   >> (Syntax.Binder o triple2);
   221 
   222 
   223 val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
   224 
   225 val mixfix =
   226   (string -- !!! (opt_pris -- Scan.optional nat Syntax.max_pri))
   227   >> (Syntax.Mixfix o triple2);
   228 
   229 fun opt_fix guard fix =
   230   Scan.optional ($$$ "(" |-- guard (fix --| $$$ ")")) Syntax.NoSyn;
   231 
   232 val opt_infix = opt_fix !!! (infxl || infxr || infx);
   233 val opt_mixfix = opt_fix !!! (mixfix || binder || infxl || infxr || infx);
   234 
   235 val opt_infix' = opt_fix I (infxl || infxr || infx);
   236 val opt_mixfix' = opt_fix I (mixfix || binder || infxl || infxr || infx);
   237 
   238 
   239 (* consts *)
   240 
   241 val const =
   242   name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
   243 
   244 
   245 (* terms *)
   246 
   247 val trm = short_ident || long_ident || sym_ident || term_var || number || string;
   248 
   249 val term = group "term" trm;
   250 val prop = group "proposition" trm;
   251 
   252 
   253 (* patterns *)
   254 
   255 val is_terms = Scan.repeat1 ($$$ "is" |-- term);
   256 val is_props = Scan.repeat1 ($$$ "is" |-- prop);
   257 val concl_props = $$$ "concl" |-- !!! is_props;
   258 val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
   259 
   260 val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
   261 val propp' = prop -- Scan.optional ($$$ "(" |-- !!! (is_props --| $$$ ")")) [];
   262 val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
   263 
   264 
   265 (* arguments *)
   266 
   267 fun keyword_symid is_symid = Scan.one (T.keyword_with is_symid) >> T.val_of;
   268 val keyword_sid = keyword_symid T.is_sid;
   269 
   270 fun atom_arg is_symid blk =
   271   group "argument"
   272     (position (short_ident || long_ident || sym_ident || term_var ||
   273         type_ident || type_var || number) >> Args.ident ||
   274       position (keyword_symid is_symid) >> Args.keyword ||
   275       position (string || verbatim) >> Args.string ||
   276       position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
   277 
   278 fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
   279   >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
   280 
   281 fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
   282 and args1 is_symid blk x =
   283   ((Scan.repeat1
   284     (Scan.repeat1 (atom_arg is_symid blk) ||
   285       paren_args "(" ")" (args is_symid) ||
   286       paren_args "[" "]" (args is_symid))) >> flat) x;
   287 
   288 val arguments = args T.is_sid false;
   289 
   290 
   291 (* theorem specifications *)
   292 
   293 val attrib = position ((keyword_sid || xname) -- !!! arguments) >> Args.src;
   294 val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
   295 val opt_attribs = Scan.optional attribs [];
   296 
   297 fun thm_name s = name -- opt_attribs --| $$$ s;
   298 fun opt_thm_name s =
   299   Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
   300 
   301 val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   302 val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
   303 
   304 val xthm = xname -- opt_attribs;
   305 val xthms1 = Scan.repeat1 xthm;
   306 
   307 
   308 (* locale elements *)
   309 
   310 local
   311 
   312 val loc_mixfix = $$$ "(" -- $$$ "structure" -- !!! ($$$ ")") >> K None || opt_mixfix >> Some;
   313 val loc_keyword = $$$ "fixes" || $$$ "assumes" || $$$ "defines" || $$$ "notes" || $$$ "uses";
   314 
   315 fun plus1 scan =
   316   scan -- Scan.repeat ($$$ "+" |-- Scan.unless loc_keyword (!!! scan)) >> op ::;
   317 
   318 fun expr2 x = (xname >> Locale.Locale || $$$ "(" |-- !!! (expr0 --| $$$ ")")) x
   319 and expr1 x = (expr2 -- Scan.repeat1 uname >> Locale.Rename || expr2) x
   320 and expr0 x = (plus1 expr1 >> (fn [e] => e | es => Locale.Merge es)) x;
   321 
   322 in
   323 
   324 val locale_expr = expr0;
   325 
   326 val locale_element = group "locale element"
   327   ($$$ "fixes" |-- !!! (and_list1 (name -- Scan.option ($$$ "::" |-- typ) -- loc_mixfix
   328     >> triple1)) >> (Locale.Elem o Locale.Fixes) ||
   329   $$$ "assumes" |-- !!! (and_list1 (opt_thm_name ":" -- Scan.repeat1 propp))
   330     >> (Locale.Elem o Locale.Assumes) ||
   331   $$$ "defines" |-- !!! (and_list1 (opt_thm_name ":" -- propp'))
   332     >> (Locale.Elem o Locale.Defines) ||
   333   $$$ "notes" |-- !!! (and_list1 (opt_thm_name "=" -- xthms1))
   334     >> (Locale.Elem o Locale.Notes) ||
   335   $$$ "uses" |-- !!! locale_expr >> Locale.Expr);
   336 
   337 end;
   338 
   339 
   340 (* proof methods *)
   341 
   342 fun is_symid_meth s =
   343   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   344 
   345 fun meth4 x =
   346  (position (xname >> rpair []) >> (Method.Source o Args.src) ||
   347   $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
   348 and meth3 x =
   349  (meth4 --| $$$ "?" >> Method.Try ||
   350   meth4 --| $$$ "+" >> Method.Repeat1 ||
   351   meth4) x
   352 and meth2 x =
   353  (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
   354   meth3) x
   355 and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
   356 and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
   357 
   358 val method = meth3;
   359 
   360 
   361 end;