blob: 17ac818ff3d95b1cd34f3029364d0f2dd4b11e42 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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
Mark Sleee9ce01c2007-05-16 02:29:53 +00009 *
David Reissea2cba82009-03-30 21:35:00 +000010 * 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.
Mark Sleee9ce01c2007-05-16 02:29:53 +000018 */
19
20/**
Mark Slee31985722006-05-24 21:45:31 +000021 * Thrift scanner.
Mark Slee27ed6ec2007-08-16 01:26:31 +000022 *
Mark Slee31985722006-05-24 21:45:31 +000023 * Tokenizes a thrift definition file.
Mark Slee31985722006-05-24 21:45:31 +000024 */
Mark Sleef5377b32006-10-10 01:42:59 +000025
Mark Slee31985722006-05-24 21:45:31 +000026%{
27
David Reiss82e6fc02009-03-26 23:32:36 +000028#include <string>
David Reissf1454162008-06-30 20:45:47 +000029#include <errno.h>
30
Mark Slee31985722006-05-24 21:45:31 +000031#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000032#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000033#include "parse/t_program.h"
34
Mark Sleef5377b32006-10-10 01:42:59 +000035/**
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 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000039#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000040
Mark Sleef12865a2007-01-12 00:23:26 +000041void thrift_reserved_keyword(char* keyword) {
42 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
43 exit(1);
44}
45
David Reissf1454162008-06-30 20:45:47 +000046void integer_overflow(char* text) {
47 yyerror("This integer is too big: \"%s\"\n", text);
48 exit(1);
49}
50
Mark Slee31985722006-05-24 21:45:31 +000051%}
52
Mark Sleef5377b32006-10-10 01:42:59 +000053/**
54 * Provides the yylineno global, useful for debugging output
55 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000056%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000057
Mark Slee27ed6ec2007-08-16 01:26:31 +000058/**
David Reiss4563acd2010-08-31 16:51:29 +000059 * Our inputs are all single files, so no need for yywrap
60 */
61%option noyywrap
62
63/**
Christian Lavoie77215d82010-11-07 19:42:48 +000064 * We don't use it, and it fires up warnings at -Wall
65 */
66%option nounput
67
68/**
Mark Sleef5377b32006-10-10 01:42:59 +000069 * Helper definitions, comments, constants, and whatnot
70 */
71
Mark Sleebd588222007-11-21 08:43:35 +000072intconstant ([+-]?[0-9]+)
73hexconstant ("0x"[0-9A-Fa-f]+)
74dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
75identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
76whitespace ([ \t\r\n]*)
77sillycomm ("/*""*"*"*/")
78multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
79doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
80comment ("//"[^\n]*)
81unixcomment ("#"[^\n]*)
82symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Sleebd588222007-11-21 08:43:35 +000083st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
David Reiss82e6fc02009-03-26 23:32:36 +000084literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +000085
86%%
87
Mark Sleebd588222007-11-21 08:43:35 +000088{whitespace} { /* do nothing */ }
89{sillycomm} { /* do nothing */ }
90{multicomm} { /* do nothing */ }
91{comment} { /* do nothing */ }
92{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000093
Mark Sleebd588222007-11-21 08:43:35 +000094{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +000095
Mark Sleebd588222007-11-21 08:43:35 +000096"namespace" { return tok_namespace; }
97"cpp_namespace" { return tok_cpp_namespace; }
98"cpp_include" { return tok_cpp_include; }
99"cpp_type" { return tok_cpp_type; }
100"java_package" { return tok_java_package; }
101"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000102"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000103"php_namespace" { return tok_php_namespace; }
104"py_module" { return tok_py_module; }
105"perl_package" { return tok_perl_package; }
106"ruby_namespace" { return tok_ruby_namespace; }
107"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000108"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000109"xsd_all" { return tok_xsd_all; }
110"xsd_optional" { return tok_xsd_optional; }
111"xsd_nillable" { return tok_xsd_nillable; }
112"xsd_namespace" { return tok_xsd_namespace; }
113"xsd_attrs" { return tok_xsd_attrs; }
114"include" { return tok_include; }
115"void" { return tok_void; }
116"bool" { return tok_bool; }
117"byte" { return tok_byte; }
118"i16" { return tok_i16; }
119"i32" { return tok_i32; }
120"i64" { return tok_i64; }
121"double" { return tok_double; }
122"string" { return tok_string; }
123"binary" { return tok_binary; }
124"slist" { return tok_slist; }
125"senum" { return tok_senum; }
126"map" { return tok_map; }
127"list" { return tok_list; }
128"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000129"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000130"typedef" { return tok_typedef; }
131"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000132"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000133"exception" { return tok_xception; }
134"extends" { return tok_extends; }
135"throws" { return tok_throws; }
136"service" { return tok_service; }
137"enum" { return tok_enum; }
138"const" { return tok_const; }
139"required" { return tok_required; }
140"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000141"async" {
142 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
143 return tok_oneway;
144}
Mark Sleef0712dc2006-10-25 19:03:57 +0000145
Mark Slee52f643d2006-08-09 00:03:43 +0000146
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000147"BEGIN" { thrift_reserved_keyword(yytext); }
148"END" { thrift_reserved_keyword(yytext); }
149"__CLASS__" { thrift_reserved_keyword(yytext); }
150"__DIR__" { thrift_reserved_keyword(yytext); }
151"__FILE__" { thrift_reserved_keyword(yytext); }
152"__FUNCTION__" { thrift_reserved_keyword(yytext); }
153"__LINE__" { thrift_reserved_keyword(yytext); }
154"__METHOD__" { thrift_reserved_keyword(yytext); }
155"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000156"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000157"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000158"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000159"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000160"as" { thrift_reserved_keyword(yytext); }
161"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000162"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000163"break" { thrift_reserved_keyword(yytext); }
164"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000165"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000166"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000167"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000168"continue" { thrift_reserved_keyword(yytext); }
169"declare" { thrift_reserved_keyword(yytext); }
170"def" { thrift_reserved_keyword(yytext); }
171"default" { thrift_reserved_keyword(yytext); }
172"del" { thrift_reserved_keyword(yytext); }
173"delete" { thrift_reserved_keyword(yytext); }
174"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000175"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000176"elif" { thrift_reserved_keyword(yytext); }
177"else" { thrift_reserved_keyword(yytext); }
178"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000179"elsif" { thrift_reserved_keyword(yytext); }
180"end" { thrift_reserved_keyword(yytext); }
181"enddeclare" { thrift_reserved_keyword(yytext); }
182"endfor" { thrift_reserved_keyword(yytext); }
183"endforeach" { thrift_reserved_keyword(yytext); }
184"endif" { thrift_reserved_keyword(yytext); }
185"endswitch" { thrift_reserved_keyword(yytext); }
186"endwhile" { thrift_reserved_keyword(yytext); }
187"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000188"except" { thrift_reserved_keyword(yytext); }
189"exec" { thrift_reserved_keyword(yytext); }
190"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000191"finally" { thrift_reserved_keyword(yytext); }
192"float" { thrift_reserved_keyword(yytext); }
193"for" { thrift_reserved_keyword(yytext); }
194"foreach" { thrift_reserved_keyword(yytext); }
195"function" { thrift_reserved_keyword(yytext); }
196"global" { thrift_reserved_keyword(yytext); }
197"goto" { thrift_reserved_keyword(yytext); }
198"if" { thrift_reserved_keyword(yytext); }
199"implements" { thrift_reserved_keyword(yytext); }
200"import" { thrift_reserved_keyword(yytext); }
201"in" { thrift_reserved_keyword(yytext); }
202"inline" { thrift_reserved_keyword(yytext); }
203"instanceof" { thrift_reserved_keyword(yytext); }
204"interface" { thrift_reserved_keyword(yytext); }
205"is" { thrift_reserved_keyword(yytext); }
206"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000207"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000208"native" { thrift_reserved_keyword(yytext); }
209"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000210"next" { thrift_reserved_keyword(yytext); }
211"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000212"not" { thrift_reserved_keyword(yytext); }
213"or" { thrift_reserved_keyword(yytext); }
214"pass" { thrift_reserved_keyword(yytext); }
215"public" { thrift_reserved_keyword(yytext); }
216"print" { thrift_reserved_keyword(yytext); }
217"private" { thrift_reserved_keyword(yytext); }
218"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000219"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000220"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000221"redo" { thrift_reserved_keyword(yytext); }
222"rescue" { thrift_reserved_keyword(yytext); }
223"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000224"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000225"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000226"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000227"sizeof" { thrift_reserved_keyword(yytext); }
228"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000229"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000230"switch" { thrift_reserved_keyword(yytext); }
231"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000232"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000233"this" { thrift_reserved_keyword(yytext); }
234"throw" { thrift_reserved_keyword(yytext); }
235"transient" { thrift_reserved_keyword(yytext); }
236"true" { thrift_reserved_keyword(yytext); }
237"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000238"undef" { thrift_reserved_keyword(yytext); }
239"union" { thrift_reserved_keyword(yytext); }
240"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000241"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000242"until" { thrift_reserved_keyword(yytext); }
243"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000244"var" { thrift_reserved_keyword(yytext); }
245"virtual" { thrift_reserved_keyword(yytext); }
246"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000247"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000248"while" { thrift_reserved_keyword(yytext); }
249"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000250"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000251"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000252
Mark Slee4f8da1d2006-10-12 02:47:27 +0000253{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000254 errno = 0;
255 yylval.iconst = strtoll(yytext, NULL, 10);
256 if (errno == ERANGE) {
257 integer_overflow(yytext);
258 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000259 return tok_int_constant;
260}
Mark Sleef5377b32006-10-10 01:42:59 +0000261
Mark Slee600cdb32006-11-29 22:06:42 +0000262{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000263 errno = 0;
264 yylval.iconst = strtoll(yytext+2, NULL, 16);
265 if (errno == ERANGE) {
266 integer_overflow(yytext);
267 }
Mark Slee600cdb32006-11-29 22:06:42 +0000268 return tok_int_constant;
269}
270
Mark Slee30152872006-11-28 01:24:07 +0000271{dubconstant} {
272 yylval.dconst = atof(yytext);
273 return tok_dub_constant;
274}
275
Mark Slee4f8da1d2006-10-12 02:47:27 +0000276{identifier} {
277 yylval.id = strdup(yytext);
278 return tok_identifier;
279}
280
Mark Sleebd588222007-11-21 08:43:35 +0000281{st_identifier} {
282 yylval.id = strdup(yytext);
283 return tok_st_identifier;
284}
285
David Reiss82e6fc02009-03-26 23:32:36 +0000286{literal_begin} {
287 char mark = yytext[0];
288 std::string result;
289 for(;;)
290 {
291 int ch = yyinput();
292 switch (ch) {
293 case EOF:
294 yyerror("End of file while read string at %d\n", yylineno);
295 exit(1);
296 case '\n':
297 yyerror("End of line while read string at %d\n", yylineno - 1);
298 exit(1);
299 case '\\':
300 ch = yyinput();
301 switch (ch) {
302 case 'r':
303 result.push_back('\r');
304 continue;
305 case 'n':
306 result.push_back('\n');
307 continue;
308 case 't':
309 result.push_back('\t');
310 continue;
311 case '"':
312 result.push_back('"');
313 continue;
314 case '\'':
315 result.push_back('\'');
316 continue;
317 case '\\':
318 result.push_back('\\');
319 continue;
320 default:
321 yyerror("Bad escape character\n");
322 return -1;
323 }
324 break;
325 default:
326 if (ch == mark) {
327 yylval.id = strdup(result.c_str());
328 return tok_literal;
329 } else {
330 result.push_back(ch);
331 }
332 }
333 }
Mark Slee30152872006-11-28 01:24:07 +0000334}
335
Mark Slee31985722006-05-24 21:45:31 +0000336
ccheeverf53b5cf2007-02-05 20:33:11 +0000337{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000338 /* This does not show up in the parse tree. */
339 /* Rather, the parser will grab it out of the global. */
340 if (g_parse_mode == PROGRAM) {
341 clear_doctext();
342 g_doctext = strdup(yytext + 3);
343 g_doctext[strlen(g_doctext) - 2] = '\0';
344 g_doctext = clean_up_doctext(g_doctext);
345 g_doctext_lineno = yylineno;
346 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000347}
348
349
David Reissfb790d72010-09-02 16:41:45 +0000350. {
351 /* Catch-all to let us catch "*" in the parser. */
352 return (int) yytext[0];
353}
354
Mark Slee31985722006-05-24 21:45:31 +0000355%%
David Reiss4a054342009-03-26 23:32:27 +0000356
357/* vim: filetype=lex
358*/