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 '"'
43 let regexp implicit = '?'
44 let regexp meta = implicit number
46 let regexp csymbol = '\'' ident
49 ("cic:/" | "theory:/") (* schema *)
50 ident ('/' ident)* (* path *)
51 ('.' ident)+ (* ext *)
52 ("#xpointer(" number ('/' number)+ ")")? (* xpointer *)
54 let error lexbuf msg =
55 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
56 raise (Error (begin_cnum, end_cnum, msg))
57 let error_at_end lexbuf msg =
58 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
59 raise (Error (begin_cnum, end_cnum, msg))
61 let return lexbuf token =
62 let begin_cnum, end_cnum = Ulexing.loc lexbuf in
63 (* TODO handle line/column numbers *)
65 { Lexing.pos_fname = "";
66 Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
67 Lexing.pos_cnum = begin_cnum }
69 let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
70 (token, (flocation_begin, flocation_end))
72 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
74 let remove_quotes s = String.sub s 1 (String.length s - 2)
78 let lexbuf = Ulexing.from_utf8_stream stream in
79 Token.make_stream_and_flocation
84 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
85 | Ulexing.InvalidCodepoint p ->
86 error_at_end lexbuf (sprintf "Invalid code point: %d" p))
89 Token.tok_func = tok_func;
90 Token.tok_using = (fun _ -> ());
91 Token.tok_removing = (fun _ -> ());
92 Token.tok_match = Token.default_match;
93 Token.tok_text = Token.lexer_text;
94 Token.tok_comm = None;
97 let expand_macro lexbuf =
99 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
102 ("SYMBOL", Utf8Macro.expand macro)
103 with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
105 let keyword lexbuf = "KEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf)
107 let rec token = lexer
108 | xml_blank+ -> token lexbuf
109 | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
110 | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
111 | ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
112 | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
113 | keyword -> return lexbuf (keyword lexbuf)
114 | delim_begin -> return lexbuf ("DELIM", Ulexing.utf8_lexeme lexbuf)
115 | delim_end -> return lexbuf ("DELIM", Ulexing.utf8_lexeme lexbuf)
116 | tex_token -> return lexbuf (expand_macro lexbuf)
117 | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
118 | eof -> return lexbuf ("EOI", "")
119 | csymbol -> return lexbuf ("CSYMBOL", Ulexing.utf8_lexeme lexbuf)
120 | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
122 (* API implementation *)
124 let notation_lexer = mk_lexer token