]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/lexer.ml
ported to latest lablgtk2 snapshot
[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 exception Not_an_extended_ident
29
30 let regexp alpha = [ 'a' - 'z' 'A' - 'Z' ]
31 let regexp digit = [ '0' - '9' ]
32 let regexp blank = [ ' ' '\t' '\n' ]
33
34 let regexp blanks = blank+
35 let regexp num = digit+
36 let regexp tex_token = '\\' alpha+
37 let regexp symbol = [^ 'a' - 'z' 'A' - 'Z' '0' - '9' ' ' '\t' '\n' ]
38 let regexp ident_cont = alpha | num | '_'
39 let regexp ident_cont' = ident_cont | tex_token
40 let regexp ident = (alpha ident_cont*) | ('_' ident_cont+)
41 let regexp ident' = ((alpha | tex_token) ident_cont'*) | ('_' ident_cont'+)
42 let regexp lparen = [ '(' '[' '{' ]
43 let regexp rparen = [ ')' ']' '}' ]
44 let regexp meta = '?' num
45 (* let regexp catchall = .* *)
46
47 let keywords = Hashtbl.create 17
48 let _ =
49   List.iter (fun keyword -> Hashtbl.add keywords keyword ("", keyword))
50     [ "Prop"; "Type"; "Set"; "let"; "rec"; "using"; "match"; "with" ]
51
52 let error lexbuf msg =
53   raise (Error (Ulexing.lexeme_start lexbuf, Ulexing.lexeme_end lexbuf, msg))
54 let error_at_end lexbuf msg =
55   raise (Error (Ulexing.lexeme_end lexbuf, Ulexing.lexeme_end lexbuf, msg))
56
57 let return lexbuf token = (token, Ulexing.loc lexbuf)
58
59 (*
60 let parse_ext_ident ident =
61   let len = String.length ident in
62   let buf = Buffer.create len in
63   let in_tex_token = ref false in
64   let tex_token = Buffer.create 10 in
65   try
66     for i = 0 to len - 1 do
67       match ident.[i] with
68       | '\' when not !in_tex_token ->
69           if i < len - 1 &&
70           in_tex_token := true
71     done
72   with Invalid_argument -> assert false
73
74 let rec token' = lexer
75   | ident' ->
76       (try
77         let ident = parse_ext_ident (Ulexing.utf8_lexeme lexbuf) in
78         return lexbuf ("IDENT'", ident)
79       with Not_an_extended_ident ->
80         Ulexing.rollback lexbuf;
81         token lexbuf)
82   | _ ->
83       Ulexing.rollback lexbuf;
84       token lexbuf
85
86 and token = lexer
87 *)
88 let rec token = lexer
89   | blanks -> token lexbuf
90   | ident ->
91       let lexeme = Ulexing.utf8_lexeme lexbuf in
92       (try
93         return lexbuf (Hashtbl.find keywords lexeme)
94       with Not_found -> return lexbuf ("IDENT", lexeme))
95   | num -> return lexbuf ("NUM", Ulexing.utf8_lexeme lexbuf)
96   | lparen -> return lexbuf ("LPAREN", Ulexing.utf8_lexeme lexbuf)
97   | rparen -> return lexbuf ("RPAREN", Ulexing.utf8_lexeme lexbuf)
98   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
99   | symbol -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
100   | tex_token ->
101       let macro =
102         Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
103       in
104       (try
105         return lexbuf ("SYMBOL", Macro.expand macro)
106       with Macro.Macro_not_found _ ->
107         return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf))
108   | eof -> return lexbuf ("EOI", "")
109   | _ -> error lexbuf "Invalid character"
110
111 let tok_func stream =
112   let lexbuf = Ulexing.from_utf8_stream stream in
113   Token.make_stream_and_location
114     (fun () ->
115       try
116         token lexbuf
117       with
118       | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
119       | Ulexing.InvalidCodepoint i -> error_at_end lexbuf "Invalid code point")
120
121 let lex =
122   { 
123     Token.tok_func = tok_func;
124     Token.tok_using = (fun _ -> ());
125     Token.tok_removing = (fun _ -> ()); 
126     Token.tok_match = Token.default_match;
127     Token.tok_text = Token.lexer_text;
128     Token.tok_comm = None;
129   }