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