blob: a6229f403d2355a2a628c2bcb22562842606e68f [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
Roger Meier3b771a12010-11-17 22:11:26 +000028#pragma GCC diagnostic ignored "-Wunused-function"
29#pragma GCC diagnostic ignored "-Wunused-label"
30
David Reiss82e6fc02009-03-26 23:32:36 +000031#include <string>
David Reissf1454162008-06-30 20:45:47 +000032#include <errno.h>
33
Mark Slee31985722006-05-24 21:45:31 +000034#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000035#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000036#include "parse/t_program.h"
37
Mark Sleef5377b32006-10-10 01:42:59 +000038/**
39 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
40 * because I wrote this a while ago.
41 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000042#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000043
Mark Sleef12865a2007-01-12 00:23:26 +000044void thrift_reserved_keyword(char* keyword) {
45 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
46 exit(1);
47}
48
David Reissf1454162008-06-30 20:45:47 +000049void integer_overflow(char* text) {
50 yyerror("This integer is too big: \"%s\"\n", text);
51 exit(1);
52}
53
Mark Slee31985722006-05-24 21:45:31 +000054%}
55
Mark Sleef5377b32006-10-10 01:42:59 +000056/**
57 * Provides the yylineno global, useful for debugging output
58 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000059%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000060
Mark Slee27ed6ec2007-08-16 01:26:31 +000061/**
David Reiss4563acd2010-08-31 16:51:29 +000062 * Our inputs are all single files, so no need for yywrap
63 */
64%option noyywrap
65
66/**
Christian Lavoie77215d82010-11-07 19:42:48 +000067 * We don't use it, and it fires up warnings at -Wall
68 */
69%option nounput
70
71/**
Mark Sleef5377b32006-10-10 01:42:59 +000072 * Helper definitions, comments, constants, and whatnot
73 */
74
Mark Sleebd588222007-11-21 08:43:35 +000075intconstant ([+-]?[0-9]+)
76hexconstant ("0x"[0-9A-Fa-f]+)
77dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
78identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
79whitespace ([ \t\r\n]*)
80sillycomm ("/*""*"*"*/")
81multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
82doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
83comment ("//"[^\n]*)
84unixcomment ("#"[^\n]*)
85symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Sleebd588222007-11-21 08:43:35 +000086st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
David Reiss82e6fc02009-03-26 23:32:36 +000087literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +000088
89%%
90
Mark Sleebd588222007-11-21 08:43:35 +000091{whitespace} { /* do nothing */ }
92{sillycomm} { /* do nothing */ }
93{multicomm} { /* do nothing */ }
94{comment} { /* do nothing */ }
95{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000096
Mark Sleebd588222007-11-21 08:43:35 +000097{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +000098
Mark Sleebd588222007-11-21 08:43:35 +000099"namespace" { return tok_namespace; }
100"cpp_namespace" { return tok_cpp_namespace; }
101"cpp_include" { return tok_cpp_include; }
102"cpp_type" { return tok_cpp_type; }
103"java_package" { return tok_java_package; }
104"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +0000105"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +0000106"php_namespace" { return tok_php_namespace; }
107"py_module" { return tok_py_module; }
108"perl_package" { return tok_perl_package; }
109"ruby_namespace" { return tok_ruby_namespace; }
110"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000111"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000112"xsd_all" { return tok_xsd_all; }
113"xsd_optional" { return tok_xsd_optional; }
114"xsd_nillable" { return tok_xsd_nillable; }
115"xsd_namespace" { return tok_xsd_namespace; }
116"xsd_attrs" { return tok_xsd_attrs; }
117"include" { return tok_include; }
118"void" { return tok_void; }
119"bool" { return tok_bool; }
120"byte" { return tok_byte; }
121"i16" { return tok_i16; }
122"i32" { return tok_i32; }
123"i64" { return tok_i64; }
124"double" { return tok_double; }
125"string" { return tok_string; }
126"binary" { return tok_binary; }
127"slist" { return tok_slist; }
128"senum" { return tok_senum; }
129"map" { return tok_map; }
130"list" { return tok_list; }
131"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000132"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000133"typedef" { return tok_typedef; }
134"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000135"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000136"exception" { return tok_xception; }
137"extends" { return tok_extends; }
138"throws" { return tok_throws; }
139"service" { return tok_service; }
140"enum" { return tok_enum; }
141"const" { return tok_const; }
142"required" { return tok_required; }
143"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000144"async" {
145 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
146 return tok_oneway;
147}
Mark Sleef0712dc2006-10-25 19:03:57 +0000148
Mark Slee52f643d2006-08-09 00:03:43 +0000149
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000150"BEGIN" { thrift_reserved_keyword(yytext); }
151"END" { thrift_reserved_keyword(yytext); }
152"__CLASS__" { thrift_reserved_keyword(yytext); }
153"__DIR__" { thrift_reserved_keyword(yytext); }
154"__FILE__" { thrift_reserved_keyword(yytext); }
155"__FUNCTION__" { thrift_reserved_keyword(yytext); }
156"__LINE__" { thrift_reserved_keyword(yytext); }
157"__METHOD__" { thrift_reserved_keyword(yytext); }
158"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000159"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000160"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000161"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000162"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000163"as" { thrift_reserved_keyword(yytext); }
164"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000165"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000166"break" { thrift_reserved_keyword(yytext); }
167"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000168"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000169"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000170"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000171"continue" { thrift_reserved_keyword(yytext); }
172"declare" { thrift_reserved_keyword(yytext); }
173"def" { thrift_reserved_keyword(yytext); }
174"default" { thrift_reserved_keyword(yytext); }
175"del" { thrift_reserved_keyword(yytext); }
176"delete" { thrift_reserved_keyword(yytext); }
177"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000178"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000179"elif" { thrift_reserved_keyword(yytext); }
180"else" { thrift_reserved_keyword(yytext); }
181"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000182"elsif" { thrift_reserved_keyword(yytext); }
183"end" { thrift_reserved_keyword(yytext); }
184"enddeclare" { thrift_reserved_keyword(yytext); }
185"endfor" { thrift_reserved_keyword(yytext); }
186"endforeach" { thrift_reserved_keyword(yytext); }
187"endif" { thrift_reserved_keyword(yytext); }
188"endswitch" { thrift_reserved_keyword(yytext); }
189"endwhile" { thrift_reserved_keyword(yytext); }
190"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000191"except" { thrift_reserved_keyword(yytext); }
192"exec" { thrift_reserved_keyword(yytext); }
193"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000194"finally" { thrift_reserved_keyword(yytext); }
195"float" { thrift_reserved_keyword(yytext); }
196"for" { thrift_reserved_keyword(yytext); }
197"foreach" { thrift_reserved_keyword(yytext); }
198"function" { thrift_reserved_keyword(yytext); }
199"global" { thrift_reserved_keyword(yytext); }
200"goto" { thrift_reserved_keyword(yytext); }
201"if" { thrift_reserved_keyword(yytext); }
202"implements" { thrift_reserved_keyword(yytext); }
203"import" { thrift_reserved_keyword(yytext); }
204"in" { thrift_reserved_keyword(yytext); }
205"inline" { thrift_reserved_keyword(yytext); }
206"instanceof" { thrift_reserved_keyword(yytext); }
207"interface" { thrift_reserved_keyword(yytext); }
208"is" { thrift_reserved_keyword(yytext); }
209"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000210"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000211"native" { thrift_reserved_keyword(yytext); }
212"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000213"next" { thrift_reserved_keyword(yytext); }
214"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000215"not" { thrift_reserved_keyword(yytext); }
216"or" { thrift_reserved_keyword(yytext); }
217"pass" { thrift_reserved_keyword(yytext); }
218"public" { thrift_reserved_keyword(yytext); }
219"print" { thrift_reserved_keyword(yytext); }
220"private" { thrift_reserved_keyword(yytext); }
221"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000222"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000223"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000224"redo" { thrift_reserved_keyword(yytext); }
225"rescue" { thrift_reserved_keyword(yytext); }
226"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000227"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000228"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000229"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000230"sizeof" { thrift_reserved_keyword(yytext); }
231"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000232"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000233"switch" { thrift_reserved_keyword(yytext); }
234"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000235"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000236"this" { thrift_reserved_keyword(yytext); }
237"throw" { thrift_reserved_keyword(yytext); }
238"transient" { thrift_reserved_keyword(yytext); }
239"true" { thrift_reserved_keyword(yytext); }
240"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000241"undef" { thrift_reserved_keyword(yytext); }
242"union" { thrift_reserved_keyword(yytext); }
243"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000244"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000245"until" { thrift_reserved_keyword(yytext); }
246"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000247"var" { thrift_reserved_keyword(yytext); }
248"virtual" { thrift_reserved_keyword(yytext); }
249"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000250"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000251"while" { thrift_reserved_keyword(yytext); }
252"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000253"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000254"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000255
Mark Slee4f8da1d2006-10-12 02:47:27 +0000256{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000257 errno = 0;
258 yylval.iconst = strtoll(yytext, NULL, 10);
259 if (errno == ERANGE) {
260 integer_overflow(yytext);
261 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000262 return tok_int_constant;
263}
Mark Sleef5377b32006-10-10 01:42:59 +0000264
Mark Slee600cdb32006-11-29 22:06:42 +0000265{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000266 errno = 0;
267 yylval.iconst = strtoll(yytext+2, NULL, 16);
268 if (errno == ERANGE) {
269 integer_overflow(yytext);
270 }
Mark Slee600cdb32006-11-29 22:06:42 +0000271 return tok_int_constant;
272}
273
Mark Slee30152872006-11-28 01:24:07 +0000274{dubconstant} {
275 yylval.dconst = atof(yytext);
276 return tok_dub_constant;
277}
278
Mark Slee4f8da1d2006-10-12 02:47:27 +0000279{identifier} {
280 yylval.id = strdup(yytext);
281 return tok_identifier;
282}
283
Mark Sleebd588222007-11-21 08:43:35 +0000284{st_identifier} {
285 yylval.id = strdup(yytext);
286 return tok_st_identifier;
287}
288
David Reiss82e6fc02009-03-26 23:32:36 +0000289{literal_begin} {
290 char mark = yytext[0];
291 std::string result;
292 for(;;)
293 {
294 int ch = yyinput();
295 switch (ch) {
296 case EOF:
297 yyerror("End of file while read string at %d\n", yylineno);
298 exit(1);
299 case '\n':
300 yyerror("End of line while read string at %d\n", yylineno - 1);
301 exit(1);
302 case '\\':
303 ch = yyinput();
304 switch (ch) {
305 case 'r':
306 result.push_back('\r');
307 continue;
308 case 'n':
309 result.push_back('\n');
310 continue;
311 case 't':
312 result.push_back('\t');
313 continue;
314 case '"':
315 result.push_back('"');
316 continue;
317 case '\'':
318 result.push_back('\'');
319 continue;
320 case '\\':
321 result.push_back('\\');
322 continue;
323 default:
324 yyerror("Bad escape character\n");
325 return -1;
326 }
327 break;
328 default:
329 if (ch == mark) {
330 yylval.id = strdup(result.c_str());
331 return tok_literal;
332 } else {
333 result.push_back(ch);
334 }
335 }
336 }
Mark Slee30152872006-11-28 01:24:07 +0000337}
338
Mark Slee31985722006-05-24 21:45:31 +0000339
ccheeverf53b5cf2007-02-05 20:33:11 +0000340{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000341 /* This does not show up in the parse tree. */
342 /* Rather, the parser will grab it out of the global. */
343 if (g_parse_mode == PROGRAM) {
344 clear_doctext();
345 g_doctext = strdup(yytext + 3);
346 g_doctext[strlen(g_doctext) - 2] = '\0';
347 g_doctext = clean_up_doctext(g_doctext);
348 g_doctext_lineno = yylineno;
349 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000350}
351
352
David Reissfb790d72010-09-02 16:41:45 +0000353. {
354 /* Catch-all to let us catch "*" in the parser. */
355 return (int) yytext[0];
356}
357
Mark Slee31985722006-05-24 21:45:31 +0000358%%
David Reiss4a054342009-03-26 23:32:27 +0000359
360/* vim: filetype=lex
361*/