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