]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualLexer2.ml
22c911eaf5b543e16bb7b1a35d21b3a758eee43c
[helm.git] / helm / ocaml / cic_disambiguation / cicTextualLexer2.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 paren = [ '(' '[' '{' ')' ']' '}' ]
43 let regexp implicit = '?'
44 let regexp meta = '?' num
45 let regexp qstring = '"' [^ '"']* '"'
46 let regexp uri =
47   (*      schema      *) (*     path     *) (*  ext   *) (*    xpointer     *)
48   ("cic:/" | "theory:/") ident ('/' ident)* ('.' ident)+ ('#' num ('/' num)*)?
49 (* let regexp catchall = .* *)
50
51 let keywords = Hashtbl.create 17
52 let _ =
53   List.iter (fun keyword -> Hashtbl.add keywords keyword ("", keyword))
54     [ "Prop"; "Type"; "Set"; "let"; "Let"; "rec"; "using"; "match"; "with" ]
55
56 let error lexbuf msg =
57   raise (Error (Ulexing.lexeme_start lexbuf, Ulexing.lexeme_end lexbuf, msg))
58 let error_at_end lexbuf msg =
59   raise (Error (Ulexing.lexeme_end lexbuf, Ulexing.lexeme_end lexbuf, msg))
60
61 let return lexbuf token = (token, Ulexing.loc lexbuf)
62
63 (*
64 let parse_ext_ident ident =
65   let len = String.length ident in
66   let buf = Buffer.create len in
67   let in_tex_token = ref false in
68   let tex_token = Buffer.create 10 in
69   try
70     for i = 0 to len - 1 do
71       match ident.[i] with
72       | '\' when not !in_tex_token ->
73           if i < len - 1 &&
74           in_tex_token := true
75     done
76   with Invalid_argument -> assert false
77
78 let rec token' = lexer
79   | ident' ->
80       (try
81         let ident = parse_ext_ident (Ulexing.utf8_lexeme lexbuf) in
82         return lexbuf ("IDENT'", ident)
83       with Not_an_extended_ident ->
84         Ulexing.rollback lexbuf;
85         token lexbuf)
86   | _ ->
87       Ulexing.rollback lexbuf;
88       token lexbuf
89
90 and token = lexer
91 *)
92 let rec token = lexer
93   | blanks -> token lexbuf
94   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
95   | ident ->
96       let lexeme = Ulexing.utf8_lexeme lexbuf in
97       (try
98         return lexbuf (Hashtbl.find keywords lexeme)
99       with Not_found -> return lexbuf ("IDENT", lexeme))
100   | num -> return lexbuf ("NUM", Ulexing.utf8_lexeme lexbuf)
101   | paren -> return lexbuf ("PAREN", Ulexing.utf8_lexeme lexbuf)
102   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
103   | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
104   | qstring ->
105       let lexeme = Ulexing.utf8_lexeme lexbuf in
106       let s = String.sub lexeme 1 (String.length lexeme - 2) in
107       return lexbuf ("QSTRING", s)
108   | symbol -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
109   | tex_token ->
110       let macro =
111         Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
112       in
113       (try
114         return lexbuf ("SYMBOL", CicTextualParser2Macro.expand macro)
115       with CicTextualParser2Macro.Macro_not_found _ ->
116         return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf))
117   | eof -> return lexbuf ("EOI", "")
118   | _ -> error lexbuf "Invalid character"
119
120 let tok_func stream =
121   let lexbuf = Ulexing.from_utf8_stream stream in
122   Token.make_stream_and_location
123     (fun () ->
124       try
125         token lexbuf
126       with
127       | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
128       | Ulexing.InvalidCodepoint i -> error_at_end lexbuf "Invalid code point")
129
130 let cic_lexer =
131   { 
132     Token.tok_func = tok_func;
133     Token.tok_using = (fun _ -> ());
134     Token.tok_removing = (fun _ -> ()); 
135     Token.tok_match = Token.default_match;
136     Token.tok_text = Token.lexer_text;
137     Token.tok_comm = None;
138   }
139