blob: 324c5ea1eadd9d5eea941587fc9c268d7151f82d [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
2 * Thrift scanner.
3 *
4 * Tokenizes a thrift definition file.
5 * @author Mark Slee <mcslee@facebook.com>
6 */
Mark Sleef5377b32006-10-10 01:42:59 +00007
Mark Slee31985722006-05-24 21:45:31 +00008%{
9
10#include "main.h"
11#include "parse/t_program.h"
12
Mark Sleef5377b32006-10-10 01:42:59 +000013/**
14 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
15 * because I wrote this a while ago.
16 */
Mark Slee31985722006-05-24 21:45:31 +000017#include "thrift.tab.hh"
18
Mark Sleef12865a2007-01-12 00:23:26 +000019void thrift_reserved_keyword(char* keyword) {
20 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
21 exit(1);
22}
23
Mark Slee31985722006-05-24 21:45:31 +000024%}
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
27 * Provides the yylineno global, useful for debugging output
28 */
Mark Slee31985722006-05-24 21:45:31 +000029%option lex-compat
30
Mark Sleef5377b32006-10-10 01:42:59 +000031/**
32 * Helper definitions, comments, constants, and whatnot
33 */
34
Mark Slee30152872006-11-28 01:24:07 +000035intconstant ([+-]?[0-9]+)
Mark Slee600cdb32006-11-29 22:06:42 +000036hexconstant ("0x"[0-9A-Fa-f]+)
Mark Slee30152872006-11-28 01:24:07 +000037dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
Mark Slee9cb7c612006-09-01 22:17:45 +000038identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
Mark Slee31985722006-05-24 21:45:31 +000039whitespace ([ \t\r\n]*)
40multicomm ("/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
41comment ("//"[^\n]*)
Mark Sleec98d0502006-09-06 02:42:25 +000042unixcomment ("#"[^\n]*)
Mark Sleeae2bc3c2006-11-08 23:44:59 +000043symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Slee30152872006-11-28 01:24:07 +000044dliteral ("\""[^"]*"\"")
45sliteral ("'"[^']*"'")
Mark Slee31985722006-05-24 21:45:31 +000046
47%%
48
49{whitespace} { /* do nothing */ }
50{multicomm} { /* do nothing */ }
51{comment} { /* do nothing */ }
Mark Sleec98d0502006-09-06 02:42:25 +000052{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000053
Mark Slee9cb7c612006-09-01 22:17:45 +000054{symbol} { return yytext[0]; }
55
Mark Sleef0712dc2006-10-25 19:03:57 +000056"namespace" { return tok_namespace; }
57"cpp_namespace" { return tok_cpp_namespace; }
58"cpp_include" { return tok_cpp_include; }
59"cpp_type" { return tok_cpp_type; }
60"java_package" { return tok_java_package; }
Mark Sleee888b372007-01-12 01:06:24 +000061"php_namespace" { return tok_php_namespace; }
Mark Sleef0712dc2006-10-25 19:03:57 +000062"include" { return tok_include; }
63
64"void" { return tok_void; }
65"bool" { return tok_bool; }
66"byte" { return tok_byte; }
67"i16" { return tok_i16; }
68"i32" { return tok_i32; }
69"i64" { return tok_i64; }
70"double" { return tok_double; }
71"string" { return tok_string; }
72"map" { return tok_map; }
73"list" { return tok_list; }
74"set" { return tok_set; }
75"async" { return tok_async; }
76"typedef" { return tok_typedef; }
77"struct" { return tok_struct; }
78"exception" { return tok_xception; }
79"extends" { return tok_extends; }
80"throws" { return tok_throws; }
81"service" { return tok_service; }
82"enum" { return tok_enum; }
Mark Slee30152872006-11-28 01:24:07 +000083"const" { return tok_const; }
Mark Slee52f643d2006-08-09 00:03:43 +000084
Mark Sleef12865a2007-01-12 00:23:26 +000085"abstract" { thrift_reserved_keyword(yytext); }
86"and" { thrift_reserved_keyword(yytext); }
87"as" { thrift_reserved_keyword(yytext); }
88"assert" { thrift_reserved_keyword(yytext); }
89"break" { thrift_reserved_keyword(yytext); }
90"case" { thrift_reserved_keyword(yytext); }
91"class" { thrift_reserved_keyword(yytext); }
92"continue" { thrift_reserved_keyword(yytext); }
93"declare" { thrift_reserved_keyword(yytext); }
94"def" { thrift_reserved_keyword(yytext); }
95"default" { thrift_reserved_keyword(yytext); }
96"del" { thrift_reserved_keyword(yytext); }
97"delete" { thrift_reserved_keyword(yytext); }
98"do" { thrift_reserved_keyword(yytext); }
99"elif" { thrift_reserved_keyword(yytext); }
100"else" { thrift_reserved_keyword(yytext); }
101"elseif" { thrift_reserved_keyword(yytext); }
102"except" { thrift_reserved_keyword(yytext); }
103"exec" { thrift_reserved_keyword(yytext); }
104"false" { thrift_reserved_keyword(yytext); }
105"final" { thrift_reserved_keyword(yytext); }
106"finally" { thrift_reserved_keyword(yytext); }
107"float" { thrift_reserved_keyword(yytext); }
108"for" { thrift_reserved_keyword(yytext); }
109"foreach" { thrift_reserved_keyword(yytext); }
110"function" { thrift_reserved_keyword(yytext); }
111"global" { thrift_reserved_keyword(yytext); }
112"goto" { thrift_reserved_keyword(yytext); }
113"if" { thrift_reserved_keyword(yytext); }
114"implements" { thrift_reserved_keyword(yytext); }
115"import" { thrift_reserved_keyword(yytext); }
116"in" { thrift_reserved_keyword(yytext); }
117"inline" { thrift_reserved_keyword(yytext); }
118"instanceof" { thrift_reserved_keyword(yytext); }
119"interface" { thrift_reserved_keyword(yytext); }
120"is" { thrift_reserved_keyword(yytext); }
121"lambda" { thrift_reserved_keyword(yytext); }
122"native" { thrift_reserved_keyword(yytext); }
123"new" { thrift_reserved_keyword(yytext); }
124"not" { thrift_reserved_keyword(yytext); }
125"or" { thrift_reserved_keyword(yytext); }
126"pass" { thrift_reserved_keyword(yytext); }
127"public" { thrift_reserved_keyword(yytext); }
128"print" { thrift_reserved_keyword(yytext); }
129"private" { thrift_reserved_keyword(yytext); }
130"protected" { thrift_reserved_keyword(yytext); }
131"raise" { thrift_reserved_keyword(yytext); }
132"return" { thrift_reserved_keyword(yytext); }
133"sizeof" { thrift_reserved_keyword(yytext); }
134"static" { thrift_reserved_keyword(yytext); }
135"switch" { thrift_reserved_keyword(yytext); }
136"synchronized" { thrift_reserved_keyword(yytext); }
137"this" { thrift_reserved_keyword(yytext); }
138"throw" { thrift_reserved_keyword(yytext); }
139"transient" { thrift_reserved_keyword(yytext); }
140"true" { thrift_reserved_keyword(yytext); }
141"try" { thrift_reserved_keyword(yytext); }
142"unsigned" { thrift_reserved_keyword(yytext); }
143"var" { thrift_reserved_keyword(yytext); }
144"virtual" { thrift_reserved_keyword(yytext); }
145"volatile" { thrift_reserved_keyword(yytext); }
146"while" { thrift_reserved_keyword(yytext); }
147"with" { thrift_reserved_keyword(yytext); }
148"union" { thrift_reserved_keyword(yytext); }
149"yield" { thrift_reserved_keyword(yytext); }
150
Mark Slee4f8da1d2006-10-12 02:47:27 +0000151{intconstant} {
152 yylval.iconst = atoi(yytext);
153 return tok_int_constant;
154}
Mark Sleef5377b32006-10-10 01:42:59 +0000155
Mark Slee600cdb32006-11-29 22:06:42 +0000156{hexconstant} {
157 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +0000158 return tok_int_constant;
159}
160
Mark Slee30152872006-11-28 01:24:07 +0000161{dubconstant} {
162 yylval.dconst = atof(yytext);
163 return tok_dub_constant;
164}
165
Mark Slee4f8da1d2006-10-12 02:47:27 +0000166{identifier} {
167 yylval.id = strdup(yytext);
168 return tok_identifier;
169}
170
Mark Slee30152872006-11-28 01:24:07 +0000171{dliteral} {
172 yylval.id = strdup(yytext+1);
173 yylval.id[strlen(yylval.id)-1] = '\0';
174 return tok_literal;
175}
176
177{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000178 yylval.id = strdup(yytext+1);
179 yylval.id[strlen(yylval.id)-1] = '\0';
180 return tok_literal;
181}
Mark Slee31985722006-05-24 21:45:31 +0000182
183%%