]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/content_pres/cicNotationLexer.ml
removing (only from the interface) functions related to ligatures that now live in...
[helm.git] / helm / software / components / content_pres / 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 (* $Id$ *)
27
28 open Printf
29
30 exception Error of int * int * string
31
32 let regexp number = xml_digit+
33 let regexp utf8_blank = " " | "\r\n" | "\n" | "\t" | [160] (* this is a nbsp *)
34 let regexp percentage = 
35   ('-' | "") [ '0' - '9' ] + '%'
36 let regexp floatwithunit = 
37   ('-' | "") [ '0' - '9' ] + ["."] [ '0' - '9' ] + ([ 'a' - 'z' ] + | "" )
38 let regexp color = "#" [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f'
39 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ]
40 [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ]
41
42   (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
43 (* let regexp ident_letter = xml_letter *)
44
45 let regexp ident_letter = [ 'a' - 'z' 'A' - 'Z' ]
46
47   (* must be in sync with "is_ligature_char" below *)
48 let regexp ligature_char = [ "'`~!?@*()[]<>-+=|:;.,/\"" ]
49 let regexp ligature = ligature_char ligature_char+
50
51 let regexp we_proved = "we" utf8_blank+ "proved"
52 let regexp we_have = "we" utf8_blank+ "have"
53 let regexp let_rec = "let" utf8_blank+ "rec" 
54 let regexp let_corec = "let" utf8_blank+  "corec"
55 let regexp ident_decoration = '\'' | '?' | '`'
56 let regexp ident_cont = ident_letter | xml_digit | '_'
57 let regexp ident = ident_letter ident_cont* ident_decoration*
58
59 let regexp tex_token = '\\' ident
60
61 let regexp delim_begin = "\\["
62 let regexp delim_end = "\\]"
63
64 let regexp qkeyword = "'" ident "'"
65
66 let regexp implicit = '?'
67 let regexp placeholder = '%'
68 let regexp meta = implicit number
69
70 let regexp csymbol = '\'' ident
71
72 let regexp begin_group = "@{" | "${"
73 let regexp end_group = '}'
74 let regexp wildcard = "$_"
75 let regexp ast_ident = "@" ident
76 let regexp ast_csymbol = "@" csymbol
77 let regexp meta_ident = "$" ident
78 let regexp meta_anonymous = "$_"
79 let regexp qstring = '"' [^ '"']* '"'
80
81 let regexp begincomment = "(**" utf8_blank
82 let regexp beginnote = "(*"
83 let regexp endcomment = "*)"
84 (* let regexp comment_char = [^'*'] | '*'[^')']
85 let regexp note = "|+" ([^'*'] | "**") comment_char* "+|" *)
86
87 let level1_layouts = 
88   [ "sub"; "sup";
89     "below"; "above";
90     "over"; "atop"; "frac";
91     "sqrt"; "root"
92   ]
93
94 let level1_keywords =
95   [ "hbox"; "hvbox"; "hovbox"; "vbox";
96     "break";
97     "list0"; "list1"; "sep";
98     "opt";
99     "term"; "ident"; "number"; "mstyle" ; "mpadded"
100   ] @ level1_layouts
101
102 let level2_meta_keywords =
103   [ "if"; "then"; "elCicNotationParser.se";
104     "fold"; "left"; "right"; "rec";
105     "fail";
106     "default";
107     "anonymous"; "ident"; "number"; "term"; "fresh"
108   ]
109
110   (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
111 let initial_level2_ast_keywords () = Hashtbl.create 23;;
112
113 let level2_ast_keywords = ref (initial_level2_ast_keywords ())
114
115 let initialize_keywords () =
116   List.iter (fun k -> Hashtbl.add !level2_ast_keywords k ())
117   [ "CProp"; "Prop"; "Type"; "Set"; "let"; "match";
118   "with"; "in"; "and"; "to"; "as"; "on"; "return"; "done" ]
119 ;;
120
121 let _ = initialize_keywords ();;
122
123 let add_level2_ast_keyword k = Hashtbl.add !level2_ast_keywords k ()
124 let remove_level2_ast_keyword k = Hashtbl.remove !level2_ast_keywords k
125
126   (* (string, int) Hashtbl.t, with multiple bindings.
127    * int is the unicode codepoint *)
128 let ligatures = Hashtbl.create 23
129
130 let _ =
131   List.iter
132     (fun (ligature, symbol) -> Hashtbl.add ligatures ligature symbol)
133     [ ("->", <:unicode<to>>);   ("=>", <:unicode<Rightarrow>>);
134       ("<=", <:unicode<leq>>);  (">=", <:unicode<geq>>);
135       ("<>", <:unicode<neq>>);  (":=", <:unicode<def>>);
136       ("==", <:unicode<equiv>>);
137     ]
138
139 let regexp uri_step = [ 'a' - 'z' 'A' - 'Z' '0' - '9' '_' '-' ''' ]+
140
141 let regexp uri =
142   ("cic:/" | "theory:/")              (* schema *)
143 (*   ident ('/' ident)*                  |+ path +| *)
144   uri_step ('/' uri_step)*            (* path *)
145   ('.' ident)+                        (* ext *)
146   ("#xpointer(" number ('/' number)+ ")")?  (* xpointer *)
147
148 let error lexbuf msg =
149   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
150   raise (Error (begin_cnum, end_cnum, msg))
151 let error_at_end lexbuf msg =
152   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
153   raise (Error (begin_cnum, end_cnum, msg))
154
155 let return_with_loc token begin_cnum end_cnum =
156   let flocation = HExtlib.floc_of_loc (begin_cnum,end_cnum) in
157    token, flocation
158
159 let return lexbuf token =
160   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
161     return_with_loc token begin_cnum end_cnum
162
163 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
164
165 let return_symbol lexbuf s = return lexbuf ("SYMBOL", s)
166 let return_eoi lexbuf = return lexbuf ("EOI", "")
167
168 let remove_quotes s = String.sub s 1 (String.length s - 2)
169
170 let mk_lexer token =
171   let tok_func stream =
172 (*     let lexbuf = Ulexing.from_utf8_stream stream in *)
173 (** XXX Obj.magic rationale.
174  * The problem.
175  *  camlp5 constraints the tok_func field of Token.glexer to have type:
176  *    Stream.t char -> (Stream.t 'te * flocation_function)
177  *  In order to use ulex we have (in theory) to instantiate a new lexbuf each
178  *  time a char Stream.t is passed, destroying the previous lexbuf which may
179  *  have consumed a character from the old stream which is lost forever :-(
180  * The "solution".
181  *  Instead of passing to camlp5 a char Stream.t we pass a lexbuf, casting it to
182  *  char Stream.t with Obj.magic where needed.
183  *)
184     let lexbuf = Obj.magic stream in
185     Token.make_stream_and_location
186       (fun () ->
187         try
188           token lexbuf
189         with
190         | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
191         | Ulexing.InvalidCodepoint p ->
192             error_at_end lexbuf (sprintf "Invalid code point: %d" p))
193   in
194   {
195     Token.tok_func = tok_func;
196     Token.tok_using = (fun _ -> ());
197     Token.tok_removing = (fun _ -> ()); 
198     Token.tok_match = Token.default_match;
199     Token.tok_text = Token.lexer_text;
200     Token.tok_comm = None;
201   }
202
203 let expand_macro lexbuf =
204   let macro =
205     Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
206   in
207   try
208     ("SYMBOL", Utf8Macro.expand macro)
209   with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
210
211 let remove_quotes s = String.sub s 1 (String.length s - 2)
212 let remove_left_quote s = String.sub s 1 (String.length s - 1)
213
214 let rec level2_pattern_token_group counter buffer =
215   lexer
216   | end_group -> 
217       if (counter > 0) then
218         Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
219       snd (Ulexing.loc lexbuf)
220   | begin_group -> 
221       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
222       ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
223       level2_pattern_token_group counter buffer lexbuf
224   | _ -> 
225       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
226       level2_pattern_token_group counter buffer lexbuf
227
228 let read_unparsed_group token_name lexbuf =
229   let buffer = Buffer.create 16 in
230   let begin_cnum, _ = Ulexing.loc lexbuf in
231   let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
232     return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
233
234 let rec level2_meta_token =
235   lexer
236   | utf8_blank+ -> level2_meta_token lexbuf
237   | ident ->
238       let s = Ulexing.utf8_lexeme lexbuf in
239         begin
240           if List.mem s level2_meta_keywords then
241             return lexbuf ("", s)
242           else
243             return lexbuf ("IDENT", s)
244         end
245   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
246   | ast_ident ->
247       return lexbuf ("UNPARSED_AST",
248         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
249   | ast_csymbol ->
250       return lexbuf ("UNPARSED_AST",
251         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
252   | eof -> return_eoi lexbuf
253
254 let rec comment_token acc depth =
255   lexer
256   | beginnote ->
257       let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
258       comment_token acc (depth + 1) lexbuf
259   | endcomment ->
260       let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
261       if depth = 0
262       then acc
263       else comment_token acc (depth - 1) lexbuf
264   | _ ->
265       let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
266       comment_token acc depth lexbuf
267
268   (** @param k continuation to be invoked when no ligature has been found *)
269 let rec ligatures_token k =
270   lexer
271   | ligature ->
272       let lexeme = Ulexing.utf8_lexeme lexbuf in
273       (match List.rev (Hashtbl.find_all ligatures lexeme) with
274       | [] -> (* ligature not found, rollback and try default lexer *)
275           Ulexing.rollback lexbuf;
276           k lexbuf
277       | default_lig :: _ -> (* ligatures found, use the default one *)
278           return_symbol lexbuf default_lig)
279   | eof -> return_eoi lexbuf
280   | _ ->  (* not a ligature, rollback and try default lexer *)
281       Ulexing.rollback lexbuf;
282       k lexbuf
283
284 and level2_ast_token =
285   lexer
286   | let_rec -> return lexbuf ("LETREC","")
287   | let_corec -> return lexbuf ("LETCOREC","")
288   | we_proved -> return lexbuf ("WEPROVED","")
289   | we_have -> return lexbuf ("WEHAVE","")
290   | utf8_blank+ -> ligatures_token level2_ast_token lexbuf
291   | meta ->
292      let s = Ulexing.utf8_lexeme lexbuf in
293       return lexbuf ("META", String.sub s 1 (String.length s - 1))
294   | implicit -> return lexbuf ("IMPLICIT", "")
295   | placeholder -> return lexbuf ("PLACEHOLDER", "")
296   | ident ->
297       let lexeme = Ulexing.utf8_lexeme lexbuf in
298       if Hashtbl.mem !level2_ast_keywords lexeme then
299         return lexbuf ("", lexeme)
300       else
301         return lexbuf ("IDENT", lexeme)
302   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
303   | tex_token -> return lexbuf (expand_macro lexbuf)
304   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
305   | qstring ->
306       return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
307   | csymbol ->
308       return lexbuf ("CSYMBOL", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
309   | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
310   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
311   | '(' -> return lexbuf ("LPAREN", "")
312   | ')' -> return lexbuf ("RPAREN", "")
313   | meta_ident ->
314       return lexbuf ("UNPARSED_META",
315         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
316   | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
317   | beginnote -> 
318       let _comment = comment_token (Ulexing.utf8_lexeme lexbuf) 0 lexbuf in
319 (*       let comment =
320         Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
321       in
322       return lexbuf ("NOTE", comment) *)
323       ligatures_token level2_ast_token lexbuf
324   | begincomment -> return lexbuf ("BEGINCOMMENT","")
325   | endcomment -> return lexbuf ("ENDCOMMENT","")
326   | eof -> return_eoi lexbuf
327   | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
328
329 and level1_pattern_token =
330   lexer
331   | utf8_blank+ -> ligatures_token level1_pattern_token lexbuf
332   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
333   | ident ->
334       let s = Ulexing.utf8_lexeme lexbuf in
335         begin
336           if List.mem s level1_keywords then
337             return lexbuf ("", s)
338           else
339             return lexbuf ("IDENT", s)
340         end
341   | color -> return lexbuf ("COLOR", Ulexing.utf8_lexeme lexbuf)
342   | percentage -> 
343       return lexbuf ("PERCENTAGE", Ulexing.utf8_lexeme lexbuf)
344   | floatwithunit -> 
345       return lexbuf ("FLOATWITHUNIT", Ulexing.utf8_lexeme lexbuf)
346   | tex_token -> return lexbuf (expand_macro lexbuf)
347   | qkeyword ->
348       return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
349   | '(' -> return lexbuf ("LPAREN", "")
350   | ')' -> return lexbuf ("RPAREN", "")
351   | eof -> return_eoi lexbuf
352   | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
353
354 let level1_pattern_token = ligatures_token level1_pattern_token
355 let level2_ast_token = ligatures_token level2_ast_token
356
357 (* API implementation *)
358
359 let initial_level1_pattern_lexer () = mk_lexer level1_pattern_token
360 let initial_level2_ast_lexer () = mk_lexer level2_ast_token
361 let initial_level2_meta_lexer () = mk_lexer level2_meta_token
362
363
364 let level1_pattern_lexer_ref = ref (initial_level1_pattern_lexer ())
365 let level2_ast_lexer_ref = ref (initial_level2_ast_lexer ())
366 let level2_meta_lexer_ref = ref (initial_level2_meta_lexer ())
367
368 let level1_pattern_lexer () = !level1_pattern_lexer_ref
369 let level2_ast_lexer () = !level2_ast_lexer_ref
370 let level2_meta_lexer () = !level2_meta_lexer_ref 
371
372 let history = ref [];;
373
374 let push () =
375   history :=
376     (!level2_ast_keywords,!level1_pattern_lexer_ref,
377      !level2_ast_lexer_ref,!level2_meta_lexer_ref) :: !history;
378   level2_ast_keywords := initial_level2_ast_keywords ();
379   initialize_keywords ();
380   level1_pattern_lexer_ref := initial_level1_pattern_lexer ();
381   level2_ast_lexer_ref := initial_level2_ast_lexer ();
382   level2_meta_lexer_ref := initial_level2_meta_lexer ();
383 ;;
384
385 let pop () =
386   match !history with
387   | [] -> assert false
388   | (kwd,pl,al,ml) :: tl -> 
389       level2_ast_keywords := kwd;
390       level1_pattern_lexer_ref := pl;
391       level2_ast_lexer_ref := al;
392       level2_meta_lexer_ref := ml;
393       history := tl
394 ;;
395