]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationLexer.ml
- synced notation pretty printing with parsing syntax
[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   (* ZACK: breaks unicode's binder followed by an ascii letter without blank *)
33 (* let regexp ident_letter = xml_letter *)
34
35 let regexp ident_letter = [ 'a' - 'z' 'A' - 'Z' ]
36
37 let regexp ident_decoration = '\'' | '!' | '?' | '`'
38 let regexp ident_cont = ident_letter | xml_digit | '_'
39 let regexp ident = ident_letter ident_cont* ident_decoration*
40
41 let regexp tex_token = '\\' ident
42
43 let regexp delim_begin = "\\["
44 let regexp delim_end = "\\]"
45
46 let regexp qkeyword = "'" ident "'"
47
48 let regexp implicit = '?'
49 let regexp placeholder = '%'
50 let regexp meta = implicit number
51
52 let regexp csymbol = '\'' ident
53
54 let regexp begin_group = "@{" | "${"
55 let regexp end_group = '}'
56 let regexp wildcard = "$_"
57 let regexp ast_ident = "@" ident
58 let regexp ast_csymbol = "@" csymbol
59 let regexp meta_ident = "$" ident
60 let regexp meta_anonymous = "$_"
61 let regexp qstring = '"' [^ '"']* '"'
62
63 let regexp begincomment = "(**" xml_blank
64 let regexp endcomment = "*)"
65 let regexp comment_char = [^'*'] | '*'[^')']
66 let regexp note = "(*" ([^'*'] | "**") comment_char* "*)"
67
68 let level1_layouts = 
69   [ "sub"; "sup";
70     "below"; "above";
71     "over"; "atop"; "frac";
72     "sqrt"; "root"
73   ]
74
75 let level1_keywords =
76   [ "hbox"; "hvbox"; "hovbox"; "vbox";
77     "break";
78     "list0"; "list1"; "sep";
79     "opt";
80     "term"; "ident"; "number"
81   ] @ level1_layouts
82
83 let level2_meta_keywords =
84   [ "if"; "then"; "else";
85     "fold"; "left"; "right"; "rec";
86     "fail";
87     "default";
88     "anonymous"; "ident"; "number"; "term"; "fresh"
89   ]
90
91   (* (string, unit) Hashtbl.t, to exploit multiple bindings *)
92 let level2_ast_keywords = Hashtbl.create 23
93 let _ =
94   (* TODO ZACK: keyword list almost cut and paste from cicTextualLexer2.ml, to
95    * be reviewed *)
96   List.iter (fun k -> Hashtbl.add level2_ast_keywords k ())
97   [ "CProp"; "Prop"; "Type"; "Set"; "let"; "rec"; "corec"; "using"; "match";
98     "with"; "in"; "and"; "to"; "as"; "on"; "names" ]
99
100 let add_level2_ast_keyword k = Hashtbl.add level2_ast_keywords k ()
101 let remove_level2_ast_keyword k = Hashtbl.remove level2_ast_keywords k
102
103 let regexp uri =
104   ("cic:/" | "theory:/")              (* schema *)
105   ident ('/' ident)*                  (* path *)
106   ('.' ident)+                        (* ext *)
107   ("#xpointer(" number ('/' number)+ ")")?  (* xpointer *)
108
109 let error lexbuf msg =
110   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
111   raise (Error (begin_cnum, end_cnum, msg))
112 let error_at_end lexbuf msg =
113   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
114   raise (Error (begin_cnum, end_cnum, msg))
115
116 let return_with_loc token begin_cnum end_cnum =
117   (* TODO handle line/column numbers *)
118   let flocation_begin =
119     { Lexing.pos_fname = "";
120       Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
121       Lexing.pos_cnum = begin_cnum }
122   in
123   let flocation_end = { flocation_begin with Lexing.pos_cnum = end_cnum } in
124   (token, (flocation_begin, flocation_end))
125
126 let return lexbuf token =
127   let begin_cnum, end_cnum = Ulexing.loc lexbuf in
128     return_with_loc token begin_cnum end_cnum
129
130 let return_lexeme lexbuf name = return lexbuf (name, Ulexing.utf8_lexeme lexbuf)
131
132 let remove_quotes s = String.sub s 1 (String.length s - 2)
133
134 let mk_lexer token =
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 p ->
144             error_at_end lexbuf (sprintf "Invalid code point: %d" p))
145   in
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
155 let expand_macro lexbuf =
156   let macro =
157     Ulexing.utf8_sub_lexeme lexbuf 1 (Ulexing.lexeme_length lexbuf - 1)
158   in
159   try
160     ("SYMBOL", Utf8Macro.expand macro)
161   with Utf8Macro.Macro_not_found _ -> "SYMBOL", Ulexing.utf8_lexeme lexbuf
162
163 let remove_quotes s = String.sub s 1 (String.length s - 2)
164 let remove_left_quote s = String.sub s 1 (String.length s - 1)
165
166 let rec level2_pattern_token_group counter buffer = lexer
167   | end_group -> 
168       if (counter > 0) then
169         Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
170       snd (Ulexing.loc lexbuf)
171   | begin_group -> 
172       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
173       ignore (level2_pattern_token_group (counter + 1) buffer lexbuf) ;
174       level2_pattern_token_group counter buffer lexbuf
175   | _ -> 
176       Buffer.add_string buffer (Ulexing.utf8_lexeme lexbuf) ;
177       level2_pattern_token_group counter buffer lexbuf
178
179 let read_unparsed_group token_name lexbuf =
180   let buffer = Buffer.create 16 in
181   let begin_cnum, _ = Ulexing.loc lexbuf in
182   let end_cnum = level2_pattern_token_group 0 buffer lexbuf in
183     return_with_loc (token_name, Buffer.contents buffer) begin_cnum end_cnum
184
185 let rec level2_meta_token = lexer
186   | xml_blank+ -> level2_meta_token lexbuf
187   | ident ->
188       let s = Ulexing.utf8_lexeme lexbuf in
189         begin
190           if List.mem s level2_meta_keywords then
191             return lexbuf ("", s)
192           else
193             return lexbuf ("IDENT", s)
194         end
195   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
196   | ast_ident ->
197       return lexbuf ("UNPARSED_AST",
198         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
199   | ast_csymbol ->
200       return lexbuf ("UNPARSED_AST",
201         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
202   | eof -> return lexbuf ("EOI", "")
203
204 let rec level2_ast_token = lexer
205   | xml_blank+ -> level2_ast_token lexbuf
206   | meta -> return lexbuf ("META", Ulexing.utf8_lexeme lexbuf)
207   | implicit -> return lexbuf ("IMPLICIT", "")
208   | placeholder -> return lexbuf ("PLACEHOLDER", "")
209   | ident ->
210       let lexeme = Ulexing.utf8_lexeme lexbuf in
211       if Hashtbl.mem level2_ast_keywords lexeme then
212         return lexbuf ("", lexeme)
213       else
214         return lexbuf ("IDENT", lexeme)
215   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
216   | tex_token -> return lexbuf (expand_macro lexbuf)
217   | uri -> return lexbuf ("URI", Ulexing.utf8_lexeme lexbuf)
218   | qstring ->
219       return lexbuf ("QSTRING", remove_quotes (Ulexing.utf8_lexeme lexbuf))
220   | csymbol ->
221       return lexbuf ("CSYMBOL", remove_left_quote (Ulexing.utf8_lexeme lexbuf))
222   | "${" -> read_unparsed_group "UNPARSED_META" lexbuf
223   | "@{" -> read_unparsed_group "UNPARSED_AST" lexbuf
224   | '(' -> return lexbuf ("LPAREN", "")
225   | ')' -> return lexbuf ("RPAREN", "")
226   | meta_ident ->
227       return lexbuf ("UNPARSED_META",
228         remove_left_quote (Ulexing.utf8_lexeme lexbuf))
229   | meta_anonymous -> return lexbuf ("UNPARSED_META", "anonymous")
230   | note -> 
231       let comment =
232         Ulexing.utf8_sub_lexeme lexbuf 2 (Ulexing.lexeme_length lexbuf - 4)
233       in
234       return lexbuf ("NOTE", comment)
235   | begincomment -> return lexbuf ("BEGINCOMMENT","")
236   | endcomment -> return lexbuf ("ENDCOMMENT","")
237   | eof -> return lexbuf ("EOI", "")
238   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
239
240 let rec level1_pattern_token = lexer
241   | xml_blank+ -> level1_pattern_token lexbuf
242   | number -> return lexbuf ("NUMBER", Ulexing.utf8_lexeme lexbuf)
243   | ident ->
244       let s = Ulexing.utf8_lexeme lexbuf in
245         begin
246           if List.mem s level1_keywords then
247             return lexbuf ("", s)
248           else
249             return lexbuf ("IDENT", s)
250         end
251   | tex_token -> return lexbuf (expand_macro lexbuf)
252   | qkeyword ->
253       return lexbuf ("QKEYWORD", remove_quotes (Ulexing.utf8_lexeme lexbuf))
254   | '(' -> return lexbuf ("LPAREN", "")
255   | ')' -> return lexbuf ("RPAREN", "")
256   | eof -> return lexbuf ("EOI", "")
257   | _ -> return lexbuf ("SYMBOL", Ulexing.utf8_lexeme lexbuf)
258
259 (* API implementation *)
260
261 let level1_pattern_lexer = mk_lexer level1_pattern_token
262 let level2_ast_lexer = mk_lexer level2_ast_token
263 let level2_meta_lexer = mk_lexer level2_meta_token
264