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 module StringSet = Set.Make(String)
35 let regexp number = xml_digit+
36 let regexp utf8_blank = " " | "\r\n" | "\n" | "\t" | [160] (* this is a nbsp *)
37 let regexp percentage =
38 ('-' | "") [ '0' - '9' ] + '%'
39 let regexp floatwithunit =
40 ('-' | "") [ '0' - '9' ] + ["."] [ '0' - '9' ] + ([ 'a' - 'z' ] + | "" )
41 let regexp color = "#" [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f'
42 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ]
43 [ '0' - '9' 'a' - 'f' 'A' - 'F' ] [ '0' - '9' 'a' - 'f' 'A' - 'F' ]
45 (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
46 (* let regexp ident_letter = xml_letter *)
48 let regexp ident_letter = [ 'a' - 'z' 'A' - 'Z' ]
50 (* must be in sync with "is_ligature_char" below *)
51 let regexp ligature_char = [ "'`~!?@*()[]<>-+=|:;.,/\"" ]
52 let regexp ligature = ligature_char ligature_char+
54 let regexp we_proved = "we" utf8_blank+ "proved"
55 let regexp we_have = "we" utf8_blank+ "have"
56 let regexp let_rec = "let" utf8_blank+ "rec"
57 let regexp let_corec = "let" utf8_blank+ "corec"
58 let regexp ident_decoration = '\'' | '?' | '`'
59 let regexp ident_cont = ident_letter | xml_digit | '_'
60 let regexp ident_start = ident_letter
61 let regexp ident = ident_letter ident_cont* ident_decoration*
62 let regexp variable_ident = '_' '_' number
63 let regexp pident = '_' ident
65 let regexp tex_token = '\\' ident
67 let regexp delim_begin = "\\["
68 let regexp delim_end = "\\]"
70 let regexp qkeyword = "'" ( ident | pident ) "'"
72 let regexp implicit = '?'
73 let regexp placeholder = '%'
74 let regexp meta = implicit number
76 let regexp csymbol = '\'' ident
78 let regexp begin_group = "@{" | "${"
79 let regexp end_group = '}'
80 let regexp wildcard = "$_"
81 let regexp ast_ident = "@" ident
82 let regexp ast_csymbol = "@" csymbol
83 let regexp meta_ident = "$" ident
84 let regexp meta_anonymous = "$_"
85 let regexp qstring = '"' [^ '"']* '"'
87 let regexp begincomment = "(**" utf8_blank
88 let regexp beginnote = "(*"
89 let regexp endcomment = "*)"
90 (* let regexp comment_char = [^'*'] | '*'[^')']
91 let regexp note = "|+" ([^'*'] | "**") comment_char* "+|" *)
96 "over"; "atop"; "frac";
97 "sqrt"; "root"; "mstyle" ; "mpadded"; "maction"
101 let level1_keywords =
102 [ "hbox"; "hvbox"; "hovbox"; "vbox";
104 "list0"; "list1"; "sep";
106 "term"; "ident"; "number";
109 let level2_meta_keywords =
110 [ "if"; "then"; "elCicNotationParser.se";
111 "fold"; "left"; "right"; "rec";
114 "anonymous"; "ident"; "number"; "term"; "fresh"
117 (* (string, int) Hashtbl.t, with multiple bindings.
118 * int is the unicode codepoint *)
119 let ligatures = Hashtbl.create 23
123 (fun (ligature, symbol) -> Hashtbl.add ligatures ligature symbol)
124 [ ("->", <:unicode<to>>); ("=>", <:unicode<Rightarrow>>);
125 (":=", <:unicode<def>>);
128 let regexp uri_step = [ 'a' - 'z' 'A' - 'Z' '0' - '9' '_' '-' ''' ]+
131 ("cic:/" | "theory:/") (* schema *)
132 (* ident ('/' ident)* |+ path +| *)
133 uri_step ('/' uri_step)* (* path *)
134 ('.' ident)+ (* ext *)
135 ("#xpointer(" number ('/' number)+ ")")? (* xpointer *)
137 let regexp nreference =
139 uri_step ('/' uri_step)* (* path *)
142 | "def" ":" number ""
143 | "fix" ":" number ":" number ":" number
145 | "ind" ":" number ":" number ":" number
146 | "con" ":" number ":" number ":" number
147 ) (* ext + reference *)
149 let error lexbuf msg =
150 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
151 raise (Error (begin_cnum, end_cnum, msg))
152 let error_at_end lexbuf msg =
153 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
154 raise (Error (begin_cnum, end_cnum, msg))
156 let return_with_loc token begin_cnum end_cnum =
157 let flocation = HExtlib.floc_of_loc (begin_cnum,end_cnum) in
160 let return lexbuf token =
161 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
162 return_with_loc token begin_cnum end_cnum
164 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
166 let return_symbol lexbuf s = return lexbuf ("SYMBOL", s)
167 let return_eoi lexbuf = return lexbuf ("EOI", "")
169 let remove_quotes s = String.sub s 1 (String.length s - 2)
172 let tok_func stream =
173 (* let lexbuf = Ulexing.from_utf8_stream stream in *)
174 (** XXX Obj.magic rationale.
176 * camlp5 constraints the tok_func field of Token.glexer to have type:
177 * Stream.t char -> (Stream.t 'te * flocation_function)
178 * In order to use ulex we have (in theory) to instantiate a new lexbuf each
179 * time a char Stream.t is passed, destroying the previous lexbuf which may
180 * have consumed a character from the old stream which is lost forever :-(
182 * Instead of passing to camlp5 a char Stream.t we pass a lexbuf, casting it to
183 * char Stream.t with Obj.magic where needed.
185 let lexbuf = Obj.magic stream in
186 Token.make_stream_and_location
191 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
192 | Ulexing.InvalidCodepoint p ->
193 error_at_end lexbuf (sprintf "Invalid code point: %d" p))
196 Token.tok_func = tok_func;
197 Token.tok_using = (fun _ -> ());
198 Token.tok_removing = (fun _ -> ());
199 Token.tok_match = Token.default_match;
200 Token.tok_text = Token.lexer_text;
201 Token.tok_comm = None;
204 let expand_macro lexbuf =
206 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
209 ("SYMBOL", Utf8Macro.expand macro)
210 with Utf8Macro.Macro_not_found _ ->
211 (* FG: unexpanded TeX macros are terminated by a space for rendering *)
212 "SYMBOL", (Ulexing.utf8_lexeme lexbuf ^ " ")
214 let remove_quotes s = String.sub s 1 (String.length s - 2)
215 let remove_left_quote s = String.sub s 1 (String.length s - 1)
217 let rec level2_pattern_token_group counter buffer =
220 if (counter > 0) then
221 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
222 snd (Ulexing.loc lexbuf)
224 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
225 ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
226 level2_pattern_token_group counter buffer lexbuf
228 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
229 level2_pattern_token_group counter buffer lexbuf
231 let read_unparsed_group token_name lexbuf =
232 let buffer = Buffer.create 16 in
233 let begin_cnum, _ = Ulexing.loc lexbuf in
234 let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
235 return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
237 let handle_keywords lexbuf k name =
238 let s = Ulexing.utf8_lexeme lexbuf in
240 return lexbuf ("", s)
242 return lexbuf (name, s)
245 let rec level2_meta_token =
247 | utf8_blank+ -> level2_meta_token lexbuf
249 handle_keywords lexbuf (fun x -> List.mem x level2_meta_keywords) "IDENT"
250 | variable_ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
252 handle_keywords lexbuf (fun x -> List.mem x level2_meta_keywords) "PIDENT"
253 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
255 return lexbuf ("UNPARSED_AST",
256 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
258 return lexbuf ("UNPARSED_AST",
259 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
260 | eof -> return_eoi lexbuf
262 let rec comment_token acc depth =
265 let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
266 comment_token acc (depth + 1) lexbuf
268 let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
271 else comment_token acc (depth - 1) lexbuf
273 let acc = acc ^ Ulexing.utf8_lexeme lexbuf in
274 comment_token acc depth lexbuf
276 (** @param k continuation to be invoked when no ligature has been found *)
277 let ligatures_token k =
280 let lexeme = Ulexing.utf8_lexeme lexbuf in
281 (match List.rev (Hashtbl.find_all ligatures lexeme) with
282 | [] -> (* ligature not found, rollback and try default lexer *)
283 Ulexing.rollback lexbuf;
285 | default_lig :: _ -> (* ligatures found, use the default one *)
286 return_symbol lexbuf default_lig)
287 | eof -> return_eoi lexbuf
288 | _ -> (* not a ligature, rollback and try default lexer *)
289 Ulexing.rollback lexbuf;
292 let rec level2_ast_token status =
294 | let_rec -> return lexbuf ("LETREC","")
295 | let_corec -> return lexbuf ("LETCOREC","")
296 | we_proved -> return lexbuf ("WEPROVED","")
297 | we_have -> return lexbuf ("WEHAVE","")
298 | utf8_blank+ -> ligatures_token (level2_ast_token status) lexbuf
300 let s = Ulexing.utf8_lexeme lexbuf in
301 return lexbuf ("META", String.sub s 1 (String.length s - 1))
302 | implicit -> return lexbuf ("IMPLICIT", "")
303 | placeholder -> return lexbuf ("PLACEHOLDER", "")
304 | ident -> handle_keywords lexbuf (fun x -> StringSet.mem x status) "IDENT"
305 | variable_ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
306 | pident -> handle_keywords lexbuf (fun x -> StringSet.mem x status) "PIDENT"
307 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
308 | tex_token -> return lexbuf (expand_macro lexbuf)
309 | nreference -> return lexbuf ("NREF", Ulexing.utf8_lexeme lexbuf)
310 | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
312 return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
314 return lexbuf ("CSYMBOL", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
315 | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
316 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
317 | '(' -> return lexbuf ("LPAREN", "")
318 | ')' -> return lexbuf ("RPAREN", "")
320 return lexbuf ("UNPARSED_META",
321 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
322 | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
324 let _comment = comment_token (Ulexing.utf8_lexeme lexbuf) 0 lexbuf in
326 Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
328 return lexbuf ("NOTE", comment) *)
329 ligatures_token (level2_ast_token status) lexbuf
330 | begincomment -> return lexbuf ("BEGINCOMMENT","")
331 | endcomment -> return lexbuf ("ENDCOMMENT","")
332 | eof -> return_eoi lexbuf
333 | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
335 and level1_pattern_token =
337 | utf8_blank+ -> ligatures_token level1_pattern_token lexbuf
338 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
339 | ident ->handle_keywords lexbuf (fun x -> List.mem x level1_keywords) "IDENT"
340 | variable_ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
341 | pident->handle_keywords lexbuf (fun x->List.mem x level1_keywords) "PIDENT"
342 | color -> return lexbuf ("COLOR", Ulexing.utf8_lexeme lexbuf)
344 return lexbuf ("PERCENTAGE", Ulexing.utf8_lexeme lexbuf)
346 return lexbuf ("FLOATWITHUNIT", Ulexing.utf8_lexeme lexbuf)
347 | tex_token -> return lexbuf (expand_macro lexbuf)
349 return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
350 | '(' -> return lexbuf ("LPAREN", "")
351 | ')' -> return lexbuf ("RPAREN", "")
352 | eof -> return_eoi lexbuf
353 | _ -> return_symbol lexbuf (Ulexing.utf8_lexeme lexbuf)
355 let level1_pattern_token = ligatures_token level1_pattern_token
356 let level2_ast_token status = ligatures_token (level2_ast_token status)
358 (* API implementation *)
360 level1_pattern_lexer : (string * string) Token.glexer;
361 level2_ast_lexer : (string * string) Token.glexer;
362 level2_meta_lexer : (string * string) Token.glexer
365 let mk_lexers keywords =
366 let initial_keywords =
367 [ "CProp"; "Prop"; "Type"; "Set"; "let"; "match";
368 "with"; "in"; "and"; "to"; "as"; "on"; "return"; "done" ]
371 List.fold_right StringSet.add (initial_keywords @ keywords) StringSet.empty
374 level1_pattern_lexer = mk_lexer level1_pattern_token;
375 level2_ast_lexer = mk_lexer (level2_ast_token status);
376 level2_meta_lexer = mk_lexer level2_meta_token