]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationLexer.ml
snapshot
[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 delim_begin = "\\["
39 let regexp delim_end = "\\]"
40
41 let regexp keyword = '"' ident '"'
42 let regexp qkeyword = "'" ident "'"
43
44 let regexp implicit = '?'
45 let regexp meta = implicit number
46
47 let regexp csymbol = '\'' ident
48
49 let regexp begin_group = "@{" | "${"
50 let regexp end_group = '}'
51 let regexp wildcard = "$_"
52 let regexp ast_ident = "@" ident
53 let regexp ast_csymbol = "@" csymbol
54 let regexp meta_ident = "$" ident
55 let regexp meta_anonymous = "$_"
56 let regexp qstring = '"' [^ '"']* '"'
57
58 let level1_layouts = 
59   [ "sub"; "sup";
60     "below"; "above";
61     "over"; "atop"; "frac";
62     "sqrt"; "root"
63   ]
64
65 let level2_meta_keywords =
66   [ "if"; "then"; "else";
67     "fold"; "left"; "right"; "rec";
68     "fail";
69     "default";
70     "anonymous"; "ident"; "number"; "term"; "fresh"
71   ]
72
73 let level1_keywords =
74   [ "hbox"; "hvbox"; "hovbox"; "vbox";
75     "break";
76     "list0"; "list1"; "sep";
77     "opt";
78     "term"; "ident"; "number"
79   ]
80
81 let regexp uri =
82   ("cic:/" | "theory:/")              (* schema *)
83   ident ('/' ident)*                  (* path *)
84   ('.' ident)+                        (* ext *)
85   ("#xpointer(" number ('/' number)+ ")")?  (* xpointer *)
86
87 let error lexbuf msg =
88   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
89   raise (Error (begin_cnum, end_cnum, msg))
90 let error_at_end lexbuf msg =
91   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
92   raise (Error (begin_cnum, end_cnum, msg))
93
94 let return_with_loc token begin_cnum end_cnum =
95   (* TODO handle line/column numbers *)
96   let flocation_begin =
97     { Lexing.pos_fname = "";
98       Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
99       Lexing.pos_cnum = begin_cnum }
100   in
101   let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
102   (token, (flocation_begin, flocation_end))
103
104 let return lexbuf token =
105   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
106     return_with_loc token begin_cnum end_cnum
107
108 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
109
110 let remove_quotes s = String.sub s 1 (String.length s - 2)
111
112 let mk_lexer token =
113   let tok_func stream =
114     let lexbuf = Ulexing.from_utf8_stream stream in
115     Token.make_stream_and_flocation
116       (fun () ->
117         try
118           token lexbuf
119         with
120         | Ulexing.Error -> error_at_end lexbuf "Unexpected character"
121         | Ulexing.InvalidCodepoint p ->
122             error_at_end lexbuf (sprintf "Invalid code point: %d" p))
123   in
124   {
125     Token.tok_func = tok_func;
126     Token.tok_using = (fun _ -> ());
127     Token.tok_removing = (fun _ -> ()); 
128     Token.tok_match = Token.default_match;
129     Token.tok_text = Token.lexer_text;
130     Token.tok_comm = None;
131   }
132
133 let expand_macro lexbuf =
134   let macro =
135     Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
136   in
137   try
138     ("SYMBOL", Utf8Macro.expand macro)
139   with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
140
141 let keyword lexbuf = "KEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf)
142
143 let remove_quotes s = String.sub s 1 (String.length s - 2)
144 let remove_left_quote s = String.sub s 1 (String.length s - 1)
145
146 let rec level2_pattern_token_group counter buffer = lexer
147   | end_group -> 
148       if (counter > 0) then
149         Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
150       snd (Ulexing.loc lexbuf)
151   | begin_group -> 
152       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
153       ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
154       level2_pattern_token_group counter buffer lexbuf
155   | _ -> 
156       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
157       level2_pattern_token_group counter buffer lexbuf
158
159 let read_unparsed_group token_name lexbuf =
160   let buffer = Buffer.create 16 in
161   let begin_cnum, _ = Ulexing.loc lexbuf in
162   let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
163     return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
164
165 let rec level2_meta_token = lexer
166   | xml_blank+ -> level2_meta_token lexbuf
167   | ident ->
168       let s = Ulexing.utf8_lexeme lexbuf in
169         begin
170           if List.mem s level2_meta_keywords then
171             return lexbuf ("", s)
172           else
173             return lexbuf ("IDENT", s)
174         end
175   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
176   | ast_ident -> return lexbuf ("UNPARSED_AST", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
177   | ast_csymbol ->  return lexbuf ("UNPARSED_AST", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
178   | eof -> return lexbuf ("EOI", "")
179
180 let rec level2_ast_token = lexer
181   | xml_blank+ -> level2_ast_token lexbuf
182   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
183   | implicit -> return lexbuf ("IMPLICIT", Ulexing.utf8_lexeme lexbuf)
184   | ident -> return lexbuf ("IDENT", Ulexing.utf8_lexeme lexbuf)
185   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
186   | keyword -> return lexbuf (keyword lexbuf)
187   | tex_token -> return lexbuf (expand_macro lexbuf)
188   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
189   | qstring -> return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
190   | csymbol -> return lexbuf ("CSYMBOL", Ulexing.utf8_lexeme lexbuf)
191   | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
192   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
193   | '(' -> return lexbuf ("LPAREN", "")
194   | ')' -> return lexbuf ("RPAREN", "")
195   | meta_ident -> return lexbuf ("UNPARSED_META", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
196   | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
197   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
198   | eof -> return lexbuf ("EOI", "")
199
200 let rec level1_pattern_token = lexer
201   | xml_blank+ -> level1_pattern_token lexbuf
202   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
203   | ident ->
204       let s = Ulexing.utf8_lexeme lexbuf in
205         begin
206           if List.mem s level1_keywords then
207             return lexbuf ("", s)
208           else
209             return lexbuf ("IDENT", s)
210         end
211   | tex_token -> return lexbuf (expand_macro lexbuf)
212   | qkeyword -> return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
213   | '(' -> return lexbuf ("LPAREN", "")
214   | ')' -> return lexbuf ("RPAREN", "")
215   | eof -> return lexbuf ("EOI", "")
216   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
217
218 (* API implementation *)
219
220 let level1_pattern_lexer = mk_lexer level1_pattern_token
221 let level2_ast_lexer = mk_lexer level2_ast_token
222 let level2_meta_lexer = mk_lexer level2_meta_token
223