blob: 96d61ecf7d4ff4858268e8c21ab13dba6874b673 [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
Jens Geyer8cd3efe2013-09-16 22:17:52 +020039#include <cassert>
David Reiss82e6fc02009-03-26 23:32:36 +000040#include <string>
David Reissf1454162008-06-30 20:45:47 +000041#include <errno.h>
Roger Meier9212e792012-06-12 21:01:06 +000042#include <stdlib.h>
David Reissf1454162008-06-30 20:45:47 +000043
Mark Slee31985722006-05-24 21:45:31 +000044#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000045#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000046#include "parse/t_program.h"
47
Mark Sleef5377b32006-10-10 01:42:59 +000048/**
49 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
50 * because I wrote this a while ago.
51 */
jfarrell92f24b22013-08-17 15:47:13 -040052#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000053
Mark Sleef12865a2007-01-12 00:23:26 +000054void thrift_reserved_keyword(char* keyword) {
55 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
56 exit(1);
57}
58
David Reissf1454162008-06-30 20:45:47 +000059void integer_overflow(char* text) {
60 yyerror("This integer is too big: \"%s\"\n", text);
61 exit(1);
62}
63
Bryan Duxbury235f8b52011-08-19 18:27:47 +000064void unexpected_token(char* text) {
65 yyerror("Unexpected token in input: \"%s\"\n", text);
66 exit(1);
67}
68
Mark Slee31985722006-05-24 21:45:31 +000069%}
70
Mark Sleef5377b32006-10-10 01:42:59 +000071/**
72 * Provides the yylineno global, useful for debugging output
73 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000074%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000075
Mark Slee27ed6ec2007-08-16 01:26:31 +000076/**
David Reiss4563acd2010-08-31 16:51:29 +000077 * Our inputs are all single files, so no need for yywrap
78 */
79%option noyywrap
80
81/**
Christian Lavoie77215d82010-11-07 19:42:48 +000082 * We don't use it, and it fires up warnings at -Wall
83 */
84%option nounput
85
86/**
Mark Sleef5377b32006-10-10 01:42:59 +000087 * Helper definitions, comments, constants, and whatnot
88 */
89
Mark Sleebd588222007-11-21 08:43:35 +000090intconstant ([+-]?[0-9]+)
91hexconstant ("0x"[0-9A-Fa-f]+)
92dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
Carl Yeksigiande074082013-06-04 04:28:31 -040093identifier ([a-zA-Z_](\.[a-zA-Z_0-9]|[a-zA-Z_0-9])*)
Mark Sleebd588222007-11-21 08:43:35 +000094whitespace ([ \t\r\n]*)
95sillycomm ("/*""*"*"*/")
96multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
97doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
98comment ("//"[^\n]*)
99unixcomment ("#"[^\n]*)
100symbol ([:;\,\{\}\(\)\=<>\[\]])
Carl Yeksigiande074082013-06-04 04:28:31 -0400101st_identifier ([a-zA-Z-](\.[a-zA-Z_0-9-]|[a-zA-Z_0-9-])*)
David Reiss82e6fc02009-03-26 23:32:36 +0000102literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +0000103
104%%
105
Mark Sleebd588222007-11-21 08:43:35 +0000106{whitespace} { /* do nothing */ }
107{sillycomm} { /* do nothing */ }
108{multicomm} { /* do nothing */ }
109{comment} { /* do nothing */ }
110{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +0000111
Mark Sleebd588222007-11-21 08:43:35 +0000112{symbol} { return yytext[0]; }
Roger Meier0c3c8952011-08-22 21:38:16 +0000113"*" { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000114
Bryan Duxbury6c928f32011-10-13 21:32:52 +0000115"false" { yylval.iconst=0; return tok_int_constant; }
116"true" { yylval.iconst=1; return tok_int_constant; }
117
Mark Sleebd588222007-11-21 08:43:35 +0000118"namespace" { return tok_namespace; }
119"cpp_namespace" { return tok_cpp_namespace; }
120"cpp_include" { return tok_cpp_include; }
121"cpp_type" { return tok_cpp_type; }
122"java_package" { return tok_java_package; }
123"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000124"csharp_namespace" { return tok_csharp_namespace; }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000125"delphi_namespace" { return tok_delphi_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000126"php_namespace" { return tok_php_namespace; }
127"py_module" { return tok_py_module; }
128"perl_package" { return tok_perl_package; }
129"ruby_namespace" { return tok_ruby_namespace; }
130"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000131"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000132"xsd_all" { return tok_xsd_all; }
133"xsd_optional" { return tok_xsd_optional; }
134"xsd_nillable" { return tok_xsd_nillable; }
135"xsd_namespace" { return tok_xsd_namespace; }
136"xsd_attrs" { return tok_xsd_attrs; }
137"include" { return tok_include; }
138"void" { return tok_void; }
139"bool" { return tok_bool; }
140"byte" { return tok_byte; }
141"i16" { return tok_i16; }
142"i32" { return tok_i32; }
143"i64" { return tok_i64; }
144"double" { return tok_double; }
145"string" { return tok_string; }
146"binary" { return tok_binary; }
Jens Geyer0ca234f2013-06-04 22:01:47 +0200147"slist" {
148 pwarning(0, "\"slist\" is deprecated and will be removed in a future compiler version. This type should be replaced with \"string\".\n");
149 return tok_slist;
150}
Carl Yeksigianc3178522013-06-07 12:31:13 -0400151"senum" {
152 pwarning(0, "\"senum\" is deprecated and will be removed in a future compiler version. This type should be replaced with \"string\".\n");
153 return tok_senum;
154}
Mark Sleebd588222007-11-21 08:43:35 +0000155"map" { return tok_map; }
156"list" { return tok_list; }
157"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000158"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000159"typedef" { return tok_typedef; }
160"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000161"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000162"exception" { return tok_xception; }
163"extends" { return tok_extends; }
164"throws" { return tok_throws; }
165"service" { return tok_service; }
166"enum" { return tok_enum; }
167"const" { return tok_const; }
168"required" { return tok_required; }
169"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000170"async" {
171 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
172 return tok_oneway;
173}
Mark Sleef0712dc2006-10-25 19:03:57 +0000174
Mark Slee52f643d2006-08-09 00:03:43 +0000175
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000176"BEGIN" { thrift_reserved_keyword(yytext); }
177"END" { thrift_reserved_keyword(yytext); }
178"__CLASS__" { thrift_reserved_keyword(yytext); }
179"__DIR__" { thrift_reserved_keyword(yytext); }
180"__FILE__" { thrift_reserved_keyword(yytext); }
181"__FUNCTION__" { thrift_reserved_keyword(yytext); }
182"__LINE__" { thrift_reserved_keyword(yytext); }
183"__METHOD__" { thrift_reserved_keyword(yytext); }
184"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000185"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000186"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000187"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000188"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000189"as" { thrift_reserved_keyword(yytext); }
190"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000191"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000192"break" { thrift_reserved_keyword(yytext); }
193"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000194"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000195"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000196"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000197"continue" { thrift_reserved_keyword(yytext); }
198"declare" { thrift_reserved_keyword(yytext); }
199"def" { thrift_reserved_keyword(yytext); }
200"default" { thrift_reserved_keyword(yytext); }
201"del" { thrift_reserved_keyword(yytext); }
202"delete" { thrift_reserved_keyword(yytext); }
203"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000204"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000205"elif" { thrift_reserved_keyword(yytext); }
206"else" { thrift_reserved_keyword(yytext); }
207"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000208"elsif" { thrift_reserved_keyword(yytext); }
209"end" { thrift_reserved_keyword(yytext); }
210"enddeclare" { thrift_reserved_keyword(yytext); }
211"endfor" { thrift_reserved_keyword(yytext); }
212"endforeach" { thrift_reserved_keyword(yytext); }
213"endif" { thrift_reserved_keyword(yytext); }
214"endswitch" { thrift_reserved_keyword(yytext); }
215"endwhile" { thrift_reserved_keyword(yytext); }
216"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000217"except" { thrift_reserved_keyword(yytext); }
218"exec" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000219"finally" { thrift_reserved_keyword(yytext); }
220"float" { thrift_reserved_keyword(yytext); }
221"for" { thrift_reserved_keyword(yytext); }
222"foreach" { thrift_reserved_keyword(yytext); }
223"function" { thrift_reserved_keyword(yytext); }
224"global" { thrift_reserved_keyword(yytext); }
225"goto" { thrift_reserved_keyword(yytext); }
226"if" { thrift_reserved_keyword(yytext); }
227"implements" { thrift_reserved_keyword(yytext); }
228"import" { thrift_reserved_keyword(yytext); }
229"in" { thrift_reserved_keyword(yytext); }
230"inline" { thrift_reserved_keyword(yytext); }
231"instanceof" { thrift_reserved_keyword(yytext); }
232"interface" { thrift_reserved_keyword(yytext); }
233"is" { thrift_reserved_keyword(yytext); }
234"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000235"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000236"native" { thrift_reserved_keyword(yytext); }
237"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000238"next" { thrift_reserved_keyword(yytext); }
239"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000240"not" { thrift_reserved_keyword(yytext); }
241"or" { thrift_reserved_keyword(yytext); }
242"pass" { thrift_reserved_keyword(yytext); }
243"public" { thrift_reserved_keyword(yytext); }
244"print" { thrift_reserved_keyword(yytext); }
245"private" { thrift_reserved_keyword(yytext); }
246"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000247"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000248"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000249"redo" { thrift_reserved_keyword(yytext); }
250"rescue" { thrift_reserved_keyword(yytext); }
251"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000252"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000253"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000254"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000255"sizeof" { thrift_reserved_keyword(yytext); }
256"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000257"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000258"switch" { thrift_reserved_keyword(yytext); }
259"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000260"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000261"this" { thrift_reserved_keyword(yytext); }
262"throw" { thrift_reserved_keyword(yytext); }
263"transient" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000264"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000265"undef" { thrift_reserved_keyword(yytext); }
266"union" { thrift_reserved_keyword(yytext); }
267"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000268"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000269"until" { thrift_reserved_keyword(yytext); }
270"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000271"var" { thrift_reserved_keyword(yytext); }
272"virtual" { thrift_reserved_keyword(yytext); }
273"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000274"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000275"while" { thrift_reserved_keyword(yytext); }
276"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000277"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000278"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000279
Mark Slee4f8da1d2006-10-12 02:47:27 +0000280{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000281 errno = 0;
282 yylval.iconst = strtoll(yytext, NULL, 10);
283 if (errno == ERANGE) {
284 integer_overflow(yytext);
285 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000286 return tok_int_constant;
287}
Mark Sleef5377b32006-10-10 01:42:59 +0000288
Mark Slee600cdb32006-11-29 22:06:42 +0000289{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000290 errno = 0;
291 yylval.iconst = strtoll(yytext+2, NULL, 16);
292 if (errno == ERANGE) {
293 integer_overflow(yytext);
294 }
Mark Slee600cdb32006-11-29 22:06:42 +0000295 return tok_int_constant;
296}
297
Mark Slee30152872006-11-28 01:24:07 +0000298{dubconstant} {
299 yylval.dconst = atof(yytext);
300 return tok_dub_constant;
301}
302
Mark Slee4f8da1d2006-10-12 02:47:27 +0000303{identifier} {
304 yylval.id = strdup(yytext);
305 return tok_identifier;
306}
307
Mark Sleebd588222007-11-21 08:43:35 +0000308{st_identifier} {
309 yylval.id = strdup(yytext);
310 return tok_st_identifier;
311}
312
David Reiss82e6fc02009-03-26 23:32:36 +0000313{literal_begin} {
314 char mark = yytext[0];
315 std::string result;
316 for(;;)
317 {
318 int ch = yyinput();
319 switch (ch) {
320 case EOF:
321 yyerror("End of file while read string at %d\n", yylineno);
322 exit(1);
323 case '\n':
324 yyerror("End of line while read string at %d\n", yylineno - 1);
325 exit(1);
326 case '\\':
327 ch = yyinput();
328 switch (ch) {
329 case 'r':
330 result.push_back('\r');
331 continue;
332 case 'n':
333 result.push_back('\n');
334 continue;
335 case 't':
336 result.push_back('\t');
337 continue;
338 case '"':
339 result.push_back('"');
340 continue;
341 case '\'':
342 result.push_back('\'');
343 continue;
344 case '\\':
345 result.push_back('\\');
346 continue;
347 default:
348 yyerror("Bad escape character\n");
349 return -1;
350 }
351 break;
352 default:
353 if (ch == mark) {
354 yylval.id = strdup(result.c_str());
355 return tok_literal;
356 } else {
357 result.push_back(ch);
358 }
359 }
360 }
Mark Slee30152872006-11-28 01:24:07 +0000361}
362
Mark Slee31985722006-05-24 21:45:31 +0000363
ccheeverf53b5cf2007-02-05 20:33:11 +0000364{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000365 /* This does not show up in the parse tree. */
366 /* Rather, the parser will grab it out of the global. */
367 if (g_parse_mode == PROGRAM) {
368 clear_doctext();
369 g_doctext = strdup(yytext + 3);
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200370 assert(strlen(g_doctext) >= 2);
371 g_doctext[strlen(g_doctext) - 2] = ' ';
372 g_doctext[strlen(g_doctext) - 1] = '\0';
David Reisscbd4bac2007-08-14 17:12:33 +0000373 g_doctext = clean_up_doctext(g_doctext);
374 g_doctext_lineno = yylineno;
375 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000376}
377
Bryan Duxbury235f8b52011-08-19 18:27:47 +0000378. {
379 unexpected_token(yytext);
380}
381
ccheeverf53b5cf2007-02-05 20:33:11 +0000382
David Reissfb790d72010-09-02 16:41:45 +0000383. {
384 /* Catch-all to let us catch "*" in the parser. */
385 return (int) yytext[0];
386}
387
Mark Slee31985722006-05-24 21:45:31 +0000388%%
David Reiss4a054342009-03-26 23:32:27 +0000389
390/* vim: filetype=lex
391*/