]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationLexer.ml
added XmlAttrs attribute for specification of xml attributes directly
[helm.git] / helm / ocaml / cic_notation / cicNotationLexer.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 exception Error of int * int * string
29
30 let regexp number = xml_digit+
31
32 let regexp ident_decoration = '\'' | '!' | '?' | '`'
33 let regexp ident_cont = xml_letter | xml_digit | '_'
34 let regexp ident = xml_letter ident_cont* ident_decoration*
35
36 let regexp tex_token = '\\' ident
37
38 let regexp delim_begin = "\\["
39 let regexp delim_end = "\\]"
40
41 let regexp keyword = '"' ident '"'
42 let regexp qkeyword = "'" ident "'"
43
44 let regexp implicit = '?'
45 let regexp meta = implicit number
46
47 let regexp csymbol = '\'' ident
48
49 let regexp begin_group = "@{" | "${"
50 let regexp end_group = '}'
51 let regexp wildcard = "$_"
52 let regexp ast_ident = "@" ident
53 let regexp ast_csymbol = "@" csymbol
54 let regexp meta_ident = "$" ident
55 let regexp meta_anonymous = "$_"
56 let regexp qstring = '"' [^ '"']* '"'
57
58 let level1_layouts = 
59   [ "sub"; "sup";
60     "below"; "above";
61     "over"; "atop"; "frac";
62     "sqrt"; "root"
63   ]
64
65 let level1_keywords =
66   [ "hbox"; "hvbox"; "hovbox"; "vbox";
67     "break";
68     "list0"; "list1"; "sep";
69     "opt";
70     "term"; "ident"; "number"
71   ] @ level1_layouts
72
73 let level2_meta_keywords =
74   [ "if"; "then"; "else";
75     "fold"; "left"; "right"; "rec";
76     "fail";
77     "default";
78     "anonymous"; "ident"; "number"; "term"; "fresh"
79   ]
80
81   (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
82 let level2_ast_keywords = Hashtbl.create 23
83 let _ =
84   (* TODO ZACK: keyword list almost cut and paste from cicTextualLexer2.ml, to
85    * be reviewed *)
86   List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
87     [ "Prop"; "Type"; "Set"; "let"; "rec"; "corec"; "match"; "with"; "in";
88       "and"; "on" ]
89
90 let add_level2_ast_keyword k = Hashtbl.add level2_ast_keywords k ()
91 let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
92
93 let regexp uri =
94   ("cic:/" | "theory:/")              (* schema *)
95   ident ('/' ident)*                  (* path *)
96   ('.' ident)+                        (* ext *)
97   ("#xpointer(" number ('/' number)+ ")")?  (* xpointer *)
98
99 let error lexbuf msg =
100   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
101   raise (Error (begin_cnum, end_cnum, msg))
102 let error_at_end lexbuf msg =
103   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
104   raise (Error (begin_cnum, end_cnum, msg))
105
106 let return_with_loc token begin_cnum end_cnum =
107   (* TODO handle line/column numbers *)
108   let flocation_begin =
109     { Lexing.pos_fname = "";
110       Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
111       Lexing.pos_cnum = begin_cnum }
112   in
113   let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
114   (token, (flocation_begin, flocation_end))
115
116 let return lexbuf token =
117   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
118     return_with_loc token begin_cnum end_cnum
119
120 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
121
122 let remove_quotes s = String.sub s 1 (String.length s - 2)
123
124 let mk_lexer token =
125   let tok_func stream =
126     let lexbuf = Ulexing.from_utf8_stream stream in
127     Token.make_stream_and_flocation
128       (fun () ->
129         try
130           token lexbuf
131         with
132         | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
133         | Ulexing.InvalidCodepoint p ->
134             error_at_end lexbuf (sprintf "Invalid code point: %d" p))
135   in
136   {
137     Token.tok_func = tok_func;
138     Token.tok_using = (fun _ -> ());
139     Token.tok_removing = (fun _ -> ()); 
140     Token.tok_match = Token.default_match;
141     Token.tok_text = Token.lexer_text;
142     Token.tok_comm = None;
143   }
144
145 let expand_macro lexbuf =
146   let macro =
147     Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
148   in
149   try
150     ("SYMBOL", Utf8Macro.expand macro)
151   with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
152
153 let keyword lexbuf = "KEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf)
154
155 let remove_quotes s = String.sub s 1 (String.length s - 2)
156 let remove_left_quote s = String.sub s 1 (String.length s - 1)
157
158 let rec level2_pattern_token_group counter buffer = lexer
159   | end_group -> 
160       if (counter > 0) then
161         Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
162       snd (Ulexing.loc lexbuf)
163   | begin_group -> 
164       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
165       ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
166       level2_pattern_token_group counter buffer lexbuf
167   | _ -> 
168       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
169       level2_pattern_token_group counter buffer lexbuf
170
171 let read_unparsed_group token_name lexbuf =
172   let buffer = Buffer.create 16 in
173   let begin_cnum, _ = Ulexing.loc lexbuf in
174   let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
175     return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
176
177 let rec level2_meta_token = lexer
178   | xml_blank+ -> level2_meta_token lexbuf
179   | ident ->
180       let s = Ulexing.utf8_lexeme lexbuf in
181         begin
182           if List.mem s level2_meta_keywords then
183             return lexbuf ("", s)
184           else
185             return lexbuf ("IDENT", s)
186         end
187   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
188   | ast_ident ->
189       return lexbuf ("UNPARSED_AST",
190         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
191   | ast_csymbol ->
192       return lexbuf ("UNPARSED_AST",
193         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
194   | eof -> return lexbuf ("EOI", "")
195
196 let rec level2_ast_token = lexer
197   | xml_blank+ -> level2_ast_token lexbuf
198   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
199   | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
200   | ident ->
201       let lexeme = Ulexing.utf8_lexeme lexbuf in
202       if Hashtbl.mem level2_ast_keywords lexeme then
203         return lexbuf ("", lexeme)
204       else
205         return lexbuf ("IDENT", lexeme)
206   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
207   | keyword -> return lexbuf (keyword lexbuf)
208   | tex_token -> return lexbuf (expand_macro lexbuf)
209   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
210   | qstring ->
211       return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
212   | csymbol -> return lexbuf ("CSYMBOL", Ulexing.utf8_lexeme lexbuf)
213   | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
214   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
215   | '(' -> return lexbuf ("LPAREN", "")
216   | ')' -> return lexbuf ("RPAREN", "")
217   | meta_ident ->
218       return lexbuf ("UNPARSED_META",
219         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
220   | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
221   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
222   | eof -> return lexbuf ("EOI", "")
223
224 let rec level1_pattern_token = lexer
225   | xml_blank+ -> level1_pattern_token lexbuf
226   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
227   | ident ->
228       let s = Ulexing.utf8_lexeme lexbuf in
229         begin
230           if List.mem s level1_keywords then
231             return lexbuf ("", s)
232           else
233             return lexbuf ("IDENT", s)
234         end
235   | tex_token -> return lexbuf (expand_macro lexbuf)
236   | qkeyword ->
237       return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
238   | '(' -> return lexbuf ("LPAREN", "")
239   | ')' -> return lexbuf ("RPAREN", "")
240   | eof -> return lexbuf ("EOI", "")
241   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
242
243 (* API implementation *)
244
245 let level1_pattern_lexer = mk_lexer level1_pattern_token
246 let level2_ast_lexer = mk_lexer level2_ast_token
247 let level2_meta_lexer = mk_lexer level2_meta_token
248