1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
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.
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.
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/>.
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 */
25 #define YYLTYPE cy::location
26 #include "Cycript.tab.hh"
27 typedef cy::parser::token tk;
29 #define YY_EXTRA_TYPE CYDriver *
31 #define F(value) do { \
33 yyextra->no_.AtImplementation = false; \
34 yyextra->no_.Function = false; \
35 yyextra->no_.OpenBrace = false; \
40 #define Y apr_pstrmemdup($pool, yytext, yyleng)
42 #define I(type, Type, value) do { \
43 yylval->type ## _ = A CY ## Type; \
47 #define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
48 #define C T yyextra->state_ = CYClear;
49 #define R T yyextra->state_ = CYRestricted;
52 if (yyextra->state_ != CYNewLine) { \
53 if (yyextra->state_ != CYRestricted) \
54 yyextra->state_ = CYNewLine; \
56 yyextra->state_ = CYClear; \
62 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
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); \
79 yylloc->columns(yyleng); \
83 if (c >= '0' && c <= '9')
85 if (c >= 'a' && c <= 'f')
87 if (c >= 'A' && c <= 'F')
92 #define YY_INPUT(data, value, size) { \
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) \
100 copy = (std::min(copy, yyextra->size_)); \
101 memcpy(data, yyextra->data_, copy); \
102 yyextra->data_ += copy; \
103 yyextra->size_ -= copy; \
112 %option bison-locations
120 Exponent [eE][+-]?[0-9]+
121 Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
123 IdentifierStart [a-zA-Z$_]
124 IdentifierPart [a-zA-Z$_0-9]
127 BackslashSequence \\{NonTerminator}
128 RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
129 RegularExpressionChar [^\n\\/]|{BackslashSequence}
130 RegularExpressionFlags {IdentifierPart}*
131 RegularExpressionChars {RegularExpressionChar}*
132 RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
135 XMLNameStart [a-zA-Z_:]
136 XMLNamePart [a-zA-Z0-9.-_:]
137 XMLName {XMLNameStart}{XMLNamePart}*
150 <RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} L C I(literal, RegEx(Y), tk::RegularExpressionLiteral);
154 /* http://ostermiller.org/findcomment.html */
155 /* XXX: unify these two rules using !? */
156 \/\*!([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V() C I(comment, Comment(Y), tk::Comment);
157 \/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V(N)
160 <RegExp>"<>" L F(tk::LeftRight);
161 <XMLContent>"</>" L F(tk::LeftSlashRight);
163 <RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() F(tk::XMLCDATA);
164 <RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() F(tk::XMLComment);
165 <RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() F(tk::XMLPI);
167 <XMLTag>"=" L F(tk::Equal);
168 <XMLTag>">" L F(tk::Right);
169 <XMLTag>"/>" L F(tk::SlashRight);
170 <XMLTag>"{" L F(tk::OpenBrace);
172 <XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() F(tk::XMLAttributeValue);
173 <XMLTag>{XMLName} L F(tk::XMLName);
174 <XMLTag>[ \t\r\n] V() F(tk::XMLWhitespace);
176 <XMLContent>"{" L F(tk::OpenBrace);
177 <XMLContent>"<" L F(tk::Left);
178 <XMLContent>"</" L F(tk::LeftSlash);
181 "..." L C F(tk::PeriodPeriodPeriod);
184 "::" L C F(tk::ColonColon);
185 ".." L C F(tk::PeriodPeriod);
188 @begin E4X ObjectiveC
192 "&" L C F(tk::Ampersand);
193 "&&" L C F(tk::AmpersandAmpersand);
194 "&=" L C F(tk::AmpersandEqual);
195 "^" L C F(tk::Carrot);
196 "^=" L C F(tk::CarrotEqual);
197 "=" L C F(tk::Equal);
198 "==" L C F(tk::EqualEqual);
199 "===" L C F(tk::EqualEqualEqual);
200 "=>" L C F(tk::EqualRight);
201 "!" L C F(tk::Exclamation);
202 "!=" L C F(tk::ExclamationEqual);
203 "!==" L C F(tk::ExclamationEqualEqual);
204 "-" L C F(tk::Hyphen);
205 "-=" L C F(tk::HyphenEqual);
206 "--" L C F(yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen);
207 "->" L C F(tk::HyphenRight);
209 "<=" L C F(tk::LeftEqual);
210 "<<" L C F(tk::LeftLeft);
211 "<<=" L C F(tk::LeftLeftEqual);
212 "%" L C F(tk::Percent);
213 "%=" L C F(tk::PercentEqual);
214 "." L C F(tk::Period);
216 "|=" L C F(tk::PipeEqual);
217 "||" L C F(tk::PipePipe);
219 "+=" L C F(tk::PlusEqual);
220 "++" L C F(yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus);
221 ">" L C F(tk::Right);
222 ">=" L C F(tk::RightEqual);
223 ">>" L C F(tk::RightRight);
224 ">>=" L C F(tk::RightRightEqual);
225 ">>>" L C F(tk::RightRightRight);
226 ">>>=" L C F(tk::RightRightRightEqual);
228 "*=" L C F(tk::StarEqual);
229 "~" L C F(tk::Tilde);
231 <Div>"/" L C F(tk::Slash);
232 <Div>"/=" L C F(tk::SlashEqual);
234 ":" L C F(tk::Colon);
235 "," L C F(tk::Comma);
236 "?" L C F(tk::Question);
237 ";" L C F(tk::SemiColon);
239 "(" L C F(tk::OpenParen);
240 ")" L C F(tk::CloseParen);
242 "{" L C F(yyextra->no_.OpenBrace ? tk::OpenBrace__ : yylval->newline_ ? tk::OpenBrace_ : tk::OpenBrace);
243 "}" L C F(tk::CloseBrace);
245 "[" L C F(tk::OpenBracket);
246 "]" L C F(tk::CloseBracket);
249 "@class" L C F(tk::AtClass);
253 "@end" L C F(tk::AtEnd);
254 "@implementation" L C F(yyextra->no_.AtImplementation ? tk::AtImplementation_ : tk::AtImplementation);
255 "@import" L C F(tk::AtImport);
256 "@selector" L C F(tk::AtSelector);
259 "false" L C I(false, False(), tk::False);
260 "null" L C I(null, Null(), tk::Null);
261 "true" L C I(true, True(), tk::True);
263 "auto" L C I(word, Word("auto"), tk::Auto);
264 "break" L R I(word, Word("break"), tk::Break);
265 "case" L C I(word, Word("case"), tk::Case);
266 "catch" L C I(word, Word("catch"), tk::Catch);
267 "continue" L R I(word, Word("continue"), tk::Continue);
268 "default" L C I(word, Word("default"), tk::Default);
269 "delete" L C I(word, Word("delete"), tk::Delete);
270 "do" L C I(word, Word("do"), tk::Do);
271 "else" L C I(word, Word("else"), tk::Else);
272 "finally" L C I(word, Word("finally"), tk::Finally);
273 "for" L C I(word, Word("for"), tk::For);
274 "function" L C I(word, Word("function"), yyextra->no_.Function ? tk::Function_ : tk::Function);
275 "if" L C I(word, Word("if"), tk::If);
276 "in" L C I(word, Word("in"), yyextra->in_.top() ? tk::In_ : tk::In);
277 "instanceof" L C I(word, Word("instanceof"), tk::InstanceOf);
278 "new" L C I(word, Word("new"), tk::New);
279 "return" L R I(word, Word("return"), tk::Return);
280 "switch" L C I(word, Word("switch"), tk::Switch);
281 "this" L C I(this, This(), tk::This);
282 "throw" L R I(word, Word("throw"), tk::Throw);
283 "try" L C I(word, Word("try"), tk::Try);
284 "typeof" L C I(word, Word("typeof"), tk::TypeOf);
285 "var" L C I(word, Word("var"), tk::Var);
286 "void" L C I(word, Word("void"), tk::Void);
287 "while" L C I(word, Word("while"), tk::While);
288 "with" L C I(word, Word("with"), tk::With);
290 "debugger" L C I(word, Word("debugger"), tk::Debugger);
292 "const" L C I(word, Word("const"), tk::Const);
294 "class" L C I(word, Word("class"), tk::Class);
295 "enum" L C I(word, Word("enum"), tk::Enum);
296 "export" L C I(word, Word("export"), tk::Export);
297 "extends" L C I(word, Word("extends"), tk::Extends);
298 "import" L C I(word, Word("import"), tk::Import);
299 "super" L C I(word, Word("super"), tk::Super);
301 "implements" L C I(identifier, Identifier("implements"), tk::Implements);
302 "interface" L C I(identifier, Identifier("interface"), tk::Interface);
303 "package" L C I(identifier, Identifier("package"), tk::Package);
304 "private" L C I(identifier, Identifier("private"), tk::Private);
305 "protected" L C I(identifier, Identifier("protected"), tk::Protected);
306 "public" L C I(identifier, Identifier("public"), tk::Public);
307 "static" L C I(identifier, Identifier("static"), tk::Static);
309 "abstract" L C I(identifier, Identifier("abstract"), tk::Abstract);
310 "boolean" L C I(identifier, Identifier("boolean"), tk::Boolean);
311 "byte" L C I(identifier, Identifier("byte"), tk::Byte);
312 "char" L C I(identifier, Identifier("char"), tk::Char);
313 "double" L C I(identifier, Identifier("double"), tk::Double);
314 "final" L C I(identifier, Identifier("final"), tk::Final);
315 "float" L C I(identifier, Identifier("float"), tk::Float);
316 "goto" L C I(identifier, Identifier("goto"), tk::Goto);
317 "int" L C I(identifier, Identifier("int"), tk::Int);
318 "long" L C I(identifier, Identifier("long"), tk::Long);
319 "native" L C I(identifier, Identifier("native"), tk::Native);
320 "short" L C I(identifier, Identifier("short"), tk::Short);
321 "synchronized" L C I(identifier, Identifier("synchronized"), tk::Synchronized);
322 "throws" L C I(identifier, Identifier("throws"), tk::Throws);
323 "transient" L C I(identifier, Identifier("transient"), tk::Transient);
324 "volatile" L C I(identifier, Identifier("volatile"), tk::Volatile);
326 "let" L C I(identifier, Identifier("let"), tk::Let);
327 "yield" L C I(identifier, Identifier("yield"), tk::Yield);
329 "each" L C I(identifier, Identifier("each"), tk::Each);
330 "of" L C I(identifier, Identifier("of"), tk::Of);
333 "namespace" L C I(identifier, Identifier("namespace"), tk::Namespace);
334 "xml" L C I(identifier, Identifier("xml"), tk::XML);
337 {IdentifierStart}{IdentifierPart}* L C I(identifier, Identifier(Y), tk::Identifier_);
339 (\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C I(number, Number(strtod(yytext, NULL)), tk::NumericLiteral);
341 0[xX][0-9a-fA-F]+ L C I(number, Number(strtoull(yytext + 2, NULL, 16)), tk::NumericLiteral);
342 0[0-7]+ L C I(number, Number(strtoull(yytext + 1, NULL, 8)), tk::NumericLiteral);
343 0[bB][0-1]+ L C I(number, Number(strtoull(yytext + 2, NULL, 2)), tk::NumericLiteral);
345 \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
346 char *value(A char[yyleng]);
349 for (yy_size_t i(1), e(yyleng - 1); i != e; ++i) {
350 char next(yytext[i]);
352 if (yytext[i] == '\\')
353 switch (next = yytext[++i]) {
355 case '\\': next = '\\'; break;
356 case '\'': next = '\''; break;
357 case '"': next = '"'; break;
358 case 'b': next = '\b'; break;
359 case 'f': next = '\f'; break;
360 case 'n': next = '\n'; break;
361 case 'r': next = '\r'; break;
362 case 't': next = '\t'; break;
363 case 'v': next = '\v'; break;
364 case '0': next = '\0'; break;
367 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
376 I(string, String(value, local - value), tk::StringLiteral);
379 \r?\n|\r|\xe2\x80[\xa8\xa9] yylloc->end.lines(); yylloc->step(); N
383 <<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; F(tk::AutoComplete); } L yyterminate();
386 CYDriver::Error error;
387 error.location_ = *yylloc;
388 error.message_ = "syntax error, unknown token";
389 yyextra->errors_.push_back(error);
395 void CYDriver::ScannerInit() {
396 cylex_init(&scanner_);
397 cyset_extra(this, scanner_);
400 void CYDriver::ScannerDestroy() {
401 cylex_destroy(scanner_);
404 CYDriver::Condition CYDriver::GetCondition() {
405 switch (yy_top_state(scanner_)) {
407 return RegExpCondition;
410 return XMLContentCondition;
412 return XMLTagCondition;
419 void CYDriver::SetCondition(Condition condition) {
420 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
423 case RegExpCondition:
427 case XMLContentCondition:
430 case XMLTagCondition:
439 void CYDriver::PushCondition(Condition condition) {
441 case RegExpCondition:
442 yy_push_state(RegExp, scanner_);
445 case XMLContentCondition:
446 yy_push_state(XMLContent, scanner_);
448 case XMLTagCondition:
449 yy_push_state(XMLTag, scanner_);
457 void CYDriver::PopCondition() {
458 yy_pop_state(scanner_);