]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite_parser/test_parser.ml
3b5c8f3751a986865747fe41056a4e34f99dc8ab
[helm.git] / helm / ocaml / 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 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   let status =
66    ref
67     (CicNotation2.load_notation
68       ~include_paths:[] (Helm_registry.get "notation.core_file"))
69   in
70     try
71       while true do
72         try
73          match
74           GrafiteParser.parse_statement ~include_paths:[] istream !status
75          with
76             newstatus, GrafiteParser.LNone _ -> status := newstatus
77           | newstatus, GrafiteParser.LSome statement ->
78               status := newstatus;
79               let floc = extract_loc statement in
80               let (_, y) = HExtlib.loc_of_floc floc in
81               char_count := y + !char_count;
82               match statement with
83     (*           | G.Executable (_, G.Macro (_, G.Check (_,
84                 P.AttributedTerm (_, P.Ident _)))) -> 
85                   prerr_endline "mega hack";
86                   (match !last_rule_id with
87                   | None -> ()
88                   | Some id ->
89                       prerr_endline "removing last notation rule ...";
90                       CicNotationParser.delete id) *)
91               | G.Executable (_, G.Macro (_, G.Check (_, t))) -> 
92                   prerr_endline (sprintf "ast: %s" (CicNotationPp.pp_term t));
93                   let t' = TermContentPres.pp_ast t in
94                   prerr_endline (sprintf "rendered ast: %s"
95                     (CicNotationPp.pp_term t'));
96                   let tbl = Hashtbl.create 0 in
97                   dump_xml t' tbl "out.xml"
98               | statement ->
99                   prerr_endline
100                    ("Unsupported statement: " ^
101                      GrafiteAstPp.pp_statement
102                       ~term_pp:CicNotationPp.pp_term
103                       ~lazy_term_pp:(fun _ -> "_lazy_term_here_")
104                       ~obj_pp:(fun _ -> "_obj_here_")
105                       statement)
106         with
107         | End_of_file -> raise End_of_file
108         | HExtlib.Localized (floc,CicNotationParser.Parse_error msg) ->
109             let (x, y) = HExtlib.loc_of_floc floc in
110 (*             let before = String.sub line 0 x in
111             let error = String.sub line x (y - x) in
112             let after = String.sub line y (String.length line - y) in
113             eprintf "%s\e[01;31m%s\e[00m%s\n" before error after;
114             prerr_endline (sprintf "at character %d-%d: %s" x y msg) *)
115             prerr_endline (sprintf "Parse error at character %d-%d: %s"
116               (!char_count + x) (!char_count + y) msg)
117         | exn ->
118             prerr_endline
119               (sprintf "Uncaught exception: %s" (Printexc.to_string exn))
120         done
121     with End_of_file -> ()
122
123 let _ =
124   let arg_spec = [ ] in
125   let usage = "" in
126   Arg.parse arg_spec (fun _ -> raise (Arg.Bad usage)) usage;
127   print_endline "Loading builtin notation ...";
128   print_endline "done.";
129   flush stdout;
130   process_stream (Ulexing.from_utf8_channel stdin)
131