1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 exception Error of int * int * string
32 let regexp number = xml_digit+
33 let regexp utf8_blank = " " | "\n" | "\t" | [160] (* this is a nbsp *)
35 (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
36 (* let regexp ident_letter = xml_letter *)
38 let regexp ident_letter = [ 'a' - 'z' 'A' - 'Z' ]
40 (* must be in sync with "is_ligature_char" below *)
41 let regexp ligature_char = [ "'`~!?@*()[]<>-+=|:;.,/\"" ]
42 let regexp ligature = ligature_char ligature_char+
44 let is_ligature_char =
45 (* must be in sync with "regexp ligature_char" above *)
46 let chars = "'`~!?@*()[]<>-+=|:;.,/\"" in
49 ignore (String.index chars char);
51 with Not_found -> false))
53 let regexp ident_decoration = '\'' | '?' | '`'
54 let regexp ident_cont = ident_letter | xml_digit | '_'
55 let regexp ident = ident_letter ident_cont* ident_decoration*
57 let regexp tex_token = '\\' ident
59 let regexp delim_begin = "\\["
60 let regexp delim_end = "\\]"
62 let regexp qkeyword = "'" ident "'"
64 let regexp implicit = '?'
65 let regexp placeholder = '%'
66 let regexp meta = implicit number
68 let regexp csymbol = '\'' ident
70 let regexp begin_group = "@{" | "${"
71 let regexp end_group = '}'
72 let regexp wildcard = "$_"
73 let regexp ast_ident = "@" ident
74 let regexp ast_csymbol = "@" csymbol
75 let regexp meta_ident = "$" ident
76 let regexp meta_anonymous = "$_"
77 let regexp qstring = '"' [^ '"']* '"'
79 let regexp begincomment = "(**" utf8_blank
80 let regexp beginnote = "(*"
81 let regexp endcomment = "*)"
82 (* let regexp comment_char = [^'*'] | '*'[^')']
83 let regexp note = "|+" ([^'*'] | "**") comment_char* "+|" *)
88 "over"; "atop"; "frac";
93 [ "hbox"; "hvbox"; "hovbox"; "vbox";
95 "list0"; "list1"; "sep";
97 "term"; "ident"; "number"
100 let level2_meta_keywords =
101 [ "if"; "then"; "else";
102 "fold"; "left"; "right"; "rec";
105 "anonymous"; "ident"; "number"; "term"; "fresh"
108 (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
109 let level2_ast_keywords = Hashtbl.create 23
111 List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
112 [ "CProp"; "Prop"; "Type"; "Set"; "let"; "rec"; "corec"; "match";
113 "with"; "in"; "and"; "to"; "as"; "on"; "return" ]
115 let add_level2_ast_keyword k = Hashtbl.add level2_ast_keywords k ()
116 let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
118 (* (string, int) Hashtbl.t, with multiple bindings.
119 * int is the unicode codepoint *)
120 let ligatures = Hashtbl.create 23
123 (fun (ligature, symbol) -> Hashtbl.add ligatures ligature symbol)
124 [ ("->", <:unicode<to>>); ("=>", <:unicode<Rightarrow>>);
125 ("<=", <:unicode<leq>>); (">=", <:unicode<geq>>);
126 ("<>", <:unicode<neq>>); (":=", <:unicode<def>>);
127 ("==", <:unicode<equiv>>);
130 let regexp uri_step = [ 'a' - 'z' 'A' - 'Z' '0' - '9' '_' '-' ''' ]+
133 ("cic:/" | "theory:/") (* schema *)
134 (* ident ('/' ident)* |+ path +| *)
135 uri_step ('/' uri_step)* (* path *)
136 ('.' ident)+ (* ext *)
137 ("#xpointer(" number ('/' number)+ ")")? (* xpointer *)
139 let error lexbuf msg =
140 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
141 raise (Error (begin_cnum, end_cnum, msg))
142 let error_at_end lexbuf msg =
143 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
144 raise (Error (begin_cnum, end_cnum, msg))
146 let return_with_loc token begin_cnum end_cnum =
147 (* TODO handle line/column numbers *)
148 let flocation_begin =
149 { Lexing.pos_fname = "";
150 Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
151 Lexing.pos_cnum = begin_cnum }
153 let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
154 (token, (flocation_begin, flocation_end))
156 let return lexbuf token =
157 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
158 return_with_loc token begin_cnum end_cnum
160 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
162 let return_symbol lexbuf s = return lexbuf ("SYMBOL", s)
163 let return_eoi lexbuf = return lexbuf ("EOI", "")
165 let remove_quotes s = String.sub s 1 (String.length s - 2)
168 let tok_func stream =
169 (* let lexbuf = Ulexing.from_utf8_stream stream in *)
170 (** XXX Obj.magic rationale.
172 * camlp4 constraints the tok_func field of Token.glexer to have type:
173 * Stream.t char -> (Stream.t 'te * flocation_function)
174 * In order to use ulex we have (in theory) to instantiate a new lexbuf each
175 * time a char Stream.t is passed, destroying the previous lexbuf which may
176 * have consumed a character from the old stream which is lost forever :-(
178 * Instead of passing to camlp4 a char Stream.t we pass a lexbuf, casting it to
179 * char Stream.t with Obj.magic where needed.
181 let lexbuf = Obj.magic stream in
182 Token.make_stream_and_flocation
187 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
188 | Ulexing.InvalidCodepoint p ->
189 error_at_end lexbuf (sprintf "Invalid code point: %d" p))
192 Token.tok_func = tok_func;
193 Token.tok_using = (fun _ -> ());
194 Token.tok_removing = (fun _ -> ());
195 Token.tok_match = Token.default_match;
196 Token.tok_text = Token.lexer_text;
197 Token.tok_comm = None;
200 let expand_macro lexbuf =
202 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
205 ("SYMBOL", Utf8Macro.expand macro)
206 with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
208 let remove_quotes s = String.sub s 1 (String.length s - 2)
209 let remove_left_quote s = String.sub s 1 (String.length s - 1)
211 let rec level2_pattern_token_group counter buffer =
214 if (counter > 0) then
215 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
216 snd (Ulexing.loc lexbuf)
218 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
219 ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
220 level2_pattern_token_group counter buffer lexbuf
222 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
223 level2_pattern_token_group counter buffer lexbuf
225 let read_unparsed_group token_name lexbuf =
226 let buffer = Buffer.create 16 in
227 let begin_cnum, _ = Ulexing.loc lexbuf in
228 let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
229 return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
231 let rec level2_meta_token =
233 | utf8_blank+ -> level2_meta_token lexbuf
235 let s = Ulexing.utf8_lexeme lexbuf in
237 if List.mem s level2_meta_keywords then
238 return lexbuf ("", s)
240 return lexbuf ("IDENT", s)
242 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
244 return lexbuf ("UNPARSED_AST",
245 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
247 return lexbuf ("UNPARSED_AST",
248 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
249 | eof -> return_eoi lexbuf
251 let rec comment_token acc depth =
254 let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
255 comment_token acc (depth + 1) lexbuf
257 let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
260 else comment_token acc (depth - 1) lexbuf
262 let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
263 comment_token acc depth lexbuf
265 (** @param k continuation to be invoked when no ligature has been found *)
266 let rec ligatures_token k =
269 let lexeme = Ulexing.utf8_lexeme lexbuf in
270 (match List.rev (Hashtbl.find_all ligatures lexeme) with
271 | [] -> (* ligature not found, rollback and try default lexer *)
272 Ulexing.rollback lexbuf;
274 | default_lig :: _ -> (* ligatures found, use the default one *)
275 return_symbol lexbuf default_lig)
276 | eof -> return_eoi lexbuf
277 | _ -> (* not a ligature, rollback and try default lexer *)
278 Ulexing.rollback lexbuf;
281 and level2_ast_token =
283 | utf8_blank+ -> ligatures_token level2_ast_token lexbuf
285 let s = Ulexing.utf8_lexeme lexbuf in
286 return lexbuf ("META", String.sub s 1 (String.length s - 1))
287 | implicit -> return lexbuf ("IMPLICIT", "")
288 | placeholder -> return lexbuf ("PLACEHOLDER", "")
290 let lexeme = Ulexing.utf8_lexeme lexbuf in
291 if Hashtbl.mem level2_ast_keywords lexeme then
292 return lexbuf ("", lexeme)
294 return lexbuf ("IDENT", lexeme)
295 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
296 | tex_token -> return lexbuf (expand_macro lexbuf)
297 | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
299 return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
301 return lexbuf ("CSYMBOL", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
302 | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
303 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
304 | '(' -> return lexbuf ("LPAREN", "")
305 | ')' -> return lexbuf ("RPAREN", "")
307 return lexbuf ("UNPARSED_META",
308 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
309 | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
311 let _comment = comment_token (Ulexing.utf8_lexeme lexbuf) 0 lexbuf in
313 Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
315 return lexbuf ("NOTE", comment) *)
316 ligatures_token level2_ast_token lexbuf
317 | begincomment -> return lexbuf ("BEGINCOMMENT","")
318 | endcomment -> return lexbuf ("ENDCOMMENT","")
319 | eof -> return_eoi lexbuf
320 | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
322 and level1_pattern_token =
324 | utf8_blank+ -> ligatures_token level1_pattern_token lexbuf
325 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
327 let s = Ulexing.utf8_lexeme lexbuf in
329 if List.mem s level1_keywords then
330 return lexbuf ("", s)
332 return lexbuf ("IDENT", s)
334 | tex_token -> return lexbuf (expand_macro lexbuf)
336 return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
337 | '(' -> return lexbuf ("LPAREN", "")
338 | ')' -> return lexbuf ("RPAREN", "")
339 | eof -> return_eoi lexbuf
340 | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
342 let level1_pattern_token = ligatures_token level1_pattern_token
343 let level2_ast_token = ligatures_token level2_ast_token
345 (* API implementation *)
347 let level1_pattern_lexer = mk_lexer level1_pattern_token
348 let level2_ast_lexer = mk_lexer level2_ast_token
349 let level2_meta_lexer = mk_lexer level2_meta_token
351 let lookup_ligatures lexeme =
354 then [ Utf8Macro.expand (String.sub lexeme 1 (String.length lexeme - 1)) ]
355 else List.rev (Hashtbl.find_all ligatures lexeme)
356 with Invalid_argument _ | Utf8Macro.Macro_not_found _ -> []