blob: 0889a122a40bd7c7064c500c5c17da6971e438ae [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 Sleebd588222007-11-21 08:43:35 +000044intconstant ([+-]?[0-9]+)
45hexconstant ("0x"[0-9A-Fa-f]+)
46dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
47identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
48whitespace ([ \t\r\n]*)
49sillycomm ("/*""*"*"*/")
50multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
51doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
52comment ("//"[^\n]*)
53unixcomment ("#"[^\n]*)
54symbol ([:;\,\{\}\(\)\=<>\[\]])
55dliteral ("\""[^"]*"\"")
56sliteral ("'"[^']*"'")
57st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
ccheeverf53b5cf2007-02-05 20:33:11 +000058
Mark Slee31985722006-05-24 21:45:31 +000059
60%%
61
Mark Sleebd588222007-11-21 08:43:35 +000062{whitespace} { /* do nothing */ }
63{sillycomm} { /* do nothing */ }
64{multicomm} { /* do nothing */ }
65{comment} { /* do nothing */ }
66{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000067
Mark Sleebd588222007-11-21 08:43:35 +000068{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +000069
Mark Sleebd588222007-11-21 08:43:35 +000070"namespace" { return tok_namespace; }
71"cpp_namespace" { return tok_cpp_namespace; }
72"cpp_include" { return tok_cpp_include; }
73"cpp_type" { return tok_cpp_type; }
74"java_package" { return tok_java_package; }
75"cocoa_prefix" { return tok_cocoa_prefix; }
76"php_namespace" { return tok_php_namespace; }
77"py_module" { return tok_py_module; }
78"perl_package" { return tok_perl_package; }
79"ruby_namespace" { return tok_ruby_namespace; }
80"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +000081"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +000082"xsd_all" { return tok_xsd_all; }
83"xsd_optional" { return tok_xsd_optional; }
84"xsd_nillable" { return tok_xsd_nillable; }
85"xsd_namespace" { return tok_xsd_namespace; }
86"xsd_attrs" { return tok_xsd_attrs; }
87"include" { return tok_include; }
88"void" { return tok_void; }
89"bool" { return tok_bool; }
90"byte" { return tok_byte; }
91"i16" { return tok_i16; }
92"i32" { return tok_i32; }
93"i64" { return tok_i64; }
94"double" { return tok_double; }
95"string" { return tok_string; }
96"binary" { return tok_binary; }
97"slist" { return tok_slist; }
98"senum" { return tok_senum; }
99"map" { return tok_map; }
100"list" { return tok_list; }
101"set" { return tok_set; }
102"async" { return tok_async; }
103"typedef" { return tok_typedef; }
104"struct" { return tok_struct; }
105"exception" { return tok_xception; }
106"extends" { return tok_extends; }
107"throws" { return tok_throws; }
108"service" { return tok_service; }
109"enum" { return tok_enum; }
110"const" { return tok_const; }
111"required" { return tok_required; }
112"optional" { return tok_optional; }
Mark Sleef0712dc2006-10-25 19:03:57 +0000113
Mark Slee52f643d2006-08-09 00:03:43 +0000114
Mark Sleebd588222007-11-21 08:43:35 +0000115"abstract" { thrift_reserved_keyword(yytext); }
116"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000117"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000118"as" { thrift_reserved_keyword(yytext); }
119"assert" { thrift_reserved_keyword(yytext); }
120"break" { thrift_reserved_keyword(yytext); }
121"case" { thrift_reserved_keyword(yytext); }
122"class" { thrift_reserved_keyword(yytext); }
123"continue" { thrift_reserved_keyword(yytext); }
124"declare" { thrift_reserved_keyword(yytext); }
125"def" { thrift_reserved_keyword(yytext); }
126"default" { thrift_reserved_keyword(yytext); }
127"del" { thrift_reserved_keyword(yytext); }
128"delete" { thrift_reserved_keyword(yytext); }
129"do" { thrift_reserved_keyword(yytext); }
130"elif" { thrift_reserved_keyword(yytext); }
131"else" { thrift_reserved_keyword(yytext); }
132"elseif" { thrift_reserved_keyword(yytext); }
133"except" { thrift_reserved_keyword(yytext); }
134"exec" { thrift_reserved_keyword(yytext); }
135"false" { thrift_reserved_keyword(yytext); }
136"final" { thrift_reserved_keyword(yytext); }
137"finally" { thrift_reserved_keyword(yytext); }
138"float" { thrift_reserved_keyword(yytext); }
139"for" { thrift_reserved_keyword(yytext); }
140"foreach" { thrift_reserved_keyword(yytext); }
141"function" { thrift_reserved_keyword(yytext); }
142"global" { thrift_reserved_keyword(yytext); }
143"goto" { thrift_reserved_keyword(yytext); }
144"if" { thrift_reserved_keyword(yytext); }
145"implements" { thrift_reserved_keyword(yytext); }
146"import" { thrift_reserved_keyword(yytext); }
147"in" { thrift_reserved_keyword(yytext); }
148"inline" { thrift_reserved_keyword(yytext); }
149"instanceof" { thrift_reserved_keyword(yytext); }
150"interface" { thrift_reserved_keyword(yytext); }
151"is" { thrift_reserved_keyword(yytext); }
152"lambda" { thrift_reserved_keyword(yytext); }
153"native" { thrift_reserved_keyword(yytext); }
154"new" { thrift_reserved_keyword(yytext); }
155"not" { thrift_reserved_keyword(yytext); }
156"or" { thrift_reserved_keyword(yytext); }
157"pass" { thrift_reserved_keyword(yytext); }
158"public" { thrift_reserved_keyword(yytext); }
159"print" { thrift_reserved_keyword(yytext); }
160"private" { thrift_reserved_keyword(yytext); }
161"protected" { thrift_reserved_keyword(yytext); }
162"raise" { thrift_reserved_keyword(yytext); }
163"return" { thrift_reserved_keyword(yytext); }
164"sizeof" { thrift_reserved_keyword(yytext); }
165"static" { thrift_reserved_keyword(yytext); }
166"switch" { thrift_reserved_keyword(yytext); }
167"synchronized" { thrift_reserved_keyword(yytext); }
168"this" { thrift_reserved_keyword(yytext); }
169"throw" { thrift_reserved_keyword(yytext); }
170"transient" { thrift_reserved_keyword(yytext); }
171"true" { thrift_reserved_keyword(yytext); }
172"try" { thrift_reserved_keyword(yytext); }
173"unsigned" { thrift_reserved_keyword(yytext); }
174"var" { thrift_reserved_keyword(yytext); }
175"virtual" { thrift_reserved_keyword(yytext); }
176"volatile" { thrift_reserved_keyword(yytext); }
177"while" { thrift_reserved_keyword(yytext); }
178"with" { thrift_reserved_keyword(yytext); }
179"union" { thrift_reserved_keyword(yytext); }
180"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000181
Mark Slee4f8da1d2006-10-12 02:47:27 +0000182{intconstant} {
183 yylval.iconst = atoi(yytext);
184 return tok_int_constant;
185}
Mark Sleef5377b32006-10-10 01:42:59 +0000186
Mark Slee600cdb32006-11-29 22:06:42 +0000187{hexconstant} {
188 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +0000189 return tok_int_constant;
190}
191
Mark Slee30152872006-11-28 01:24:07 +0000192{dubconstant} {
193 yylval.dconst = atof(yytext);
194 return tok_dub_constant;
195}
196
Mark Slee4f8da1d2006-10-12 02:47:27 +0000197{identifier} {
198 yylval.id = strdup(yytext);
199 return tok_identifier;
200}
201
Mark Sleebd588222007-11-21 08:43:35 +0000202{st_identifier} {
203 yylval.id = strdup(yytext);
204 return tok_st_identifier;
205}
206
Mark Slee30152872006-11-28 01:24:07 +0000207{dliteral} {
208 yylval.id = strdup(yytext+1);
209 yylval.id[strlen(yylval.id)-1] = '\0';
210 return tok_literal;
211}
212
213{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000214 yylval.id = strdup(yytext+1);
215 yylval.id[strlen(yylval.id)-1] = '\0';
216 return tok_literal;
217}
Mark Slee31985722006-05-24 21:45:31 +0000218
ccheeverf53b5cf2007-02-05 20:33:11 +0000219{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000220 /* This does not show up in the parse tree. */
221 /* Rather, the parser will grab it out of the global. */
222 if (g_parse_mode == PROGRAM) {
223 clear_doctext();
224 g_doctext = strdup(yytext + 3);
225 g_doctext[strlen(g_doctext) - 2] = '\0';
226 g_doctext = clean_up_doctext(g_doctext);
227 g_doctext_lineno = yylineno;
228 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000229}
230
231
Mark Slee31985722006-05-24 21:45:31 +0000232%%