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/
28 exception Error of int * int * string
30 let regexp number = xml_digit+
32 (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
33 (* let regexp ident_letter = xml_letter *)
35 let regexp ident_letter = [ 'a' - 'z' 'A' - 'Z' ]
37 let regexp ident_decoration = '\'' | '!' | '?' | '`'
38 let regexp ident_cont = ident_letter | xml_digit | '_'
39 let regexp ident = ident_letter ident_cont* ident_decoration*
41 let regexp tex_token = '\\' ident
43 let regexp delim_begin = "\\["
44 let regexp delim_end = "\\]"
46 let regexp qkeyword = "'" ident "'"
48 let regexp implicit = '?'
49 let regexp placeholder = '%'
50 let regexp meta = implicit number
52 let regexp csymbol = '\'' ident
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 = '"' [^ '"']* '"'
63 let regexp begincomment = "(**" xml_blank
64 let regexp endcomment = "*)"
65 let regexp comment_char = [^'*'] | '*'[^')']
66 let regexp note = "(*" ([^'*'] | "**") comment_char* "*)"
71 "over"; "atop"; "frac";
76 [ "hbox"; "hvbox"; "hovbox"; "vbox";
78 "list0"; "list1"; "sep";
80 "term"; "ident"; "number"
83 let level2_meta_keywords =
84 [ "if"; "then"; "else";
85 "fold"; "left"; "right"; "rec";
88 "anonymous"; "ident"; "number"; "term"; "fresh"
91 (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
92 let level2_ast_keywords = Hashtbl.create 23
94 (* TODO ZACK: keyword list almost cut and paste from cicTextualLexer2.ml, to
96 List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
97 [ "Prop"; "Type"; "Set"; "let"; "rec"; "corec"; "match"; "with"; "in";
100 let add_level2_ast_keyword k = Hashtbl.add level2_ast_keywords k ()
101 let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
104 ("cic:/" | "theory:/") (* schema *)
105 ident ('/' ident)* (* path *)
106 ('.' ident)+ (* ext *)
107 ("#xpointer(" number ('/' number)+ ")")? (* xpointer *)
109 let error lexbuf msg =
110 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
111 raise (Error (begin_cnum, end_cnum, msg))
112 let error_at_end lexbuf msg =
113 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
114 raise (Error (begin_cnum, end_cnum, msg))
116 let return_with_loc token begin_cnum end_cnum =
117 (* TODO handle line/column numbers *)
118 let flocation_begin =
119 { Lexing.pos_fname = "";
120 Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
121 Lexing.pos_cnum = begin_cnum }
123 let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
124 (token, (flocation_begin, flocation_end))
126 let return lexbuf token =
127 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
128 return_with_loc token begin_cnum end_cnum
130 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
132 let remove_quotes s = String.sub s 1 (String.length s - 2)
135 let tok_func stream =
136 let lexbuf = Ulexing.from_utf8_stream stream in
137 Token.make_stream_and_flocation
142 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
143 | Ulexing.InvalidCodepoint p ->
144 error_at_end lexbuf (sprintf "Invalid code point: %d" p))
147 Token.tok_func = tok_func;
148 Token.tok_using = (fun _ -> ());
149 Token.tok_removing = (fun _ -> ());
150 Token.tok_match = Token.default_match;
151 Token.tok_text = Token.lexer_text;
152 Token.tok_comm = None;
155 let expand_macro lexbuf =
157 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
160 ("SYMBOL", Utf8Macro.expand macro)
161 with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
163 let remove_quotes s = String.sub s 1 (String.length s - 2)
164 let remove_left_quote s = String.sub s 1 (String.length s - 1)
166 let rec level2_pattern_token_group counter buffer = lexer
168 if (counter > 0) then
169 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
170 snd (Ulexing.loc lexbuf)
172 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
173 ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
174 level2_pattern_token_group counter buffer lexbuf
176 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
177 level2_pattern_token_group counter buffer lexbuf
179 let read_unparsed_group token_name lexbuf =
180 let buffer = Buffer.create 16 in
181 let begin_cnum, _ = Ulexing.loc lexbuf in
182 let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
183 return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
185 let rec level2_meta_token = lexer
186 | xml_blank+ -> level2_meta_token lexbuf
188 let s = Ulexing.utf8_lexeme lexbuf in
190 if List.mem s level2_meta_keywords then
191 return lexbuf ("", s)
193 return lexbuf ("IDENT", s)
195 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
197 return lexbuf ("UNPARSED_AST",
198 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
200 return lexbuf ("UNPARSED_AST",
201 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
202 | eof -> return lexbuf ("EOI", "")
204 let rec level2_ast_token = lexer
205 | xml_blank+ -> level2_ast_token lexbuf
206 | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
207 | implicit -> return lexbuf ("IMPLICIT", "")
208 | placeholder -> return lexbuf ("PLACEHOLDER", "")
210 let lexeme = Ulexing.utf8_lexeme lexbuf in
211 if Hashtbl.mem level2_ast_keywords lexeme then
212 return lexbuf ("", lexeme)
214 return lexbuf ("IDENT", lexeme)
215 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
216 | tex_token -> return lexbuf (expand_macro lexbuf)
217 | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
219 return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
220 | csymbol -> return lexbuf ("CSYMBOL", Ulexing.utf8_lexeme lexbuf)
221 | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
222 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
223 | '(' -> return lexbuf ("LPAREN", "")
224 | ')' -> return lexbuf ("RPAREN", "")
226 return lexbuf ("UNPARSED_META",
227 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
228 | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
231 Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
233 return lexbuf ("NOTE", comment)
234 | begincomment -> return lexbuf ("BEGINCOMMENT","")
235 | endcomment -> return lexbuf ("ENDCOMMENT","")
236 | eof -> return lexbuf ("EOI", "")
237 | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
239 let rec level1_pattern_token = lexer
240 | xml_blank+ -> level1_pattern_token lexbuf
241 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
243 let s = Ulexing.utf8_lexeme lexbuf in
245 if List.mem s level1_keywords then
246 return lexbuf ("", s)
248 return lexbuf ("IDENT", s)
250 | tex_token -> return lexbuf (expand_macro lexbuf)
252 return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
253 | '(' -> return lexbuf ("LPAREN", "")
254 | ')' -> return lexbuf ("RPAREN", "")
255 | eof -> return lexbuf ("EOI", "")
256 | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
258 (* API implementation *)
260 let level1_pattern_lexer = mk_lexer level1_pattern_token
261 let level2_ast_lexer = mk_lexer level2_ast_token
262 let level2_meta_lexer = mk_lexer level2_meta_token