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 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" ]
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
102 ("cic:/" | "theory:/") (* schema *)
103 ident ('/' ident)* (* path *)
104 ('.' ident)+ (* ext *)
105 ("#xpointer(" number ('/' number)+ ")")? (* xpointer *)
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))
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 }
121 let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
122 (token, (flocation_begin, flocation_end))
124 let return lexbuf token =
125 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
126 return_with_loc token begin_cnum end_cnum
128 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
130 let remove_quotes s = String.sub s 1 (String.length s - 2)
133 let tok_func stream =
134 let lexbuf = Ulexing.from_utf8_stream stream in
135 Token.make_stream_and_flocation
140 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
141 | Ulexing.InvalidCodepoint p ->
142 error_at_end lexbuf (sprintf "Invalid code point: %d" p))
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;
153 let expand_macro lexbuf =
155 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
158 ("SYMBOL", Utf8Macro.expand macro)
159 with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
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)
164 let rec level2_pattern_token_group counter buffer = lexer
166 if (counter > 0) then
167 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
168 snd (Ulexing.loc lexbuf)
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
174 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
175 level2_pattern_token_group counter buffer lexbuf
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
183 let rec level2_meta_token = lexer
184 | xml_blank+ -> level2_meta_token lexbuf
186 let s = Ulexing.utf8_lexeme lexbuf in
188 if List.mem s level2_meta_keywords then
189 return lexbuf ("", s)
191 return lexbuf ("IDENT", s)
193 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
195 return lexbuf ("UNPARSED_AST",
196 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
198 return lexbuf ("UNPARSED_AST",
199 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
200 | eof -> return lexbuf ("EOI", "")
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", "")
208 let lexeme = Ulexing.utf8_lexeme lexbuf in
209 if Hashtbl.mem level2_ast_keywords lexeme then
210 return lexbuf ("", lexeme)
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)
217 return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
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", "")
225 return lexbuf ("UNPARSED_META",
226 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
227 | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
230 Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
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)
238 let rec level1_pattern_token = lexer
239 | xml_blank+ -> level1_pattern_token lexbuf
240 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
242 let s = Ulexing.utf8_lexeme lexbuf in
244 if List.mem s level1_keywords then
245 return lexbuf ("", s)
247 return lexbuf ("IDENT", s)
249 | tex_token -> return lexbuf (expand_macro lexbuf)
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)
257 (* API implementation *)
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