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