]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/grafite_parser/test_parser.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / grafite_parser / 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 (* $Id$ *)
27
28 open Printf
29
30 let _ = Helm_registry.load_from "test_parser.conf.xml"
31
32 let xml_stream_of_markup =
33   let rec print_box (t: CicNotationPres.boxml_markup) =
34     Box.box2xml print_mpres t
35   and print_mpres (t: CicNotationPres.mathml_markup) =
36     Mpresentation.print_mpres print_box t
37   in
38   print_mpres
39
40 let dump_xml t id_to_uri fname =
41   prerr_endline (sprintf "dumping MathML to %s ..." fname);
42   flush stdout;
43   let oc = open_out fname in
44   let markup =
45    CicNotationPres.render ~lookup_uri:(CicNotationPres.lookup_uri id_to_uri)t in
46   let xml_stream = CicNotationPres.print_xml markup in
47   Xml.pp_to_outchan xml_stream oc;
48   close_out oc
49
50 let extract_loc =
51   function
52     | GrafiteAst.Executable (loc, _)
53     | GrafiteAst.Comment (loc, _) -> loc
54
55 let pp_associativity = function
56   | Gramext.LeftA -> "left"
57   | Gramext.RightA -> "right"
58   | Gramext.NonA -> "non"
59
60 let pp_precedence = string_of_int
61
62 (* let last_rule_id = ref None *)
63
64 let process_stream istream =
65   let char_count = ref 0 in
66   let module P = CicNotationPt in
67   let module G = GrafiteAst in
68   let status =
69    ref
70     (CicNotation2.load_notation (new LexiconEngine.status)
71       ~include_paths:[] (Helm_registry.get "notation.core_file"))
72   in
73     try
74       while true do
75         try
76          match
77           GrafiteParser.parse_statement ~include_paths:[] istream !status
78          with
79             newstatus, GrafiteParser.LNone _ -> status := newstatus
80           | newstatus, GrafiteParser.LSome statement ->
81               status := newstatus;
82               let floc = extract_loc statement in
83               let (_, y) = HExtlib.loc_of_floc floc in
84               char_count := y + !char_count;
85               match statement with
86     (*           | G.Executable (_, G.Macro (_, G.Check (_,
87                 P.AttributedTerm (_, P.Ident _)))) -> 
88                   prerr_endline "mega hack";
89                   (match !last_rule_id with
90                   | None -> ()
91                   | Some id ->
92                       prerr_endline "removing last notation rule ...";
93                       CicNotationParser.delete id) *)
94               | G.Executable (_, G.Macro (_, G.Check (_, t))) -> 
95                   prerr_endline (sprintf "ast: %s" (CicNotationPp.pp_term t));
96                   let t' = TermContentPres.pp_ast t in
97                   prerr_endline (sprintf "rendered ast: %s"
98                     (CicNotationPp.pp_term t'));
99                   let tbl = Hashtbl.create 0 in
100                   dump_xml t' tbl "out.xml"
101               | statement ->
102                   prerr_endline
103                    ("Unsupported statement: " ^
104                      GrafiteAstPp.pp_statement
105                       ~map_unicode_to_tex:(Helm_registry.get_bool
106                         "matita.paste_unicode_as_tex")
107                       ~term_pp:CicNotationPp.pp_term
108                       ~lazy_term_pp:(fun _ -> "_lazy_term_here_")
109                       ~obj_pp:(fun _ -> "_obj_here_")
110                       statement)
111         with
112         | End_of_file -> raise End_of_file
113         | HExtlib.Localized (floc,CicNotationParser.Parse_error msg) ->
114             let (x, y) = HExtlib.loc_of_floc floc in
115 (*             let before = String.sub line 0 x in
116             let error = String.sub line x (y - x) in
117             let after = String.sub line y (String.length line - y) in
118             eprintf "%s\e[01;31m%s\e[00m%s\n" before error after;
119             prerr_endline (sprintf "at character %d-%d: %s" x y msg) *)
120             prerr_endline (sprintf "Parse error at character %d-%d: %s"
121               (!char_count + x) (!char_count + y) msg)
122         | exn ->
123             prerr_endline
124               (sprintf "TestParser: Uncaught exception: %s" (Printexc.to_string exn))
125         done
126     with End_of_file -> ()
127
128 let _ =
129   let arg_spec = [ ] in
130   let usage = "" in
131   Arg.parse arg_spec (fun _ -> raise (Arg.Bad usage)) usage;
132   print_endline "Loading builtin notation ...";
133   print_endline "done.";
134   flush stdout;
135   process_stream (Ulexing.from_utf8_channel stdin)
136