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