blob: 762cb2fd7fd4c109693760fa083442d5af2d9d9b [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
Mark Slee31985722006-05-24 21:45:31 +000062%}
63
Mark Sleef5377b32006-10-10 01:42:59 +000064/**
65 * Provides the yylineno global, useful for debugging output
66 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000067%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000068
Mark Slee27ed6ec2007-08-16 01:26:31 +000069/**
David Reiss4563acd2010-08-31 16:51:29 +000070 * Our inputs are all single files, so no need for yywrap
71 */
72%option noyywrap
73
74/**
Christian Lavoie77215d82010-11-07 19:42:48 +000075 * We don't use it, and it fires up warnings at -Wall
76 */
77%option nounput
78
79/**
Mark Sleef5377b32006-10-10 01:42:59 +000080 * Helper definitions, comments, constants, and whatnot
81 */
82
Mark Sleebd588222007-11-21 08:43:35 +000083intconstant ([+-]?[0-9]+)
84hexconstant ("0x"[0-9A-Fa-f]+)
85dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
86identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
87whitespace ([ \t\r\n]*)
88sillycomm ("/*""*"*"*/")
89multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
90doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
91comment ("//"[^\n]*)
92unixcomment ("#"[^\n]*)
93symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Sleebd588222007-11-21 08:43:35 +000094st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
David Reiss82e6fc02009-03-26 23:32:36 +000095literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +000096
97%%
98
Mark Sleebd588222007-11-21 08:43:35 +000099{whitespace} { /* do nothing */ }
100{sillycomm} { /* do nothing */ }
101{multicomm} { /* do nothing */ }
102{comment} { /* do nothing */ }
103{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +0000104
Mark Sleebd588222007-11-21 08:43:35 +0000105{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000106
Mark Sleebd588222007-11-21 08:43:35 +0000107"namespace" { return tok_namespace; }
108"cpp_namespace" { return tok_cpp_namespace; }
109"cpp_include" { return tok_cpp_include; }
110"cpp_type" { return tok_cpp_type; }
111"java_package" { return tok_java_package; }
112"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000113"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000114"php_namespace" { return tok_php_namespace; }
115"py_module" { return tok_py_module; }
116"perl_package" { return tok_perl_package; }
117"ruby_namespace" { return tok_ruby_namespace; }
118"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000119"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000120"xsd_all" { return tok_xsd_all; }
121"xsd_optional" { return tok_xsd_optional; }
122"xsd_nillable" { return tok_xsd_nillable; }
123"xsd_namespace" { return tok_xsd_namespace; }
124"xsd_attrs" { return tok_xsd_attrs; }
125"include" { return tok_include; }
126"void" { return tok_void; }
127"bool" { return tok_bool; }
128"byte" { return tok_byte; }
129"i16" { return tok_i16; }
130"i32" { return tok_i32; }
131"i64" { return tok_i64; }
132"double" { return tok_double; }
133"string" { return tok_string; }
134"binary" { return tok_binary; }
135"slist" { return tok_slist; }
136"senum" { return tok_senum; }
137"map" { return tok_map; }
138"list" { return tok_list; }
139"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000140"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000141"typedef" { return tok_typedef; }
142"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000143"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000144"exception" { return tok_xception; }
145"extends" { return tok_extends; }
146"throws" { return tok_throws; }
147"service" { return tok_service; }
148"enum" { return tok_enum; }
149"const" { return tok_const; }
150"required" { return tok_required; }
151"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000152"async" {
153 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
154 return tok_oneway;
155}
Mark Sleef0712dc2006-10-25 19:03:57 +0000156
Mark Slee52f643d2006-08-09 00:03:43 +0000157
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000158"BEGIN" { thrift_reserved_keyword(yytext); }
159"END" { thrift_reserved_keyword(yytext); }
160"__CLASS__" { thrift_reserved_keyword(yytext); }
161"__DIR__" { thrift_reserved_keyword(yytext); }
162"__FILE__" { thrift_reserved_keyword(yytext); }
163"__FUNCTION__" { thrift_reserved_keyword(yytext); }
164"__LINE__" { thrift_reserved_keyword(yytext); }
165"__METHOD__" { thrift_reserved_keyword(yytext); }
166"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000167"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000168"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000169"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000170"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000171"as" { thrift_reserved_keyword(yytext); }
172"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000173"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000174"break" { thrift_reserved_keyword(yytext); }
175"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000176"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000177"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000178"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000179"continue" { thrift_reserved_keyword(yytext); }
180"declare" { thrift_reserved_keyword(yytext); }
181"def" { thrift_reserved_keyword(yytext); }
182"default" { thrift_reserved_keyword(yytext); }
183"del" { thrift_reserved_keyword(yytext); }
184"delete" { thrift_reserved_keyword(yytext); }
185"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000186"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000187"elif" { thrift_reserved_keyword(yytext); }
188"else" { thrift_reserved_keyword(yytext); }
189"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000190"elsif" { thrift_reserved_keyword(yytext); }
191"end" { thrift_reserved_keyword(yytext); }
192"enddeclare" { thrift_reserved_keyword(yytext); }
193"endfor" { thrift_reserved_keyword(yytext); }
194"endforeach" { thrift_reserved_keyword(yytext); }
195"endif" { thrift_reserved_keyword(yytext); }
196"endswitch" { thrift_reserved_keyword(yytext); }
197"endwhile" { thrift_reserved_keyword(yytext); }
198"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000199"except" { thrift_reserved_keyword(yytext); }
200"exec" { thrift_reserved_keyword(yytext); }
201"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000202"finally" { thrift_reserved_keyword(yytext); }
203"float" { thrift_reserved_keyword(yytext); }
204"for" { thrift_reserved_keyword(yytext); }
205"foreach" { thrift_reserved_keyword(yytext); }
206"function" { thrift_reserved_keyword(yytext); }
207"global" { thrift_reserved_keyword(yytext); }
208"goto" { thrift_reserved_keyword(yytext); }
209"if" { thrift_reserved_keyword(yytext); }
210"implements" { thrift_reserved_keyword(yytext); }
211"import" { thrift_reserved_keyword(yytext); }
212"in" { thrift_reserved_keyword(yytext); }
213"inline" { thrift_reserved_keyword(yytext); }
214"instanceof" { thrift_reserved_keyword(yytext); }
215"interface" { thrift_reserved_keyword(yytext); }
216"is" { thrift_reserved_keyword(yytext); }
217"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000218"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000219"native" { thrift_reserved_keyword(yytext); }
220"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000221"next" { thrift_reserved_keyword(yytext); }
222"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000223"not" { thrift_reserved_keyword(yytext); }
224"or" { thrift_reserved_keyword(yytext); }
225"pass" { thrift_reserved_keyword(yytext); }
226"public" { thrift_reserved_keyword(yytext); }
227"print" { thrift_reserved_keyword(yytext); }
228"private" { thrift_reserved_keyword(yytext); }
229"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000230"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000231"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000232"redo" { thrift_reserved_keyword(yytext); }
233"rescue" { thrift_reserved_keyword(yytext); }
234"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000235"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000236"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000237"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000238"sizeof" { thrift_reserved_keyword(yytext); }
239"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000240"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000241"switch" { thrift_reserved_keyword(yytext); }
242"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000243"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000244"this" { thrift_reserved_keyword(yytext); }
245"throw" { thrift_reserved_keyword(yytext); }
246"transient" { thrift_reserved_keyword(yytext); }
247"true" { thrift_reserved_keyword(yytext); }
248"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000249"undef" { thrift_reserved_keyword(yytext); }
250"union" { thrift_reserved_keyword(yytext); }
251"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000252"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000253"until" { thrift_reserved_keyword(yytext); }
254"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000255"var" { thrift_reserved_keyword(yytext); }
256"virtual" { thrift_reserved_keyword(yytext); }
257"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000258"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000259"while" { thrift_reserved_keyword(yytext); }
260"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000261"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000262"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000263
Mark Slee4f8da1d2006-10-12 02:47:27 +0000264{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000265 errno = 0;
266 yylval.iconst = strtoll(yytext, NULL, 10);
267 if (errno == ERANGE) {
268 integer_overflow(yytext);
269 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000270 return tok_int_constant;
271}
Mark Sleef5377b32006-10-10 01:42:59 +0000272
Mark Slee600cdb32006-11-29 22:06:42 +0000273{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000274 errno = 0;
275 yylval.iconst = strtoll(yytext+2, NULL, 16);
276 if (errno == ERANGE) {
277 integer_overflow(yytext);
278 }
Mark Slee600cdb32006-11-29 22:06:42 +0000279 return tok_int_constant;
280}
281
Mark Slee30152872006-11-28 01:24:07 +0000282{dubconstant} {
283 yylval.dconst = atof(yytext);
284 return tok_dub_constant;
285}
286
Mark Slee4f8da1d2006-10-12 02:47:27 +0000287{identifier} {
288 yylval.id = strdup(yytext);
289 return tok_identifier;
290}
291
Mark Sleebd588222007-11-21 08:43:35 +0000292{st_identifier} {
293 yylval.id = strdup(yytext);
294 return tok_st_identifier;
295}
296
David Reiss82e6fc02009-03-26 23:32:36 +0000297{literal_begin} {
298 char mark = yytext[0];
299 std::string result;
300 for(;;)
301 {
302 int ch = yyinput();
303 switch (ch) {
304 case EOF:
305 yyerror("End of file while read string at %d\n", yylineno);
306 exit(1);
307 case '\n':
308 yyerror("End of line while read string at %d\n", yylineno - 1);
309 exit(1);
310 case '\\':
311 ch = yyinput();
312 switch (ch) {
313 case 'r':
314 result.push_back('\r');
315 continue;
316 case 'n':
317 result.push_back('\n');
318 continue;
319 case 't':
320 result.push_back('\t');
321 continue;
322 case '"':
323 result.push_back('"');
324 continue;
325 case '\'':
326 result.push_back('\'');
327 continue;
328 case '\\':
329 result.push_back('\\');
330 continue;
331 default:
332 yyerror("Bad escape character\n");
333 return -1;
334 }
335 break;
336 default:
337 if (ch == mark) {
338 yylval.id = strdup(result.c_str());
339 return tok_literal;
340 } else {
341 result.push_back(ch);
342 }
343 }
344 }
Mark Slee30152872006-11-28 01:24:07 +0000345}
346
Mark Slee31985722006-05-24 21:45:31 +0000347
ccheeverf53b5cf2007-02-05 20:33:11 +0000348{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000349 /* This does not show up in the parse tree. */
350 /* Rather, the parser will grab it out of the global. */
351 if (g_parse_mode == PROGRAM) {
352 clear_doctext();
353 g_doctext = strdup(yytext + 3);
354 g_doctext[strlen(g_doctext) - 2] = '\0';
355 g_doctext = clean_up_doctext(g_doctext);
356 g_doctext_lineno = yylineno;
357 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000358}
359
360
David Reissfb790d72010-09-02 16:41:45 +0000361. {
362 /* Catch-all to let us catch "*" in the parser. */
363 return (int) yytext[0];
364}
365
Mark Slee31985722006-05-24 21:45:31 +0000366%%
David Reiss4a054342009-03-26 23:32:27 +0000367
368/* vim: filetype=lex
369*/