blob: 84e02bb5d79ce9b005efc2044031f79f9919ee64 [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
David Reiss82e6fc02009-03-26 23:32:36 +000028#include <string>
David Reissf1454162008-06-30 20:45:47 +000029#include <errno.h>
30
Mark Slee31985722006-05-24 21:45:31 +000031#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000032#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000033#include "parse/t_program.h"
34
Mark Sleef5377b32006-10-10 01:42:59 +000035/**
36 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
37 * because I wrote this a while ago.
38 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000039#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000040
Mark Sleef12865a2007-01-12 00:23:26 +000041void thrift_reserved_keyword(char* keyword) {
42 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
43 exit(1);
44}
45
David Reissf1454162008-06-30 20:45:47 +000046void integer_overflow(char* text) {
47 yyerror("This integer is too big: \"%s\"\n", text);
48 exit(1);
49}
50
Mark Slee31985722006-05-24 21:45:31 +000051%}
52
Mark Sleef5377b32006-10-10 01:42:59 +000053/**
54 * Provides the yylineno global, useful for debugging output
55 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000056%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000057
Mark Slee27ed6ec2007-08-16 01:26:31 +000058/**
David Reiss4563acd2010-08-31 16:51:29 +000059 * Our inputs are all single files, so no need for yywrap
60 */
61%option noyywrap
62
63/**
Mark Sleef5377b32006-10-10 01:42:59 +000064 * Helper definitions, comments, constants, and whatnot
65 */
66
Mark Sleebd588222007-11-21 08:43:35 +000067intconstant ([+-]?[0-9]+)
68hexconstant ("0x"[0-9A-Fa-f]+)
69dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
70identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
71whitespace ([ \t\r\n]*)
72sillycomm ("/*""*"*"*/")
73multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
74doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
75comment ("//"[^\n]*)
76unixcomment ("#"[^\n]*)
77symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Sleebd588222007-11-21 08:43:35 +000078st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
David Reiss82e6fc02009-03-26 23:32:36 +000079literal_begin (['\"])
Mark Slee31985722006-05-24 21:45:31 +000080
81%%
82
Mark Sleebd588222007-11-21 08:43:35 +000083{whitespace} { /* do nothing */ }
84{sillycomm} { /* do nothing */ }
85{multicomm} { /* do nothing */ }
86{comment} { /* do nothing */ }
87{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000088
Mark Sleebd588222007-11-21 08:43:35 +000089{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +000090
Mark Sleebd588222007-11-21 08:43:35 +000091"namespace" { return tok_namespace; }
92"cpp_namespace" { return tok_cpp_namespace; }
93"cpp_include" { return tok_cpp_include; }
94"cpp_type" { return tok_cpp_type; }
95"java_package" { return tok_java_package; }
96"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +000097"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +000098"php_namespace" { return tok_php_namespace; }
99"py_module" { return tok_py_module; }
100"perl_package" { return tok_perl_package; }
101"ruby_namespace" { return tok_ruby_namespace; }
102"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +0000103"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +0000104"xsd_all" { return tok_xsd_all; }
105"xsd_optional" { return tok_xsd_optional; }
106"xsd_nillable" { return tok_xsd_nillable; }
107"xsd_namespace" { return tok_xsd_namespace; }
108"xsd_attrs" { return tok_xsd_attrs; }
109"include" { return tok_include; }
110"void" { return tok_void; }
111"bool" { return tok_bool; }
112"byte" { return tok_byte; }
113"i16" { return tok_i16; }
114"i32" { return tok_i32; }
115"i64" { return tok_i64; }
116"double" { return tok_double; }
117"string" { return tok_string; }
118"binary" { return tok_binary; }
119"slist" { return tok_slist; }
120"senum" { return tok_senum; }
121"map" { return tok_map; }
122"list" { return tok_list; }
123"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000124"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000125"typedef" { return tok_typedef; }
126"struct" { return tok_struct; }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000127"union" { return tok_union; }
Mark Sleebd588222007-11-21 08:43:35 +0000128"exception" { return tok_xception; }
129"extends" { return tok_extends; }
130"throws" { return tok_throws; }
131"service" { return tok_service; }
132"enum" { return tok_enum; }
133"const" { return tok_const; }
134"required" { return tok_required; }
135"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000136"async" {
137 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
138 return tok_oneway;
139}
Mark Sleef0712dc2006-10-25 19:03:57 +0000140
Mark Slee52f643d2006-08-09 00:03:43 +0000141
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000142"BEGIN" { thrift_reserved_keyword(yytext); }
143"END" { thrift_reserved_keyword(yytext); }
144"__CLASS__" { thrift_reserved_keyword(yytext); }
145"__DIR__" { thrift_reserved_keyword(yytext); }
146"__FILE__" { thrift_reserved_keyword(yytext); }
147"__FUNCTION__" { thrift_reserved_keyword(yytext); }
148"__LINE__" { thrift_reserved_keyword(yytext); }
149"__METHOD__" { thrift_reserved_keyword(yytext); }
150"__NAMESPACE__" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000151"abstract" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000152"alias" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000153"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000154"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000155"as" { thrift_reserved_keyword(yytext); }
156"assert" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000157"begin" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000158"break" { thrift_reserved_keyword(yytext); }
159"case" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000160"catch" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000161"class" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000162"clone" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000163"continue" { thrift_reserved_keyword(yytext); }
164"declare" { thrift_reserved_keyword(yytext); }
165"def" { thrift_reserved_keyword(yytext); }
166"default" { thrift_reserved_keyword(yytext); }
167"del" { thrift_reserved_keyword(yytext); }
168"delete" { thrift_reserved_keyword(yytext); }
169"do" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000170"dynamic" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000171"elif" { thrift_reserved_keyword(yytext); }
172"else" { thrift_reserved_keyword(yytext); }
173"elseif" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000174"elsif" { thrift_reserved_keyword(yytext); }
175"end" { thrift_reserved_keyword(yytext); }
176"enddeclare" { thrift_reserved_keyword(yytext); }
177"endfor" { thrift_reserved_keyword(yytext); }
178"endforeach" { thrift_reserved_keyword(yytext); }
179"endif" { thrift_reserved_keyword(yytext); }
180"endswitch" { thrift_reserved_keyword(yytext); }
181"endwhile" { thrift_reserved_keyword(yytext); }
182"ensure" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000183"except" { thrift_reserved_keyword(yytext); }
184"exec" { thrift_reserved_keyword(yytext); }
185"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000186"finally" { thrift_reserved_keyword(yytext); }
187"float" { thrift_reserved_keyword(yytext); }
188"for" { thrift_reserved_keyword(yytext); }
189"foreach" { thrift_reserved_keyword(yytext); }
190"function" { thrift_reserved_keyword(yytext); }
191"global" { thrift_reserved_keyword(yytext); }
192"goto" { thrift_reserved_keyword(yytext); }
193"if" { thrift_reserved_keyword(yytext); }
194"implements" { thrift_reserved_keyword(yytext); }
195"import" { thrift_reserved_keyword(yytext); }
196"in" { thrift_reserved_keyword(yytext); }
197"inline" { thrift_reserved_keyword(yytext); }
198"instanceof" { thrift_reserved_keyword(yytext); }
199"interface" { thrift_reserved_keyword(yytext); }
200"is" { thrift_reserved_keyword(yytext); }
201"lambda" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000202"module" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000203"native" { thrift_reserved_keyword(yytext); }
204"new" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000205"next" { thrift_reserved_keyword(yytext); }
206"nil" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000207"not" { thrift_reserved_keyword(yytext); }
208"or" { thrift_reserved_keyword(yytext); }
209"pass" { thrift_reserved_keyword(yytext); }
210"public" { thrift_reserved_keyword(yytext); }
211"print" { thrift_reserved_keyword(yytext); }
212"private" { thrift_reserved_keyword(yytext); }
213"protected" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000214"public" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000215"raise" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000216"redo" { thrift_reserved_keyword(yytext); }
217"rescue" { thrift_reserved_keyword(yytext); }
218"retry" { thrift_reserved_keyword(yytext); }
Mark Sleef5a0b3d2009-08-13 19:21:40 +0000219"register" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000220"return" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000221"self" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000222"sizeof" { thrift_reserved_keyword(yytext); }
223"static" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000224"super" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000225"switch" { thrift_reserved_keyword(yytext); }
226"synchronized" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000227"then" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000228"this" { thrift_reserved_keyword(yytext); }
229"throw" { thrift_reserved_keyword(yytext); }
230"transient" { thrift_reserved_keyword(yytext); }
231"true" { thrift_reserved_keyword(yytext); }
232"try" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000233"undef" { thrift_reserved_keyword(yytext); }
234"union" { thrift_reserved_keyword(yytext); }
235"unless" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000236"unsigned" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000237"until" { thrift_reserved_keyword(yytext); }
238"use" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000239"var" { thrift_reserved_keyword(yytext); }
240"virtual" { thrift_reserved_keyword(yytext); }
241"volatile" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000242"when" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000243"while" { thrift_reserved_keyword(yytext); }
244"with" { thrift_reserved_keyword(yytext); }
Bryan Duxbury7f3285e2010-08-05 23:28:14 +0000245"xor" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000246"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000247
Mark Slee4f8da1d2006-10-12 02:47:27 +0000248{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000249 errno = 0;
250 yylval.iconst = strtoll(yytext, NULL, 10);
251 if (errno == ERANGE) {
252 integer_overflow(yytext);
253 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000254 return tok_int_constant;
255}
Mark Sleef5377b32006-10-10 01:42:59 +0000256
Mark Slee600cdb32006-11-29 22:06:42 +0000257{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000258 errno = 0;
259 yylval.iconst = strtoll(yytext+2, NULL, 16);
260 if (errno == ERANGE) {
261 integer_overflow(yytext);
262 }
Mark Slee600cdb32006-11-29 22:06:42 +0000263 return tok_int_constant;
264}
265
Mark Slee30152872006-11-28 01:24:07 +0000266{dubconstant} {
267 yylval.dconst = atof(yytext);
268 return tok_dub_constant;
269}
270
Mark Slee4f8da1d2006-10-12 02:47:27 +0000271{identifier} {
272 yylval.id = strdup(yytext);
273 return tok_identifier;
274}
275
Mark Sleebd588222007-11-21 08:43:35 +0000276{st_identifier} {
277 yylval.id = strdup(yytext);
278 return tok_st_identifier;
279}
280
David Reiss82e6fc02009-03-26 23:32:36 +0000281{literal_begin} {
282 char mark = yytext[0];
283 std::string result;
284 for(;;)
285 {
286 int ch = yyinput();
287 switch (ch) {
288 case EOF:
289 yyerror("End of file while read string at %d\n", yylineno);
290 exit(1);
291 case '\n':
292 yyerror("End of line while read string at %d\n", yylineno - 1);
293 exit(1);
294 case '\\':
295 ch = yyinput();
296 switch (ch) {
297 case 'r':
298 result.push_back('\r');
299 continue;
300 case 'n':
301 result.push_back('\n');
302 continue;
303 case 't':
304 result.push_back('\t');
305 continue;
306 case '"':
307 result.push_back('"');
308 continue;
309 case '\'':
310 result.push_back('\'');
311 continue;
312 case '\\':
313 result.push_back('\\');
314 continue;
315 default:
316 yyerror("Bad escape character\n");
317 return -1;
318 }
319 break;
320 default:
321 if (ch == mark) {
322 yylval.id = strdup(result.c_str());
323 return tok_literal;
324 } else {
325 result.push_back(ch);
326 }
327 }
328 }
Mark Slee30152872006-11-28 01:24:07 +0000329}
330
Mark Slee31985722006-05-24 21:45:31 +0000331
ccheeverf53b5cf2007-02-05 20:33:11 +0000332{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000333 /* This does not show up in the parse tree. */
334 /* Rather, the parser will grab it out of the global. */
335 if (g_parse_mode == PROGRAM) {
336 clear_doctext();
337 g_doctext = strdup(yytext + 3);
338 g_doctext[strlen(g_doctext) - 2] = '\0';
339 g_doctext = clean_up_doctext(g_doctext);
340 g_doctext_lineno = yylineno;
341 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000342}
343
344
Mark Slee31985722006-05-24 21:45:31 +0000345%%
David Reiss4a054342009-03-26 23:32:27 +0000346
347/* vim: filetype=lex
348*/