blob: ab0976ef9d111dfbaa47f3c061422630d3cec373 [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]; }
Roger Meier0c3c8952011-08-22 21:38:16 +0000111"*" { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000112
Bryan Duxbury6c928f32011-10-13 21:32:52 +0000113"false" { yylval.iconst=0; return tok_int_constant; }
114"true" { yylval.iconst=1; return tok_int_constant; }
115
Mark Sleebd588222007-11-21 08:43:35 +0000116"namespace" { return tok_namespace; }
117"cpp_namespace" { return tok_cpp_namespace; }
118"cpp_include" { return tok_cpp_include; }
119"cpp_type" { return tok_cpp_type; }
120"java_package" { return tok_java_package; }
121"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000122"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000123"php_namespace" { return tok_php_namespace; }
124"py_module" { return tok_py_module; }
125"perl_package" { return tok_perl_package; }
126"ruby_namespace" { return tok_ruby_namespace; }
127"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000128"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000129"xsd_all" { return tok_xsd_all; }
130"xsd_optional" { return tok_xsd_optional; }
131"xsd_nillable" { return tok_xsd_nillable; }
132"xsd_namespace" { return tok_xsd_namespace; }
133"xsd_attrs" { return tok_xsd_attrs; }
134"include" { return tok_include; }
135"void" { return tok_void; }
136"bool" { return tok_bool; }
137"byte" { return tok_byte; }
138"i16" { return tok_i16; }
139"i32" { return tok_i32; }
140"i64" { return tok_i64; }
141"double" { return tok_double; }
142"string" { return tok_string; }
143"binary" { return tok_binary; }
144"slist" { return tok_slist; }
145"senum" { return tok_senum; }
146"map" { return tok_map; }
147"list" { return tok_list; }
148"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000149"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000150"typedef" { return tok_typedef; }
151"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000152"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000153"exception" { return tok_xception; }
154"extends" { return tok_extends; }
155"throws" { return tok_throws; }
156"service" { return tok_service; }
157"enum" { return tok_enum; }
158"const" { return tok_const; }
159"required" { return tok_required; }
160"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000161"async" {
162 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
163 return tok_oneway;
164}
Mark Sleef0712dc2006-10-25 19:03:57 +0000165
Mark Slee52f643d2006-08-09 00:03:43 +0000166
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000167"BEGIN" { thrift_reserved_keyword(yytext); }
168"END" { thrift_reserved_keyword(yytext); }
169"__CLASS__" { thrift_reserved_keyword(yytext); }
170"__DIR__" { thrift_reserved_keyword(yytext); }
171"__FILE__" { thrift_reserved_keyword(yytext); }
172"__FUNCTION__" { thrift_reserved_keyword(yytext); }
173"__LINE__" { thrift_reserved_keyword(yytext); }
174"__METHOD__" { thrift_reserved_keyword(yytext); }
175"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000176"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000177"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000178"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000179"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000180"as" { thrift_reserved_keyword(yytext); }
181"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000182"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000183"break" { thrift_reserved_keyword(yytext); }
184"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000185"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000186"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000187"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000188"continue" { thrift_reserved_keyword(yytext); }
189"declare" { thrift_reserved_keyword(yytext); }
190"def" { thrift_reserved_keyword(yytext); }
191"default" { thrift_reserved_keyword(yytext); }
192"del" { thrift_reserved_keyword(yytext); }
193"delete" { thrift_reserved_keyword(yytext); }
194"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000195"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000196"elif" { thrift_reserved_keyword(yytext); }
197"else" { thrift_reserved_keyword(yytext); }
198"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000199"elsif" { thrift_reserved_keyword(yytext); }
200"end" { thrift_reserved_keyword(yytext); }
201"enddeclare" { thrift_reserved_keyword(yytext); }
202"endfor" { thrift_reserved_keyword(yytext); }
203"endforeach" { thrift_reserved_keyword(yytext); }
204"endif" { thrift_reserved_keyword(yytext); }
205"endswitch" { thrift_reserved_keyword(yytext); }
206"endwhile" { thrift_reserved_keyword(yytext); }
207"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000208"except" { thrift_reserved_keyword(yytext); }
209"exec" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000210"finally" { thrift_reserved_keyword(yytext); }
211"float" { thrift_reserved_keyword(yytext); }
212"for" { thrift_reserved_keyword(yytext); }
213"foreach" { thrift_reserved_keyword(yytext); }
214"function" { thrift_reserved_keyword(yytext); }
215"global" { thrift_reserved_keyword(yytext); }
216"goto" { thrift_reserved_keyword(yytext); }
217"if" { thrift_reserved_keyword(yytext); }
218"implements" { thrift_reserved_keyword(yytext); }
219"import" { thrift_reserved_keyword(yytext); }
220"in" { thrift_reserved_keyword(yytext); }
221"inline" { thrift_reserved_keyword(yytext); }
222"instanceof" { thrift_reserved_keyword(yytext); }
223"interface" { thrift_reserved_keyword(yytext); }
224"is" { thrift_reserved_keyword(yytext); }
225"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000226"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000227"native" { thrift_reserved_keyword(yytext); }
228"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000229"next" { thrift_reserved_keyword(yytext); }
230"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000231"not" { thrift_reserved_keyword(yytext); }
232"or" { thrift_reserved_keyword(yytext); }
233"pass" { thrift_reserved_keyword(yytext); }
234"public" { thrift_reserved_keyword(yytext); }
235"print" { thrift_reserved_keyword(yytext); }
236"private" { thrift_reserved_keyword(yytext); }
237"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000238"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000239"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000240"redo" { thrift_reserved_keyword(yytext); }
241"rescue" { thrift_reserved_keyword(yytext); }
242"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000243"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000244"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000245"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000246"sizeof" { thrift_reserved_keyword(yytext); }
247"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000248"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000249"switch" { thrift_reserved_keyword(yytext); }
250"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000251"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000252"this" { thrift_reserved_keyword(yytext); }
253"throw" { thrift_reserved_keyword(yytext); }
254"transient" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000255"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000256"undef" { thrift_reserved_keyword(yytext); }
257"union" { thrift_reserved_keyword(yytext); }
258"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000259"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000260"until" { thrift_reserved_keyword(yytext); }
261"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000262"var" { thrift_reserved_keyword(yytext); }
263"virtual" { thrift_reserved_keyword(yytext); }
264"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000265"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000266"while" { thrift_reserved_keyword(yytext); }
267"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000268"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000269"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000270
Mark Slee4f8da1d2006-10-12 02:47:27 +0000271{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000272 errno = 0;
273 yylval.iconst = strtoll(yytext, NULL, 10);
274 if (errno == ERANGE) {
275 integer_overflow(yytext);
276 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000277 return tok_int_constant;
278}
Mark Sleef5377b32006-10-10 01:42:59 +0000279
Mark Slee600cdb32006-11-29 22:06:42 +0000280{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000281 errno = 0;
282 yylval.iconst = strtoll(yytext+2, NULL, 16);
283 if (errno == ERANGE) {
284 integer_overflow(yytext);
285 }
Mark Slee600cdb32006-11-29 22:06:42 +0000286 return tok_int_constant;
287}
288
Mark Slee30152872006-11-28 01:24:07 +0000289{dubconstant} {
290 yylval.dconst = atof(yytext);
291 return tok_dub_constant;
292}
293
Mark Slee4f8da1d2006-10-12 02:47:27 +0000294{identifier} {
295 yylval.id = strdup(yytext);
296 return tok_identifier;
297}
298
Mark Sleebd588222007-11-21 08:43:35 +0000299{st_identifier} {
300 yylval.id = strdup(yytext);
301 return tok_st_identifier;
302}
303
David Reiss82e6fc02009-03-26 23:32:36 +0000304{literal_begin} {
305 char mark = yytext[0];
306 std::string result;
307 for(;;)
308 {
309 int ch = yyinput();
310 switch (ch) {
311 case EOF:
312 yyerror("End of file while read string at %d\n", yylineno);
313 exit(1);
314 case '\n':
315 yyerror("End of line while read string at %d\n", yylineno - 1);
316 exit(1);
317 case '\\':
318 ch = yyinput();
319 switch (ch) {
320 case 'r':
321 result.push_back('\r');
322 continue;
323 case 'n':
324 result.push_back('\n');
325 continue;
326 case 't':
327 result.push_back('\t');
328 continue;
329 case '"':
330 result.push_back('"');
331 continue;
332 case '\'':
333 result.push_back('\'');
334 continue;
335 case '\\':
336 result.push_back('\\');
337 continue;
338 default:
339 yyerror("Bad escape character\n");
340 return -1;
341 }
342 break;
343 default:
344 if (ch == mark) {
345 yylval.id = strdup(result.c_str());
346 return tok_literal;
347 } else {
348 result.push_back(ch);
349 }
350 }
351 }
Mark Slee30152872006-11-28 01:24:07 +0000352}
353
Mark Slee31985722006-05-24 21:45:31 +0000354
ccheeverf53b5cf2007-02-05 20:33:11 +0000355{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000356 /* This does not show up in the parse tree. */
357 /* Rather, the parser will grab it out of the global. */
358 if (g_parse_mode == PROGRAM) {
359 clear_doctext();
360 g_doctext = strdup(yytext + 3);
361 g_doctext[strlen(g_doctext) - 2] = '\0';
362 g_doctext = clean_up_doctext(g_doctext);
363 g_doctext_lineno = yylineno;
364 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000365}
366
Bryan Duxbury235f8b52011-08-19 18:27:47 +0000367. {
368 unexpected_token(yytext);
369}
370
ccheeverf53b5cf2007-02-05 20:33:11 +0000371
David Reissfb790d72010-09-02 16:41:45 +0000372. {
373 /* Catch-all to let us catch "*" in the parser. */
374 return (int) yytext[0];
375}
376
Mark Slee31985722006-05-24 21:45:31 +0000377%%
David Reiss4a054342009-03-26 23:32:27 +0000378
379/* vim: filetype=lex
380*/