blob: fbad955d742c418f1b8a542f4c025278afe8b036 [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.
13 * @author Mark Slee <mcslee@facebook.com>
14 */
Mark Sleef5377b32006-10-10 01:42:59 +000015
Mark Slee31985722006-05-24 21:45:31 +000016%{
17
18#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000019#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000020#include "parse/t_program.h"
21
Mark Sleef5377b32006-10-10 01:42:59 +000022/**
23 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
24 * because I wrote this a while ago.
25 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000026#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000027
Mark Sleef12865a2007-01-12 00:23:26 +000028void thrift_reserved_keyword(char* keyword) {
29 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
30 exit(1);
31}
32
Mark Slee31985722006-05-24 21:45:31 +000033%}
34
Mark Sleef5377b32006-10-10 01:42:59 +000035/**
36 * Provides the yylineno global, useful for debugging output
37 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000038%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000039
Mark Slee27ed6ec2007-08-16 01:26:31 +000040/**
Mark Sleef5377b32006-10-10 01:42:59 +000041 * Helper definitions, comments, constants, and whatnot
42 */
43
Mark Slee30152872006-11-28 01:24:07 +000044intconstant ([+-]?[0-9]+)
Mark Slee600cdb32006-11-29 22:06:42 +000045hexconstant ("0x"[0-9A-Fa-f]+)
Mark Slee30152872006-11-28 01:24:07 +000046dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
Mark Slee9cb7c612006-09-01 22:17:45 +000047identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
Mark Slee31985722006-05-24 21:45:31 +000048whitespace ([ \t\r\n]*)
David Reiss1ac05802007-07-30 22:00:27 +000049sillycomm ("/*""*"*"*/")
50multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
51doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
Mark Slee31985722006-05-24 21:45:31 +000052comment ("//"[^\n]*)
Mark Sleec98d0502006-09-06 02:42:25 +000053unixcomment ("#"[^\n]*)
Mark Sleeae2bc3c2006-11-08 23:44:59 +000054symbol ([:;\,\{\}\(\)\=<>\[\]])
ccheeverf53b5cf2007-02-05 20:33:11 +000055dliteral ("\""[^"]*"\"")
56sliteral ("'"[^']*"'")
57
Mark Slee31985722006-05-24 21:45:31 +000058
59%%
60
61{whitespace} { /* do nothing */ }
David Reiss1ac05802007-07-30 22:00:27 +000062{sillycomm} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000063{multicomm} { /* do nothing */ }
64{comment} { /* do nothing */ }
Mark Sleec98d0502006-09-06 02:42:25 +000065{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000066
Mark Slee9cb7c612006-09-01 22:17:45 +000067{symbol} { return yytext[0]; }
68
Mark Slee58dfb4f2007-07-06 02:45:25 +000069"namespace" { return tok_namespace; }
70"cpp_namespace" { return tok_cpp_namespace; }
71"cpp_include" { return tok_cpp_include; }
72"cpp_type" { return tok_cpp_type; }
73"java_package" { return tok_java_package; }
74"php_namespace" { return tok_php_namespace; }
David Reissc6fc3292007-08-30 00:58:43 +000075"py_module" { return tok_py_module; }
Mark Slee27ed6ec2007-08-16 01:26:31 +000076"perl_package" { return tok_perl_package; }
Mark Slee58dfb4f2007-07-06 02:45:25 +000077"ruby_namespace" { return tok_ruby_namespace; }
78"xsd_all" { return tok_xsd_all; }
79"xsd_optional" { return tok_xsd_optional; }
80"xsd_nillable" { return tok_xsd_nillable; }
81"xsd_namespace" { return tok_xsd_namespace; }
82"xsd_attrs" { return tok_xsd_attrs; }
83"include" { return tok_include; }
Mark Sleef0712dc2006-10-25 19:03:57 +000084
Mark Slee58dfb4f2007-07-06 02:45:25 +000085"void" { return tok_void; }
86"bool" { return tok_bool; }
87"byte" { return tok_byte; }
88"i16" { return tok_i16; }
89"i32" { return tok_i32; }
90"i64" { return tok_i64; }
91"double" { return tok_double; }
92"string" { return tok_string; }
93"binary" { return tok_binary; }
94"slist" { return tok_slist; }
95"senum" { return tok_senum; }
96"map" { return tok_map; }
97"list" { return tok_list; }
98"set" { return tok_set; }
99"async" { return tok_async; }
100"typedef" { return tok_typedef; }
101"struct" { return tok_struct; }
102"exception" { return tok_xception; }
103"extends" { return tok_extends; }
104"throws" { return tok_throws; }
105"service" { return tok_service; }
106"enum" { return tok_enum; }
107"const" { return tok_const; }
David Reiss8320a922007-08-14 19:59:26 +0000108"required" { return tok_required; }
109"optional" { return tok_optional; }
Mark Slee52f643d2006-08-09 00:03:43 +0000110
Mark Sleef12865a2007-01-12 00:23:26 +0000111"abstract" { thrift_reserved_keyword(yytext); }
112"and" { thrift_reserved_keyword(yytext); }
113"as" { thrift_reserved_keyword(yytext); }
114"assert" { thrift_reserved_keyword(yytext); }
115"break" { thrift_reserved_keyword(yytext); }
116"case" { thrift_reserved_keyword(yytext); }
117"class" { thrift_reserved_keyword(yytext); }
118"continue" { thrift_reserved_keyword(yytext); }
119"declare" { thrift_reserved_keyword(yytext); }
120"def" { thrift_reserved_keyword(yytext); }
121"default" { thrift_reserved_keyword(yytext); }
122"del" { thrift_reserved_keyword(yytext); }
123"delete" { thrift_reserved_keyword(yytext); }
124"do" { thrift_reserved_keyword(yytext); }
125"elif" { thrift_reserved_keyword(yytext); }
126"else" { thrift_reserved_keyword(yytext); }
127"elseif" { thrift_reserved_keyword(yytext); }
128"except" { thrift_reserved_keyword(yytext); }
129"exec" { thrift_reserved_keyword(yytext); }
130"false" { thrift_reserved_keyword(yytext); }
131"final" { thrift_reserved_keyword(yytext); }
132"finally" { thrift_reserved_keyword(yytext); }
133"float" { thrift_reserved_keyword(yytext); }
134"for" { thrift_reserved_keyword(yytext); }
135"foreach" { thrift_reserved_keyword(yytext); }
136"function" { thrift_reserved_keyword(yytext); }
137"global" { thrift_reserved_keyword(yytext); }
138"goto" { thrift_reserved_keyword(yytext); }
139"if" { thrift_reserved_keyword(yytext); }
140"implements" { thrift_reserved_keyword(yytext); }
141"import" { thrift_reserved_keyword(yytext); }
142"in" { thrift_reserved_keyword(yytext); }
143"inline" { thrift_reserved_keyword(yytext); }
144"instanceof" { thrift_reserved_keyword(yytext); }
145"interface" { thrift_reserved_keyword(yytext); }
146"is" { thrift_reserved_keyword(yytext); }
147"lambda" { thrift_reserved_keyword(yytext); }
148"native" { thrift_reserved_keyword(yytext); }
149"new" { thrift_reserved_keyword(yytext); }
150"not" { thrift_reserved_keyword(yytext); }
151"or" { thrift_reserved_keyword(yytext); }
152"pass" { thrift_reserved_keyword(yytext); }
153"public" { thrift_reserved_keyword(yytext); }
154"print" { thrift_reserved_keyword(yytext); }
155"private" { thrift_reserved_keyword(yytext); }
156"protected" { thrift_reserved_keyword(yytext); }
157"raise" { thrift_reserved_keyword(yytext); }
158"return" { thrift_reserved_keyword(yytext); }
159"sizeof" { thrift_reserved_keyword(yytext); }
160"static" { thrift_reserved_keyword(yytext); }
161"switch" { thrift_reserved_keyword(yytext); }
162"synchronized" { thrift_reserved_keyword(yytext); }
163"this" { thrift_reserved_keyword(yytext); }
164"throw" { thrift_reserved_keyword(yytext); }
165"transient" { thrift_reserved_keyword(yytext); }
166"true" { thrift_reserved_keyword(yytext); }
167"try" { thrift_reserved_keyword(yytext); }
168"unsigned" { thrift_reserved_keyword(yytext); }
169"var" { thrift_reserved_keyword(yytext); }
170"virtual" { thrift_reserved_keyword(yytext); }
171"volatile" { thrift_reserved_keyword(yytext); }
172"while" { thrift_reserved_keyword(yytext); }
173"with" { thrift_reserved_keyword(yytext); }
174"union" { thrift_reserved_keyword(yytext); }
175"yield" { thrift_reserved_keyword(yytext); }
176
Mark Slee4f8da1d2006-10-12 02:47:27 +0000177{intconstant} {
178 yylval.iconst = atoi(yytext);
179 return tok_int_constant;
180}
Mark Sleef5377b32006-10-10 01:42:59 +0000181
Mark Slee600cdb32006-11-29 22:06:42 +0000182{hexconstant} {
183 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +0000184 return tok_int_constant;
185}
186
Mark Slee30152872006-11-28 01:24:07 +0000187{dubconstant} {
188 yylval.dconst = atof(yytext);
189 return tok_dub_constant;
190}
191
Mark Slee4f8da1d2006-10-12 02:47:27 +0000192{identifier} {
193 yylval.id = strdup(yytext);
194 return tok_identifier;
195}
196
Mark Slee30152872006-11-28 01:24:07 +0000197{dliteral} {
198 yylval.id = strdup(yytext+1);
199 yylval.id[strlen(yylval.id)-1] = '\0';
200 return tok_literal;
201}
202
203{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000204 yylval.id = strdup(yytext+1);
205 yylval.id[strlen(yylval.id)-1] = '\0';
206 return tok_literal;
207}
Mark Slee31985722006-05-24 21:45:31 +0000208
ccheeverf53b5cf2007-02-05 20:33:11 +0000209{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000210 /* This does not show up in the parse tree. */
211 /* Rather, the parser will grab it out of the global. */
212 if (g_parse_mode == PROGRAM) {
213 clear_doctext();
214 g_doctext = strdup(yytext + 3);
215 g_doctext[strlen(g_doctext) - 2] = '\0';
216 g_doctext = clean_up_doctext(g_doctext);
217 g_doctext_lineno = yylineno;
218 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000219}
220
221
Mark Slee31985722006-05-24 21:45:31 +0000222%%