1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 let _ = Helm_registry.load_from "test_parser.conf.xml"
30 let xml_stream_of_markup =
31 let rec print_box (t: CicNotationPres.boxml_markup) =
32 Box.box2xml print_mpres t
33 and print_mpres (t: CicNotationPres.mathml_markup) =
34 Mpresentation.print_mpres print_box t
38 let dump_xml t id_to_uri fname =
39 prerr_endline (sprintf "dumping MathML to %s ..." fname);
41 let oc = open_out fname in
42 Xml.pp_to_outchan (CicNotationPres.render_to_boxml id_to_uri t) oc;
47 | GrafiteAst.Executable (loc, _)
48 | GrafiteAst.Comment (loc, _) -> loc
50 let pp_associativity = function
51 | Gramext.LeftA -> "left"
52 | Gramext.RightA -> "right"
53 | Gramext.NonA -> "non"
55 let pp_precedence = string_of_int
57 (* let last_rule_id = ref None *)
59 let process_stream istream =
60 let char_count = ref 0 in
61 let module P = CicNotationPt in
62 let module G = GrafiteAst in
66 let statement = GrafiteParser.parse_statement istream in
67 let floc = extract_loc statement in
68 let (_, y) = P.loc_of_floc floc in
69 char_count := y + !char_count;
71 (* | G.Executable (_, G.Macro (_, G.Check (_,
72 P.AttributedTerm (_, P.Ident _)))) ->
73 prerr_endline "mega hack";
74 (match !last_rule_id with
77 prerr_endline "removing last notation rule ...";
78 CicNotationParser.delete id) *)
79 | G.Executable (_, G.Macro (_, G.Check (_, t))) ->
80 prerr_endline (sprintf "ast: %s" (CicNotationPp.pp_term t));
81 let t' = CicNotationRew.pp_ast t in
82 prerr_endline (sprintf "rendered ast: %s"
83 (CicNotationPp.pp_term t'));
84 let tbl = Hashtbl.create 0 in
85 dump_xml t' tbl "out.xml"
86 | G.Executable (_, G.Command (_,
87 G.Notation (_, dir, l1, associativity, precedence, l2))) ->
88 prerr_endline "notation";
89 prerr_endline (sprintf "l1: %s" (CicNotationPp.pp_term l1));
90 prerr_endline (sprintf "l2: %s" (CicNotationPp.pp_term l2));
91 prerr_endline (sprintf "prec: %s" (pp_precedence precedence));
92 prerr_endline (sprintf "assoc: %s" (pp_associativity associativity));
93 let keywords = CicNotationUtil.keywords_of_term l1 in
94 if keywords <> [] then
95 prerr_endline (sprintf "keywords: %s"
96 (String.concat " " keywords));
97 if dir <> Some `RightToLeft then
99 (CicNotationParser.extend l1 ?precedence ?associativity
100 (fun env loc -> CicNotationFwd.instantiate_level2 env l2));
101 (* last_rule_id := Some rule_id; *)
102 if dir <> Some `LeftToRight then
103 ignore (CicNotationRew.add_pretty_printer
104 ?precedence ?associativity l2 l1)
105 | G.Executable (_, G.Command (_, G.Interpretation (_, id, l2, l3))) ->
106 prerr_endline "interpretation";
107 prerr_endline (sprintf "dsc: %s" id);
108 ignore (CicNotationRew.add_interpretation id l2 l3);
110 | G.Executable (_, G.Command (_, G.Dump _)) ->
111 CicNotationParser.print_l2_pattern (); print_newline ()
112 | G.Executable (_, G.Command (_, G.Render (_, uri))) ->
113 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
114 let annobj, _, _, id_to_sort, _, _, _ =
115 Cic2acic.acic_object_of_cic_object obj
119 | Cic.AConstant (_, _, _, _, ty, _, _)
120 | Cic.AVariable (_, _, _, ty, _, _) -> ty
124 CicNotationRew.ast_of_acic id_to_sort annterm
126 prerr_endline "Raw AST";
127 prerr_endline (CicNotationPp.pp_term t);
128 let t' = CicNotationRew.pp_ast t in
129 prerr_endline "Rendered AST";
130 prerr_endline (CicNotationPp.pp_term t');
131 dump_xml t' id_to_uri "out.xml"
132 | _ -> prerr_endline "Unsupported statement"
134 | End_of_file -> raise End_of_file
135 | CicNotationParser.Parse_error (floc, msg) ->
136 let (x, y) = P.loc_of_floc floc in
137 (* let before = String.sub line 0 x in
138 let error = String.sub line x (y - x) in
139 let after = String.sub line y (String.length line - y) in
140 eprintf "%s
\e[01;31m%s
\e[00m%s\n" before error after;
141 prerr_endline (sprintf "at character %d-%d: %s" x y msg) *)
142 prerr_endline (sprintf "Parse error at character %d-%d: %s"
143 (!char_count + x) (!char_count + y) msg)
146 (sprintf "Uncaught exception: %s" (Printexc.to_string exn))
148 with End_of_file -> ()
151 let arg_spec = [ ] in
153 Arg.parse arg_spec (fun _ -> raise (Arg.Bad usage)) usage;
154 print_endline "Loading builtin notation ...";
155 CicNotation.load_notation (Helm_registry.get "notation.core_file");
156 print_endline "done.";
158 process_stream (Stream.of_channel stdin)