blob: 1051e27f7545062604e8b277e08b561fc4780ebe [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
David Reissf1454162008-06-30 20:45:47 +000018#include <errno.h>
19
Mark Slee31985722006-05-24 21:45:31 +000020#include "main.h"
David Reisscbd4bac2007-08-14 17:12:33 +000021#include "globals.h"
Mark Slee31985722006-05-24 21:45:31 +000022#include "parse/t_program.h"
23
Mark Sleef5377b32006-10-10 01:42:59 +000024/**
25 * Must be included AFTER parse/t_program.h, but I can't remember why anymore
26 * because I wrote this a while ago.
27 */
Mark Sleeeb0d0242007-01-25 07:58:55 +000028#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000029
Mark Sleef12865a2007-01-12 00:23:26 +000030void thrift_reserved_keyword(char* keyword) {
31 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
32 exit(1);
33}
34
David Reissf1454162008-06-30 20:45:47 +000035void integer_overflow(char* text) {
36 yyerror("This integer is too big: \"%s\"\n", text);
37 exit(1);
38}
39
Mark Slee31985722006-05-24 21:45:31 +000040%}
41
Mark Sleef5377b32006-10-10 01:42:59 +000042/**
43 * Provides the yylineno global, useful for debugging output
44 */
Mark Slee27ed6ec2007-08-16 01:26:31 +000045%option lex-compat
Mark Slee31985722006-05-24 21:45:31 +000046
Mark Slee27ed6ec2007-08-16 01:26:31 +000047/**
Mark Sleef5377b32006-10-10 01:42:59 +000048 * Helper definitions, comments, constants, and whatnot
49 */
50
Mark Sleebd588222007-11-21 08:43:35 +000051intconstant ([+-]?[0-9]+)
52hexconstant ("0x"[0-9A-Fa-f]+)
53dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
54identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
55whitespace ([ \t\r\n]*)
56sillycomm ("/*""*"*"*/")
57multicomm ("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
58doctext ("/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
59comment ("//"[^\n]*)
60unixcomment ("#"[^\n]*)
61symbol ([:;\,\{\}\(\)\=<>\[\]])
62dliteral ("\""[^"]*"\"")
63sliteral ("'"[^']*"'")
64st_identifier ([a-zA-Z-][\.a-zA-Z_0-9-]*)
ccheeverf53b5cf2007-02-05 20:33:11 +000065
Mark Slee31985722006-05-24 21:45:31 +000066
67%%
68
Mark Sleebd588222007-11-21 08:43:35 +000069{whitespace} { /* do nothing */ }
70{sillycomm} { /* do nothing */ }
71{multicomm} { /* do nothing */ }
72{comment} { /* do nothing */ }
73{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000074
Mark Sleebd588222007-11-21 08:43:35 +000075{symbol} { return yytext[0]; }
Mark Slee9cb7c612006-09-01 22:17:45 +000076
Mark Sleebd588222007-11-21 08:43:35 +000077"namespace" { return tok_namespace; }
78"cpp_namespace" { return tok_cpp_namespace; }
79"cpp_include" { return tok_cpp_include; }
80"cpp_type" { return tok_cpp_type; }
81"java_package" { return tok_java_package; }
82"cocoa_prefix" { return tok_cocoa_prefix; }
David Reiss7f42bcf2008-01-11 20:59:12 +000083"csharp_namespace" { return tok_csharp_namespace; }
Mark Sleebd588222007-11-21 08:43:35 +000084"php_namespace" { return tok_php_namespace; }
85"py_module" { return tok_py_module; }
86"perl_package" { return tok_perl_package; }
87"ruby_namespace" { return tok_ruby_namespace; }
88"smalltalk_category" { return tok_smalltalk_category; }
David Reiss15457c92007-12-14 07:03:03 +000089"smalltalk_prefix" { return tok_smalltalk_prefix; }
Mark Sleebd588222007-11-21 08:43:35 +000090"xsd_all" { return tok_xsd_all; }
91"xsd_optional" { return tok_xsd_optional; }
92"xsd_nillable" { return tok_xsd_nillable; }
93"xsd_namespace" { return tok_xsd_namespace; }
94"xsd_attrs" { return tok_xsd_attrs; }
95"include" { return tok_include; }
96"void" { return tok_void; }
97"bool" { return tok_bool; }
98"byte" { return tok_byte; }
99"i16" { return tok_i16; }
100"i32" { return tok_i32; }
101"i64" { return tok_i64; }
102"double" { return tok_double; }
103"string" { return tok_string; }
104"binary" { return tok_binary; }
105"slist" { return tok_slist; }
106"senum" { return tok_senum; }
107"map" { return tok_map; }
108"list" { return tok_list; }
109"set" { return tok_set; }
110"async" { return tok_async; }
111"typedef" { return tok_typedef; }
112"struct" { return tok_struct; }
113"exception" { return tok_xception; }
114"extends" { return tok_extends; }
115"throws" { return tok_throws; }
116"service" { return tok_service; }
117"enum" { return tok_enum; }
118"const" { return tok_const; }
119"required" { return tok_required; }
120"optional" { return tok_optional; }
Mark Sleef0712dc2006-10-25 19:03:57 +0000121
Mark Slee52f643d2006-08-09 00:03:43 +0000122
Mark Sleebd588222007-11-21 08:43:35 +0000123"abstract" { thrift_reserved_keyword(yytext); }
124"and" { thrift_reserved_keyword(yytext); }
Mark Sleec27fc312007-12-21 23:52:19 +0000125"args" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000126"as" { thrift_reserved_keyword(yytext); }
127"assert" { thrift_reserved_keyword(yytext); }
128"break" { thrift_reserved_keyword(yytext); }
129"case" { thrift_reserved_keyword(yytext); }
130"class" { thrift_reserved_keyword(yytext); }
131"continue" { thrift_reserved_keyword(yytext); }
132"declare" { thrift_reserved_keyword(yytext); }
133"def" { thrift_reserved_keyword(yytext); }
134"default" { thrift_reserved_keyword(yytext); }
135"del" { thrift_reserved_keyword(yytext); }
136"delete" { thrift_reserved_keyword(yytext); }
137"do" { thrift_reserved_keyword(yytext); }
138"elif" { thrift_reserved_keyword(yytext); }
139"else" { thrift_reserved_keyword(yytext); }
140"elseif" { thrift_reserved_keyword(yytext); }
141"except" { thrift_reserved_keyword(yytext); }
142"exec" { thrift_reserved_keyword(yytext); }
143"false" { thrift_reserved_keyword(yytext); }
Mark Sleebd588222007-11-21 08:43:35 +0000144"finally" { thrift_reserved_keyword(yytext); }
145"float" { thrift_reserved_keyword(yytext); }
146"for" { thrift_reserved_keyword(yytext); }
147"foreach" { thrift_reserved_keyword(yytext); }
148"function" { thrift_reserved_keyword(yytext); }
149"global" { thrift_reserved_keyword(yytext); }
150"goto" { thrift_reserved_keyword(yytext); }
151"if" { thrift_reserved_keyword(yytext); }
152"implements" { thrift_reserved_keyword(yytext); }
153"import" { thrift_reserved_keyword(yytext); }
154"in" { thrift_reserved_keyword(yytext); }
155"inline" { thrift_reserved_keyword(yytext); }
156"instanceof" { thrift_reserved_keyword(yytext); }
157"interface" { thrift_reserved_keyword(yytext); }
158"is" { thrift_reserved_keyword(yytext); }
159"lambda" { thrift_reserved_keyword(yytext); }
160"native" { thrift_reserved_keyword(yytext); }
161"new" { thrift_reserved_keyword(yytext); }
162"not" { thrift_reserved_keyword(yytext); }
163"or" { thrift_reserved_keyword(yytext); }
164"pass" { thrift_reserved_keyword(yytext); }
165"public" { thrift_reserved_keyword(yytext); }
166"print" { thrift_reserved_keyword(yytext); }
167"private" { thrift_reserved_keyword(yytext); }
168"protected" { thrift_reserved_keyword(yytext); }
169"raise" { thrift_reserved_keyword(yytext); }
170"return" { thrift_reserved_keyword(yytext); }
171"sizeof" { thrift_reserved_keyword(yytext); }
172"static" { thrift_reserved_keyword(yytext); }
173"switch" { thrift_reserved_keyword(yytext); }
174"synchronized" { thrift_reserved_keyword(yytext); }
175"this" { thrift_reserved_keyword(yytext); }
176"throw" { thrift_reserved_keyword(yytext); }
177"transient" { thrift_reserved_keyword(yytext); }
178"true" { thrift_reserved_keyword(yytext); }
179"try" { thrift_reserved_keyword(yytext); }
180"unsigned" { thrift_reserved_keyword(yytext); }
181"var" { thrift_reserved_keyword(yytext); }
182"virtual" { thrift_reserved_keyword(yytext); }
183"volatile" { thrift_reserved_keyword(yytext); }
184"while" { thrift_reserved_keyword(yytext); }
185"with" { thrift_reserved_keyword(yytext); }
186"union" { thrift_reserved_keyword(yytext); }
187"yield" { thrift_reserved_keyword(yytext); }
Mark Sleef12865a2007-01-12 00:23:26 +0000188
Mark Slee4f8da1d2006-10-12 02:47:27 +0000189{intconstant} {
David Reissf1454162008-06-30 20:45:47 +0000190 errno = 0;
191 yylval.iconst = strtoll(yytext, NULL, 10);
192 if (errno == ERANGE) {
193 integer_overflow(yytext);
194 }
Mark Slee4f8da1d2006-10-12 02:47:27 +0000195 return tok_int_constant;
196}
Mark Sleef5377b32006-10-10 01:42:59 +0000197
Mark Slee600cdb32006-11-29 22:06:42 +0000198{hexconstant} {
David Reissf1454162008-06-30 20:45:47 +0000199 errno = 0;
200 yylval.iconst = strtoll(yytext+2, NULL, 16);
201 if (errno == ERANGE) {
202 integer_overflow(yytext);
203 }
Mark Slee600cdb32006-11-29 22:06:42 +0000204 return tok_int_constant;
205}
206
Mark Slee30152872006-11-28 01:24:07 +0000207{dubconstant} {
208 yylval.dconst = atof(yytext);
209 return tok_dub_constant;
210}
211
Mark Slee4f8da1d2006-10-12 02:47:27 +0000212{identifier} {
213 yylval.id = strdup(yytext);
214 return tok_identifier;
215}
216
Mark Sleebd588222007-11-21 08:43:35 +0000217{st_identifier} {
218 yylval.id = strdup(yytext);
219 return tok_st_identifier;
220}
221
Mark Slee30152872006-11-28 01:24:07 +0000222{dliteral} {
223 yylval.id = strdup(yytext+1);
224 yylval.id[strlen(yylval.id)-1] = '\0';
225 return tok_literal;
226}
227
228{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000229 yylval.id = strdup(yytext+1);
230 yylval.id[strlen(yylval.id)-1] = '\0';
231 return tok_literal;
232}
Mark Slee31985722006-05-24 21:45:31 +0000233
ccheeverf53b5cf2007-02-05 20:33:11 +0000234{doctext} {
David Reisscbd4bac2007-08-14 17:12:33 +0000235 /* This does not show up in the parse tree. */
236 /* Rather, the parser will grab it out of the global. */
237 if (g_parse_mode == PROGRAM) {
238 clear_doctext();
239 g_doctext = strdup(yytext + 3);
240 g_doctext[strlen(g_doctext) - 2] = '\0';
241 g_doctext = clean_up_doctext(g_doctext);
242 g_doctext_lineno = yylineno;
243 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000244}
245
246
Mark Slee31985722006-05-24 21:45:31 +0000247%%