1 (* Copyright (C) 2004, 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/
26 exception Error of int * int * string
28 exception Not_an_extended_ident
30 let regexp alpha = [ 'a' - 'z' 'A' - 'Z' ]
31 let regexp digit = [ '0' - '9' ]
32 let regexp blank = [ ' ' '\t' '\n' ]
33 let regexp paren = [ '(' '[' '{' ')' ']' '}' ]
34 let regexp implicit = '?'
35 let regexp symbol_char =
36 [^ 'a' - 'z' 'A' - 'Z' '0' - '9'
38 '\\' '(' '[' '{' ')' ']' '}' '?'
40 let regexp comment_char = [^ '\n' ]
41 let regexp comment = "%%" comment_char*
42 let regexp blanks = blank+
43 let regexp num = digit+
44 let regexp tex_token = '\\' alpha+
45 let regexp symbol = symbol_char+
46 let regexp ident_cont = alpha | num | '_' | '\''
47 let regexp ident_cont' = ident_cont | tex_token
48 let regexp ident = (alpha ident_cont*) | ('_' ident_cont+)
49 let regexp ident' = ((alpha | tex_token) ident_cont'*) | ('_' ident_cont'+)
50 let regexp meta = implicit num
51 let regexp qstring = '"' [^ '"']* '"'
53 ("cic:/" | "theory:/") (* schema *)
54 ident ('/' ident)* (* path *)
55 ('.' ident)+ (* ext *)
56 ("#xpointer(" num ('/' num)+ ")")? (* xpointer *)
57 (* let regexp catchall = .* *)
59 let keywords = Hashtbl.create 17
61 List.iter (fun keyword -> Hashtbl.add keywords keyword ("", keyword))
62 [ "Prop"; "Type"; "Set"; "let"; "Let"; "rec"; "using"; "match"; "with";
65 let error lexbuf msg =
66 raise (Error (Ulexing.lexeme_start lexbuf, Ulexing.lexeme_end lexbuf, msg))
67 let error_at_end lexbuf msg =
68 raise (Error (Ulexing.lexeme_end lexbuf, Ulexing.lexeme_end lexbuf, msg))
70 let return lexbuf token =
72 { Lexing.pos_fname = ""; Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
73 Lexing.pos_cnum = Ulexing.lexeme_start lexbuf }
76 { flocation_begin with Lexing.pos_cnum = Ulexing.lexeme_end lexbuf }
78 (token, (flocation_begin, flocation_end))
81 let parse_ext_ident ident =
82 let len = String.length ident in
83 let buf = Buffer.create len in
84 let in_tex_token = ref false in
85 let tex_token = Buffer.create 10 in
87 for i = 0 to len - 1 do
89 | '\' when not !in_tex_token ->
93 with Invalid_argument -> assert false
95 let rec token' = lexer
98 let ident = parse_ext_ident (Ulexing.utf8_lexeme lexbuf) in
99 return lexbuf ("IDENT'", ident)
100 with Not_an_extended_ident ->
101 Ulexing.rollback lexbuf;
104 Ulexing.rollback lexbuf;
109 let rec token = lexer
110 | blanks -> token lexbuf
111 | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
113 let lexeme = Ulexing.utf8_lexeme lexbuf in
115 return lexbuf (Hashtbl.find keywords lexeme)
116 with Not_found -> return lexbuf ("IDENT", lexeme))
117 | num -> return lexbuf ("NUM", Ulexing.utf8_lexeme lexbuf)
118 | paren -> return lexbuf ("PAREN", Ulexing.utf8_lexeme lexbuf)
119 | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
120 | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
122 let lexeme = Ulexing.utf8_lexeme lexbuf in
123 let s = String.sub lexeme 1 (String.length lexeme - 2) in
124 return lexbuf ("QSTRING", s)
125 | symbol -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
128 Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
131 return lexbuf ("SYMBOL", Utf8Macro.expand macro)
132 with Utf8Macro.Macro_not_found _ ->
133 return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf))
136 Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 2)
138 return lexbuf ("COMMENT", comment)
139 | eof -> return lexbuf ("EOI", "")
140 | _ -> error lexbuf "Invalid character"
142 let tok_func stream =
143 let lexbuf = Ulexing.from_utf8_stream stream in
144 Token.make_stream_and_flocation
149 | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
150 | Ulexing.InvalidCodepoint i -> error_at_end lexbuf "Invalid code point")
154 Token.tok_func = tok_func;
155 Token.tok_using = (fun _ -> ());
156 Token.tok_removing = (fun _ -> ());
157 Token.tok_match = Token.default_match;
158 Token.tok_text = Token.lexer_text;
159 Token.tok_comm = None;