]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/test_parser.ml
implemented transformations on top of notation code
[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   Xml.pp_to_outchan (CicNotationPres.render_to_boxml id_to_uri t) oc;
43   close_out oc
44
45 let extract_loc =
46   function
47     | GrafiteAst.Executable (loc, _)
48     | GrafiteAst.Comment (loc, _) -> loc
49
50 let pp_associativity = function
51   | Gramext.LeftA -> "left"
52   | Gramext.RightA -> "right"
53   | Gramext.NonA -> "non"
54
55 let pp_precedence = string_of_int
56
57 (* let last_rule_id = ref None *)
58
59 let process_stream istream =
60   let char_count = ref 0 in
61   let module P = CicNotationPt in
62   let module G = GrafiteAst in
63     try
64       while true do
65         try
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;
70           match statement with
71 (*           | G.Executable (_, G.Macro (_, G.Check (_,
72             P.AttributedTerm (_, P.Ident _)))) -> 
73               prerr_endline "mega hack";
74               (match !last_rule_id with
75               | None -> ()
76               | Some id ->
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
98                 ignore
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);
109               flush stdout
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
116               in
117               let annterm =
118                 match annobj with
119                   | Cic.AConstant (_, _, _, _, ty, _, _)
120                   | Cic.AVariable (_, _, _, ty, _, _) -> ty
121                   | _ -> assert false
122               in
123               let t, id_to_uri =
124                 CicNotationRew.ast_of_acic id_to_sort annterm
125               in
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"
133         with
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)
144         | exn ->
145             prerr_endline
146               (sprintf "Uncaught exception: %s" (Printexc.to_string exn))
147         done
148     with End_of_file -> ()
149
150 let _ =
151   let arg_spec = [ ] in
152   let usage = "" 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.";
157   flush stdout;
158   process_stream (Stream.of_channel stdin)
159