blob: bdc41b1b76b8d8f15a581db101b1cfae8fed4c73 [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; }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000123"delphi_namespace" { return tok_delphi_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000124"php_namespace" { return tok_php_namespace; }
125"py_module" { return tok_py_module; }
126"perl_package" { return tok_perl_package; }
127"ruby_namespace" { return tok_ruby_namespace; }
128"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000129"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000130"xsd_all" { return tok_xsd_all; }
131"xsd_optional" { return tok_xsd_optional; }
132"xsd_nillable" { return tok_xsd_nillable; }
133"xsd_namespace" { return tok_xsd_namespace; }
134"xsd_attrs" { return tok_xsd_attrs; }
135"include" { return tok_include; }
136"void" { return tok_void; }
137"bool" { return tok_bool; }
138"byte" { return tok_byte; }
139"i16" { return tok_i16; }
140"i32" { return tok_i32; }
141"i64" { return tok_i64; }
142"double" { return tok_double; }
143"string" { return tok_string; }
144"binary" { return tok_binary; }
145"slist" { return tok_slist; }
146"senum" { return tok_senum; }
147"map" { return tok_map; }
148"list" { return tok_list; }
149"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000150"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000151"typedef" { return tok_typedef; }
152"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000153"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000154"exception" { return tok_xception; }
155"extends" { return tok_extends; }
156"throws" { return tok_throws; }
157"service" { return tok_service; }
158"enum" { return tok_enum; }
159"const" { return tok_const; }
160"required" { return tok_required; }
161"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000162"async" {
163 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
164 return tok_oneway;
165}
Mark Sleef0712dc2006-10-25 19:03:57 +0000166
Mark Slee52f643d2006-08-09 00:03:43 +0000167
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000168"BEGIN" { thrift_reserved_keyword(yytext); }
169"END" { thrift_reserved_keyword(yytext); }
170"__CLASS__" { thrift_reserved_keyword(yytext); }
171"__DIR__" { thrift_reserved_keyword(yytext); }
172"__FILE__" { thrift_reserved_keyword(yytext); }
173"__FUNCTION__" { thrift_reserved_keyword(yytext); }
174"__LINE__" { thrift_reserved_keyword(yytext); }
175"__METHOD__" { thrift_reserved_keyword(yytext); }
176"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000177"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000178"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000179"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000180"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000181"as" { thrift_reserved_keyword(yytext); }
182"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000183"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000184"break" { thrift_reserved_keyword(yytext); }
185"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000186"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000187"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000188"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000189"continue" { thrift_reserved_keyword(yytext); }
190"declare" { thrift_reserved_keyword(yytext); }
191"def" { thrift_reserved_keyword(yytext); }
192"default" { thrift_reserved_keyword(yytext); }
193"del" { thrift_reserved_keyword(yytext); }
194"delete" { thrift_reserved_keyword(yytext); }
195"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000196"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000197"elif" { thrift_reserved_keyword(yytext); }
198"else" { thrift_reserved_keyword(yytext); }
199"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000200"elsif" { thrift_reserved_keyword(yytext); }
201"end" { thrift_reserved_keyword(yytext); }
202"enddeclare" { thrift_reserved_keyword(yytext); }
203"endfor" { thrift_reserved_keyword(yytext); }
204"endforeach" { thrift_reserved_keyword(yytext); }
205"endif" { thrift_reserved_keyword(yytext); }
206"endswitch" { thrift_reserved_keyword(yytext); }
207"endwhile" { thrift_reserved_keyword(yytext); }
208"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000209"except" { thrift_reserved_keyword(yytext); }
210"exec" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000211"finally" { thrift_reserved_keyword(yytext); }
212"float" { thrift_reserved_keyword(yytext); }
213"for" { thrift_reserved_keyword(yytext); }
214"foreach" { thrift_reserved_keyword(yytext); }
215"function" { thrift_reserved_keyword(yytext); }
216"global" { thrift_reserved_keyword(yytext); }
217"goto" { thrift_reserved_keyword(yytext); }
218"if" { thrift_reserved_keyword(yytext); }
219"implements" { thrift_reserved_keyword(yytext); }
220"import" { thrift_reserved_keyword(yytext); }
221"in" { thrift_reserved_keyword(yytext); }
222"inline" { thrift_reserved_keyword(yytext); }
223"instanceof" { thrift_reserved_keyword(yytext); }
224"interface" { thrift_reserved_keyword(yytext); }
225"is" { thrift_reserved_keyword(yytext); }
226"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000227"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000228"native" { thrift_reserved_keyword(yytext); }
229"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000230"next" { thrift_reserved_keyword(yytext); }
231"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000232"not" { thrift_reserved_keyword(yytext); }
233"or" { thrift_reserved_keyword(yytext); }
234"pass" { thrift_reserved_keyword(yytext); }
235"public" { thrift_reserved_keyword(yytext); }
236"print" { thrift_reserved_keyword(yytext); }
237"private" { thrift_reserved_keyword(yytext); }
238"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000239"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000240"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000241"redo" { thrift_reserved_keyword(yytext); }
242"rescue" { thrift_reserved_keyword(yytext); }
243"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000244"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000245"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000246"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000247"sizeof" { thrift_reserved_keyword(yytext); }
248"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000249"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000250"switch" { thrift_reserved_keyword(yytext); }
251"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000252"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000253"this" { thrift_reserved_keyword(yytext); }
254"throw" { thrift_reserved_keyword(yytext); }
255"transient" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000256"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000257"undef" { thrift_reserved_keyword(yytext); }
258"union" { thrift_reserved_keyword(yytext); }
259"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000260"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000261"until" { thrift_reserved_keyword(yytext); }
262"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000263"var" { thrift_reserved_keyword(yytext); }
264"virtual" { thrift_reserved_keyword(yytext); }
265"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000266"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000267"while" { thrift_reserved_keyword(yytext); }
268"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000269"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000270"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000271
Mark Slee4f8da1d2006-10-12 02:47:27 +0000272{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000273 errno = 0;
274 yylval.iconst = strtoll(yytext, NULL, 10);
275 if (errno == ERANGE) {
276 integer_overflow(yytext);
277 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000278 return tok_int_constant;
279}
Mark Sleef5377b32006-10-10 01:42:59 +0000280
Mark Slee600cdb32006-11-29 22:06:42 +0000281{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000282 errno = 0;
283 yylval.iconst = strtoll(yytext+2, NULL, 16);
284 if (errno == ERANGE) {
285 integer_overflow(yytext);
286 }
Mark Slee600cdb32006-11-29 22:06:42 +0000287 return tok_int_constant;
288}
289
Mark Slee30152872006-11-28 01:24:07 +0000290{dubconstant} {
291 yylval.dconst = atof(yytext);
292 return tok_dub_constant;
293}
294
Mark Slee4f8da1d2006-10-12 02:47:27 +0000295{identifier} {
296 yylval.id = strdup(yytext);
297 return tok_identifier;
298}
299
Mark Sleebd588222007-11-21 08:43:35 +0000300{st_identifier} {
301 yylval.id = strdup(yytext);
302 return tok_st_identifier;
303}
304
David Reiss82e6fc02009-03-26 23:32:36 +0000305{literal_begin} {
306 char mark = yytext[0];
307 std::string result;
308 for(;;)
309 {
310 int ch = yyinput();
311 switch (ch) {
312 case EOF:
313 yyerror("End of file while read string at %d\n", yylineno);
314 exit(1);
315 case '\n':
316 yyerror("End of line while read string at %d\n", yylineno - 1);
317 exit(1);
318 case '\\':
319 ch = yyinput();
320 switch (ch) {
321 case 'r':
322 result.push_back('\r');
323 continue;
324 case 'n':
325 result.push_back('\n');
326 continue;
327 case 't':
328 result.push_back('\t');
329 continue;
330 case '"':
331 result.push_back('"');
332 continue;
333 case '\'':
334 result.push_back('\'');
335 continue;
336 case '\\':
337 result.push_back('\\');
338 continue;
339 default:
340 yyerror("Bad escape character\n");
341 return -1;
342 }
343 break;
344 default:
345 if (ch == mark) {
346 yylval.id = strdup(result.c_str());
347 return tok_literal;
348 } else {
349 result.push_back(ch);
350 }
351 }
352 }
Mark Slee30152872006-11-28 01:24:07 +0000353}
354
Mark Slee31985722006-05-24 21:45:31 +0000355
ccheeverf53b5cf2007-02-05 20:33:11 +0000356{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000357 /* This does not show up in the parse tree. */
358 /* Rather, the parser will grab it out of the global. */
359 if (g_parse_mode == PROGRAM) {
360 clear_doctext();
361 g_doctext = strdup(yytext + 3);
362 g_doctext[strlen(g_doctext) - 2] = '\0';
363 g_doctext = clean_up_doctext(g_doctext);
364 g_doctext_lineno = yylineno;
365 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000366}
367
Bryan Duxbury235f8b52011-08-19 18:27:47 +0000368. {
369 unexpected_token(yytext);
370}
371
ccheeverf53b5cf2007-02-05 20:33:11 +0000372
David Reissfb790d72010-09-02 16:41:45 +0000373. {
374 /* Catch-all to let us catch "*" in the parser. */
375 return (int) yytext[0];
376}
377
Mark Slee31985722006-05-24 21:45:31 +0000378%%
David Reiss4a054342009-03-26 23:32:27 +0000379
380/* vim: filetype=lex
381*/