blob: 0b4e9606628c73a9e31ee23078103d53f3903b04 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
Mark Sleee9ce01c2007-05-16 02:29:53 +00002 * Copyright (c) 2006- Facebook
3 * Distributed under the Thrift Software License
4 *
5 * See accompanying file LICENSE or visit the Thrift site at:
6 * http://developers.facebook.com/thrift/
7 */
8
9/**
Mark Slee31985722006-05-24 21:45:31 +000010 * Thrift scanner.
Mark Slee27ed6ec2007-08-16 01:26:31 +000011 *
Mark Slee31985722006-05-24 21:45:31 +000012 * Tokenizes a thrift definition file.
Mark Slee31985722006-05-24 21:45:31 +000013 */
Mark Sleef5377b32006-10-10 01:42:59 +000014
Mark Slee31985722006-05-24 21:45:31 +000015%{
16
David Reissf1454162008-06-30 20:45:47 +000017#include <errno.h>
18
Mark Slee31985722006-05-24 21:45:31 +000019#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000020#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000021#include "parse/t_program.h"
22
Mark Sleef5377b32006-10-10 01:42:59 +000023/**
24 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
25 * because I wrote this a while ago.
26 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000027#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000028
Mark Sleef12865a2007-01-12 00:23:26 +000029void thrift_reserved_keyword(char* keyword) {
30 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
31 exit(1);
32}
33
David Reissf1454162008-06-30 20:45:47 +000034void integer_overflow(char* text) {
35 yyerror("This integer is too big: \"%s\"\n", text);
36 exit(1);
37}
38
Mark Slee31985722006-05-24 21:45:31 +000039%}
40
Mark Sleef5377b32006-10-10 01:42:59 +000041/**
42 * Provides the yylineno global, useful for debugging output
43 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000044%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000045
Mark Slee27ed6ec2007-08-16 01:26:31 +000046/**
Mark Sleef5377b32006-10-10 01:42:59 +000047 * Helper definitions, comments, constants, and whatnot
48 */
49
Mark Sleebd588222007-11-21 08:43:35 +000050intconstant ([+-]?[0-9]+)
51hexconstant ("0x"[0-9A-Fa-f]+)
52dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
53identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
54whitespace ([ \t\r\n]*)
55sillycomm ("/*""*"*"*/")
56multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
57doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
58comment ("//"[^\n]*)
59unixcomment ("#"[^\n]*)
60symbol ([:;\,\{\}\(\)\=<>\[\]])
61dliteral ("\""[^"]*"\"")
62sliteral ("'"[^']*"'")
63st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
ccheeverf53b5cf2007-02-05 20:33:11 +000064
Mark Slee31985722006-05-24 21:45:31 +000065
66%%
67
Mark Sleebd588222007-11-21 08:43:35 +000068{whitespace} { /* do nothing */ }
69{sillycomm} { /* do nothing */ }
70{multicomm} { /* do nothing */ }
71{comment} { /* do nothing */ }
72{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000073
Mark Sleebd588222007-11-21 08:43:35 +000074{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +000075
Mark Sleebd588222007-11-21 08:43:35 +000076"namespace" { return tok_namespace; }
77"cpp_namespace" { return tok_cpp_namespace; }
78"cpp_include" { return tok_cpp_include; }
79"cpp_type" { return tok_cpp_type; }
80"java_package" { return tok_java_package; }
81"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +000082"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +000083"php_namespace" { return tok_php_namespace; }
84"py_module" { return tok_py_module; }
85"perl_package" { return tok_perl_package; }
86"ruby_namespace" { return tok_ruby_namespace; }
87"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +000088"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +000089"xsd_all" { return tok_xsd_all; }
90"xsd_optional" { return tok_xsd_optional; }
91"xsd_nillable" { return tok_xsd_nillable; }
92"xsd_namespace" { return tok_xsd_namespace; }
93"xsd_attrs" { return tok_xsd_attrs; }
94"include" { return tok_include; }
95"void" { return tok_void; }
96"bool" { return tok_bool; }
97"byte" { return tok_byte; }
98"i16" { return tok_i16; }
99"i32" { return tok_i32; }
100"i64" { return tok_i64; }
101"double" { return tok_double; }
102"string" { return tok_string; }
103"binary" { return tok_binary; }
104"slist" { return tok_slist; }
105"senum" { return tok_senum; }
106"map" { return tok_map; }
107"list" { return tok_list; }
108"set" { return tok_set; }
David Reisscecbed82009-03-24 20:02:22 +0000109"oneway" { return tok_oneway; }
Mark Sleebd588222007-11-21 08:43:35 +0000110"typedef" { return tok_typedef; }
111"struct" { return tok_struct; }
112"exception" { return tok_xception; }
113"extends" { return tok_extends; }
114"throws" { return tok_throws; }
115"service" { return tok_service; }
116"enum" { return tok_enum; }
117"const" { return tok_const; }
118"required" { return tok_required; }
119"optional" { return tok_optional; }
David Reisscecbed82009-03-24 20:02:22 +0000120"async" {
121 pwarning(0, "\"async\" is deprecated. It is called \"oneway\" now.\n");
122 return tok_oneway;
123}
Mark Sleef0712dc2006-10-25 19:03:57 +0000124
Mark Slee52f643d2006-08-09 00:03:43 +0000125
Mark Sleebd588222007-11-21 08:43:35 +0000126"abstract" { thrift_reserved_keyword(yytext); }
127"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000128"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000129"as" { thrift_reserved_keyword(yytext); }
130"assert" { thrift_reserved_keyword(yytext); }
131"break" { thrift_reserved_keyword(yytext); }
132"case" { thrift_reserved_keyword(yytext); }
133"class" { thrift_reserved_keyword(yytext); }
134"continue" { thrift_reserved_keyword(yytext); }
135"declare" { thrift_reserved_keyword(yytext); }
136"def" { thrift_reserved_keyword(yytext); }
137"default" { thrift_reserved_keyword(yytext); }
138"del" { thrift_reserved_keyword(yytext); }
139"delete" { thrift_reserved_keyword(yytext); }
140"do" { thrift_reserved_keyword(yytext); }
141"elif" { thrift_reserved_keyword(yytext); }
142"else" { thrift_reserved_keyword(yytext); }
143"elseif" { thrift_reserved_keyword(yytext); }
144"except" { thrift_reserved_keyword(yytext); }
145"exec" { thrift_reserved_keyword(yytext); }
146"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000147"finally" { thrift_reserved_keyword(yytext); }
148"float" { thrift_reserved_keyword(yytext); }
149"for" { thrift_reserved_keyword(yytext); }
150"foreach" { thrift_reserved_keyword(yytext); }
151"function" { thrift_reserved_keyword(yytext); }
152"global" { thrift_reserved_keyword(yytext); }
153"goto" { thrift_reserved_keyword(yytext); }
154"if" { thrift_reserved_keyword(yytext); }
155"implements" { thrift_reserved_keyword(yytext); }
156"import" { thrift_reserved_keyword(yytext); }
157"in" { thrift_reserved_keyword(yytext); }
158"inline" { thrift_reserved_keyword(yytext); }
159"instanceof" { thrift_reserved_keyword(yytext); }
160"interface" { thrift_reserved_keyword(yytext); }
161"is" { thrift_reserved_keyword(yytext); }
162"lambda" { thrift_reserved_keyword(yytext); }
163"native" { thrift_reserved_keyword(yytext); }
164"new" { thrift_reserved_keyword(yytext); }
165"not" { thrift_reserved_keyword(yytext); }
166"or" { thrift_reserved_keyword(yytext); }
167"pass" { thrift_reserved_keyword(yytext); }
168"public" { thrift_reserved_keyword(yytext); }
169"print" { thrift_reserved_keyword(yytext); }
170"private" { thrift_reserved_keyword(yytext); }
171"protected" { thrift_reserved_keyword(yytext); }
172"raise" { thrift_reserved_keyword(yytext); }
173"return" { thrift_reserved_keyword(yytext); }
174"sizeof" { thrift_reserved_keyword(yytext); }
175"static" { thrift_reserved_keyword(yytext); }
176"switch" { thrift_reserved_keyword(yytext); }
177"synchronized" { thrift_reserved_keyword(yytext); }
178"this" { thrift_reserved_keyword(yytext); }
179"throw" { thrift_reserved_keyword(yytext); }
180"transient" { thrift_reserved_keyword(yytext); }
181"true" { thrift_reserved_keyword(yytext); }
182"try" { thrift_reserved_keyword(yytext); }
183"unsigned" { thrift_reserved_keyword(yytext); }
184"var" { thrift_reserved_keyword(yytext); }
185"virtual" { thrift_reserved_keyword(yytext); }
186"volatile" { thrift_reserved_keyword(yytext); }
187"while" { thrift_reserved_keyword(yytext); }
188"with" { thrift_reserved_keyword(yytext); }
189"union" { thrift_reserved_keyword(yytext); }
190"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000191
Mark Slee4f8da1d2006-10-12 02:47:27 +0000192{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000193 errno = 0;
194 yylval.iconst = strtoll(yytext, NULL, 10);
195 if (errno == ERANGE) {
196 integer_overflow(yytext);
197 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000198 return tok_int_constant;
199}
Mark Sleef5377b32006-10-10 01:42:59 +0000200
Mark Slee600cdb32006-11-29 22:06:42 +0000201{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000202 errno = 0;
203 yylval.iconst = strtoll(yytext+2, NULL, 16);
204 if (errno == ERANGE) {
205 integer_overflow(yytext);
206 }
Mark Slee600cdb32006-11-29 22:06:42 +0000207 return tok_int_constant;
208}
209
Mark Slee30152872006-11-28 01:24:07 +0000210{dubconstant} {
211 yylval.dconst = atof(yytext);
212 return tok_dub_constant;
213}
214
Mark Slee4f8da1d2006-10-12 02:47:27 +0000215{identifier} {
216 yylval.id = strdup(yytext);
217 return tok_identifier;
218}
219
Mark Sleebd588222007-11-21 08:43:35 +0000220{st_identifier} {
221 yylval.id = strdup(yytext);
222 return tok_st_identifier;
223}
224
Mark Slee30152872006-11-28 01:24:07 +0000225{dliteral} {
226 yylval.id = strdup(yytext+1);
227 yylval.id[strlen(yylval.id)-1] = '\0';
228 return tok_literal;
229}
230
231{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 yylval.id = strdup(yytext+1);
233 yylval.id[strlen(yylval.id)-1] = '\0';
234 return tok_literal;
235}
Mark Slee31985722006-05-24 21:45:31 +0000236
ccheeverf53b5cf2007-02-05 20:33:11 +0000237{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000238 /* This does not show up in the parse tree. */
239 /* Rather, the parser will grab it out of the global. */
240 if (g_parse_mode == PROGRAM) {
241 clear_doctext();
242 g_doctext = strdup(yytext + 3);
243 g_doctext[strlen(g_doctext) - 2] = '\0';
244 g_doctext = clean_up_doctext(g_doctext);
245 g_doctext_lineno = yylineno;
246 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000247}
248
249
Mark Slee31985722006-05-24 21:45:31 +0000250%%
David Reiss4a054342009-03-26 23:32:27 +0000251
252/* vim: filetype=lex
253*/