blob: af033d6aa382508b79d69c6996471dc62370be53 [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
Mark Sleebd588222007-11-21 08:43:35 +0000113"namespace" { return tok_namespace; }
114"cpp_namespace" { return tok_cpp_namespace; }
115"cpp_include" { return tok_cpp_include; }
116"cpp_type" { return tok_cpp_type; }
117"java_package" { return tok_java_package; }
118"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000119"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000120"php_namespace" { return tok_php_namespace; }
121"py_module" { return tok_py_module; }
122"perl_package" { return tok_perl_package; }
123"ruby_namespace" { return tok_ruby_namespace; }
124"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000125"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000126"xsd_all" { return tok_xsd_all; }
127"xsd_optional" { return tok_xsd_optional; }
128"xsd_nillable" { return tok_xsd_nillable; }
129"xsd_namespace" { return tok_xsd_namespace; }
130"xsd_attrs" { return tok_xsd_attrs; }
131"include" { return tok_include; }
132"void" { return tok_void; }
133"bool" { return tok_bool; }
134"byte" { return tok_byte; }
135"i16" { return tok_i16; }
136"i32" { return tok_i32; }
137"i64" { return tok_i64; }
138"double" { return tok_double; }
139"string" { return tok_string; }
140"binary" { return tok_binary; }
141"slist" { return tok_slist; }
142"senum" { return tok_senum; }
143"map" { return tok_map; }
144"list" { return tok_list; }
145"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000146"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000147"typedef" { return tok_typedef; }
148"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000149"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000150"exception" { return tok_xception; }
151"extends" { return tok_extends; }
152"throws" { return tok_throws; }
153"service" { return tok_service; }
154"enum" { return tok_enum; }
155"const" { return tok_const; }
156"required" { return tok_required; }
157"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000158"async" {
159 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
160 return tok_oneway;
161}
Mark Sleef0712dc2006-10-25 19:03:57 +0000162
Mark Slee52f643d2006-08-09 00:03:43 +0000163
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000164"BEGIN" { thrift_reserved_keyword(yytext); }
165"END" { thrift_reserved_keyword(yytext); }
166"__CLASS__" { thrift_reserved_keyword(yytext); }
167"__DIR__" { thrift_reserved_keyword(yytext); }
168"__FILE__" { thrift_reserved_keyword(yytext); }
169"__FUNCTION__" { thrift_reserved_keyword(yytext); }
170"__LINE__" { thrift_reserved_keyword(yytext); }
171"__METHOD__" { thrift_reserved_keyword(yytext); }
172"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000173"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000174"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000175"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000176"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000177"as" { thrift_reserved_keyword(yytext); }
178"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000179"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000180"break" { thrift_reserved_keyword(yytext); }
181"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000182"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000183"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000184"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000185"continue" { thrift_reserved_keyword(yytext); }
186"declare" { thrift_reserved_keyword(yytext); }
187"def" { thrift_reserved_keyword(yytext); }
188"default" { thrift_reserved_keyword(yytext); }
189"del" { thrift_reserved_keyword(yytext); }
190"delete" { thrift_reserved_keyword(yytext); }
191"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000192"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000193"elif" { thrift_reserved_keyword(yytext); }
194"else" { thrift_reserved_keyword(yytext); }
195"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000196"elsif" { thrift_reserved_keyword(yytext); }
197"end" { thrift_reserved_keyword(yytext); }
198"enddeclare" { thrift_reserved_keyword(yytext); }
199"endfor" { thrift_reserved_keyword(yytext); }
200"endforeach" { thrift_reserved_keyword(yytext); }
201"endif" { thrift_reserved_keyword(yytext); }
202"endswitch" { thrift_reserved_keyword(yytext); }
203"endwhile" { thrift_reserved_keyword(yytext); }
204"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000205"except" { thrift_reserved_keyword(yytext); }
206"exec" { thrift_reserved_keyword(yytext); }
207"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000208"finally" { thrift_reserved_keyword(yytext); }
209"float" { thrift_reserved_keyword(yytext); }
210"for" { thrift_reserved_keyword(yytext); }
211"foreach" { thrift_reserved_keyword(yytext); }
212"function" { thrift_reserved_keyword(yytext); }
213"global" { thrift_reserved_keyword(yytext); }
214"goto" { thrift_reserved_keyword(yytext); }
215"if" { thrift_reserved_keyword(yytext); }
216"implements" { thrift_reserved_keyword(yytext); }
217"import" { thrift_reserved_keyword(yytext); }
218"in" { thrift_reserved_keyword(yytext); }
219"inline" { thrift_reserved_keyword(yytext); }
220"instanceof" { thrift_reserved_keyword(yytext); }
221"interface" { thrift_reserved_keyword(yytext); }
222"is" { thrift_reserved_keyword(yytext); }
223"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000224"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000225"native" { thrift_reserved_keyword(yytext); }
226"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000227"next" { thrift_reserved_keyword(yytext); }
228"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000229"not" { thrift_reserved_keyword(yytext); }
230"or" { thrift_reserved_keyword(yytext); }
231"pass" { thrift_reserved_keyword(yytext); }
232"public" { thrift_reserved_keyword(yytext); }
233"print" { thrift_reserved_keyword(yytext); }
234"private" { thrift_reserved_keyword(yytext); }
235"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000236"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000237"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000238"redo" { thrift_reserved_keyword(yytext); }
239"rescue" { thrift_reserved_keyword(yytext); }
240"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000241"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000242"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000243"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000244"sizeof" { thrift_reserved_keyword(yytext); }
245"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000246"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000247"switch" { thrift_reserved_keyword(yytext); }
248"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000249"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000250"this" { thrift_reserved_keyword(yytext); }
251"throw" { thrift_reserved_keyword(yytext); }
252"transient" { thrift_reserved_keyword(yytext); }
253"true" { thrift_reserved_keyword(yytext); }
254"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000255"undef" { thrift_reserved_keyword(yytext); }
256"union" { thrift_reserved_keyword(yytext); }
257"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000258"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000259"until" { thrift_reserved_keyword(yytext); }
260"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000261"var" { thrift_reserved_keyword(yytext); }
262"virtual" { thrift_reserved_keyword(yytext); }
263"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000264"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000265"while" { thrift_reserved_keyword(yytext); }
266"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000267"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000268"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000269
Mark Slee4f8da1d2006-10-12 02:47:27 +0000270{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000271 errno = 0;
272 yylval.iconst = strtoll(yytext, NULL, 10);
273 if (errno == ERANGE) {
274 integer_overflow(yytext);
275 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000276 return tok_int_constant;
277}
Mark Sleef5377b32006-10-10 01:42:59 +0000278
Mark Slee600cdb32006-11-29 22:06:42 +0000279{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000280 errno = 0;
281 yylval.iconst = strtoll(yytext+2, NULL, 16);
282 if (errno == ERANGE) {
283 integer_overflow(yytext);
284 }
Mark Slee600cdb32006-11-29 22:06:42 +0000285 return tok_int_constant;
286}
287
Mark Slee30152872006-11-28 01:24:07 +0000288{dubconstant} {
289 yylval.dconst = atof(yytext);
290 return tok_dub_constant;
291}
292
Mark Slee4f8da1d2006-10-12 02:47:27 +0000293{identifier} {
294 yylval.id = strdup(yytext);
295 return tok_identifier;
296}
297
Mark Sleebd588222007-11-21 08:43:35 +0000298{st_identifier} {
299 yylval.id = strdup(yytext);
300 return tok_st_identifier;
301}
302
David Reiss82e6fc02009-03-26 23:32:36 +0000303{literal_begin} {
304 char mark = yytext[0];
305 std::string result;
306 for(;;)
307 {
308 int ch = yyinput();
309 switch (ch) {
310 case EOF:
311 yyerror("End of file while read string at %d\n", yylineno);
312 exit(1);
313 case '\n':
314 yyerror("End of line while read string at %d\n", yylineno - 1);
315 exit(1);
316 case '\\':
317 ch = yyinput();
318 switch (ch) {
319 case 'r':
320 result.push_back('\r');
321 continue;
322 case 'n':
323 result.push_back('\n');
324 continue;
325 case 't':
326 result.push_back('\t');
327 continue;
328 case '"':
329 result.push_back('"');
330 continue;
331 case '\'':
332 result.push_back('\'');
333 continue;
334 case '\\':
335 result.push_back('\\');
336 continue;
337 default:
338 yyerror("Bad escape character\n");
339 return -1;
340 }
341 break;
342 default:
343 if (ch == mark) {
344 yylval.id = strdup(result.c_str());
345 return tok_literal;
346 } else {
347 result.push_back(ch);
348 }
349 }
350 }
Mark Slee30152872006-11-28 01:24:07 +0000351}
352
Mark Slee31985722006-05-24 21:45:31 +0000353
ccheeverf53b5cf2007-02-05 20:33:11 +0000354{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000355 /* This does not show up in the parse tree. */
356 /* Rather, the parser will grab it out of the global. */
357 if (g_parse_mode == PROGRAM) {
358 clear_doctext();
359 g_doctext = strdup(yytext + 3);
360 g_doctext[strlen(g_doctext) - 2] = '\0';
361 g_doctext = clean_up_doctext(g_doctext);
362 g_doctext_lineno = yylineno;
363 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000364}
365
Bryan Duxbury235f8b52011-08-19 18:27:47 +0000366. {
367 unexpected_token(yytext);
368}
369
ccheeverf53b5cf2007-02-05 20:33:11 +0000370
David Reissfb790d72010-09-02 16:41:45 +0000371. {
372 /* Catch-all to let us catch "*" in the parser. */
373 return (int) yytext[0];
374}
375
Mark Slee31985722006-05-24 21:45:31 +0000376%%
David Reiss4a054342009-03-26 23:32:27 +0000377
378/* vim: filetype=lex
379*/