]> git.saurik.com Git - cycript.git/blame - Cycript.l.in
Add support for ECMA6 for-of iteration statements.
[cycript.git] / Cycript.l.in
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
d15b59f5
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
d15b59f5 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
d15b59f5 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
d15b59f5 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
d15b59f5
JF
20/* }}} */
21
2155ab92 22/* XXX: supposedly I will be screwed on very very long multi-line comments and need to replace these with a manual lexer. http://websrv.cs.fsu.edu/~engelen/courses/COP5621/Pr2.pdf */
cb02f8ae 23
d15b59f5 24%{
cac61857 25#define YYLTYPE cy::location
63b4c5a8
JF
26#include "Cycript.tab.hh"
27typedef cy::parser::token tk;
693d501b 28
5999c315 29#define YY_EXTRA_TYPE CYDriver *
db5e2840 30
3ea7eed0
JF
31#define F(value) do { \
32 int token(value); \
c34648c0 33 yyextra->nobrace_ = false; \
3ea7eed0
JF
34 return token; \
35} while (false)
36
2eb8215d
JF
37#define A new($pool)
38#define Y apr_pstrmemdup($pool, yytext, yyleng)
39
3ea7eed0 40#define I(type, Type, value) do { \
2eb8215d 41 yylval->type ## _ = A CY ## Type; \
3ea7eed0 42 F(value); \
2eb8215d
JF
43} while (false)
44
697d6fd2 45#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
db5e2840
JF
46#define C T yyextra->state_ = CYClear;
47#define R T yyextra->state_ = CYRestricted;
5befe15e 48
3ea7eed0 49#define E L C I(literal, RegEx(Y), tk::RegularExpressionLiteral);
63cd45c9 50
5befe15e
JF
51#define N \
52 if (yyextra->state_ != CYNewLine) { \
2eb8215d
JF
53 if (yyextra->state_ != CYRestricted) \
54 yyextra->state_ = CYNewLine; \
55 else { \
5befe15e 56 yyextra->state_ = CYClear; \
3ea7eed0 57 F(tk::NewLine); \
2eb8215d 58 } \
5befe15e
JF
59 }
60
691e4717 61#define V(more) { \
cb02f8ae
JF
62 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
63 unsigned lines(0); \
64 size_t left; \
65 do { \
66 ++lines; \
67 left = yyleng - (nl - yytext) - 1; \
68 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
69 } while (nl != NULL); \
70 yylloc->end.lines(lines); \
71 yylloc->end.columns(left); \
72 yylloc->step(); \
691e4717 73 more \
cb02f8ae
JF
74 } else L \
75}
76
5befe15e
JF
77#define L { \
78 yylloc->step(); \
79 yylloc->columns(yyleng); \
80}
e7ed5354 81
931b816a
JF
82int H(char c) {
83 if (c >= '0' && c <= '9')
84 return c - '0';
85 if (c >= 'a' && c <= 'f')
86 return c - 'a' + 10;
87 if (c >= 'A' && c <= 'F')
88 return c - 'A' + 10;
89 return -1;
90}
91
e7ed5354 92#define YY_INPUT(data, value, size) { \
48e3be8a
JF
93 if (yyextra->file_ != NULL) { \
94 size_t copy(fread(data, 1, size, yyextra->file_)); \
95 value = copy == 0 ? YY_NULL : copy; \
96 } else if (yyextra->size_ == 0) \
e7ed5354
JF
97 value = YY_NULL; \
98 else { \
7c6c5b0a
JF
99 size_t copy(size); \
100 copy = (std::min(copy, yyextra->size_)); \
e7ed5354
JF
101 memcpy(data, yyextra->data_, copy); \
102 yyextra->data_ += copy; \
103 yyextra->size_ -= copy; \
104 value = copy; \
105 } \
106}
107
e5332278
JF
108%}
109
110%option prefix="cy"
111%option bison-bridge
112%option bison-locations
113%option noyywrap
114%option yylineno
115%option nounput
116%option interactive
924f67b2 117%option reentrant
691e4717 118%option stack
e5332278 119
2bf24581 120Exponent [eE][+-]?[0-9]+
367eebb1 121Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
e5332278 122
63cd45c9
JF
123IdentifierStart [a-zA-Z$_]
124IdentifierPart [a-zA-Z$_0-9]
125
126NonTerminator [^\n]
127BackslashSequence \\{NonTerminator}
128RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
129RegularExpressionChar [^\n\\/]|{BackslashSequence}
130RegularExpressionFlags {IdentifierPart}*
131RegularExpressionChars {RegularExpressionChar}*
697d6fd2 132RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
63cd45c9 133
691e4717
JF
134@begin E4X
135XMLNameStart [a-zA-Z_:]
136XMLNamePart [a-zA-Z0-9.-_:]
137XMLName {XMLNameStart}{XMLNamePart}*
138@end
139
697d6fd2
JF
140%s Div
141%s RegExp
63cd45c9 142
691e4717
JF
143@begin E4X
144%x XMLContent
145%x XMLTag
146@end
147
e5332278
JF
148%%
149
e4676127 150<RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} E
63cd45c9 151
66fb559f 152\/\/[^\n]* L
fe123f47
JF
153
154 /* http://ostermiller.org/findcomment.html */
155 /* XXX: unify these two rules using !? */
3ea7eed0 156\/\*!([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V() C I(comment, Comment(Y), tk::Comment);
fe123f47 157\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V(N)
66fb559f 158
cb02f8ae 159@begin E4X
3ea7eed0
JF
160<RegExp>"<>" L F(tk::LeftRight);
161<XMLContent>"</>" L F(tk::LeftSlashRight);
691e4717 162
3ea7eed0
JF
163<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() F(tk::XMLCDATA);
164<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() F(tk::XMLComment);
165<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() F(tk::XMLPI);
691e4717 166
3ea7eed0
JF
167<XMLTag>"=" L F(tk::Equal);
168<XMLTag>">" L F(tk::Right);
169<XMLTag>"/>" L F(tk::SlashRight);
170<XMLTag>"{" L F(tk::OpenBrace);
691e4717 171
3ea7eed0
JF
172<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() F(tk::XMLAttributeValue);
173<XMLTag>{XMLName} L F(tk::XMLName);
174<XMLTag>[ \t\r\n] V() F(tk::XMLWhitespace);
db5e2840 175
3ea7eed0
JF
176<XMLContent>"{" L F(tk::OpenBrace);
177<XMLContent>"<" L F(tk::Left);
178<XMLContent>"</" L F(tk::LeftSlash);
691e4717
JF
179@end
180
3ea7eed0 181"..." L C F(tk::PeriodPeriodPeriod);
c8a0500b 182
691e4717 183@begin E4X
3ea7eed0
JF
184"::" L C F(tk::ColonColon);
185".." L C F(tk::PeriodPeriod);
cb02f8ae 186@end
ac9a5ce1 187
313708a9 188@begin E4X ObjectiveC
3ea7eed0 189"@" L C F(tk::At);
313708a9
JF
190@end
191
3ea7eed0
JF
192"&" L C F(tk::Ampersand);
193"&&" L C F(tk::AmpersandAmpersand);
194"&=" L C F(tk::AmpersandEqual);
195"^" L C F(tk::Carrot);
196"^=" L C F(tk::CarrotEqual);
197"=" L C F(tk::Equal);
198"==" L C F(tk::EqualEqual);
199"===" L C F(tk::EqualEqualEqual);
4b2fd91c 200"=>" L C F(tk::EqualRight);
3ea7eed0
JF
201"!" L C F(tk::Exclamation);
202"!=" L C F(tk::ExclamationEqual);
203"!==" L C F(tk::ExclamationEqualEqual);
204"-" L C F(tk::Hyphen);
205"-=" L C F(tk::HyphenEqual);
206"--" L C F(yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen);
207"->" L C F(tk::HyphenRight);
208"<" L C F(tk::Left);
209"<=" L C F(tk::LeftEqual);
210"<<" L C F(tk::LeftLeft);
211"<<=" L C F(tk::LeftLeftEqual);
212"%" L C F(tk::Percent);
213"%=" L C F(tk::PercentEqual);
214"." L C F(tk::Period);
215"|" L C F(tk::Pipe);
216"|=" L C F(tk::PipeEqual);
217"||" L C F(tk::PipePipe);
218"+" L C F(tk::Plus);
219"+=" L C F(tk::PlusEqual);
220"++" L C F(yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus);
221">" L C F(tk::Right);
222">=" L C F(tk::RightEqual);
223">>" L C F(tk::RightRight);
224">>=" L C F(tk::RightRightEqual);
225">>>" L C F(tk::RightRightRight);
226">>>=" L C F(tk::RightRightRightEqual);
227"*" L C F(tk::Star);
228"*=" L C F(tk::StarEqual);
229"~" L C F(tk::Tilde);
230
231<Div>"/" L C F(tk::Slash);
232<Div>"/=" L C F(tk::SlashEqual);
233
234":" L C F(tk::Colon);
235"," L C F(tk::Comma);
236"?" L C F(tk::Question);
237";" L C F(tk::SemiColon);
238
239"(" L C F(tk::OpenParen);
240")" L C F(tk::CloseParen);
241
c34648c0 242"{" L C F(yyextra->nobrace_ ? tk::OpenBrace__ : yylval->newline_ ? tk::OpenBrace_ : tk::OpenBrace);
3ea7eed0
JF
243"}" L C F(tk::CloseBrace);
244
245"[" L C F(tk::OpenBracket);
246"]" L C F(tk::CloseBracket);
5befe15e 247
1ba6903e 248@begin Java
3ea7eed0 249"@class" L C F(tk::AtClass);
1ba6903e
JF
250@end
251
cb02f8ae 252@begin ObjectiveC
3ea7eed0 253"@end" L C F(tk::AtEnd);
066da9f6 254"@implementation" L C F(tk::AtImplementation);
3ea7eed0
JF
255"@import" L C F(tk::AtImport);
256"@selector" L C F(tk::AtSelector);
cb02f8ae 257@end
d35a3b07 258
3ea7eed0
JF
259"false" L C I(false, False(), tk::False);
260"null" L C I(null, Null(), tk::Null);
261"true" L C I(true, True(), tk::True);
262
263"break" L R I(word, Word("break"), tk::Break);
264"case" L C I(word, Word("case"), tk::Case);
265"catch" L C I(word, Word("catch"), tk::Catch);
266"continue" L R I(word, Word("continue"), tk::Continue);
267"default" L C I(word, Word("default"), tk::Default);
268"delete" L C I(word, Word("delete"), tk::Delete);
269"do" L C I(word, Word("do"), tk::Do);
270"else" L C I(word, Word("else"), tk::Else);
271"finally" L C I(word, Word("finally"), tk::Finally);
272"for" L C I(word, Word("for"), tk::For);
066da9f6 273"function" L C I(word, Word("function"), tk::Function);
3ea7eed0
JF
274"if" L C I(word, Word("if"), tk::If);
275"in" L C I(word, Word("in"), yyextra->in_.top() ? tk::In_ : tk::In);
276"instanceof" L C I(word, Word("instanceof"), tk::InstanceOf);
277"new" L C I(word, Word("new"), tk::New);
0d56ef32 278"of" L C I(word, Word("of"), tk::Of);
3ea7eed0
JF
279"return" L R I(word, Word("return"), tk::Return);
280"switch" L C I(word, Word("switch"), tk::Switch);
281"this" L C I(this, This(), tk::This);
282"throw" L R I(word, Word("throw"), tk::Throw);
283"try" L C I(word, Word("try"), tk::Try);
284"typeof" L C I(word, Word("typeof"), tk::TypeOf);
285"var" L C I(word, Word("var"), tk::Var);
286"void" L C I(word, Word("void"), tk::Void);
287"while" L C I(word, Word("while"), tk::While);
288"with" L C I(word, Word("with"), tk::With);
289
290"debugger" L C I(word, Word("debugger"), tk::Debugger);
291
292"const" L C I(word, Word("const"), tk::Const);
293
294"class" L C I(word, Word("class"), tk::Class);
295"enum" L C I(word, Word("enum"), tk::Enum);
296"export" L C I(word, Word("export"), tk::Export);
297"extends" L C I(word, Word("extends"), tk::Extends);
298"import" L C I(word, Word("import"), tk::Import);
299"super" L C I(word, Word("super"), tk::Super);
300
301"implements" L C I(identifier, Identifier("implements"), tk::Implements);
302"interface" L C I(identifier, Identifier("interface"), tk::Interface);
303"package" L C I(identifier, Identifier("package"), tk::Package);
304"private" L C I(identifier, Identifier("private"), tk::Private);
305"protected" L C I(identifier, Identifier("protected"), tk::Protected);
306"public" L C I(identifier, Identifier("public"), tk::Public);
307"static" L C I(identifier, Identifier("static"), tk::Static);
308
309"abstract" L C I(identifier, Identifier("abstract"), tk::Abstract);
310"boolean" L C I(identifier, Identifier("boolean"), tk::Boolean);
311"byte" L C I(identifier, Identifier("byte"), tk::Byte);
312"char" L C I(identifier, Identifier("char"), tk::Char);
313"double" L C I(identifier, Identifier("double"), tk::Double);
314"final" L C I(identifier, Identifier("final"), tk::Final);
315"float" L C I(identifier, Identifier("float"), tk::Float);
316"goto" L C I(identifier, Identifier("goto"), tk::Goto);
317"int" L C I(identifier, Identifier("int"), tk::Int);
318"long" L C I(identifier, Identifier("long"), tk::Long);
319"native" L C I(identifier, Identifier("native"), tk::Native);
320"short" L C I(identifier, Identifier("short"), tk::Short);
321"synchronized" L C I(identifier, Identifier("synchronized"), tk::Synchronized);
322"throws" L C I(identifier, Identifier("throws"), tk::Throws);
323"transient" L C I(identifier, Identifier("transient"), tk::Transient);
324"volatile" L C I(identifier, Identifier("volatile"), tk::Volatile);
325
326"let" L C I(identifier, Identifier("let"), tk::Let);
327"yield" L C I(identifier, Identifier("yield"), tk::Yield);
328
329"each" L C I(identifier, Identifier("each"), tk::Each);
5d646fb5 330
691e4717 331@begin E4X
3ea7eed0
JF
332"namespace" L C I(identifier, Identifier("namespace"), tk::Namespace);
333"xml" L C I(identifier, Identifier("xml"), tk::XML);
691e4717
JF
334@end
335
3ea7eed0 336{IdentifierStart}{IdentifierPart}* L C I(identifier, Identifier(Y), tk::Identifier_);
5d646fb5 337
3ea7eed0 338(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C I(number, Number(strtod(yytext, NULL)), tk::NumericLiteral);
5d646fb5 339
3ea7eed0
JF
3400[xX][0-9a-fA-F]+ L C I(number, Number(strtoull(yytext + 2, NULL, 16)), tk::NumericLiteral);
3410[0-7]+ L C I(number, Number(strtoull(yytext + 1, NULL, 8)), tk::NumericLiteral);
3420[bB][0-1]+ L C I(number, Number(strtoull(yytext + 2, NULL, 2)), tk::NumericLiteral);
5befe15e 343
931b816a 344\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
2eb8215d 345 char *value(A char[yyleng]);
931b816a
JF
346 char *local(value);
347
66f8d960 348 for (yy_size_t i(1), e(yyleng - 1); i != e; ++i) {
931b816a
JF
349 char next(yytext[i]);
350
351 if (yytext[i] == '\\')
352 switch (next = yytext[++i]) {
367eebb1 353 case '\n': continue;
931b816a
JF
354 case '\\': next = '\\'; break;
355 case '\'': next = '\''; break;
356 case '"': next = '"'; break;
357 case 'b': next = '\b'; break;
358 case 'f': next = '\f'; break;
359 case 'n': next = '\n'; break;
360 case 'r': next = '\r'; break;
361 case 't': next = '\t'; break;
362 case 'v': next = '\v'; break;
363 case '0': next = '\0'; break;
364
365 case 'x':
366 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
367 i += 2;
368 break;
369 }
370
371 *local++ = next;
372 }
373
374 *local = '\0';
3ea7eed0 375 I(string, String(value, local - value), tk::StringLiteral);
931b816a 376}
5befe15e 377
3ea7eed0 378\r?\n|\r|\xe2\x80[\xa8\xa9] yylloc->end.lines(); yylloc->step(); N
5befe15e
JF
379
380[ \t] L
7e5391fd 381
3ea7eed0 382<<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; F(tk::AutoComplete); } L yyterminate();
94d55b5c 383
48e3be8a 384. L {
94d55b5c
JF
385 CYDriver::Error error;
386 error.location_ = *yylloc;
387 error.message_ = "syntax error, unknown token";
388 yyextra->errors_.push_back(error);
389 yyterminate();
390}
924f67b2
JF
391
392%%
393
5999c315 394void CYDriver::ScannerInit() {
924f67b2
JF
395 cylex_init(&scanner_);
396 cyset_extra(this, scanner_);
397}
398
5999c315 399void CYDriver::ScannerDestroy() {
924f67b2
JF
400 cylex_destroy(scanner_);
401}
63cd45c9 402
691e4717
JF
403CYDriver::Condition CYDriver::GetCondition() {
404 switch (yy_top_state(scanner_)) {
405 case RegExp:
406 return RegExpCondition;
407@begin E4X
408 case XMLContent:
409 return XMLContentCondition;
410 case XMLTag:
411 return XMLTagCondition;
412@end
413 default:
414 _assert(false);
415 }
416}
417
418void CYDriver::SetCondition(Condition condition) {
63cd45c9
JF
419 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
420
421 switch (condition) {
697d6fd2
JF
422 case RegExpCondition:
423 BEGIN(RegExp);
63cd45c9 424 break;
691e4717
JF
425@begin E4X
426 case XMLContentCondition:
427 BEGIN(XMLContent);
428 break;
429 case XMLTagCondition:
430 BEGIN(XMLTag);
431 break;
432@end
63cd45c9
JF
433 default:
434 _assert(false);
435 }
436}
691e4717
JF
437
438void CYDriver::PushCondition(Condition condition) {
439 switch (condition) {
440 case RegExpCondition:
441 yy_push_state(RegExp, scanner_);
442 break;
443@begin E4X
444 case XMLContentCondition:
445 yy_push_state(XMLContent, scanner_);
446 break;
447 case XMLTagCondition:
448 yy_push_state(XMLTag, scanner_);
449 break;
450@end
451 default:
452 _assert(false);
453 }
454}
455
456void CYDriver::PopCondition() {
457 yy_pop_state(scanner_);
458}