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