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