]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationLexer.ml
snapshot, notably:
[helm.git] / helm / ocaml / cic_notation / cicNotationLexer.ml
1 (* Copyright (C) 2005, 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 open Printf
27
28 exception Error of int * int * string
29
30 let regexp number = xml_digit+
31
32 let regexp ident_decoration = '\'' | '!' | '?' | '`'
33 let regexp ident_cont = xml_letter | xml_digit | '_'
34 let regexp ident = xml_letter ident_cont* ident_decoration*
35
36 let regexp tex_token = '\\' ident
37
38 let regexp keyword = '"' ident '"'
39
40 let regexp implicit = '?'
41 let regexp meta = implicit number
42
43 let regexp uri =
44   ("cic:/" | "theory:/")              (* schema *)
45   ident ('/' ident)*                  (* path *)
46   ('.' ident)+                        (* ext *)
47   ("#xpointer(" number ('/' number)+ ")")?  (* xpointer *)
48
49 let error lexbuf msg =
50   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
51   raise (Error (begin_cnum, end_cnum, msg))
52 let error_at_end lexbuf msg =
53   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
54   raise (Error (begin_cnum, end_cnum, msg))
55
56 let return lexbuf token =
57   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
58   (* TODO handle line/column numbers *)
59   let flocation_begin =
60     { Lexing.pos_fname = "";
61       Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
62       Lexing.pos_cnum = begin_cnum }
63   in
64   let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
65   (token, (flocation_begin, flocation_end))
66
67 let remove_quotes s = String.sub s 1 (String.length s - 2)
68
69 let mk_lexer token =
70   let tok_func stream =
71     let lexbuf = Ulexing.from_utf8_stream stream in
72     Token.make_stream_and_flocation
73       (fun () ->
74         try
75           token lexbuf
76         with
77         | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
78         | Ulexing.InvalidCodepoint p ->
79             error_at_end lexbuf (sprintf "Invalid code point: %d" p))
80   in
81   {
82     Token.tok_func = tok_func;
83     Token.tok_using = (fun _ -> ());
84     Token.tok_removing = (fun _ -> ()); 
85     Token.tok_match = Token.default_match;
86     Token.tok_text = Token.lexer_text;
87     Token.tok_comm = None;
88   }
89
90 let expand_macro lexbuf =
91   let macro =
92     Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
93   in
94   try
95     ("SYMBOL", Utf8Macro.expand macro)
96   with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
97
98 let keyword lexbuf = "KEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf)
99
100 (** {2 Level 1 lexer} *)
101
102 let rec level1_token = lexer
103   | xml_blank+ -> level1_token lexbuf
104   | ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
105   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
106   | keyword -> return lexbuf (keyword lexbuf)
107   | tex_token -> return lexbuf (expand_macro lexbuf)
108   | eof -> return lexbuf ("EOI", "")
109   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
110
111 (** {2 Level 2 lexer} *)
112
113 let rec level2_token = lexer
114   | xml_blank+ -> level2_token lexbuf
115   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
116   | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
117   | ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
118   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
119   | keyword -> return lexbuf (keyword lexbuf)
120   | tex_token -> return lexbuf (expand_macro lexbuf)
121   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
122   | eof -> return lexbuf ("EOI", "")
123   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
124
125 (* API implementation *)
126
127 let syntax_pattern_lexer = mk_lexer level1_token
128 let ast_pattern_lexer = mk_lexer level2_token
129