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 let regexp ident_decoration = '\'' | '!' | '?' | '`'
33 let regexp ident_cont = xml_letter | xml_digit | '_'
34 let regexp ident = xml_letter ident_cont* ident_decoration*
36 let regexp tex_token = '\\' ident
38 let regexp delim_begin = "\\["
39 let regexp delim_end = "\\]"
41 let regexp keyword = '"' ident '"'
42 let regexp qkeyword = "'" ident "'"
44 let regexp implicit = '?'
45 let regexp meta = implicit number
47 let regexp csymbol = '\'' ident
49 let regexp begin_group = "@{" | "${"
50 let regexp end_group = '}'
51 let regexp wildcard = "$_"
52 let regexp ast_ident = "@" ident
53 let regexp ast_csymbol = "@" csymbol
54 let regexp meta_ident = "$" ident
55 let regexp meta_anonymous = "$_"
56 let regexp qstring = '"' [^ '"']* '"'
61 "over"; "atop"; "frac";
66 [ "hbox"; "hvbox"; "hovbox"; "vbox";
68 "list0"; "list1"; "sep";
70 "term"; "ident"; "number"
73 let level2_meta_keywords =
74 [ "if"; "then"; "else";
75 "fold"; "left"; "right"; "rec";
78 "anonymous"; "ident"; "number"; "term"; "fresh"
81 (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
82 let level2_ast_keywords = Hashtbl.create 23
84 (* TODO ZACK: keyword list almost cut and paste from cicTextualLexer2.ml, to
86 List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
87 [ "Prop"; "Type"; "Set"; "let"; "rec"; "corec"; "match"; "with"; "in";
90 let add_level2_ast_keyword k =
91 prerr_endline ("Adding keyword " ^ k);
92 Hashtbl.add level2_ast_keywords k ()
93 let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
96 ("cic:/" | "theory:/") (* schema *)
97 ident ('/' ident)* (* path *)
98 ('.' ident)+ (* ext *)
99 ("#xpointer(" number ('/' number)+ ")")? (* xpointer *)
101 let error lexbuf msg =
102 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
103 raise (Error (begin_cnum, end_cnum, msg))
104 let error_at_end lexbuf msg =
105 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
106 raise (Error (begin_cnum, end_cnum, msg))
108 let return_with_loc token begin_cnum end_cnum =
109 (* TODO handle line/column numbers *)
110 let flocation_begin =
111 { Lexing.pos_fname = "";
112 Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
113 Lexing.pos_cnum = begin_cnum }
115 let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
116 (token, (flocation_begin, flocation_end))
118 let return lexbuf token =
119 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
120 return_with_loc token begin_cnum end_cnum
122 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
124 let remove_quotes s = String.sub s 1 (String.length s - 2)
127 let tok_func stream =
128 let lexbuf = Ulexing.from_utf8_stream stream in
129 Token.make_stream_and_flocation
134 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
135 | Ulexing.InvalidCodepoint p ->
136 error_at_end lexbuf (sprintf "Invalid code point: %d" p))
139 Token.tok_func = tok_func;
140 Token.tok_using = (fun _ -> ());
141 Token.tok_removing = (fun _ -> ());
142 Token.tok_match = Token.default_match;
143 Token.tok_text = Token.lexer_text;
144 Token.tok_comm = None;
147 let expand_macro lexbuf =
149 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
152 ("SYMBOL", Utf8Macro.expand macro)
153 with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
155 let keyword lexbuf = "KEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf)
157 let remove_quotes s = String.sub s 1 (String.length s - 2)
158 let remove_left_quote s = String.sub s 1 (String.length s - 1)
160 let rec level2_pattern_token_group counter buffer = lexer
162 if (counter > 0) then
163 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
164 snd (Ulexing.loc lexbuf)
166 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
167 ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
168 level2_pattern_token_group counter buffer lexbuf
170 Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
171 level2_pattern_token_group counter buffer lexbuf
173 let read_unparsed_group token_name lexbuf =
174 let buffer = Buffer.create 16 in
175 let begin_cnum, _ = Ulexing.loc lexbuf in
176 let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
177 return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
179 let rec level2_meta_token = lexer
180 | xml_blank+ -> level2_meta_token lexbuf
182 let s = Ulexing.utf8_lexeme lexbuf in
184 if List.mem s level2_meta_keywords then
185 return lexbuf ("", s)
187 return lexbuf ("IDENT", s)
189 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
191 return lexbuf ("UNPARSED_AST",
192 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
194 return lexbuf ("UNPARSED_AST",
195 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
196 | eof -> return lexbuf ("EOI", "")
198 let rec level2_ast_token = lexer
199 | xml_blank+ -> level2_ast_token lexbuf
200 | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
201 | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
203 let lexeme = Ulexing.utf8_lexeme lexbuf in
204 if Hashtbl.mem level2_ast_keywords lexeme then
205 return lexbuf ("", lexeme)
207 return lexbuf ("IDENT", lexeme)
208 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
209 | keyword -> return lexbuf (keyword lexbuf)
210 | tex_token -> return lexbuf (expand_macro lexbuf)
211 | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
213 return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
214 | csymbol -> return lexbuf ("CSYMBOL", Ulexing.utf8_lexeme lexbuf)
215 | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
216 | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
217 | '(' -> return lexbuf ("LPAREN", "")
218 | ')' -> return lexbuf ("RPAREN", "")
220 return lexbuf ("UNPARSED_META",
221 remove_left_quote (Ulexing.utf8_lexeme lexbuf))
222 | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
223 | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
224 | eof -> return lexbuf ("EOI", "")
226 let rec level1_pattern_token = lexer
227 | xml_blank+ -> level1_pattern_token lexbuf
228 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
230 let s = Ulexing.utf8_lexeme lexbuf in
232 if List.mem s level1_keywords then
233 return lexbuf ("", s)
235 return lexbuf ("IDENT", s)
237 | tex_token -> return lexbuf (expand_macro lexbuf)
239 return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
240 | '(' -> return lexbuf ("LPAREN", "")
241 | ')' -> return lexbuf ("RPAREN", "")
242 | eof -> return lexbuf ("EOI", "")
243 | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
245 (* API implementation *)
247 let level1_pattern_lexer = mk_lexer level1_pattern_token
248 let level2_ast_lexer = mk_lexer level2_ast_token
249 let level2_meta_lexer = mk_lexer level2_meta_token