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