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