]> git.saurik.com Git - cycript.git/blame - Cycript.l.in
Port Objective-C/Replace to C++.
[cycript.git] / Cycript.l.in
CommitLineData
d15b59f5
JF
1/* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/* }}} */
39
2155ab92 40/* 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 41
d15b59f5 42%{
cac61857 43#define YYLTYPE cy::location
63b4c5a8
JF
44#include "Cycript.tab.hh"
45typedef cy::parser::token tk;
693d501b 46
5999c315 47#define YY_EXTRA_TYPE CYDriver *
db5e2840 48
2eb8215d
JF
49#define A new($pool)
50#define Y apr_pstrmemdup($pool, yytext, yyleng)
51
52#define I(type, Type, Name) do { \
53 yylval->type ## _ = A CY ## Type; \
54 return tk::Name; \
55} while (false)
56
697d6fd2 57#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
db5e2840
JF
58#define C T yyextra->state_ = CYClear;
59#define R T yyextra->state_ = CYRestricted;
5befe15e 60
697d6fd2 61#define E(prefix) L C { \
2eb8215d 62 char *value(A char[yyleng + sizeof(prefix)]); \
63cd45c9
JF
63 memcpy(value, prefix, sizeof(prefix) - 1); \
64 memcpy(value + sizeof(prefix) - 1, yytext, yyleng); \
65 value[yyleng + sizeof(prefix) - 1] = '\0'; \
2eb8215d 66 I(literal, RegEx(value), RegularExpressionLiteral); \
63cd45c9
JF
67}
68
5befe15e
JF
69#define N \
70 if (yyextra->state_ != CYNewLine) { \
2eb8215d
JF
71 if (yyextra->state_ != CYRestricted) \
72 yyextra->state_ = CYNewLine; \
73 else { \
5befe15e
JF
74 yyextra->state_ = CYClear; \
75 return tk::NewLine; \
2eb8215d 76 } \
5befe15e
JF
77 }
78
691e4717 79#define V(more) { \
cb02f8ae
JF
80 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
81 unsigned lines(0); \
82 size_t left; \
83 do { \
84 ++lines; \
85 left = yyleng - (nl - yytext) - 1; \
86 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
87 } while (nl != NULL); \
88 yylloc->end.lines(lines); \
89 yylloc->end.columns(left); \
90 yylloc->step(); \
691e4717 91 more \
cb02f8ae
JF
92 } else L \
93}
94
5befe15e
JF
95#define L { \
96 yylloc->step(); \
97 yylloc->columns(yyleng); \
98}
e7ed5354 99
931b816a
JF
100int H(char c) {
101 if (c >= '0' && c <= '9')
102 return c - '0';
103 if (c >= 'a' && c <= 'f')
104 return c - 'a' + 10;
105 if (c >= 'A' && c <= 'F')
106 return c - 'A' + 10;
107 return -1;
108}
109
e7ed5354 110#define YY_INPUT(data, value, size) { \
48e3be8a
JF
111 if (yyextra->file_ != NULL) { \
112 size_t copy(fread(data, 1, size, yyextra->file_)); \
113 value = copy == 0 ? YY_NULL : copy; \
114 } else if (yyextra->size_ == 0) \
e7ed5354
JF
115 value = YY_NULL; \
116 else { \
7c6c5b0a
JF
117 size_t copy(size); \
118 copy = (std::min(copy, yyextra->size_)); \
e7ed5354
JF
119 memcpy(data, yyextra->data_, copy); \
120 yyextra->data_ += copy; \
121 yyextra->size_ -= copy; \
122 value = copy; \
123 } \
124}
125
e5332278
JF
126%}
127
128%option prefix="cy"
129%option bison-bridge
130%option bison-locations
131%option noyywrap
132%option yylineno
133%option nounput
134%option interactive
924f67b2 135%option reentrant
691e4717 136%option stack
e5332278 137
2bf24581 138Exponent [eE][+-]?[0-9]+
367eebb1 139Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
e5332278 140
63cd45c9
JF
141IdentifierStart [a-zA-Z$_]
142IdentifierPart [a-zA-Z$_0-9]
143
144NonTerminator [^\n]
145BackslashSequence \\{NonTerminator}
146RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
147RegularExpressionChar [^\n\\/]|{BackslashSequence}
148RegularExpressionFlags {IdentifierPart}*
149RegularExpressionChars {RegularExpressionChar}*
697d6fd2 150RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
63cd45c9 151
691e4717
JF
152@begin E4X
153XMLNameStart [a-zA-Z_:]
154XMLNamePart [a-zA-Z0-9.-_:]
155XMLName {XMLNameStart}{XMLNamePart}*
156@end
157
697d6fd2
JF
158%s Div
159%s RegExp
63cd45c9 160
691e4717
JF
161@begin E4X
162%x XMLContent
163%x XMLTag
164@end
165
e5332278
JF
166%%
167
697d6fd2 168<RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} E("")
63cd45c9 169
66fb559f 170\/\/[^\n]* L
fe123f47
JF
171
172 /* http://ostermiller.org/findcomment.html */
173 /* XXX: unify these two rules using !? */
2eb8215d 174\/\*!([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V() C I(comment, Comment(Y), Comment);
fe123f47 175\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V(N)
66fb559f 176
cb02f8ae 177@begin E4X
691e4717
JF
178<RegExp>"<>" L return tk::LeftRight;
179<XMLContent>"</>" L return tk::LeftSlashRight;
180
181<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() return tk::XMLCDATA;
182<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() return tk::XMLComment;
183<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() return tk::XMLPI;
184
185<XMLTag>"=" L return tk::Equal;
186<XMLTag>">" L return tk::Right;
187<XMLTag>"/>" L return tk::SlashRight;
188<XMLTag>"{" L return tk::OpenBrace;
189
2eb8215d 190<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() return tk::XMLAttributeValue;
691e4717
JF
191<XMLTag>{XMLName} L return tk::XMLName;
192<XMLTag>[ \t\r\n] V() return tk::XMLWhitespace;
db5e2840 193
691e4717
JF
194<XMLContent>"{" L return tk::OpenBrace;
195<XMLContent>"<" L return tk::Left;
196<XMLContent>"</" L return tk::LeftSlash;
197@end
198
199@begin E4X
cb02f8ae 200"::" L C return tk::ColonColon;
cb02f8ae 201".." L C return tk::PeriodPeriod;
cb02f8ae 202@end
ac9a5ce1 203
313708a9
JF
204@begin E4X ObjectiveC
205"@" L C return tk::At;
206@end
207
5befe15e
JF
208"&" L C return tk::Ampersand;
209"&&" L C return tk::AmpersandAmpersand;
210"&=" L C return tk::AmpersandEqual;
211"^" L C return tk::Carrot;
212"^=" L C return tk::CarrotEqual;
213"=" L C return tk::Equal;
214"==" L C return tk::EqualEqual;
215"===" L C return tk::EqualEqualEqual;
216"!" L C return tk::Exclamation;
217"!=" L C return tk::ExclamationEqual;
218"!==" L C return tk::ExclamationEqualEqual;
219"-" L C return tk::Hyphen;
220"-=" L C return tk::HyphenEqual;
221"--" L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
222"->" L C return tk::HyphenRight;
223"<" L C return tk::Left;
224"<=" L C return tk::LeftEqual;
225"<<" L C return tk::LeftLeft;
226"<<=" L C return tk::LeftLeftEqual;
227"%" L C return tk::Percent;
228"%=" L C return tk::PercentEqual;
229"." L C return tk::Period;
230"|" L C return tk::Pipe;
231"|=" L C return tk::PipeEqual;
232"||" L C return tk::PipePipe;
233"+" L C return tk::Plus;
234"+=" L C return tk::PlusEqual;
235"++" L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
236">" L C return tk::Right;
237">=" L C return tk::RightEqual;
238">>" L C return tk::RightRight;
239">>=" L C return tk::RightRightEqual;
240">>>" L C return tk::RightRightRight;
241">>>=" L C return tk::RightRightRightEqual;
5befe15e
JF
242"*" L C return tk::Star;
243"*=" L C return tk::StarEqual;
244"~" L C return tk::Tilde;
245
697d6fd2
JF
246<Div>"/" L C return tk::Slash;
247<Div>"/=" L C return tk::SlashEqual;
248
5befe15e
JF
249":" L C return tk::Colon;
250"," L C return tk::Comma;
251"?" L C return tk::Question;
252";" L C return tk::SemiColon;
253
254"(" L C return tk::OpenParen;
255")" L C return tk::CloseParen;
256
6c093cce 257"{" L C return yylval->newline_ ? tk::OpenBrace_ : tk::OpenBrace;
5befe15e
JF
258"}" L C return tk::CloseBrace;
259
260"[" L C return tk::OpenBracket;
261"]" L C return tk::CloseBracket;
262
1ba6903e
JF
263@begin Java
264"@class" L C return tk::AtClass;
265@end
266
cb02f8ae 267@begin ObjectiveC
1ba6903e
JF
268"@end" L C return tk::AtEnd;
269"@implementation" L C return tk::AtImplementation;
270"@import" L C return tk::AtImport;
271"@selector" L C return tk::AtSelector;
cb02f8ae 272@end
d35a3b07 273
2eb8215d
JF
274"false" L C I(false, False(), False);
275"null" L C I(null, Null(), Null);
276"true" L C I(true, True(), True);
277
278"break" L R I(word, Word("break"), Break);
279"case" L C I(word, Word("case"), Case);
280"catch" L C I(word, Word("catch"), Catch);
281"continue" L R I(word, Word("continue"), Continue);
282"default" L C I(word, Word("default"), Default);
283"delete" L C I(word, Word("delete"), Delete);
284"do" L C I(word, Word("do"), Do);
285"else" L C I(word, Word("else"), Else);
286"finally" L C I(word, Word("finally"), Finally);
287"for" L C I(word, Word("for"), For);
288"function" L C I(word, Word("function"), Function);
289"if" L C I(word, Word("if"), If);
290"in" L C I(word, Word("in"), In);
291"instanceof" L C I(word, Word("instanceof"), InstanceOf);
292"new" L C I(word, Word("new"), New);
293"return" L R I(word, Word("return"), Return);
294"switch" L C I(word, Word("switch"), Switch);
295"this" L C I(this, This(), This);
296"throw" L R I(word, Word("throw"), Throw);
297"try" L C I(word, Word("try"), Try);
298"typeof" L C I(word, Word("typeof"), TypeOf);
299"var" L C I(word, Word("var"), Var);
300"void" L C I(word, Word("void"), Void);
301"while" L C I(word, Word("while"), While);
302"with" L C I(word, Word("with"), With);
303
304"debugger" L C I(word, Word("debugger"), Debugger);
305
306"const" L C I(word, Word("const"), Const);
307
308"class" L C I(word, Word("class"), Class);
309"enum" L C I(word, Word("enum"), Enum);
310"export" L C I(word, Word("export"), Export);
311"extends" L C I(word, Word("extends"), Extends);
312"import" L C I(word, Word("import"), Import);
313"super" L C I(word, Word("super"), Super);
314
315"implements" L C I(identifier, Identifier("implements"), Implements);
316"interface" L C I(identifier, Identifier("interface"), Interface);
317"package" L C I(identifier, Identifier("package"), Package);
318"private" L C I(identifier, Identifier("private"), Private);
319"protected" L C I(identifier, Identifier("protected"), Protected);
320"public" L C I(identifier, Identifier("public"), Public);
321"static" L C I(identifier, Identifier("static"), Static);
322
323"abstract" L C I(identifier, Identifier("abstract"), Abstract);
324"boolean" L C I(identifier, Identifier("boolean"), Boolean);
325"byte" L C I(identifier, Identifier("byte"), Byte);
326"char" L C I(identifier, Identifier("char"), Char);
327"double" L C I(identifier, Identifier("double"), Double);
328"final" L C I(identifier, Identifier("final"), Final);
329"float" L C I(identifier, Identifier("float"), Float);
330"goto" L C I(identifier, Identifier("goto"), Goto);
331"int" L C I(identifier, Identifier("int"), Int);
332"long" L C I(identifier, Identifier("long"), Long);
333"native" L C I(identifier, Identifier("native"), Native);
334"short" L C I(identifier, Identifier("short"), Short);
335"synchronized" L C I(identifier, Identifier("synchronized"), Synchronized);
336"throws" L C I(identifier, Identifier("throws"), Throws);
337"transient" L C I(identifier, Identifier("transient"), Transient);
338"volatile" L C I(identifier, Identifier("volatile"), Volatile);
339
340"let" L C I(identifier, Identifier("let"), Let);
341"yield" L C I(identifier, Identifier("yield"), Yield);
342
343"each" L C I(identifier, Identifier("each"), Each);
5d646fb5 344
691e4717 345@begin E4X
2eb8215d
JF
346"namespace" L C I(identifier, Identifier("namespace"), Namespace);
347"xml" L C I(identifier, Identifier("xml"), XML);
691e4717
JF
348@end
349
2eb8215d 350{IdentifierStart}{IdentifierPart}* L C I(identifier, Identifier(Y), Identifier_);
5d646fb5 351
2eb8215d 352(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C I(number, Number(strtod(yytext, NULL)), NumericLiteral);
5d646fb5 353
2eb8215d
JF
3540[xX][0-9a-fA-F]+ L C I(number, Number(strtoull(yytext + 2, NULL, 16)), NumericLiteral);
3550[0-7]+ L C I(number, Number(strtoull(yytext + 1, NULL, 8)), NumericLiteral);
3560[bB][0-1]+ L C I(number, Number(strtoull(yytext + 2, NULL, 2)), NumericLiteral);
5befe15e 357
931b816a 358\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
2eb8215d 359 char *value(A char[yyleng]);
931b816a
JF
360 char *local(value);
361
66f8d960 362 for (yy_size_t i(1), e(yyleng - 1); i != e; ++i) {
931b816a
JF
363 char next(yytext[i]);
364
365 if (yytext[i] == '\\')
366 switch (next = yytext[++i]) {
367eebb1 367 case '\n': continue;
931b816a
JF
368 case '\\': next = '\\'; break;
369 case '\'': next = '\''; break;
370 case '"': next = '"'; break;
371 case 'b': next = '\b'; break;
372 case 'f': next = '\f'; break;
373 case 'n': next = '\n'; break;
374 case 'r': next = '\r'; break;
375 case 't': next = '\t'; break;
376 case 'v': next = '\v'; break;
377 case '0': next = '\0'; break;
378
379 case 'x':
380 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
381 i += 2;
382 break;
383 }
384
385 *local++ = next;
386 }
387
388 *local = '\0';
2eb8215d 389 I(string, String(value, local - value), StringLiteral);
931b816a 390}
5befe15e 391
b10bd496 392\r?\n yylloc->end.lines(); yylloc->step(); N
5befe15e
JF
393
394[ \t] L
7e5391fd 395
972c6054 396<<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; return tk::AutoComplete; } L yyterminate();
94d55b5c 397
48e3be8a 398. L {
94d55b5c
JF
399 CYDriver::Error error;
400 error.location_ = *yylloc;
401 error.message_ = "syntax error, unknown token";
402 yyextra->errors_.push_back(error);
403 yyterminate();
404}
924f67b2
JF
405
406%%
407
5999c315 408void CYDriver::ScannerInit() {
924f67b2
JF
409 cylex_init(&scanner_);
410 cyset_extra(this, scanner_);
411}
412
5999c315 413void CYDriver::ScannerDestroy() {
924f67b2
JF
414 cylex_destroy(scanner_);
415}
63cd45c9 416
691e4717
JF
417CYDriver::Condition CYDriver::GetCondition() {
418 switch (yy_top_state(scanner_)) {
419 case RegExp:
420 return RegExpCondition;
421@begin E4X
422 case XMLContent:
423 return XMLContentCondition;
424 case XMLTag:
425 return XMLTagCondition;
426@end
427 default:
428 _assert(false);
429 }
430}
431
432void CYDriver::SetCondition(Condition condition) {
63cd45c9
JF
433 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
434
435 switch (condition) {
697d6fd2
JF
436 case RegExpCondition:
437 BEGIN(RegExp);
63cd45c9 438 break;
691e4717
JF
439@begin E4X
440 case XMLContentCondition:
441 BEGIN(XMLContent);
442 break;
443 case XMLTagCondition:
444 BEGIN(XMLTag);
445 break;
446@end
63cd45c9
JF
447 default:
448 _assert(false);
449 }
450}
691e4717
JF
451
452void CYDriver::PushCondition(Condition condition) {
453 switch (condition) {
454 case RegExpCondition:
455 yy_push_state(RegExp, scanner_);
456 break;
457@begin E4X
458 case XMLContentCondition:
459 yy_push_state(XMLContent, scanner_);
460 break;
461 case XMLTagCondition:
462 yy_push_state(XMLTag, scanner_);
463 break;
464@end
465 default:
466 _assert(false);
467 }
468}
469
470void CYDriver::PopCondition() {
471 yy_pop_state(scanner_);
472}