]> git.saurik.com Git - cycript.git/blame - Cycript.l.in
Replace use of FILE* with std::ostream* in Console.
[cycript.git] / Cycript.l.in
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
8d7447c1 2 * Copyright (C) 2009-2012 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); \
5602b1ee
JF
33 yyextra->no_.AtImplementation = false; \
34 yyextra->no_.Function = false; \
35 yyextra->no_.OpenBrace = false; \
3ea7eed0
JF
36 return token; \
37} while (false)
38
2eb8215d
JF
39#define A new($pool)
40#define Y apr_pstrmemdup($pool, yytext, yyleng)
41
3ea7eed0 42#define I(type, Type, value) do { \
2eb8215d 43 yylval->type ## _ = A CY ## Type; \
3ea7eed0 44 F(value); \
2eb8215d
JF
45} while (false)
46
697d6fd2 47#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
db5e2840
JF
48#define C T yyextra->state_ = CYClear;
49#define R T yyextra->state_ = CYRestricted;
5befe15e
JF
50
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) { \
d3b63265 93 if (yyextra->data_.eof()) \
e7ed5354
JF
94 value = YY_NULL; \
95 else { \
d3b63265
JF
96 yyextra->data_.read(data, size); \
97 size_t copy(yyextra->data_.gcount()); \
98 value = copy == 0 ? YY_NULL : copy; \
e7ed5354
JF
99 } \
100}
101
e5332278
JF
102%}
103
104%option prefix="cy"
105%option bison-bridge
106%option bison-locations
107%option noyywrap
108%option yylineno
109%option nounput
110%option interactive
924f67b2 111%option reentrant
691e4717 112%option stack
e5332278 113
2bf24581 114Exponent [eE][+-]?[0-9]+
367eebb1 115Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
e5332278 116
63cd45c9
JF
117IdentifierStart [a-zA-Z$_]
118IdentifierPart [a-zA-Z$_0-9]
119
120NonTerminator [^\n]
121BackslashSequence \\{NonTerminator}
122RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
123RegularExpressionChar [^\n\\/]|{BackslashSequence}
124RegularExpressionFlags {IdentifierPart}*
125RegularExpressionChars {RegularExpressionChar}*
697d6fd2 126RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
63cd45c9 127
691e4717
JF
128@begin E4X
129XMLNameStart [a-zA-Z_:]
130XMLNamePart [a-zA-Z0-9.-_:]
131XMLName {XMLNameStart}{XMLNamePart}*
132@end
133
697d6fd2
JF
134%s Div
135%s RegExp
63cd45c9 136
691e4717
JF
137@begin E4X
138%x XMLContent
139%x XMLTag
140@end
141
e5332278
JF
142%%
143
9205202e 144<RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} L C I(literal, RegEx(Y), tk::RegularExpressionLiteral);
63cd45c9 145
66fb559f 146\/\/[^\n]* L
fe123f47
JF
147
148 /* http://ostermiller.org/findcomment.html */
149 /* XXX: unify these two rules using !? */
3ea7eed0 150\/\*!([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V() C I(comment, Comment(Y), tk::Comment);
fe123f47 151\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V(N)
66fb559f 152
cb02f8ae 153@begin E4X
3ea7eed0
JF
154<RegExp>"<>" L F(tk::LeftRight);
155<XMLContent>"</>" L F(tk::LeftSlashRight);
691e4717 156
3ea7eed0
JF
157<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() F(tk::XMLCDATA);
158<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() F(tk::XMLComment);
159<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() F(tk::XMLPI);
691e4717 160
3ea7eed0
JF
161<XMLTag>"=" L F(tk::Equal);
162<XMLTag>">" L F(tk::Right);
163<XMLTag>"/>" L F(tk::SlashRight);
164<XMLTag>"{" L F(tk::OpenBrace);
691e4717 165
3ea7eed0
JF
166<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() F(tk::XMLAttributeValue);
167<XMLTag>{XMLName} L F(tk::XMLName);
168<XMLTag>[ \t\r\n] V() F(tk::XMLWhitespace);
db5e2840 169
3ea7eed0
JF
170<XMLContent>"{" L F(tk::OpenBrace);
171<XMLContent>"<" L F(tk::Left);
172<XMLContent>"</" L F(tk::LeftSlash);
691e4717
JF
173@end
174
3ea7eed0 175"..." L C F(tk::PeriodPeriodPeriod);
c8a0500b 176
691e4717 177@begin E4X
3ea7eed0
JF
178"::" L C F(tk::ColonColon);
179".." L C F(tk::PeriodPeriod);
cb02f8ae 180@end
ac9a5ce1 181
313708a9 182@begin E4X ObjectiveC
3ea7eed0 183"@" L C F(tk::At);
313708a9
JF
184@end
185
3ea7eed0
JF
186"&" L C F(tk::Ampersand);
187"&&" L C F(tk::AmpersandAmpersand);
188"&=" L C F(tk::AmpersandEqual);
189"^" L C F(tk::Carrot);
190"^=" L C F(tk::CarrotEqual);
191"=" L C F(tk::Equal);
192"==" L C F(tk::EqualEqual);
193"===" L C F(tk::EqualEqualEqual);
4b2fd91c 194"=>" L C F(tk::EqualRight);
3ea7eed0
JF
195"!" L C F(tk::Exclamation);
196"!=" L C F(tk::ExclamationEqual);
197"!==" L C F(tk::ExclamationEqualEqual);
198"-" L C F(tk::Hyphen);
199"-=" L C F(tk::HyphenEqual);
200"--" L C F(yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen);
201"->" L C F(tk::HyphenRight);
202"<" L C F(tk::Left);
203"<=" L C F(tk::LeftEqual);
204"<<" L C F(tk::LeftLeft);
205"<<=" L C F(tk::LeftLeftEqual);
206"%" L C F(tk::Percent);
207"%=" L C F(tk::PercentEqual);
208"." L C F(tk::Period);
209"|" L C F(tk::Pipe);
210"|=" L C F(tk::PipeEqual);
211"||" L C F(tk::PipePipe);
212"+" L C F(tk::Plus);
213"+=" L C F(tk::PlusEqual);
214"++" L C F(yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus);
215">" L C F(tk::Right);
216">=" L C F(tk::RightEqual);
217">>" L C F(tk::RightRight);
218">>=" L C F(tk::RightRightEqual);
219">>>" L C F(tk::RightRightRight);
220">>>=" L C F(tk::RightRightRightEqual);
221"*" L C F(tk::Star);
222"*=" L C F(tk::StarEqual);
223"~" L C F(tk::Tilde);
224
225<Div>"/" L C F(tk::Slash);
226<Div>"/=" L C F(tk::SlashEqual);
227
228":" L C F(tk::Colon);
229"," L C F(tk::Comma);
230"?" L C F(tk::Question);
231";" L C F(tk::SemiColon);
232
233"(" L C F(tk::OpenParen);
234")" L C F(tk::CloseParen);
235
5602b1ee 236"{" L C F(yyextra->no_.OpenBrace ? tk::OpenBrace__ : yylval->newline_ ? tk::OpenBrace_ : tk::OpenBrace);
3ea7eed0
JF
237"}" L C F(tk::CloseBrace);
238
239"[" L C F(tk::OpenBracket);
240"]" L C F(tk::CloseBracket);
5befe15e 241
1ba6903e 242@begin Java
3ea7eed0 243"@class" L C F(tk::AtClass);
1ba6903e
JF
244@end
245
cb02f8ae 246@begin ObjectiveC
3ea7eed0 247"@end" L C F(tk::AtEnd);
5602b1ee 248"@implementation" L C F(yyextra->no_.AtImplementation ? tk::AtImplementation_ : tk::AtImplementation);
3ea7eed0
JF
249"@import" L C F(tk::AtImport);
250"@selector" L C F(tk::AtSelector);
cb02f8ae 251@end
d35a3b07 252
3ea7eed0
JF
253"false" L C I(false, False(), tk::False);
254"null" L C I(null, Null(), tk::Null);
255"true" L C I(true, True(), tk::True);
256
a87d7060 257"auto" L C I(word, Word("auto"), tk::Auto);
3ea7eed0
JF
258"break" L R I(word, Word("break"), tk::Break);
259"case" L C I(word, Word("case"), tk::Case);
260"catch" L C I(word, Word("catch"), tk::Catch);
261"continue" L R I(word, Word("continue"), tk::Continue);
262"default" L C I(word, Word("default"), tk::Default);
263"delete" L C I(word, Word("delete"), tk::Delete);
264"do" L C I(word, Word("do"), tk::Do);
265"else" L C I(word, Word("else"), tk::Else);
266"finally" L C I(word, Word("finally"), tk::Finally);
267"for" L C I(word, Word("for"), tk::For);
5602b1ee 268"function" L C I(word, Word("function"), yyextra->no_.Function ? tk::Function_ : tk::Function);
3ea7eed0
JF
269"if" L C I(word, Word("if"), tk::If);
270"in" L C I(word, Word("in"), yyextra->in_.top() ? tk::In_ : tk::In);
271"instanceof" L C I(word, Word("instanceof"), tk::InstanceOf);
272"new" L C I(word, Word("new"), tk::New);
273"return" L R I(word, Word("return"), tk::Return);
274"switch" L C I(word, Word("switch"), tk::Switch);
275"this" L C I(this, This(), tk::This);
276"throw" L R I(word, Word("throw"), tk::Throw);
277"try" L C I(word, Word("try"), tk::Try);
278"typeof" L C I(word, Word("typeof"), tk::TypeOf);
279"var" L C I(word, Word("var"), tk::Var);
280"void" L C I(word, Word("void"), tk::Void);
281"while" L C I(word, Word("while"), tk::While);
282"with" L C I(word, Word("with"), tk::With);
283
284"debugger" L C I(word, Word("debugger"), tk::Debugger);
285
286"const" L C I(word, Word("const"), tk::Const);
287
288"class" L C I(word, Word("class"), tk::Class);
289"enum" L C I(word, Word("enum"), tk::Enum);
290"export" L C I(word, Word("export"), tk::Export);
291"extends" L C I(word, Word("extends"), tk::Extends);
292"import" L C I(word, Word("import"), tk::Import);
293"super" L C I(word, Word("super"), tk::Super);
294
295"implements" L C I(identifier, Identifier("implements"), tk::Implements);
296"interface" L C I(identifier, Identifier("interface"), tk::Interface);
297"package" L C I(identifier, Identifier("package"), tk::Package);
298"private" L C I(identifier, Identifier("private"), tk::Private);
299"protected" L C I(identifier, Identifier("protected"), tk::Protected);
300"public" L C I(identifier, Identifier("public"), tk::Public);
301"static" L C I(identifier, Identifier("static"), tk::Static);
302
303"abstract" L C I(identifier, Identifier("abstract"), tk::Abstract);
304"boolean" L C I(identifier, Identifier("boolean"), tk::Boolean);
305"byte" L C I(identifier, Identifier("byte"), tk::Byte);
306"char" L C I(identifier, Identifier("char"), tk::Char);
307"double" L C I(identifier, Identifier("double"), tk::Double);
308"final" L C I(identifier, Identifier("final"), tk::Final);
309"float" L C I(identifier, Identifier("float"), tk::Float);
310"goto" L C I(identifier, Identifier("goto"), tk::Goto);
311"int" L C I(identifier, Identifier("int"), tk::Int);
312"long" L C I(identifier, Identifier("long"), tk::Long);
313"native" L C I(identifier, Identifier("native"), tk::Native);
314"short" L C I(identifier, Identifier("short"), tk::Short);
315"synchronized" L C I(identifier, Identifier("synchronized"), tk::Synchronized);
316"throws" L C I(identifier, Identifier("throws"), tk::Throws);
317"transient" L C I(identifier, Identifier("transient"), tk::Transient);
318"volatile" L C I(identifier, Identifier("volatile"), tk::Volatile);
319
320"let" L C I(identifier, Identifier("let"), tk::Let);
321"yield" L C I(identifier, Identifier("yield"), tk::Yield);
322
323"each" L C I(identifier, Identifier("each"), tk::Each);
480c8834 324"of" L C I(identifier, Identifier("of"), tk::Of);
5d646fb5 325
691e4717 326@begin E4X
3ea7eed0
JF
327"namespace" L C I(identifier, Identifier("namespace"), tk::Namespace);
328"xml" L C I(identifier, Identifier("xml"), tk::XML);
691e4717
JF
329@end
330
3ea7eed0 331{IdentifierStart}{IdentifierPart}* L C I(identifier, Identifier(Y), tk::Identifier_);
5d646fb5 332
3ea7eed0 333(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C I(number, Number(strtod(yytext, NULL)), tk::NumericLiteral);
5d646fb5 334
3ea7eed0
JF
3350[xX][0-9a-fA-F]+ L C I(number, Number(strtoull(yytext + 2, NULL, 16)), tk::NumericLiteral);
3360[0-7]+ L C I(number, Number(strtoull(yytext + 1, NULL, 8)), tk::NumericLiteral);
3370[bB][0-1]+ L C I(number, Number(strtoull(yytext + 2, NULL, 2)), tk::NumericLiteral);
5befe15e 338
931b816a 339\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
2eb8215d 340 char *value(A char[yyleng]);
931b816a
JF
341 char *local(value);
342
66f8d960 343 for (yy_size_t i(1), e(yyleng - 1); i != e; ++i) {
931b816a
JF
344 char next(yytext[i]);
345
346 if (yytext[i] == '\\')
347 switch (next = yytext[++i]) {
367eebb1 348 case '\n': continue;
931b816a
JF
349 case '\\': next = '\\'; break;
350 case '\'': next = '\''; break;
351 case '"': next = '"'; break;
352 case 'b': next = '\b'; break;
353 case 'f': next = '\f'; break;
354 case 'n': next = '\n'; break;
355 case 'r': next = '\r'; break;
356 case 't': next = '\t'; break;
357 case 'v': next = '\v'; break;
358 case '0': next = '\0'; break;
359
360 case 'x':
361 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
362 i += 2;
363 break;
364 }
365
366 *local++ = next;
367 }
368
369 *local = '\0';
3ea7eed0 370 I(string, String(value, local - value), tk::StringLiteral);
931b816a 371}
5befe15e 372
3ea7eed0 373\r?\n|\r|\xe2\x80[\xa8\xa9] yylloc->end.lines(); yylloc->step(); N
5befe15e
JF
374
375[ \t] L
7e5391fd 376
3ea7eed0 377<<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; F(tk::AutoComplete); } L yyterminate();
94d55b5c 378
48e3be8a 379. L {
94d55b5c
JF
380 CYDriver::Error error;
381 error.location_ = *yylloc;
382 error.message_ = "syntax error, unknown token";
383 yyextra->errors_.push_back(error);
384 yyterminate();
385}
924f67b2
JF
386
387%%
388
5999c315 389void CYDriver::ScannerInit() {
924f67b2
JF
390 cylex_init(&scanner_);
391 cyset_extra(this, scanner_);
392}
393
5999c315 394void CYDriver::ScannerDestroy() {
924f67b2
JF
395 cylex_destroy(scanner_);
396}
63cd45c9 397
691e4717
JF
398CYDriver::Condition CYDriver::GetCondition() {
399 switch (yy_top_state(scanner_)) {
400 case RegExp:
401 return RegExpCondition;
402@begin E4X
403 case XMLContent:
404 return XMLContentCondition;
405 case XMLTag:
406 return XMLTagCondition;
407@end
408 default:
409 _assert(false);
410 }
411}
412
413void CYDriver::SetCondition(Condition condition) {
63cd45c9
JF
414 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
415
416 switch (condition) {
697d6fd2
JF
417 case RegExpCondition:
418 BEGIN(RegExp);
63cd45c9 419 break;
691e4717
JF
420@begin E4X
421 case XMLContentCondition:
422 BEGIN(XMLContent);
423 break;
424 case XMLTagCondition:
425 BEGIN(XMLTag);
426 break;
427@end
63cd45c9
JF
428 default:
429 _assert(false);
430 }
431}
691e4717
JF
432
433void CYDriver::PushCondition(Condition condition) {
434 switch (condition) {
435 case RegExpCondition:
436 yy_push_state(RegExp, scanner_);
437 break;
438@begin E4X
439 case XMLContentCondition:
440 yy_push_state(XMLContent, scanner_);
441 break;
442 case XMLTagCondition:
443 yy_push_state(XMLTag, scanner_);
444 break;
445@end
446 default:
447 _assert(false);
448 }
449}
450
451void CYDriver::PopCondition() {
452 yy_pop_state(scanner_);
453}