blob: 0a048406100f7b54c1a4212def9c5ec5c50e25aa [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>
Roger Meier9212e792012-06-12 21:01:06 +000041#include <stdlib.h>
David Reissf1454162008-06-30 20:45:47 +000042
Mark Slee31985722006-05-24 21:45:31 +000043#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000044#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000045#include "parse/t_program.h"
46
Mark Sleef5377b32006-10-10 01:42:59 +000047/**
48 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
49 * because I wrote this a while ago.
50 */
Chris Piro92e38602013-03-07 11:24:51 -050051#include "thrifty.hh"
Mark Slee31985722006-05-24 21:45:31 +000052
Mark Sleef12865a2007-01-12 00:23:26 +000053void thrift_reserved_keyword(char* keyword) {
54 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
55 exit(1);
56}
57
David Reissf1454162008-06-30 20:45:47 +000058void integer_overflow(char* text) {
59 yyerror("This integer is too big: \"%s\"\n", text);
60 exit(1);
61}
62
Bryan Duxbury235f8b52011-08-19 18:27:47 +000063void unexpected_token(char* text) {
64 yyerror("Unexpected token in input: \"%s\"\n", text);
65 exit(1);
66}
67
Mark Slee31985722006-05-24 21:45:31 +000068%}
69
Mark Sleef5377b32006-10-10 01:42:59 +000070/**
71 * Provides the yylineno global, useful for debugging output
72 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000073%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000074
Mark Slee27ed6ec2007-08-16 01:26:31 +000075/**
David Reiss4563acd2010-08-31 16:51:29 +000076 * Our inputs are all single files, so no need for yywrap
77 */
78%option noyywrap
79
80/**
Christian Lavoie77215d82010-11-07 19:42:48 +000081 * We don't use it, and it fires up warnings at -Wall
82 */
83%option nounput
84
85/**
Mark Sleef5377b32006-10-10 01:42:59 +000086 * Helper definitions, comments, constants, and whatnot
87 */
88
Mark Sleebd588222007-11-21 08:43:35 +000089intconstant ([+-]?[0-9]+)
90hexconstant ("0x"[0-9A-Fa-f]+)
91dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
92identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
93whitespace ([ \t\r\n]*)
94sillycomm ("/*""*"*"*/")
95multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
96doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
97comment ("//"[^\n]*)
98unixcomment ("#"[^\n]*)
99symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Sleebd588222007-11-21 08:43:35 +0000100st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
David Reiss82e6fc02009-03-26 23:32:36 +0000101literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +0000102
103%%
104
Mark Sleebd588222007-11-21 08:43:35 +0000105{whitespace} { /* do nothing */ }
106{sillycomm} { /* do nothing */ }
107{multicomm} { /* do nothing */ }
108{comment} { /* do nothing */ }
109{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +0000110
Mark Sleebd588222007-11-21 08:43:35 +0000111{symbol} { return yytext[0]; }
Roger Meier0c3c8952011-08-22 21:38:16 +0000112"*" { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +0000113
Bryan Duxbury6c928f32011-10-13 21:32:52 +0000114"false" { yylval.iconst=0; return tok_int_constant; }
115"true" { yylval.iconst=1; return tok_int_constant; }
116
Mark Sleebd588222007-11-21 08:43:35 +0000117"namespace" { return tok_namespace; }
118"cpp_namespace" { return tok_cpp_namespace; }
119"cpp_include" { return tok_cpp_include; }
120"cpp_type" { return tok_cpp_type; }
121"java_package" { return tok_java_package; }
122"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000123"csharp_namespace" { return tok_csharp_namespace; }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000124"delphi_namespace" { return tok_delphi_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000125"php_namespace" { return tok_php_namespace; }
126"py_module" { return tok_py_module; }
127"perl_package" { return tok_perl_package; }
128"ruby_namespace" { return tok_ruby_namespace; }
129"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000130"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000131"xsd_all" { return tok_xsd_all; }
132"xsd_optional" { return tok_xsd_optional; }
133"xsd_nillable" { return tok_xsd_nillable; }
134"xsd_namespace" { return tok_xsd_namespace; }
135"xsd_attrs" { return tok_xsd_attrs; }
136"include" { return tok_include; }
137"void" { return tok_void; }
138"bool" { return tok_bool; }
139"byte" { return tok_byte; }
140"i16" { return tok_i16; }
141"i32" { return tok_i32; }
142"i64" { return tok_i64; }
143"double" { return tok_double; }
144"string" { return tok_string; }
145"binary" { return tok_binary; }
Jens Geyer0ca234f2013-06-04 22:01:47 +0200146"slist" {
147 pwarning(0, "\"slist\" is deprecated and will be removed in a future compiler version. This type should be replaced with \"string\".\n");
148 return tok_slist;
149}
Carl Yeksigianc3178522013-06-07 12:31:13 -0400150"senum" {
151 pwarning(0, "\"senum\" is deprecated and will be removed in a future compiler version. This type should be replaced with \"string\".\n");
152 return tok_senum;
153}
Mark Sleebd588222007-11-21 08:43:35 +0000154"map" { return tok_map; }
155"list" { return tok_list; }
156"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000157"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000158"typedef" { return tok_typedef; }
159"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000160"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000161"exception" { return tok_xception; }
162"extends" { return tok_extends; }
163"throws" { return tok_throws; }
164"service" { return tok_service; }
165"enum" { return tok_enum; }
166"const" { return tok_const; }
167"required" { return tok_required; }
168"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000169"async" {
170 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
171 return tok_oneway;
172}
Mark Sleef0712dc2006-10-25 19:03:57 +0000173
Mark Slee52f643d2006-08-09 00:03:43 +0000174
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000175"BEGIN" { thrift_reserved_keyword(yytext); }
176"END" { thrift_reserved_keyword(yytext); }
177"__CLASS__" { thrift_reserved_keyword(yytext); }
178"__DIR__" { thrift_reserved_keyword(yytext); }
179"__FILE__" { thrift_reserved_keyword(yytext); }
180"__FUNCTION__" { thrift_reserved_keyword(yytext); }
181"__LINE__" { thrift_reserved_keyword(yytext); }
182"__METHOD__" { thrift_reserved_keyword(yytext); }
183"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000184"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000185"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000186"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000187"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000188"as" { thrift_reserved_keyword(yytext); }
189"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000190"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000191"break" { thrift_reserved_keyword(yytext); }
192"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000193"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000194"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000195"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000196"continue" { thrift_reserved_keyword(yytext); }
197"declare" { thrift_reserved_keyword(yytext); }
198"def" { thrift_reserved_keyword(yytext); }
199"default" { thrift_reserved_keyword(yytext); }
200"del" { thrift_reserved_keyword(yytext); }
201"delete" { thrift_reserved_keyword(yytext); }
202"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000203"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000204"elif" { thrift_reserved_keyword(yytext); }
205"else" { thrift_reserved_keyword(yytext); }
206"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000207"elsif" { thrift_reserved_keyword(yytext); }
208"end" { thrift_reserved_keyword(yytext); }
209"enddeclare" { thrift_reserved_keyword(yytext); }
210"endfor" { thrift_reserved_keyword(yytext); }
211"endforeach" { thrift_reserved_keyword(yytext); }
212"endif" { thrift_reserved_keyword(yytext); }
213"endswitch" { thrift_reserved_keyword(yytext); }
214"endwhile" { thrift_reserved_keyword(yytext); }
215"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000216"except" { thrift_reserved_keyword(yytext); }
217"exec" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000218"finally" { thrift_reserved_keyword(yytext); }
219"float" { thrift_reserved_keyword(yytext); }
220"for" { thrift_reserved_keyword(yytext); }
221"foreach" { thrift_reserved_keyword(yytext); }
222"function" { thrift_reserved_keyword(yytext); }
223"global" { thrift_reserved_keyword(yytext); }
224"goto" { thrift_reserved_keyword(yytext); }
225"if" { thrift_reserved_keyword(yytext); }
226"implements" { thrift_reserved_keyword(yytext); }
227"import" { thrift_reserved_keyword(yytext); }
228"in" { thrift_reserved_keyword(yytext); }
229"inline" { thrift_reserved_keyword(yytext); }
230"instanceof" { thrift_reserved_keyword(yytext); }
231"interface" { thrift_reserved_keyword(yytext); }
232"is" { thrift_reserved_keyword(yytext); }
233"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000234"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000235"native" { thrift_reserved_keyword(yytext); }
236"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000237"next" { thrift_reserved_keyword(yytext); }
238"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000239"not" { thrift_reserved_keyword(yytext); }
240"or" { thrift_reserved_keyword(yytext); }
241"pass" { thrift_reserved_keyword(yytext); }
242"public" { thrift_reserved_keyword(yytext); }
243"print" { thrift_reserved_keyword(yytext); }
244"private" { thrift_reserved_keyword(yytext); }
245"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000246"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000247"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000248"redo" { thrift_reserved_keyword(yytext); }
249"rescue" { thrift_reserved_keyword(yytext); }
250"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000251"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000252"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000253"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000254"sizeof" { thrift_reserved_keyword(yytext); }
255"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000256"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000257"switch" { thrift_reserved_keyword(yytext); }
258"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000259"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000260"this" { thrift_reserved_keyword(yytext); }
261"throw" { thrift_reserved_keyword(yytext); }
262"transient" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000263"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000264"undef" { thrift_reserved_keyword(yytext); }
265"union" { thrift_reserved_keyword(yytext); }
266"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000267"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000268"until" { thrift_reserved_keyword(yytext); }
269"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000270"var" { thrift_reserved_keyword(yytext); }
271"virtual" { thrift_reserved_keyword(yytext); }
272"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000273"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000274"while" { thrift_reserved_keyword(yytext); }
275"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000276"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000277"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000278
Mark Slee4f8da1d2006-10-12 02:47:27 +0000279{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000280 errno = 0;
281 yylval.iconst = strtoll(yytext, NULL, 10);
282 if (errno == ERANGE) {
283 integer_overflow(yytext);
284 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000285 return tok_int_constant;
286}
Mark Sleef5377b32006-10-10 01:42:59 +0000287
Mark Slee600cdb32006-11-29 22:06:42 +0000288{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000289 errno = 0;
290 yylval.iconst = strtoll(yytext+2, NULL, 16);
291 if (errno == ERANGE) {
292 integer_overflow(yytext);
293 }
Mark Slee600cdb32006-11-29 22:06:42 +0000294 return tok_int_constant;
295}
296
Mark Slee30152872006-11-28 01:24:07 +0000297{dubconstant} {
298 yylval.dconst = atof(yytext);
299 return tok_dub_constant;
300}
301
Mark Slee4f8da1d2006-10-12 02:47:27 +0000302{identifier} {
303 yylval.id = strdup(yytext);
304 return tok_identifier;
305}
306
Mark Sleebd588222007-11-21 08:43:35 +0000307{st_identifier} {
308 yylval.id = strdup(yytext);
309 return tok_st_identifier;
310}
311
David Reiss82e6fc02009-03-26 23:32:36 +0000312{literal_begin} {
313 char mark = yytext[0];
314 std::string result;
315 for(;;)
316 {
317 int ch = yyinput();
318 switch (ch) {
319 case EOF:
320 yyerror("End of file while read string at %d\n", yylineno);
321 exit(1);
322 case '\n':
323 yyerror("End of line while read string at %d\n", yylineno - 1);
324 exit(1);
325 case '\\':
326 ch = yyinput();
327 switch (ch) {
328 case 'r':
329 result.push_back('\r');
330 continue;
331 case 'n':
332 result.push_back('\n');
333 continue;
334 case 't':
335 result.push_back('\t');
336 continue;
337 case '"':
338 result.push_back('"');
339 continue;
340 case '\'':
341 result.push_back('\'');
342 continue;
343 case '\\':
344 result.push_back('\\');
345 continue;
346 default:
347 yyerror("Bad escape character\n");
348 return -1;
349 }
350 break;
351 default:
352 if (ch == mark) {
353 yylval.id = strdup(result.c_str());
354 return tok_literal;
355 } else {
356 result.push_back(ch);
357 }
358 }
359 }
Mark Slee30152872006-11-28 01:24:07 +0000360}
361
Mark Slee31985722006-05-24 21:45:31 +0000362
ccheeverf53b5cf2007-02-05 20:33:11 +0000363{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000364 /* This does not show up in the parse tree. */
365 /* Rather, the parser will grab it out of the global. */
366 if (g_parse_mode == PROGRAM) {
367 clear_doctext();
368 g_doctext = strdup(yytext + 3);
369 g_doctext[strlen(g_doctext) - 2] = '\0';
370 g_doctext = clean_up_doctext(g_doctext);
371 g_doctext_lineno = yylineno;
372 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000373}
374
Bryan Duxbury235f8b52011-08-19 18:27:47 +0000375. {
376 unexpected_token(yytext);
377}
378
ccheeverf53b5cf2007-02-05 20:33:11 +0000379
David Reissfb790d72010-09-02 16:41:45 +0000380. {
381 /* Catch-all to let us catch "*" in the parser. */
382 return (int) yytext[0];
383}
384
Mark Slee31985722006-05-24 21:45:31 +0000385%%
David Reiss4a054342009-03-26 23:32:27 +0000386
387/* vim: filetype=lex
388*/