]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/test_parser.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / cic_notation / test_parser.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 let _ = Helm_registry.load_from "test_parser.conf.xml"
29
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
35   in
36   print_mpres
37
38 let dump_xml t id_to_uri fname =
39   prerr_endline (sprintf "dumping MathML to %s ..." fname);
40   flush stdout;
41   let oc = open_out fname in
42   let markup = CicNotationPres.render id_to_uri t in
43   let xml_stream = CicNotationPres.print_xml markup in
44   Xml.pp_to_outchan xml_stream oc;
45   close_out oc
46
47 let extract_loc =
48   function
49     | GrafiteAst.Executable (loc, _)
50     | GrafiteAst.Comment (loc, _) -> loc
51
52 let pp_associativity = function
53   | Gramext.LeftA -> "left"
54   | Gramext.RightA -> "right"
55   | Gramext.NonA -> "non"
56
57 let pp_precedence = string_of_int
58
59 (* let last_rule_id = ref None *)
60
61 let process_stream istream =
62   let char_count = ref 0 in
63   let module P = CicNotationPt in
64   let module G = GrafiteAst in
65     try
66       while true do
67         try
68           let statement = GrafiteParser.parse_statement istream in
69           let floc = extract_loc statement in
70           let (_, y) = P.loc_of_floc floc in
71           char_count := y + !char_count;
72           match statement with
73 (*           | G.Executable (_, G.Macro (_, G.Check (_,
74             P.AttributedTerm (_, P.Ident _)))) -> 
75               prerr_endline "mega hack";
76               (match !last_rule_id with
77               | None -> ()
78               | Some id ->
79                   prerr_endline "removing last notation rule ...";
80                   CicNotationParser.delete id) *)
81           | G.Executable (_, G.Macro (_, G.Check (_, t))) -> 
82               prerr_endline (sprintf "ast: %s" (CicNotationPp.pp_term t));
83               let t' = CicNotationRew.pp_ast t in
84               prerr_endline (sprintf "rendered ast: %s"
85                 (CicNotationPp.pp_term t'));
86               let tbl = Hashtbl.create 0 in
87               dump_xml t' tbl "out.xml"
88           | G.Executable (_, G.Command (_,
89             G.Notation (_, dir, l1, associativity, precedence, l2))) ->
90               prerr_endline "notation";
91               prerr_endline (sprintf "l1: %s" (CicNotationPp.pp_term l1));
92               prerr_endline (sprintf "l2: %s" (CicNotationPp.pp_term l2));
93               prerr_endline (sprintf "prec: %s" (pp_precedence precedence));
94               prerr_endline (sprintf "assoc: %s" (pp_associativity associativity));
95               let keywords = CicNotationUtil.keywords_of_term l1 in
96               if keywords <> [] then
97                 prerr_endline (sprintf "keywords: %s"
98                   (String.concat " " keywords));
99               if dir <> Some `RightToLeft then
100                 ignore
101                   (CicNotationParser.extend l1 ?precedence ?associativity
102                     (fun env loc -> CicNotationFwd.instantiate_level2 env l2));
103 (*               last_rule_id := Some rule_id; *)
104               if dir <> Some `LeftToRight then
105                 ignore (CicNotationRew.add_pretty_printer
106                   ?precedence ?associativity l2 l1)
107           | G.Executable (_, G.Command (_, G.Interpretation (_, id, l2, l3))) ->
108               prerr_endline "interpretation";
109               prerr_endline (sprintf "dsc: %s" id);
110               ignore (CicNotationRew.add_interpretation id l2 l3);
111               flush stdout
112           | G.Executable (_, G.Command (_, G.Dump _)) ->
113               CicNotationParser.print_l2_pattern (); print_newline ()
114           | G.Executable (_, G.Command (_, G.Render (_, uri))) ->
115               let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
116               let annobj, _, _, id_to_sort, _, _, _ =
117                 Cic2acic.acic_object_of_cic_object obj
118               in
119               let annterm =
120                 match annobj with
121                   | Cic.AConstant (_, _, _, _, ty, _, _)
122                   | Cic.AVariable (_, _, _, ty, _, _) -> ty
123                   | _ -> assert false
124               in
125               let t, id_to_uri =
126                 CicNotationRew.ast_of_acic id_to_sort annterm
127               in
128               prerr_endline "Raw AST";
129               prerr_endline (CicNotationPp.pp_term t);
130               let t' = CicNotationRew.pp_ast t in
131               prerr_endline "Rendered AST";
132               prerr_endline (CicNotationPp.pp_term t');
133               dump_xml t' id_to_uri "out.xml"
134           | _ -> prerr_endline "Unsupported statement"
135         with
136         | End_of_file -> raise End_of_file
137         | CicNotationParser.Parse_error (floc, msg) ->
138             let (x, y) = P.loc_of_floc floc in
139 (*             let before = String.sub line 0 x in
140             let error = String.sub line x (y - x) in
141             let after = String.sub line y (String.length line - y) in
142             eprintf "%s\e[01;31m%s\e[00m%s\n" before error after;
143             prerr_endline (sprintf "at character %d-%d: %s" x y msg) *)
144             prerr_endline (sprintf "Parse error at character %d-%d: %s"
145               (!char_count + x) (!char_count + y) msg)
146         | exn ->
147             prerr_endline
148               (sprintf "Uncaught exception: %s" (Printexc.to_string exn))
149         done
150     with End_of_file -> ()
151
152 let _ =
153   let arg_spec = [ ] in
154   let usage = "" in
155   Arg.parse arg_spec (fun _ -> raise (Arg.Bad usage)) usage;
156   print_endline "Loading builtin notation ...";
157   CicNotation.load_notation (Helm_registry.get "notation.core_file");
158   print_endline "done.";
159   flush stdout;
160   process_stream (Ulexing.from_utf8_channel stdin)
161