]> git.saurik.com Git - cycript.git/blame - Cycript.l.in
Updated todo, license, and title.
[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
cb02f8ae
JF
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
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
697d6fd2 49#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
db5e2840
JF
50#define C T yyextra->state_ = CYClear;
51#define R T yyextra->state_ = CYRestricted;
5befe15e 52
697d6fd2 53#define E(prefix) L C { \
63cd45c9
JF
54 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng + sizeof(prefix)))); \
55 memcpy(value, prefix, sizeof(prefix) - 1); \
56 memcpy(value + sizeof(prefix) - 1, yytext, yyleng); \
57 value[yyleng + sizeof(prefix) - 1] = '\0'; \
5d646fb5 58 yylval->literal_ = new(yyextra->pool_) CYRegEx(value); \
63cd45c9
JF
59 return tk::RegularExpressionLiteral; \
60}
61
5befe15e
JF
62#define N \
63 if (yyextra->state_ != CYNewLine) { \
64 bool restricted(yyextra->state_ == CYRestricted); \
65 if (restricted) { \
66 yyextra->state_ = CYClear; \
67 return tk::NewLine; \
68 } else \
69 yyextra->state_ = CYNewLine; \
70 }
71
691e4717 72#define V(more) { \
cb02f8ae
JF
73 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
74 unsigned lines(0); \
75 size_t left; \
76 do { \
77 ++lines; \
78 left = yyleng - (nl - yytext) - 1; \
79 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
80 } while (nl != NULL); \
81 yylloc->end.lines(lines); \
82 yylloc->end.columns(left); \
83 yylloc->step(); \
691e4717 84 more \
cb02f8ae
JF
85 } else L \
86}
87
5befe15e
JF
88#define L { \
89 yylloc->step(); \
90 yylloc->columns(yyleng); \
91}
e7ed5354 92
931b816a
JF
93int H(char c) {
94 if (c >= '0' && c <= '9')
95 return c - '0';
96 if (c >= 'a' && c <= 'f')
97 return c - 'a' + 10;
98 if (c >= 'A' && c <= 'F')
99 return c - 'A' + 10;
100 return -1;
101}
102
e7ed5354 103#define YY_INPUT(data, value, size) { \
48e3be8a
JF
104 if (yyextra->file_ != NULL) { \
105 size_t copy(fread(data, 1, size, yyextra->file_)); \
106 value = copy == 0 ? YY_NULL : copy; \
107 } else if (yyextra->size_ == 0) \
e7ed5354
JF
108 value = YY_NULL; \
109 else { \
110 size_t copy(std::min(size, yyextra->size_)); \
111 memcpy(data, yyextra->data_, copy); \
112 yyextra->data_ += copy; \
113 yyextra->size_ -= copy; \
114 value = copy; \
115 } \
116}
117
e5332278
JF
118%}
119
120%option prefix="cy"
121%option bison-bridge
122%option bison-locations
123%option noyywrap
124%option yylineno
125%option nounput
126%option interactive
924f67b2 127%option reentrant
691e4717 128%option stack
e5332278 129
2bf24581 130Exponent [eE][+-]?[0-9]+
367eebb1 131Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
e5332278 132
63cd45c9
JF
133IdentifierStart [a-zA-Z$_]
134IdentifierPart [a-zA-Z$_0-9]
135
136NonTerminator [^\n]
137BackslashSequence \\{NonTerminator}
138RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
139RegularExpressionChar [^\n\\/]|{BackslashSequence}
140RegularExpressionFlags {IdentifierPart}*
141RegularExpressionChars {RegularExpressionChar}*
697d6fd2 142RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
63cd45c9 143
691e4717
JF
144@begin E4X
145XMLNameStart [a-zA-Z_:]
146XMLNamePart [a-zA-Z0-9.-_:]
147XMLName {XMLNameStart}{XMLNamePart}*
148@end
149
697d6fd2
JF
150%s Div
151%s RegExp
63cd45c9 152
691e4717
JF
153@begin E4X
154%x XMLContent
155%x XMLTag
156@end
157
e5332278
JF
158%%
159
697d6fd2 160<RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} E("")
63cd45c9 161
66fb559f 162\/\/[^\n]* L
691e4717 163\/\*(\n|[^\*]|\*[^/])*\*\/ V(N)
66fb559f 164
cb02f8ae 165@begin E4X
691e4717
JF
166<RegExp>"<>" L return tk::LeftRight;
167<XMLContent>"</>" L return tk::LeftSlashRight;
168
169<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() return tk::XMLCDATA;
170<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() return tk::XMLComment;
171<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() return tk::XMLPI;
172
173<XMLTag>"=" L return tk::Equal;
174<XMLTag>">" L return tk::Right;
175<XMLTag>"/>" L return tk::SlashRight;
176<XMLTag>"{" L return tk::OpenBrace;
177
178<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() {
179 return tk::XMLAttributeValue;
180}
181
182<XMLTag>{XMLName} L return tk::XMLName;
183<XMLTag>[ \t\r\n] V() return tk::XMLWhitespace;
db5e2840 184
691e4717
JF
185<XMLContent>"{" L return tk::OpenBrace;
186<XMLContent>"<" L return tk::Left;
187<XMLContent>"</" L return tk::LeftSlash;
188@end
189
190@begin E4X
ac9a5ce1 191"@" L C return tk::At;
cb02f8ae 192"::" L C return tk::ColonColon;
cb02f8ae 193".." L C return tk::PeriodPeriod;
cb02f8ae 194@end
ac9a5ce1 195
5befe15e
JF
196"&" L C return tk::Ampersand;
197"&&" L C return tk::AmpersandAmpersand;
198"&=" L C return tk::AmpersandEqual;
199"^" L C return tk::Carrot;
200"^=" L C return tk::CarrotEqual;
201"=" L C return tk::Equal;
202"==" L C return tk::EqualEqual;
203"===" L C return tk::EqualEqualEqual;
204"!" L C return tk::Exclamation;
205"!=" L C return tk::ExclamationEqual;
206"!==" L C return tk::ExclamationEqualEqual;
207"-" L C return tk::Hyphen;
208"-=" L C return tk::HyphenEqual;
209"--" L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
210"->" L C return tk::HyphenRight;
211"<" L C return tk::Left;
212"<=" L C return tk::LeftEqual;
213"<<" L C return tk::LeftLeft;
214"<<=" L C return tk::LeftLeftEqual;
215"%" L C return tk::Percent;
216"%=" L C return tk::PercentEqual;
217"." L C return tk::Period;
218"|" L C return tk::Pipe;
219"|=" L C return tk::PipeEqual;
220"||" L C return tk::PipePipe;
221"+" L C return tk::Plus;
222"+=" L C return tk::PlusEqual;
223"++" L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
224">" L C return tk::Right;
225">=" L C return tk::RightEqual;
226">>" L C return tk::RightRight;
227">>=" L C return tk::RightRightEqual;
228">>>" L C return tk::RightRightRight;
229">>>=" L C return tk::RightRightRightEqual;
5befe15e
JF
230"*" L C return tk::Star;
231"*=" L C return tk::StarEqual;
232"~" L C return tk::Tilde;
233
697d6fd2
JF
234<Div>"/" L C return tk::Slash;
235<Div>"/=" L C return tk::SlashEqual;
236
5befe15e
JF
237":" L C return tk::Colon;
238"," L C return tk::Comma;
239"?" L C return tk::Question;
240";" L C return tk::SemiColon;
241
242"(" L C return tk::OpenParen;
243")" L C return tk::CloseParen;
244
245"{" L C return tk::OpenBrace;
246"}" L C return tk::CloseBrace;
247
248"[" L C return tk::OpenBracket;
249"]" L C return tk::CloseBracket;
250
cb02f8ae 251@begin ObjectiveC
b09da87b
JF
252"@class" L C return tk::AtClass;
253"@end" L C return tk::AtEnd;
d35a3b07 254"@selector" L C return tk::AtSelector;
cb02f8ae 255@end
d35a3b07 256
5d646fb5
JF
257"false" L C yylval->false_ = new(yyextra->pool_) CYFalse(); return tk::False;
258"null" L C yylval->null_ = new(yyextra->pool_) CYNull(); return tk::Null;
259"true" L C yylval->true_ = new(yyextra->pool_) CYTrue(); return tk::True;
260
261"break" L R yylval->word_ = new(yyextra->pool_) CYWord("break"); return tk::Break;
262"case" L C yylval->word_ = new(yyextra->pool_) CYWord("case"); return tk::Case;
263"catch" L C yylval->word_ = new(yyextra->pool_) CYWord("catch"); return tk::Catch;
264"continue" L R yylval->word_ = new(yyextra->pool_) CYWord("continue"); return tk::Continue;
265"default" L C yylval->word_ = new(yyextra->pool_) CYWord("default"); return tk::Default;
266"delete" L C yylval->word_ = new(yyextra->pool_) CYWord("delete"); return tk::Delete;
267"do" L C yylval->word_ = new(yyextra->pool_) CYWord("do"); return tk::Do;
268"else" L C yylval->word_ = new(yyextra->pool_) CYWord("else"); return tk::Else;
269"finally" L C yylval->word_ = new(yyextra->pool_) CYWord("finally"); return tk::Finally;
270"for" L C yylval->word_ = new(yyextra->pool_) CYWord("for"); return tk::For;
271"function" L C yylval->word_ = new(yyextra->pool_) CYWord("function"); return tk::Function;
272"if" L C yylval->word_ = new(yyextra->pool_) CYWord("if"); return tk::If;
273"in" L C yylval->word_ = new(yyextra->pool_) CYWord("in"); return tk::In;
274"instanceof" L C yylval->word_ = new(yyextra->pool_) CYWord("instanceof"); return tk::InstanceOf;
275"new" L C yylval->word_ = new(yyextra->pool_) CYWord("new"); return tk::New;
276"return" L R yylval->word_ = new(yyextra->pool_) CYWord("return"); return tk::Return;
277"switch" L C yylval->word_ = new(yyextra->pool_) CYWord("switch"); return tk::Switch;
278"this" L C yylval->this_ = new(yyextra->pool_) CYThis(); return tk::This;
279"throw" L R yylval->word_ = new(yyextra->pool_) CYWord("throw"); return tk::Throw;
280"try" L C yylval->word_ = new(yyextra->pool_) CYWord("try"); return tk::Try;
281"typeof" L C yylval->word_ = new(yyextra->pool_) CYWord("typeof"); return tk::TypeOf;
282"var" L C yylval->word_ = new(yyextra->pool_) CYWord("var"); return tk::Var;
283"void" L C yylval->word_ = new(yyextra->pool_) CYWord("void"); return tk::Void;
284"while" L C yylval->word_ = new(yyextra->pool_) CYWord("while"); return tk::While;
285"with" L C yylval->word_ = new(yyextra->pool_) CYWord("with"); return tk::With;
286
287"debugger" L C yylval->word_ = new(yyextra->pool_) CYWord("debugger"); return tk::Debugger;
288
289"const" L C yylval->word_ = new(yyextra->pool_) CYWord("const"); return tk::Const;
290
291"class" L C yylval->word_ = new(yyextra->pool_) CYWord("class"); return tk::Class;
292"enum" L C yylval->word_ = new(yyextra->pool_) CYWord("enum"); return tk::Enum;
293"export" L C yylval->word_ = new(yyextra->pool_) CYWord("export"); return tk::Export;
294"extends" L C yylval->word_ = new(yyextra->pool_) CYWord("extends"); return tk::Extends;
295"import" L C yylval->word_ = new(yyextra->pool_) CYWord("import"); return tk::Import;
296"super" L C yylval->word_ = new(yyextra->pool_) CYWord("super"); return tk::Super;
297
298"implements" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("implements"); return tk::Implements;
299"interface" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("interface"); return tk::Interface;
300"package" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("package"); return tk::Package;
301"private" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("private"); return tk::Private;
302"protected" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("protected"); return tk::Protected;
303"public" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("public"); return tk::Public;
304"static" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("static"); return tk::Static;
305
306"abstract" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("abstract"); return tk::Abstract;
307"boolean" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("boolean"); return tk::Boolean;
308"byte" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("byte"); return tk::Byte;
309"char" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("char"); return tk::Char;
310"double" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("double"); return tk::Double;
311"final" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("final"); return tk::Final;
312"float" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("float"); return tk::Float;
313"goto" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("goto"); return tk::Goto;
314"int" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("int"); return tk::Int;
315"long" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("long"); return tk::Long;
316"native" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("native"); return tk::Native;
317"short" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("short"); return tk::Short;
318"synchronized" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("synchronized"); return tk::Synchronized;
319"throws" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("throws"); return tk::Throws;
320"transient" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("transient"); return tk::Transient;
321"volatile" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("volatile"); return tk::Volatile;
322
323"let" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("let"); return tk::Let;
324"yield" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("yield"); return tk::Yield;
325
326"each" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("each"); return tk::Each;
327
691e4717
JF
328@begin E4X
329"namespace" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("namespace"); return tk::Namespace;
330"xml" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("xml"); return tk::XML;
331@end
332
697d6fd2 333{IdentifierStart}{IdentifierPart}* L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); return tk::Identifier_;
5d646fb5 334
697d6fd2 335(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C yylval->number_ = new(yyextra->pool_) CYNumber(strtod(yytext, NULL)); return tk::NumericLiteral;
5d646fb5
JF
336
3370[xX][0-9a-fA-F]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
338
3390[bB][0-1]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;
5befe15e 340
931b816a
JF
341\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
342 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
343 char *local(value);
344
345 for (int i(1); i != yyleng - 1; ++i) {
346 char next(yytext[i]);
347
348 if (yytext[i] == '\\')
349 switch (next = yytext[++i]) {
367eebb1 350 case '\n': continue;
931b816a
JF
351 case '\\': next = '\\'; break;
352 case '\'': next = '\''; break;
353 case '"': next = '"'; break;
354 case 'b': next = '\b'; break;
355 case 'f': next = '\f'; break;
356 case 'n': next = '\n'; break;
357 case 'r': next = '\r'; break;
358 case 't': next = '\t'; break;
359 case 'v': next = '\v'; break;
360 case '0': next = '\0'; break;
361
362 case 'x':
363 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
364 i += 2;
365 break;
366 }
367
368 *local++ = next;
369 }
370
371 *local = '\0';
5d646fb5 372 yylval->string_ = new(yyextra->pool_) CYString(value, local - value);
931b816a
JF
373 return tk::StringLiteral;
374}
5befe15e 375
b10bd496 376\r?\n yylloc->end.lines(); yylloc->step(); N
5befe15e
JF
377
378[ \t] L
379<<EOF>> L yyterminate();
94d55b5c 380
48e3be8a 381. L {
94d55b5c
JF
382 CYDriver::Error error;
383 error.location_ = *yylloc;
384 error.message_ = "syntax error, unknown token";
385 yyextra->errors_.push_back(error);
386 yyterminate();
387}
924f67b2
JF
388
389%%
390
5999c315 391void CYDriver::ScannerInit() {
924f67b2
JF
392 cylex_init(&scanner_);
393 cyset_extra(this, scanner_);
394}
395
5999c315 396void CYDriver::ScannerDestroy() {
924f67b2
JF
397 cylex_destroy(scanner_);
398}
63cd45c9 399
691e4717
JF
400CYDriver::Condition CYDriver::GetCondition() {
401 switch (yy_top_state(scanner_)) {
402 case RegExp:
403 return RegExpCondition;
404@begin E4X
405 case XMLContent:
406 return XMLContentCondition;
407 case XMLTag:
408 return XMLTagCondition;
409@end
410 default:
411 _assert(false);
412 }
413}
414
415void CYDriver::SetCondition(Condition condition) {
63cd45c9
JF
416 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
417
418 switch (condition) {
697d6fd2
JF
419 case RegExpCondition:
420 BEGIN(RegExp);
63cd45c9 421 break;
691e4717
JF
422@begin E4X
423 case XMLContentCondition:
424 BEGIN(XMLContent);
425 break;
426 case XMLTagCondition:
427 BEGIN(XMLTag);
428 break;
429@end
63cd45c9
JF
430 default:
431 _assert(false);
432 }
433}
691e4717
JF
434
435void CYDriver::PushCondition(Condition condition) {
436 switch (condition) {
437 case RegExpCondition:
438 yy_push_state(RegExp, scanner_);
439 break;
440@begin E4X
441 case XMLContentCondition:
442 yy_push_state(XMLContent, scanner_);
443 break;
444 case XMLTagCondition:
445 yy_push_state(XMLTag, scanner_);
446 break;
447@end
448 default:
449 _assert(false);
450 }
451}
452
453void CYDriver::PopCondition() {
454 yy_pop_state(scanner_);
455}