blob: c3d577e9ecf7ec5394dc846e6df2b1fef670f30d [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
Christian Lavoieaf65f1b2010-11-24 21:58:05 +000028/* This is redundant with some of the flags in Makefile.am, but it works
29 * when people override CXXFLAGS without being careful. The pragmas are
30 * the 'right' way to do it, but don't work on old-enough GCC (in particular
31 * the GCC that ship on Mac OS X 10.6.5, *counter* to what the GNU docs say)
32 *
33 * We should revert the Makefile.am changes once Apple ships a reasonable
34 * GCC.
35 */
Roger Meier3b771a12010-11-17 22:11:26 +000036#pragma GCC diagnostic ignored "-Wunused-function"
37#pragma GCC diagnostic ignored "-Wunused-label"
38
David Reiss82e6fc02009-03-26 23:32:36 +000039#include <string>
David Reissf1454162008-06-30 20:45:47 +000040#include <errno.h>
41
Mark Slee31985722006-05-24 21:45:31 +000042#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000043#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000044#include "parse/t_program.h"
45
Mark Sleef5377b32006-10-10 01:42:59 +000046/**
47 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
48 * because I wrote this a while ago.
49 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000050#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000051
Mark Sleef12865a2007-01-12 00:23:26 +000052void thrift_reserved_keyword(char* keyword) {
53 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
54 exit(1);
55}
56
David Reissf1454162008-06-30 20:45:47 +000057void integer_overflow(char* text) {
58 yyerror("This integer is too big: \"%s\"\n", text);
59 exit(1);
60}
61
Bryan Duxbury235f8b52011-08-19 18:27:47 +000062void unexpected_token(char* text) {
63 yyerror("Unexpected token in input: \"%s\"\n", text);
64 exit(1);
65}
66
Mark Slee31985722006-05-24 21:45:31 +000067%}
68
Mark Sleef5377b32006-10-10 01:42:59 +000069/**
70 * Provides the yylineno global, useful for debugging output
71 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000072%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000073
Mark Slee27ed6ec2007-08-16 01:26:31 +000074/**
David Reiss4563acd2010-08-31 16:51:29 +000075 * Our inputs are all single files, so no need for yywrap
76 */
77%option noyywrap
78
79/**
Christian Lavoie77215d82010-11-07 19:42:48 +000080 * We don't use it, and it fires up warnings at -Wall
81 */
82%option nounput
83
84/**
Mark Sleef5377b32006-10-10 01:42:59 +000085 * Helper definitions, comments, constants, and whatnot
86 */
87
Mark Sleebd588222007-11-21 08:43:35 +000088intconstant ([+-]?[0-9]+)
89hexconstant ("0x"[0-9A-Fa-f]+)
90dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
91identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
92whitespace ([ \t\r\n]*)
93sillycomm ("/*""*"*"*/")
94multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
95doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
96comment ("//"[^\n]*)
97unixcomment ("#"[^\n]*)
98symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Sleebd588222007-11-21 08:43:35 +000099st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
David Reiss82e6fc02009-03-26 23:32:36 +0000100literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +0000101
102%%
103
Mark Sleebd588222007-11-21 08:43:35 +0000104{whitespace} { /* do nothing */ }
105{sillycomm} { /* do nothing */ }
106{multicomm} { /* do nothing */ }
107{comment} { /* do nothing */ }
108{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +0000109
Mark Sleebd588222007-11-21 08:43:35 +0000110{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000111
Mark Sleebd588222007-11-21 08:43:35 +0000112"namespace" { return tok_namespace; }
113"cpp_namespace" { return tok_cpp_namespace; }
114"cpp_include" { return tok_cpp_include; }
115"cpp_type" { return tok_cpp_type; }
116"java_package" { return tok_java_package; }
117"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000118"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000119"php_namespace" { return tok_php_namespace; }
120"py_module" { return tok_py_module; }
121"perl_package" { return tok_perl_package; }
122"ruby_namespace" { return tok_ruby_namespace; }
123"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000124"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000125"xsd_all" { return tok_xsd_all; }
126"xsd_optional" { return tok_xsd_optional; }
127"xsd_nillable" { return tok_xsd_nillable; }
128"xsd_namespace" { return tok_xsd_namespace; }
129"xsd_attrs" { return tok_xsd_attrs; }
130"include" { return tok_include; }
131"void" { return tok_void; }
132"bool" { return tok_bool; }
133"byte" { return tok_byte; }
134"i16" { return tok_i16; }
135"i32" { return tok_i32; }
136"i64" { return tok_i64; }
137"double" { return tok_double; }
138"string" { return tok_string; }
139"binary" { return tok_binary; }
140"slist" { return tok_slist; }
141"senum" { return tok_senum; }
142"map" { return tok_map; }
143"list" { return tok_list; }
144"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000145"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000146"typedef" { return tok_typedef; }
147"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000148"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000149"exception" { return tok_xception; }
150"extends" { return tok_extends; }
151"throws" { return tok_throws; }
152"service" { return tok_service; }
153"enum" { return tok_enum; }
154"const" { return tok_const; }
155"required" { return tok_required; }
156"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000157"async" {
158 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
159 return tok_oneway;
160}
Mark Sleef0712dc2006-10-25 19:03:57 +0000161
Mark Slee52f643d2006-08-09 00:03:43 +0000162
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000163"BEGIN" { thrift_reserved_keyword(yytext); }
164"END" { thrift_reserved_keyword(yytext); }
165"__CLASS__" { thrift_reserved_keyword(yytext); }
166"__DIR__" { thrift_reserved_keyword(yytext); }
167"__FILE__" { thrift_reserved_keyword(yytext); }
168"__FUNCTION__" { thrift_reserved_keyword(yytext); }
169"__LINE__" { thrift_reserved_keyword(yytext); }
170"__METHOD__" { thrift_reserved_keyword(yytext); }
171"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000172"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000173"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000174"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000175"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000176"as" { thrift_reserved_keyword(yytext); }
177"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000178"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000179"break" { thrift_reserved_keyword(yytext); }
180"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000181"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000182"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000183"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000184"continue" { thrift_reserved_keyword(yytext); }
185"declare" { thrift_reserved_keyword(yytext); }
186"def" { thrift_reserved_keyword(yytext); }
187"default" { thrift_reserved_keyword(yytext); }
188"del" { thrift_reserved_keyword(yytext); }
189"delete" { thrift_reserved_keyword(yytext); }
190"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000191"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000192"elif" { thrift_reserved_keyword(yytext); }
193"else" { thrift_reserved_keyword(yytext); }
194"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000195"elsif" { thrift_reserved_keyword(yytext); }
196"end" { thrift_reserved_keyword(yytext); }
197"enddeclare" { thrift_reserved_keyword(yytext); }
198"endfor" { thrift_reserved_keyword(yytext); }
199"endforeach" { thrift_reserved_keyword(yytext); }
200"endif" { thrift_reserved_keyword(yytext); }
201"endswitch" { thrift_reserved_keyword(yytext); }
202"endwhile" { thrift_reserved_keyword(yytext); }
203"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000204"except" { thrift_reserved_keyword(yytext); }
205"exec" { thrift_reserved_keyword(yytext); }
206"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000207"finally" { thrift_reserved_keyword(yytext); }
208"float" { thrift_reserved_keyword(yytext); }
209"for" { thrift_reserved_keyword(yytext); }
210"foreach" { thrift_reserved_keyword(yytext); }
211"function" { thrift_reserved_keyword(yytext); }
212"global" { thrift_reserved_keyword(yytext); }
213"goto" { thrift_reserved_keyword(yytext); }
214"if" { thrift_reserved_keyword(yytext); }
215"implements" { thrift_reserved_keyword(yytext); }
216"import" { thrift_reserved_keyword(yytext); }
217"in" { thrift_reserved_keyword(yytext); }
218"inline" { thrift_reserved_keyword(yytext); }
219"instanceof" { thrift_reserved_keyword(yytext); }
220"interface" { thrift_reserved_keyword(yytext); }
221"is" { thrift_reserved_keyword(yytext); }
222"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000223"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000224"native" { thrift_reserved_keyword(yytext); }
225"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000226"next" { thrift_reserved_keyword(yytext); }
227"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000228"not" { thrift_reserved_keyword(yytext); }
229"or" { thrift_reserved_keyword(yytext); }
230"pass" { thrift_reserved_keyword(yytext); }
231"public" { thrift_reserved_keyword(yytext); }
232"print" { thrift_reserved_keyword(yytext); }
233"private" { thrift_reserved_keyword(yytext); }
234"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000235"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000236"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000237"redo" { thrift_reserved_keyword(yytext); }
238"rescue" { thrift_reserved_keyword(yytext); }
239"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000240"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000241"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000242"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000243"sizeof" { thrift_reserved_keyword(yytext); }
244"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000245"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000246"switch" { thrift_reserved_keyword(yytext); }
247"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000248"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000249"this" { thrift_reserved_keyword(yytext); }
250"throw" { thrift_reserved_keyword(yytext); }
251"transient" { thrift_reserved_keyword(yytext); }
252"true" { thrift_reserved_keyword(yytext); }
253"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000254"undef" { thrift_reserved_keyword(yytext); }
255"union" { thrift_reserved_keyword(yytext); }
256"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000257"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000258"until" { thrift_reserved_keyword(yytext); }
259"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000260"var" { thrift_reserved_keyword(yytext); }
261"virtual" { thrift_reserved_keyword(yytext); }
262"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000263"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000264"while" { thrift_reserved_keyword(yytext); }
265"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000266"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000267"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000268
Mark Slee4f8da1d2006-10-12 02:47:27 +0000269{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000270 errno = 0;
271 yylval.iconst = strtoll(yytext, NULL, 10);
272 if (errno == ERANGE) {
273 integer_overflow(yytext);
274 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000275 return tok_int_constant;
276}
Mark Sleef5377b32006-10-10 01:42:59 +0000277
Mark Slee600cdb32006-11-29 22:06:42 +0000278{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000279 errno = 0;
280 yylval.iconst = strtoll(yytext+2, NULL, 16);
281 if (errno == ERANGE) {
282 integer_overflow(yytext);
283 }
Mark Slee600cdb32006-11-29 22:06:42 +0000284 return tok_int_constant;
285}
286
Mark Slee30152872006-11-28 01:24:07 +0000287{dubconstant} {
288 yylval.dconst = atof(yytext);
289 return tok_dub_constant;
290}
291
Mark Slee4f8da1d2006-10-12 02:47:27 +0000292{identifier} {
293 yylval.id = strdup(yytext);
294 return tok_identifier;
295}
296
Mark Sleebd588222007-11-21 08:43:35 +0000297{st_identifier} {
298 yylval.id = strdup(yytext);
299 return tok_st_identifier;
300}
301
David Reiss82e6fc02009-03-26 23:32:36 +0000302{literal_begin} {
303 char mark = yytext[0];
304 std::string result;
305 for(;;)
306 {
307 int ch = yyinput();
308 switch (ch) {
309 case EOF:
310 yyerror("End of file while read string at %d\n", yylineno);
311 exit(1);
312 case '\n':
313 yyerror("End of line while read string at %d\n", yylineno - 1);
314 exit(1);
315 case '\\':
316 ch = yyinput();
317 switch (ch) {
318 case 'r':
319 result.push_back('\r');
320 continue;
321 case 'n':
322 result.push_back('\n');
323 continue;
324 case 't':
325 result.push_back('\t');
326 continue;
327 case '"':
328 result.push_back('"');
329 continue;
330 case '\'':
331 result.push_back('\'');
332 continue;
333 case '\\':
334 result.push_back('\\');
335 continue;
336 default:
337 yyerror("Bad escape character\n");
338 return -1;
339 }
340 break;
341 default:
342 if (ch == mark) {
343 yylval.id = strdup(result.c_str());
344 return tok_literal;
345 } else {
346 result.push_back(ch);
347 }
348 }
349 }
Mark Slee30152872006-11-28 01:24:07 +0000350}
351
Mark Slee31985722006-05-24 21:45:31 +0000352
ccheeverf53b5cf2007-02-05 20:33:11 +0000353{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000354 /* This does not show up in the parse tree. */
355 /* Rather, the parser will grab it out of the global. */
356 if (g_parse_mode == PROGRAM) {
357 clear_doctext();
358 g_doctext = strdup(yytext + 3);
359 g_doctext[strlen(g_doctext) - 2] = '\0';
360 g_doctext = clean_up_doctext(g_doctext);
361 g_doctext_lineno = yylineno;
362 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000363}
364
Bryan Duxbury235f8b52011-08-19 18:27:47 +0000365. {
366 unexpected_token(yytext);
367}
368
ccheeverf53b5cf2007-02-05 20:33:11 +0000369
David Reissfb790d72010-09-02 16:41:45 +0000370. {
371 /* Catch-all to let us catch "*" in the parser. */
372 return (int) yytext[0];
373}
374
Mark Slee31985722006-05-24 21:45:31 +0000375%%
David Reiss4a054342009-03-26 23:32:27 +0000376
377/* vim: filetype=lex
378*/