| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Thrift scanner.
|
|
|
22 |
*
|
|
|
23 |
* Tokenizes a thrift definition file.
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
%{
|
|
|
27 |
|
|
|
28 |
#include <string>
|
|
|
29 |
#include <errno.h>
|
|
|
30 |
|
|
|
31 |
#include "main.h"
|
|
|
32 |
#include "globals.h"
|
|
|
33 |
#include "parse/t_program.h"
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Must be included AFTER parse/t_program.h, but I can't remember why anymore
|
|
|
37 |
* because I wrote this a while ago.
|
|
|
38 |
*/
|
|
|
39 |
#include "thrifty.h"
|
|
|
40 |
|
|
|
41 |
void thrift_reserved_keyword(char* keyword) {
|
|
|
42 |
yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
|
|
|
43 |
exit(1);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
void integer_overflow(char* text) {
|
|
|
47 |
yyerror("This integer is too big: \"%s\"\n", text);
|
|
|
48 |
exit(1);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
%}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Provides the yylineno global, useful for debugging output
|
|
|
55 |
*/
|
|
|
56 |
%option lex-compat
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Helper definitions, comments, constants, and whatnot
|
|
|
60 |
*/
|
|
|
61 |
|
|
|
62 |
intconstant ([+-]?[0-9]+)
|
|
|
63 |
hexconstant ("0x"[0-9A-Fa-f]+)
|
|
|
64 |
dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
|
|
|
65 |
identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
|
|
|
66 |
whitespace ([ \t\r\n]*)
|
|
|
67 |
sillycomm ("/*""*"*"*/")
|
|
|
68 |
multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
|
|
|
69 |
doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
|
|
|
70 |
comment ("//"[^\n]*)
|
|
|
71 |
unixcomment ("#"[^\n]*)
|
|
|
72 |
symbol ([:;\,\{\}\(\)\=<>\[\]])
|
|
|
73 |
st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
|
|
|
74 |
literal_begin (['\"])
|
|
|
75 |
|
|
|
76 |
%%
|
|
|
77 |
|
|
|
78 |
{whitespace} { /* do nothing */ }
|
|
|
79 |
{sillycomm} { /* do nothing */ }
|
|
|
80 |
{multicomm} { /* do nothing */ }
|
|
|
81 |
{comment} { /* do nothing */ }
|
|
|
82 |
{unixcomment} { /* do nothing */ }
|
|
|
83 |
|
|
|
84 |
{symbol} { return yytext[0]; }
|
|
|
85 |
|
|
|
86 |
"namespace" { return tok_namespace; }
|
|
|
87 |
"cpp_namespace" { return tok_cpp_namespace; }
|
|
|
88 |
"cpp_include" { return tok_cpp_include; }
|
|
|
89 |
"cpp_type" { return tok_cpp_type; }
|
|
|
90 |
"java_package" { return tok_java_package; }
|
|
|
91 |
"cocoa_prefix" { return tok_cocoa_prefix; }
|
|
|
92 |
"csharp_namespace" { return tok_csharp_namespace; }
|
|
|
93 |
"php_namespace" { return tok_php_namespace; }
|
|
|
94 |
"py_module" { return tok_py_module; }
|
|
|
95 |
"perl_package" { return tok_perl_package; }
|
|
|
96 |
"ruby_namespace" { return tok_ruby_namespace; }
|
|
|
97 |
"smalltalk_category" { return tok_smalltalk_category; }
|
|
|
98 |
"smalltalk_prefix" { return tok_smalltalk_prefix; }
|
|
|
99 |
"xsd_all" { return tok_xsd_all; }
|
|
|
100 |
"xsd_optional" { return tok_xsd_optional; }
|
|
|
101 |
"xsd_nillable" { return tok_xsd_nillable; }
|
|
|
102 |
"xsd_namespace" { return tok_xsd_namespace; }
|
|
|
103 |
"xsd_attrs" { return tok_xsd_attrs; }
|
|
|
104 |
"include" { return tok_include; }
|
|
|
105 |
"void" { return tok_void; }
|
|
|
106 |
"bool" { return tok_bool; }
|
|
|
107 |
"byte" { return tok_byte; }
|
|
|
108 |
"i16" { return tok_i16; }
|
|
|
109 |
"i32" { return tok_i32; }
|
|
|
110 |
"i64" { return tok_i64; }
|
|
|
111 |
"double" { return tok_double; }
|
|
|
112 |
"string" { return tok_string; }
|
|
|
113 |
"binary" { return tok_binary; }
|
|
|
114 |
"slist" { return tok_slist; }
|
|
|
115 |
"senum" { return tok_senum; }
|
|
|
116 |
"map" { return tok_map; }
|
|
|
117 |
"list" { return tok_list; }
|
|
|
118 |
"set" { return tok_set; }
|
|
|
119 |
"oneway" { return tok_oneway; }
|
|
|
120 |
"typedef" { return tok_typedef; }
|
|
|
121 |
"struct" { return tok_struct; }
|
|
|
122 |
"union" { return tok_union; }
|
|
|
123 |
"exception" { return tok_xception; }
|
|
|
124 |
"extends" { return tok_extends; }
|
|
|
125 |
"throws" { return tok_throws; }
|
|
|
126 |
"service" { return tok_service; }
|
|
|
127 |
"enum" { return tok_enum; }
|
|
|
128 |
"const" { return tok_const; }
|
|
|
129 |
"required" { return tok_required; }
|
|
|
130 |
"optional" { return tok_optional; }
|
|
|
131 |
"async" {
|
|
|
132 |
pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
|
|
|
133 |
return tok_oneway;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
"abstract" { thrift_reserved_keyword(yytext); }
|
|
|
138 |
"and" { thrift_reserved_keyword(yytext); }
|
|
|
139 |
"args" { thrift_reserved_keyword(yytext); }
|
|
|
140 |
"as" { thrift_reserved_keyword(yytext); }
|
|
|
141 |
"assert" { thrift_reserved_keyword(yytext); }
|
|
|
142 |
"break" { thrift_reserved_keyword(yytext); }
|
|
|
143 |
"case" { thrift_reserved_keyword(yytext); }
|
|
|
144 |
"class" { thrift_reserved_keyword(yytext); }
|
|
|
145 |
"continue" { thrift_reserved_keyword(yytext); }
|
|
|
146 |
"declare" { thrift_reserved_keyword(yytext); }
|
|
|
147 |
"def" { thrift_reserved_keyword(yytext); }
|
|
|
148 |
"default" { thrift_reserved_keyword(yytext); }
|
|
|
149 |
"del" { thrift_reserved_keyword(yytext); }
|
|
|
150 |
"delete" { thrift_reserved_keyword(yytext); }
|
|
|
151 |
"do" { thrift_reserved_keyword(yytext); }
|
|
|
152 |
"elif" { thrift_reserved_keyword(yytext); }
|
|
|
153 |
"else" { thrift_reserved_keyword(yytext); }
|
|
|
154 |
"elseif" { thrift_reserved_keyword(yytext); }
|
|
|
155 |
"except" { thrift_reserved_keyword(yytext); }
|
|
|
156 |
"exec" { thrift_reserved_keyword(yytext); }
|
|
|
157 |
"false" { thrift_reserved_keyword(yytext); }
|
|
|
158 |
"finally" { thrift_reserved_keyword(yytext); }
|
|
|
159 |
"float" { thrift_reserved_keyword(yytext); }
|
|
|
160 |
"for" { thrift_reserved_keyword(yytext); }
|
|
|
161 |
"foreach" { thrift_reserved_keyword(yytext); }
|
|
|
162 |
"function" { thrift_reserved_keyword(yytext); }
|
|
|
163 |
"global" { thrift_reserved_keyword(yytext); }
|
|
|
164 |
"goto" { thrift_reserved_keyword(yytext); }
|
|
|
165 |
"if" { thrift_reserved_keyword(yytext); }
|
|
|
166 |
"implements" { thrift_reserved_keyword(yytext); }
|
|
|
167 |
"import" { thrift_reserved_keyword(yytext); }
|
|
|
168 |
"in" { thrift_reserved_keyword(yytext); }
|
|
|
169 |
"inline" { thrift_reserved_keyword(yytext); }
|
|
|
170 |
"instanceof" { thrift_reserved_keyword(yytext); }
|
|
|
171 |
"interface" { thrift_reserved_keyword(yytext); }
|
|
|
172 |
"is" { thrift_reserved_keyword(yytext); }
|
|
|
173 |
"lambda" { thrift_reserved_keyword(yytext); }
|
|
|
174 |
"native" { thrift_reserved_keyword(yytext); }
|
|
|
175 |
"new" { thrift_reserved_keyword(yytext); }
|
|
|
176 |
"not" { thrift_reserved_keyword(yytext); }
|
|
|
177 |
"or" { thrift_reserved_keyword(yytext); }
|
|
|
178 |
"pass" { thrift_reserved_keyword(yytext); }
|
|
|
179 |
"public" { thrift_reserved_keyword(yytext); }
|
|
|
180 |
"print" { thrift_reserved_keyword(yytext); }
|
|
|
181 |
"private" { thrift_reserved_keyword(yytext); }
|
|
|
182 |
"protected" { thrift_reserved_keyword(yytext); }
|
|
|
183 |
"raise" { thrift_reserved_keyword(yytext); }
|
|
|
184 |
"register" { thrift_reserved_keyword(yytext); }
|
|
|
185 |
"return" { thrift_reserved_keyword(yytext); }
|
|
|
186 |
"sizeof" { thrift_reserved_keyword(yytext); }
|
|
|
187 |
"static" { thrift_reserved_keyword(yytext); }
|
|
|
188 |
"switch" { thrift_reserved_keyword(yytext); }
|
|
|
189 |
"synchronized" { thrift_reserved_keyword(yytext); }
|
|
|
190 |
"this" { thrift_reserved_keyword(yytext); }
|
|
|
191 |
"throw" { thrift_reserved_keyword(yytext); }
|
|
|
192 |
"transient" { thrift_reserved_keyword(yytext); }
|
|
|
193 |
"true" { thrift_reserved_keyword(yytext); }
|
|
|
194 |
"try" { thrift_reserved_keyword(yytext); }
|
|
|
195 |
"unsigned" { thrift_reserved_keyword(yytext); }
|
|
|
196 |
"var" { thrift_reserved_keyword(yytext); }
|
|
|
197 |
"virtual" { thrift_reserved_keyword(yytext); }
|
|
|
198 |
"volatile" { thrift_reserved_keyword(yytext); }
|
|
|
199 |
"while" { thrift_reserved_keyword(yytext); }
|
|
|
200 |
"with" { thrift_reserved_keyword(yytext); }
|
|
|
201 |
"yield" { thrift_reserved_keyword(yytext); }
|
|
|
202 |
|
|
|
203 |
{intconstant} {
|
|
|
204 |
errno = 0;
|
|
|
205 |
yylval.iconst = strtoll(yytext, NULL, 10);
|
|
|
206 |
if (errno == ERANGE) {
|
|
|
207 |
integer_overflow(yytext);
|
|
|
208 |
}
|
|
|
209 |
return tok_int_constant;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
{hexconstant} {
|
|
|
213 |
errno = 0;
|
|
|
214 |
yylval.iconst = strtoll(yytext+2, NULL, 16);
|
|
|
215 |
if (errno == ERANGE) {
|
|
|
216 |
integer_overflow(yytext);
|
|
|
217 |
}
|
|
|
218 |
return tok_int_constant;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
{dubconstant} {
|
|
|
222 |
yylval.dconst = atof(yytext);
|
|
|
223 |
return tok_dub_constant;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
{identifier} {
|
|
|
227 |
yylval.id = strdup(yytext);
|
|
|
228 |
return tok_identifier;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
{st_identifier} {
|
|
|
232 |
yylval.id = strdup(yytext);
|
|
|
233 |
return tok_st_identifier;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
{literal_begin} {
|
|
|
237 |
char mark = yytext[0];
|
|
|
238 |
std::string result;
|
|
|
239 |
for(;;)
|
|
|
240 |
{
|
|
|
241 |
int ch = yyinput();
|
|
|
242 |
switch (ch) {
|
|
|
243 |
case EOF:
|
|
|
244 |
yyerror("End of file while read string at %d\n", yylineno);
|
|
|
245 |
exit(1);
|
|
|
246 |
case '\n':
|
|
|
247 |
yyerror("End of line while read string at %d\n", yylineno - 1);
|
|
|
248 |
exit(1);
|
|
|
249 |
case '\\':
|
|
|
250 |
ch = yyinput();
|
|
|
251 |
switch (ch) {
|
|
|
252 |
case 'r':
|
|
|
253 |
result.push_back('\r');
|
|
|
254 |
continue;
|
|
|
255 |
case 'n':
|
|
|
256 |
result.push_back('\n');
|
|
|
257 |
continue;
|
|
|
258 |
case 't':
|
|
|
259 |
result.push_back('\t');
|
|
|
260 |
continue;
|
|
|
261 |
case '"':
|
|
|
262 |
result.push_back('"');
|
|
|
263 |
continue;
|
|
|
264 |
case '\'':
|
|
|
265 |
result.push_back('\'');
|
|
|
266 |
continue;
|
|
|
267 |
case '\\':
|
|
|
268 |
result.push_back('\\');
|
|
|
269 |
continue;
|
|
|
270 |
default:
|
|
|
271 |
yyerror("Bad escape character\n");
|
|
|
272 |
return -1;
|
|
|
273 |
}
|
|
|
274 |
break;
|
|
|
275 |
default:
|
|
|
276 |
if (ch == mark) {
|
|
|
277 |
yylval.id = strdup(result.c_str());
|
|
|
278 |
return tok_literal;
|
|
|
279 |
} else {
|
|
|
280 |
result.push_back(ch);
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
}
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
{doctext} {
|
|
|
288 |
/* This does not show up in the parse tree. */
|
|
|
289 |
/* Rather, the parser will grab it out of the global. */
|
|
|
290 |
if (g_parse_mode == PROGRAM) {
|
|
|
291 |
clear_doctext();
|
|
|
292 |
g_doctext = strdup(yytext + 3);
|
|
|
293 |
g_doctext[strlen(g_doctext) - 2] = '\0';
|
|
|
294 |
g_doctext = clean_up_doctext(g_doctext);
|
|
|
295 |
g_doctext_lineno = yylineno;
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
%%
|
|
|
301 |
|
|
|
302 |
/* vim: filetype=lex
|
|
|
303 |
*/
|