]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationLexer.ml
better distinction of (* *) and (** *) comments
[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   (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
33 (* let regexp ident_letter = xml_letter *)
34
35 let regexp ident_letter = [ 'a' - 'z' 'A' - 'Z' ]
36
37 let regexp ligature_char = [ "'`~!?@*()[]<>-+=|:;.,/\"" ]
38 let regexp ligature = ligature_char ligature_char+
39
40 let regexp ident_decoration = '\'' | '?' | '`'
41 let regexp ident_cont = ident_letter | xml_digit | '_'
42 let regexp ident = ident_letter ident_cont* ident_decoration*
43
44 let regexp tex_token = '\\' ident
45
46 let regexp delim_begin = "\\["
47 let regexp delim_end = "\\]"
48
49 let regexp qkeyword = "'" ident "'"
50
51 let regexp implicit = '?'
52 let regexp placeholder = '%'
53 let regexp meta = implicit number
54
55 let regexp csymbol = '\'' ident
56
57 let regexp begin_group = "@{" | "${"
58 let regexp end_group = '}'
59 let regexp wildcard = "$_"
60 let regexp ast_ident = "@" ident
61 let regexp ast_csymbol = "@" csymbol
62 let regexp meta_ident = "$" ident
63 let regexp meta_anonymous = "$_"
64 let regexp qstring = '"' [^ '"']* '"'
65
66 let regexp begincomment = "(**" xml_blank
67 let regexp beginnote = "(*"
68 let regexp endcomment = "*)"
69 (* let regexp comment_char = [^'*'] | '*'[^')']
70 let regexp note = "|+" ([^'*'] | "**") comment_char* "+|" *)
71
72 let level1_layouts = 
73   [ "sub"; "sup";
74     "below"; "above";
75     "over"; "atop"; "frac";
76     "sqrt"; "root"
77   ]
78
79 let level1_keywords =
80   [ "hbox"; "hvbox"; "hovbox"; "vbox";
81     "break";
82     "list0"; "list1"; "sep";
83     "opt";
84     "term"; "ident"; "number"
85   ] @ level1_layouts
86
87 let level2_meta_keywords =
88   [ "if"; "then"; "else";
89     "fold"; "left"; "right"; "rec";
90     "fail";
91     "default";
92     "anonymous"; "ident"; "number"; "term"; "fresh"
93   ]
94
95   (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
96 let level2_ast_keywords = Hashtbl.create 23
97 let _ =
98   List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
99   [ "CProp"; "Prop"; "Type"; "Set"; "let"; "rec"; "corec"; "match";
100     "with"; "in"; "and"; "to"; "as"; "on" ]
101
102 let add_level2_ast_keyword k = Hashtbl.add level2_ast_keywords k ()
103 let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
104
105   (* (string, int) Hashtbl.t, with multiple bindings.
106    * int is the unicode codepoint *)
107 let ligatures = Hashtbl.create 23
108 let _ =
109   List.iter
110     (fun (ligature, symbol) -> Hashtbl.add ligatures ligature symbol)
111     [ ("->", <:unicode<to>>);   ("=>", <:unicode<Rightarrow>>);
112       ("<=", <:unicode<leq>>);  (">=", <:unicode<geq>>);
113       ("<>", <:unicode<neq>>);  (":=", <:unicode<def>>);
114     ]
115
116 let regexp uri =
117   ("cic:/" | "theory:/")              (* schema *)
118   ident ('/' ident)*                  (* path *)
119   ('.' ident)+                        (* ext *)
120   ("#xpointer(" number ('/' number)+ ")")?  (* xpointer *)
121
122 let error lexbuf msg =
123   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
124   raise (Error (begin_cnum, end_cnum, msg))
125 let error_at_end lexbuf msg =
126   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
127   raise (Error (begin_cnum, end_cnum, msg))
128
129 let return_with_loc token begin_cnum end_cnum =
130   (* TODO handle line/column numbers *)
131   let flocation_begin =
132     { Lexing.pos_fname = "";
133       Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
134       Lexing.pos_cnum = begin_cnum }
135   in
136   let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
137   (token, (flocation_begin, flocation_end))
138
139 let return lexbuf token =
140   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
141     return_with_loc token begin_cnum end_cnum
142
143 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
144
145 let return_symbol lexbuf s = return lexbuf ("SYMBOL", s)
146 let return_eoi lexbuf = return lexbuf ("EOI", "")
147
148 let remove_quotes s = String.sub s 1 (String.length s - 2)
149
150 let mk_lexer token =
151   let tok_func stream =
152 (*     let lexbuf = Ulexing.from_utf8_stream stream in *)
153 (** XXX Obj.magic rationale.
154  * The problem.
155  *  camlp4 constraints the tok_func field of Token.glexer to have type:
156  *    Stream.t char -> (Stream.t 'te * flocation_function)
157  *  In order to use ulex we have (in theory) to instantiate a new lexbuf each
158  *  time a char Stream.t is passed, destroying the previous lexbuf which may
159  *  have consumed a character from the old stream which is lost forever :-(
160  * The "solution".
161  *  Instead of passing to camlp4 a char Stream.t we pass a lexbuf, casting it to
162  *  char Stream.t with Obj.magic where needed.
163  *)
164     let lexbuf = Obj.magic stream in
165     Token.make_stream_and_flocation
166       (fun () ->
167         try
168           token lexbuf
169         with
170         | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
171         | Ulexing.InvalidCodepoint p ->
172             error_at_end lexbuf (sprintf "Invalid code point: %d" p))
173   in
174   {
175     Token.tok_func = tok_func;
176     Token.tok_using = (fun _ -> ());
177     Token.tok_removing = (fun _ -> ()); 
178     Token.tok_match = Token.default_match;
179     Token.tok_text = Token.lexer_text;
180     Token.tok_comm = None;
181   }
182
183 let expand_macro lexbuf =
184   let macro =
185     Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
186   in
187   try
188     ("SYMBOL", Utf8Macro.expand macro)
189   with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
190
191 let remove_quotes s = String.sub s 1 (String.length s - 2)
192 let remove_left_quote s = String.sub s 1 (String.length s - 1)
193
194 let rec level2_pattern_token_group counter buffer =
195   lexer
196   | end_group -> 
197       if (counter > 0) then
198         Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
199       snd (Ulexing.loc lexbuf)
200   | begin_group -> 
201       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
202       ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
203       level2_pattern_token_group counter buffer lexbuf
204   | _ -> 
205       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
206       level2_pattern_token_group counter buffer lexbuf
207
208 let read_unparsed_group token_name lexbuf =
209   let buffer = Buffer.create 16 in
210   let begin_cnum, _ = Ulexing.loc lexbuf in
211   let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
212     return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
213
214 let rec level2_meta_token =
215   lexer
216   | xml_blank+ -> level2_meta_token lexbuf
217   | ident ->
218       let s = Ulexing.utf8_lexeme lexbuf in
219         begin
220           if List.mem s level2_meta_keywords then
221             return lexbuf ("", s)
222           else
223             return lexbuf ("IDENT", s)
224         end
225   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
226   | ast_ident ->
227       return lexbuf ("UNPARSED_AST",
228         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
229   | ast_csymbol ->
230       return lexbuf ("UNPARSED_AST",
231         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
232   | eof -> return_eoi lexbuf
233
234 let rec comment_token acc depth =
235   lexer
236   | beginnote ->
237       let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
238       comment_token acc (depth + 1) lexbuf
239   | endcomment ->
240       let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
241       if depth = 0
242       then acc
243       else comment_token acc (depth - 1) lexbuf
244   | _ ->
245       let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
246       comment_token acc depth lexbuf
247
248   (** @param k continuation to be invoked when no ligature has been found *)
249 let rec ligatures_token k =
250   lexer
251   | ligature ->
252       let lexeme = Ulexing.utf8_lexeme lexbuf in
253       (match Hashtbl.find_all ligatures lexeme with
254       | [] -> (* ligature not found, rollback and try default lexer *)
255           Ulexing.rollback lexbuf;
256           k lexbuf
257       | ligs -> (* ligatures found, use the default one *)
258           let default_lig = List.hd (List.rev ligs) in
259           return_symbol lexbuf default_lig)
260   | eof -> return_eoi lexbuf
261   | _ ->  (* not a ligature, rollback and try default lexer *)
262       Ulexing.rollback lexbuf;
263       k lexbuf
264
265 and level2_ast_token =
266   lexer
267   | xml_blank+ -> ligatures_token level2_ast_token lexbuf
268   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
269   | implicit -> return lexbuf ("IMPLICIT", "")
270   | placeholder -> return lexbuf ("PLACEHOLDER", "")
271   | ident ->
272       let lexeme = Ulexing.utf8_lexeme lexbuf in
273       if Hashtbl.mem level2_ast_keywords lexeme then
274         return lexbuf ("", lexeme)
275       else
276         return lexbuf ("IDENT", lexeme)
277   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
278   | tex_token -> return lexbuf (expand_macro lexbuf)
279   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
280   | qstring ->
281       return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
282   | csymbol ->
283       return lexbuf ("CSYMBOL", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
284   | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
285   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
286   | '(' -> return lexbuf ("LPAREN", "")
287   | ')' -> return lexbuf ("RPAREN", "")
288   | meta_ident ->
289       return lexbuf ("UNPARSED_META",
290         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
291   | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
292   | beginnote -> 
293       let comment = comment_token (Ulexing.utf8_lexeme lexbuf) 0 lexbuf in
294 (*       let comment =
295         Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
296       in
297       return lexbuf ("NOTE", comment) *)
298       ligatures_token level2_ast_token lexbuf
299   | begincomment -> return lexbuf ("BEGINCOMMENT","")
300   | endcomment -> return lexbuf ("ENDCOMMENT","")
301   | eof -> return_eoi lexbuf
302   | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
303
304 and level1_pattern_token =
305   lexer
306   | xml_blank+ -> ligatures_token level1_pattern_token lexbuf
307   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
308   | ident ->
309       let s = Ulexing.utf8_lexeme lexbuf in
310         begin
311           if List.mem s level1_keywords then
312             return lexbuf ("", s)
313           else
314             return lexbuf ("IDENT", s)
315         end
316   | tex_token -> return lexbuf (expand_macro lexbuf)
317   | qkeyword ->
318       return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
319   | '(' -> return lexbuf ("LPAREN", "")
320   | ')' -> return lexbuf ("RPAREN", "")
321   | eof -> return_eoi lexbuf
322   | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
323
324 let level1_pattern_token = ligatures_token level1_pattern_token
325 let level2_ast_token = ligatures_token level2_ast_token
326
327 (* API implementation *)
328
329 let level1_pattern_lexer = mk_lexer level1_pattern_token
330 let level2_ast_lexer = mk_lexer level2_ast_token
331 let level2_meta_lexer = mk_lexer level2_meta_token
332