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