src/Pure/Thy/thy_parse.ML
author wenzelm
Thu Jun 16 12:07:40 1994 +0200 (1994-06-16)
changeset 425 c42f384c89de
parent 414 9dca566d6d96
child 451 9ebdead316e0
permissions -rw-r--r--
added 'subclass' section;
minor internal cleanups;
     1 (*  Title:      Pure/Thy/thy_parse.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The parser for theory files.
     6 *)
     7 
     8 infix 5 -- --$$ $$-- ^^;
     9 infix 3 >>;
    10 infix 0 ||;
    11 
    12 signature THY_PARSE =
    13 sig
    14   type token
    15   val !! : ('a -> 'b * 'c) -> 'a -> 'b * 'c
    16   val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
    17   val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
    18   val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
    19   val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
    20   val $$ : string -> token list -> string * token list
    21   val $$-- : string * (token list -> 'b * 'c) -> token list -> 'b * 'c
    22   val --$$ : ('a -> 'b * token list) * string -> 'a -> 'b * token list
    23   val ident: token list -> string * token list
    24   val long_ident: token list -> string * token list
    25   val long_id: token list -> string * token list
    26   val type_var: token list -> string * token list
    27   val nat: token list -> string * token list
    28   val string: token list -> string * token list
    29   val verbatim: token list -> string * token list
    30   val empty: 'a -> 'b list * 'a
    31   val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
    32   val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    33   val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    34   val enum: string -> (token list -> 'a * token list)
    35     -> token list -> 'a list * token list
    36   val enum1: string -> (token list -> 'a * token list)
    37     -> token list -> 'a list * token list
    38   val list: (token list -> 'a * token list)
    39     -> token list -> 'a list * token list
    40   val list1: (token list -> 'a * token list)
    41     -> token list -> 'a list * token list
    42   val name: token list -> string * token list
    43   val sort: token list -> string * token list
    44   type syntax
    45   val make_syntax: string list ->
    46     (string * (token list -> (string * string) * token list)) list -> syntax
    47   val parse_thy: syntax -> string -> string
    48   val section: string -> string -> (token list -> string * token list)
    49     -> (string * (token list -> (string * string) * token list))
    50   val axm_section: string -> string
    51     -> (token list -> (string * string list) * token list)
    52     -> (string * (token list -> (string * string) * token list))
    53   val pure_keywords: string list
    54   val pure_sections:
    55     (string * (token list -> (string * string) * token list)) list
    56 end;
    57 
    58 functor ThyParseFun(structure Symtab: SYMTAB and ThyScan: THY_SCAN): THY_PARSE =
    59 struct
    60 
    61 open ThyScan;
    62 
    63 
    64 (** parser toolbox **)
    65 
    66 type token = token_kind * string * int;
    67 
    68 
    69 (* errors *)
    70 
    71 exception SYNTAX_ERROR of string * string * int;
    72 
    73 fun syn_err s1 s2 n = raise SYNTAX_ERROR (s1, s2, n);
    74 
    75 fun eof_err () = error "Unexpected end-of-file";
    76 
    77 fun !! parse toks = parse toks
    78   handle SYNTAX_ERROR (s1, s2, n) => error ("Syntax error on line " ^
    79     string_of_int n ^ ": " ^ s1 ^ " expected and " ^ s2 ^ " was found");
    80 
    81 
    82 (* parser combinators *)
    83 
    84 fun (parse >> f) toks = apfst f (parse toks);
    85 
    86 fun (parse1 || parse2) toks =
    87   parse1 toks handle SYNTAX_ERROR _ => parse2 toks;
    88 
    89 fun (parse1 -- parse2) toks =
    90   let
    91     val (x, toks') = parse1 toks;
    92     val (y, toks'') = parse2 toks';
    93   in
    94     ((x, y), toks'')
    95   end;
    96 
    97 fun (parse1 ^^ parse2) = parse1 -- parse2 >> op ^;
    98 
    99 
   100 (* generic parsers *)
   101 
   102 fun $$ a ((k, b, n) :: toks) =
   103       if k = Keyword andalso a = b then (a, toks)
   104       else syn_err (quote a) (quote b) n
   105   | $$ _ [] = eof_err ();
   106 
   107 fun (a $$-- parse) = $$ a -- parse >> #2;
   108 
   109 fun (parse --$$ a) = parse -- $$ a >> #1;
   110 
   111 
   112 fun kind k1 ((k2, s, n) :: toks) =
   113       if k1 = k2 then (s, toks)
   114       else syn_err (name_of_kind k1) (quote s) n
   115   | kind _ [] = eof_err ();
   116 
   117 val ident = kind Ident;
   118 val long_ident = kind LongIdent;
   119 val long_id = ident || long_ident;
   120 val type_var = kind TypeVar >> quote;
   121 val nat = kind Nat;
   122 val string = kind String;
   123 val verbatim = kind Verbatim;
   124 val eof = kind EOF;
   125 
   126 fun empty toks = ([], toks);
   127 
   128 fun optional parse def = parse || empty >> K def;
   129 
   130 fun repeat parse toks = (parse -- repeat parse >> op :: || empty) toks;
   131 fun repeat1 parse = parse -- repeat parse >> op ::;
   132 
   133 fun enum1 sep parse = parse -- repeat (sep $$-- parse) >> op ::;
   134 fun enum sep parse = enum1 sep parse || empty;
   135 
   136 val list = enum ",";
   137 val list1 = enum1 ",";
   138 
   139 
   140 
   141 (** theory parsers **)
   142 
   143 (* misc utilities *)
   144 
   145 fun cat s1 s2 = s1 ^ " " ^ s2;
   146 
   147 val pars = parents "(" ")";
   148 val brackets = parents "[" "]";
   149 
   150 val mk_list = brackets o commas;
   151 val mk_big_list = brackets o space_implode ",\n ";
   152 
   153 fun mk_pair (x, y) = pars (commas [x, y]);
   154 fun mk_triple (x, y, z) = pars (commas [x, y, z]);
   155 fun mk_triple1 ((x, y), z) = mk_triple (x, y, z);
   156 fun mk_triple2 (x, (y, z)) = mk_triple (x, y, z);
   157 
   158 val split_decls = flat o map (fn (xs, y) => map (rpair y) xs);
   159 
   160 fun strip_quotes str =
   161   implode (tl (take (size str - 1, explode str)));
   162 
   163 
   164 (* names *)
   165 
   166 val name = ident >> quote || string;
   167 val names = list name;
   168 val names1 = list1 name;
   169 val name_list = names >> mk_list;
   170 val name_list1 = names1 >> mk_list;
   171 
   172 
   173 (* classes *)
   174 
   175 fun mk_subclass (c, cs) = mk_triple ("[]", c, cs);
   176 
   177 val subclass = name -- optional ("<" $$-- !! name_list1) "[]";
   178 
   179 val class_decls = repeat1 (subclass >> mk_subclass) >> mk_big_list;
   180 
   181 
   182 (* arities *)
   183 
   184 val sort =
   185   name >> brackets ||
   186   "{" $$-- name_list --$$ "}";
   187 
   188 val sort_list1 = list1 sort >> mk_list;
   189 
   190 
   191 val arity = optional ("(" $$-- !! (sort_list1 --$$")")) "[]" -- sort;
   192 
   193 val arity_decls = repeat1 (names1 --$$ "::" -- !! arity)
   194   >> (mk_big_list o map mk_triple2 o split_decls);
   195 
   196 
   197 (* mixfix annotations *)
   198 
   199 val infxl = "infixl" $$-- !! nat >> cat "Infixl";
   200 val infxr = "infixr" $$-- !! nat >> cat "Infixr";
   201 
   202 val binder = "binder" $$-- !! (string -- nat) >> (cat "Binder" o mk_pair);
   203 
   204 val opt_pris = optional ("[" $$-- !! (list nat --$$ "]")) [] >> mk_list;
   205 
   206 val mixfix = string -- !! (opt_pris -- optional nat "max_pri")
   207   >> (cat "Mixfix" o mk_triple2);
   208 
   209 fun opt_syn fx =
   210   "(" $$-- fx --$$ ")" ||
   211   empty >> K "NoSyn";
   212 
   213 val opt_infix = opt_syn (infxl || infxr);
   214 val opt_mixfix = opt_syn (mixfix || infxl || infxr || binder);
   215 
   216 
   217 (* types *)
   218 
   219 fun mk_old_type_decl ((ts, n), syn) =
   220   map (fn t => (mk_triple (t, n, syn), false)) ts;
   221 
   222 fun mk_type_decl (((xs, t), None), syn) =
   223       [(mk_triple (t, string_of_int (length xs), syn), false)]
   224   | mk_type_decl (((xs, t), Some rhs), syn) =
   225       [(pars (commas [t, mk_list xs, rhs, syn]), true)];
   226 
   227 fun mk_type_decls tys =
   228   "|> add_types\n" ^ mk_big_list (keyfilter tys false) ^ "\n\n\
   229   \|> add_tyabbrs\n" ^ mk_big_list (keyfilter tys true);
   230 
   231 
   232 val old_type_decl = names1 -- nat -- opt_infix >> mk_old_type_decl;
   233 
   234 val type_args =
   235   type_var >> (fn x => [x]) ||
   236   "(" $$-- !! (list1 type_var --$$ ")") ||
   237   empty >> K [];
   238 
   239 val type_decl = type_args -- name -- optional ("=" $$-- !! string >> Some) None
   240   -- opt_infix >> mk_type_decl;
   241 
   242 val type_decls = repeat1 (old_type_decl || type_decl)
   243   >> (rpair "" o mk_type_decls o flat);
   244 
   245 
   246 (* consts *)
   247 
   248 val const_decls = repeat1 (names1 --$$ "::" -- !! (string -- opt_mixfix))
   249   >> (mk_big_list o map mk_triple2 o split_decls);
   250 
   251 
   252 (* translations *)
   253 
   254 val trans_pat =
   255   optional ("(" $$-- !! (name --$$ ")")) "\"logic\"" -- string >> mk_pair;
   256 
   257 val trans_arrow =
   258   $$ "=>" >> K " |-> " ||
   259   $$ "<=" >> K " <-| " ||
   260   $$ "==" >> K " <-> ";
   261 
   262 val trans_decls = repeat1 (trans_pat ^^ !! (trans_arrow ^^ trans_pat))
   263   >> mk_big_list;
   264 
   265 
   266 (* ML translations *)
   267 
   268 val trfun_defs =
   269   " val parse_ast_translation = [];\n\
   270   \ val parse_translation = [];\n\
   271   \ val print_translation = [];\n\
   272   \ val print_ast_translation = [];";
   273 
   274 val trfun_args =
   275   "(parse_ast_translation, parse_translation, \
   276   \print_translation, print_ast_translation)";
   277 
   278 fun mk_mltrans txt =
   279   "let\n"
   280   ^ trfun_defs ^ "\n"
   281   ^ txt ^ "\n\
   282   \in\n\
   283   \ " ^ trfun_args ^ "\n\
   284   \end";
   285 
   286 val mltrans = verbatim >> mk_mltrans;
   287 
   288 
   289 (* axioms *)
   290 
   291 val mk_axms = mk_big_list o map (mk_pair o apfst quote);
   292 
   293 fun mk_axiom_decls axms = (mk_axms axms, map fst axms);
   294 
   295 val axiom_decls = repeat1 (ident -- !! string) >> mk_axiom_decls;
   296 
   297 
   298 (* axclass *)
   299 
   300 fun mk_axclass_decl ((c, cs), axms) =
   301   (mk_pair (c, cs) ^ "\n" ^ mk_axms axms,
   302     (strip_quotes c ^ "I") :: map fst axms);
   303 
   304 val axclass_decl = subclass -- repeat (ident -- !! string) >> mk_axclass_decl;
   305 
   306 
   307 (* sigclass *)
   308 
   309 fun mk_sigclass_decl ((c, cs), consts) =
   310   (mk_pair (c, cs) ^ "\n" ^ consts, [strip_quotes c ^ "I"]);
   311 
   312 val sigclass_decl = subclass -- optional const_decls "[]" >> mk_sigclass_decl;
   313 
   314 
   315 (* subclass, instance *)
   316 
   317 fun mk_witness (axths, opt_tac) =
   318   mk_list (keyfilter axths false) ^ "\n" ^
   319   mk_list (keyfilter axths true) ^ "\n" ^
   320   opt_tac;
   321 
   322 val axm_or_thm =
   323   string >> rpair false ||
   324   long_id >> rpair true;
   325 
   326 val opt_witness =
   327   optional ("(" $$-- list1 axm_or_thm --$$ ")") [] --
   328   optional (verbatim >> (pars o cat "Some" o pars)) "None"
   329   >> mk_witness;
   330 
   331 
   332 val subclass_decl =
   333   name --$$ "<" -- name -- opt_witness
   334   >> (fn ((c1, c2), witn) => mk_pair (c1, c2) ^ "\n" ^ witn);
   335 
   336 val instance_decl =
   337   name --$$ "::" -- arity -- opt_witness
   338   >> (fn ((t, (ss, s)), wit) => mk_triple (t, ss, s) ^ "\n" ^ wit);
   339 
   340 
   341 
   342 (** theory syntax **)
   343 
   344 type syntax =
   345   lexicon * (token list -> (string * string) * token list) Symtab.table;
   346 
   347 fun make_syntax keywords sects =
   348   (make_lexicon keywords, Symtab.make sects handle Symtab.DUPS dups
   349     => error ("Duplicate sections in thy syntax: " ^ commas_quote dups));
   350 
   351 
   352 (* header *)
   353 
   354 fun mk_header (thy_name, bases) =
   355   (thy_name, "base_on " ^ mk_list bases ^ " " ^ quote thy_name);
   356 
   357 val base =
   358   ident >> (cat "Thy" o quote) ||
   359   string >> cat "File";
   360 
   361 val header = ident --$$ "=" -- enum1 "+" base >> mk_header;
   362 
   363 
   364 (* extension *)
   365 
   366 fun mk_extension (txts, mltxt) =
   367   let
   368     val cat_sects = space_implode "\n\n" o filter_out (equal "");
   369     val (extxts, postxts) = split_list txts;
   370   in
   371     (cat_sects extxts, cat_sects postxts, mltxt)
   372   end;
   373 
   374 fun sect tab ((Keyword, s, n) :: toks) =
   375       (case Symtab.lookup (tab, s) of
   376         Some parse => !! parse toks
   377       | None => syn_err "section" s n)
   378   | sect _ ((_, s, n) :: _) = syn_err "section" s n
   379   | sect _ [] = eof_err ();
   380 
   381 fun extension sectab = "+" $$-- !! (repeat (sect sectab) --$$ "end") --
   382   optional ("ML" $$-- verbatim) "" >> mk_extension;
   383 
   384 
   385 (* theory definition *)
   386 
   387 fun mk_structure ((thy_name, old_thys), Some (extxt, postxt, mltxt)) =
   388       "structure " ^ thy_name ^ " =\n\
   389       \struct\n\
   390       \\n\
   391       \local\n"
   392       ^ trfun_defs ^ "\n\
   393       \in\n\
   394       \\n"
   395       ^ mltxt ^ "\n\
   396       \\n\
   397       \val thy = (" ^ old_thys ^ " true)\n\n\
   398       \|> add_trfuns\n"
   399       ^ trfun_args ^ "\n\
   400       \\n"
   401       ^ extxt ^ "\n\
   402       \\n\
   403       \|> add_thyname " ^ quote thy_name ^ ";\n\
   404       \\n\
   405       \\n"
   406       ^ postxt ^ "\n\
   407       \\n\
   408       \end;\n\
   409       \end;\n"
   410   | mk_structure ((thy_name, old_thys), None) =
   411       "structure " ^ thy_name ^ " =\n\
   412       \struct\n\
   413       \\n\
   414       \val thy = (" ^ old_thys ^ " false);\n\
   415       \\n\
   416       \end;\n";
   417 
   418 fun theory_defn sectab =
   419   header -- optional (extension sectab >> Some) None -- eof
   420   >> (mk_structure o #1);
   421 
   422 fun parse_thy (lex, sectab) str =
   423   #1 (!! (theory_defn sectab) (tokenize lex str));
   424 
   425 
   426 (* standard sections *)
   427 
   428 fun mk_val ax = "val " ^ ax ^ " = get_axiom thy " ^ quote ax ^ ";";
   429 
   430 fun mk_axm_sect pretxt (txt, axs) =
   431   (pretxt ^ "\n" ^ txt, cat_lines (map mk_val axs));
   432 
   433 fun axm_section name pretxt parse =
   434   (name, parse >> mk_axm_sect pretxt);
   435 
   436 fun section name pretxt parse =
   437   axm_section name pretxt (parse >> rpair []);
   438 
   439 
   440 val pure_keywords =
   441  ["classes", "default", "types", "arities", "consts", "syntax",
   442   "translations", "MLtrans", "MLtext", "rules", "defns", "axclass",
   443   "sigclass", "subclass", "instance", "end", "ML", "mixfix", "infixr",
   444   "infixl", "binder", "=", "+", ",", "<", "{", "}", "(", ")", "[", "]", "::",
   445   "==", "=>", "<="];
   446 
   447 val pure_sections =
   448  [section "classes" "|> add_classes" class_decls,
   449   section "default" "|> add_defsort" sort,
   450   ("types", type_decls),
   451   section "arities" "|> add_arities" arity_decls,
   452   section "consts" "|> add_consts" const_decls,
   453   section "syntax" "|> add_syntax" const_decls,
   454   section "translations" "|> add_trrules" trans_decls,
   455   section "MLtrans" "|> add_trfuns" mltrans,
   456   ("MLtext", verbatim >> rpair ""),
   457   axm_section "rules" "|> add_axioms" axiom_decls,
   458   axm_section "defns" "|> add_defns" axiom_decls,
   459   axm_section "axclass" "|> add_axclass" axclass_decl,
   460   axm_section "sigclass" "|> add_sigclass" sigclass_decl,
   461   section "subclass" "|> add_subclass" subclass_decl,
   462   section "instance" "|> add_instance" instance_decl];
   463 
   464 
   465 end;
   466