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