]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/lexer.ml
10f0667f37391f5fca5aed97dd93339ff0fc591b
[helm.git] / helm / ocaml / cic_disambiguation / lexer.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 exception Error of int * int * string
27
28 let regexp alpha = [ 'a' - 'z' 'A' - 'Z' ]
29 let regexp digit = [ '0' - '9' ]
30 let regexp blank = [ ' ' '\t' '\n' ]
31
32 let regexp blanks = blank+
33 let regexp num = digit+
34 let regexp ident = alpha (alpha | num)*
35 let regexp symbol = [^ 'a' - 'z' 'A' - 'Z' '0' - '9' ' ' '\t' '\n' ]
36 let regexp tex_token = '\\' alpha+
37 let regexp lparen = [ '(' '[' '{' ]
38 let regexp rparen = [ ')' ']' '}' ]
39 let regexp meta = '?' num
40
41 let keywords = Hashtbl.create 17
42 let _ =
43   List.iter (fun keyword -> Hashtbl.add keywords keyword ("", keyword))
44     [ "Prop"; "Type"; "Set"; "let"; "rec"; "using"; "match"; "with" ]
45
46 let error lexbuf msg =
47   raise (Error (Ulexing.lexeme_start lexbuf, Ulexing.lexeme_end lexbuf, msg))
48 let error_at_end lexbuf msg =
49   raise (Error (Ulexing.lexeme_end lexbuf, Ulexing.lexeme_end lexbuf, msg))
50
51 let return lexbuf token = (token, Ulexing.loc lexbuf)
52
53 let rec token = lexer
54 | blanks -> token lexbuf
55 | ident ->
56     let lexeme = Ulexing.utf8_lexeme lexbuf in
57     (try
58       return lexbuf (Hashtbl.find keywords lexeme)
59     with Not_found ->
60       return lexbuf ("IDENT", lexeme))
61 | num -> return lexbuf ("INT", Ulexing.utf8_lexeme lexbuf)
62 | lparen -> return lexbuf ("LPAREN", Ulexing.utf8_lexeme lexbuf)
63 | rparen -> return lexbuf ("RPAREN", Ulexing.utf8_lexeme lexbuf)
64 | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
65 | symbol -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
66 | tex_token ->
67     let macro =
68       Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
69     in
70     (try
71       return lexbuf ("SYMBOL", Macro.expand macro)
72     with Macro.Macro_not_found _ ->
73       return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf))
74 | eof -> return lexbuf ("EOI", "")
75 | _ -> error lexbuf "Invalid character"
76
77 let tok_func stream =
78   let lexbuf = Ulexing.from_utf8_stream stream in
79   Token.make_stream_and_location
80     (fun () ->
81       try
82         token lexbuf
83       with
84       | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
85       | Ulexing.InvalidCodepoint i -> error_at_end lexbuf "Invalid code point")
86
87 let lex =
88   { 
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;
95   }