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