]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualLexer2.ml
version 0.7.1
[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 let regexp paren = [ '(' '[' '{' ')' ']' '}' ]
34 let regexp implicit = '?'
35 let regexp placeholder = '%'
36 let regexp symbol_char =
37   [^ 'a' - 'z' 'A' - 'Z' '0' - '9'
38      ' ' '\t' '\n'
39      '\\' '(' '[' '{' ')' ']' '}' '?'
40   ]
41
42 let regexp comment_char = [^'*'] | '*'[^')']
43 let regexp note = "(*" ([^'*'] | "**") comment_char* "*)"
44
45 let regexp commentbegin = "(**" blank
46 let regexp commentend = "*)"
47
48 let regexp blanks = blank+
49 let regexp num = digit+
50 let regexp tex_token = '\\' alpha+
51 let regexp symbol = symbol_char
52 let regexp ident_cont = alpha | num | '_' | '\''
53 let regexp ident_cont' = ident_cont | tex_token
54 let regexp ident = (alpha ident_cont*) | ('_' ident_cont+)
55 let regexp ident' = ((alpha | tex_token) ident_cont'*) | ('_' ident_cont'+)
56 let regexp uri_step = (alpha (ident_cont|'-')*)
57 let regexp meta = implicit num
58 let regexp qstring = '"' [^ '"']* '"'
59 let regexp uri_suffix = "con"|"ind"|"var"
60 let regexp uri =
61   ("cic:/" | "theory:/")              (* schema *)
62   uri_step ('/' uri_step)*            (* path *)
63   ('.' uri_suffix)+                   (* ext *)
64   ("#xpointer(" num ('/' num)+ ")")?  (* xpointer *)
65 (* let regexp catchall = .* *)
66
67 let keywords = Hashtbl.create 17
68 let _ =
69   List.iter (fun keyword -> Hashtbl.add keywords keyword ("", keyword))
70     [ "Prop"; "Type"; "Set"; "let"; "Let"; "rec"; "using"; "match"; "with";
71     "in"; "and"; "to"; "as"; "on"; "names"]
72
73 let error lexbuf msg =
74   raise (Error (Ulexing.lexeme_start lexbuf, Ulexing.lexeme_end lexbuf, msg))
75 let error_at_end lexbuf msg =
76   raise (Error (Ulexing.lexeme_end lexbuf, Ulexing.lexeme_end lexbuf, msg))
77
78 let return lexbuf token =
79   let flocation_begin =
80     { Lexing.pos_fname = ""; Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
81       Lexing.pos_cnum = Ulexing.lexeme_start lexbuf }
82   in
83   let flocation_end =
84     { flocation_begin with Lexing.pos_cnum = Ulexing.lexeme_end lexbuf }
85   in
86   (token, (flocation_begin, flocation_end))
87
88 (*
89 let parse_ext_ident ident =
90   let len = String.length ident in
91   let buf = Buffer.create len in
92   let in_tex_token = ref false in
93   let tex_token = Buffer.create 10 in
94   try
95     for i = 0 to len - 1 do
96       match ident.[i] with
97       | '\' when not !in_tex_token ->
98           if i < len - 1 &&
99           in_tex_token := true
100     done
101   with Invalid_argument -> assert false
102
103 let rec token' = lexer
104   | ident' ->
105       (try
106         let ident = parse_ext_ident (Ulexing.utf8_lexeme lexbuf) in
107         return lexbuf ("IDENT'", ident)
108       with Not_an_extended_ident ->
109         Ulexing.rollback lexbuf;
110         token lexbuf)
111   | _ ->
112       Ulexing.rollback lexbuf;
113       token lexbuf
114
115 and token = lexer
116 *)
117
118 let remove_quotes s = String.sub s 1 (String.length s - 2)
119
120 let rec token comments = lexer
121   | blanks -> token comments lexbuf
122   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
123   | ident ->
124       let lexeme = Ulexing.utf8_lexeme lexbuf in
125       (try
126         return lexbuf (Hashtbl.find keywords lexeme)
127       with Not_found -> return lexbuf ("IDENT", lexeme))
128   | num -> return lexbuf ("NUM", Ulexing.utf8_lexeme lexbuf)
129   | paren -> return lexbuf ("PAREN", Ulexing.utf8_lexeme lexbuf)
130   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
131   | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
132   | placeholder -> return lexbuf ("PLACEHOLDER", Ulexing.utf8_lexeme lexbuf)
133   | qstring ->
134       return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
135   | symbol -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
136   | tex_token ->
137       let macro =
138         Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
139       in
140       (try
141         return lexbuf ("SYMBOL", Utf8Macro.expand macro)
142       with Utf8Macro.Macro_not_found _ ->
143         return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf))
144   | note -> 
145       (*if comments then*)
146         let comment =
147           Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
148         in
149         return lexbuf ("NOTE", comment)
150       (*else
151         token comments lexbuf*)
152   | commentbegin -> return lexbuf ("BEGINCOMMENT","")
153   | commentend -> return lexbuf ("ENDCOMMENT","")
154   | eof -> return lexbuf ("EOI", "")
155   | _ -> error lexbuf "Invalid character"
156
157 let tok_func comments stream =
158   let lexbuf = Ulexing.from_utf8_stream stream in
159   Token.make_stream_and_flocation
160     (fun () ->
161       try
162         token comments lexbuf
163       with
164       | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
165       | Ulexing.InvalidCodepoint i -> error_at_end lexbuf "Invalid code point")
166
167 let cic_lexer ?(comments = false) () =
168   {
169     Token.tok_func = tok_func comments;
170     Token.tok_using = (fun _ -> ());
171     Token.tok_removing = (fun _ -> ()); 
172     Token.tok_match = Token.default_match;
173     Token.tok_text = Token.lexer_text;
174     Token.tok_comm = None;
175   }
176