1 /* Cycript - Remove Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
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
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.
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.
47 #include "location.hh"
48 #include "Pooling.hpp"
50 template <typename Type_>
64 void SetNext(Type_ *next) {
70 virtual void Output(struct CYOutput &out) const = 0;
78 CYOutput(std::ostream &out) :
85 _finline CYOutput &operator <<(char rhs) {
90 _finline CYOutput &operator <<(const char *rhs) {
95 _finline CYOutput &operator <<(const CYThing &rhs) {
103 struct CYPropertyName {
104 virtual void PropertyName(CYOutput &out) const = 0;
108 virtual void ClassName(CYOutput &out, bool object) const = 0;
118 CYWord(const char *word) :
123 const char *Value() const {
127 virtual void Output(CYOutput &out) const;
129 virtual void ClassName(CYOutput &out, bool object) const;
130 virtual void PropertyName(CYOutput &out) const;
133 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
134 return lhs << rhs.Value();
137 struct CYIdentifier :
140 CYIdentifier(const char *word) :
151 CYLabel(CYIdentifier *name, CYLabel *next) :
152 CYNext<CYLabel>(next),
166 CYNoBrace = (1 << 0),
167 CYNoFunction = (1 << 1),
168 CYNoLeader = (1 << 2),
169 CYNoTrailer = (1 << 3),
171 CYNoHyphen = (1 << 5),
173 CYNoRightHand = (1 << 7),
174 CYNoDangle = (1 << 8),
175 CYNoTerminator = (1 << 9),
176 CYNoBF = (CYNoBrace | CYNoFunction),
189 void AddLabel(CYIdentifier *identifier) {
190 labels_ = new CYLabel(identifier, labels_);
193 virtual bool IsBlock() const {
194 return next_ != NULL;
197 virtual void Single(CYOutput &out, CYFlags flags) const;
198 virtual void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
201 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
207 CYStatement *statements_;
209 CYBlock(CYStatement *statements) :
210 statements_(statements)
214 virtual bool IsBlock() const {
218 virtual void Output(CYOutput &out, CYFlags flags) const;
245 std::string filename_;
249 cy::location location_;
250 std::string message_;
253 typedef std::vector<Error> Errors;
255 CYStatement *program_;
260 void ScannerDestroy();
263 CYDriver(const std::string &filename);
266 void SetCondition(Condition condition);
268 void Warning(const cy::location &location, const char *message);
271 struct CYForInitialiser {
272 virtual void For(CYOutput &out) const = 0;
275 struct CYForInInitialiser {
276 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
277 virtual const char *ForEachIn() const = 0;
278 virtual void ForEachIn(CYOutput &out) const = 0;
281 struct CYExpression :
282 CYNext<CYExpression>,
287 virtual unsigned Precedence() const = 0;
289 virtual bool RightHand() const {
293 virtual void For(CYOutput &out) const;
294 virtual void ForIn(CYOutput &out, CYFlags flags) const;
296 virtual const char *ForEachIn() const;
297 virtual void ForEachIn(CYOutput &out) const;
299 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
300 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
302 virtual void ClassName(CYOutput &out, bool object) const;
304 virtual const char *Word() const {
309 #define CYAlphabetic(value) \
310 virtual bool Alphabetic() const { \
314 #define CYPrecedence(value) \
315 virtual unsigned Precedence() const { \
319 #define CYRightHand(value) \
320 virtual bool RightHand() const { \
327 CYExpression *expressions_;
329 CYCompound(CYExpression *expressions) :
330 expressions_(expressions)
334 void AddPrev(CYExpression *expression) {
335 CYExpression *last(expression);
336 while (last->next_ != NULL)
338 last->SetNext(expressions_);
339 expressions_ = expression;
344 void Output(CYOutput &out, CYFlags flags) const;
347 struct CYComprehension :
348 CYNext<CYComprehension>
350 void Output(CYOutput &out) const;
351 virtual const char *Name() const = 0;
353 virtual void Begin_(CYOutput &out) const = 0;
355 virtual void End_(CYOutput &out) const {
359 struct CYForInComprehension :
365 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
371 virtual const char *Name() const {
372 return name_->Value();
375 virtual void Begin_(CYOutput &out) const;
378 struct CYForEachInComprehension :
384 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
390 virtual const char *Name() const {
391 return name_->Value();
394 virtual void Begin_(CYOutput &out) const;
395 virtual void End_(CYOutput &out) const;
398 struct CYIfComprehension :
403 CYIfComprehension(CYExpression *test) :
408 virtual const char *Name() const {
412 virtual void Begin_(CYOutput &out) const;
415 struct CYArrayComprehension :
418 CYExpression *expression_;
419 CYComprehension *comprehensions_;
421 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
422 expression_(expression),
423 comprehensions_(comprehensions)
429 virtual void Output(CYOutput &out, CYFlags flags) const;
446 struct CYSelectorPart :
447 CYNext<CYSelectorPart>
452 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
453 CYNext<CYSelectorPart>(next),
459 virtual void Output(CYOutput &out) const;
465 CYSelectorPart *name_;
467 CYSelector(CYSelectorPart *name) :
474 virtual void Output(CYOutput &out, CYFlags flags) const;
481 CYRange(uint64_t lo, uint64_t hi) :
486 bool operator [](uint8_t value) const {
487 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
490 void operator()(uint8_t value) {
493 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
497 extern CYRange DigitRange_;
498 extern CYRange WordStartRange_;
499 extern CYRange WordEndRange_;
508 CYString(const char *value, size_t size) :
514 CYString(const CYIdentifier *identifier) :
515 value_(identifier->Value()),
516 size_(strlen(value_))
520 const char *Value() const {
524 virtual const char *Word() const;
526 virtual void Output(CYOutput &out, CYFlags flags) const;
527 virtual void PropertyName(CYOutput &out) const;
536 CYNumber(double value) :
541 double Value() const {
545 virtual void Output(CYOutput &out, CYFlags flags) const;
546 virtual void PropertyName(CYOutput &out) const;
554 CYRegEx(const char *value) :
559 const char *Value() const {
563 virtual void Output(CYOutput &out, CYFlags flags) const;
575 virtual void Output(CYOutput &out, CYFlags flags) const;
587 virtual void Output(CYOutput &out, CYFlags flags) const;
593 virtual bool Value() const = 0;
594 virtual void Output(CYOutput &out, CYFlags flags) const;
606 virtual bool Value() const {
620 virtual bool Value() const {
630 CYVariable(CYIdentifier *name) :
638 virtual void Output(CYOutput &out, CYFlags flags) const;
646 CYPrefix(CYExpression *rhs) :
651 virtual bool Alphabetic() const = 0;
652 virtual const char *Operator() const = 0;
654 virtual void Output(CYOutput &out, CYFlags flags) const;
663 CYInfix(CYExpression *lhs, CYExpression *rhs) :
669 void SetLeft(CYExpression *lhs) {
673 virtual bool Alphabetic() const = 0;
674 virtual const char *Operator() const = 0;
676 virtual void Output(CYOutput &out, CYFlags flags) const;
684 CYPostfix(CYExpression *lhs) :
689 virtual const char *Operator() const = 0;
691 virtual void Output(CYOutput &out, CYFlags flags) const;
694 struct CYAssignment :
700 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
706 void SetLeft(CYExpression *lhs) {
710 virtual const char *Operator() const = 0;
712 virtual void Output(CYOutput &out, CYFlags flags) const;
719 CYExpression *value_;
721 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
722 CYNext<CYArgument>(next),
728 void Output(CYOutput &out) const;
747 CYClause(CYExpression *_case, CYStatement *code) :
753 virtual void Output(CYOutput &out) const;
759 CYExpression *value_;
761 CYElement(CYExpression *value, CYElement *next) :
762 CYNext<CYElement>(next),
767 void Output(CYOutput &out) const;
773 CYElement *elements_;
775 CYArray(CYElement *elements) :
780 virtual void Output(CYOutput &out, CYFlags flags) const;
783 struct CYDeclaration :
786 CYIdentifier *identifier_;
787 CYExpression *initialiser_;
789 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
790 identifier_(identifier),
791 initialiser_(initialiser)
795 virtual void ForIn(CYOutput &out, CYFlags flags) const;
797 virtual const char *ForEachIn() const;
798 virtual void ForEachIn(CYOutput &out) const;
800 virtual void Output(CYOutput &out, CYFlags flags) const;
803 struct CYDeclarations :
804 CYNext<CYDeclarations>,
807 CYDeclaration *declaration_;
809 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
810 CYNext<CYDeclarations>(next),
811 declaration_(declaration)
815 virtual void For(CYOutput &out) const;
816 virtual void Output(CYOutput &out, CYFlags flags) const;
822 CYDeclarations *declarations_;
824 CYVar(CYDeclarations *declarations) :
825 declarations_(declarations)
829 virtual void Output(CYOutput &out, CYFlags flags) const;
835 CYDeclarations *declarations_;
836 CYStatement *statements_;
838 CYLet(CYDeclarations *declarations, CYStatement *statements) :
839 declarations_(declarations),
840 statements_(statements)
844 virtual void Output(CYOutput &out, CYFlags flags) const;
850 virtual void Output(CYOutput &out) const;
853 struct CYMessageParameter :
854 CYNext<CYMessageParameter>
860 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
873 CYMessageParameter *parameter_;
876 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYStatement *body) :
879 parameter_(parameter),
884 virtual void Output(CYOutput &out, bool replace) const;
889 CYExpression *super_;
891 CYMessage *messages_;
893 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
901 virtual void Output(CYOutput &out, CYFlags flags) const;
904 struct CYClassExpression :
908 CYClassExpression(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
909 CYClass(name, super, fields, messages)
915 virtual void Output(CYOutput &out, CYFlags flags) const;
918 struct CYClassStatement :
922 CYClassStatement(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
923 CYClass(name, super, fields, messages)
927 virtual void Output(CYOutput &out, CYFlags flags) const;
934 CYMessage *messages_;
936 CYCategory(CYClassName *name, CYMessage *messages) :
942 virtual void Output(CYOutput &out, CYFlags flags) const;
945 struct CYFunctionParameter :
946 CYNext<CYFunctionParameter>,
951 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
952 CYNext<CYFunctionParameter>(next),
957 virtual void Output(CYOutput &out) const;
963 CYForInitialiser *initialiser_;
965 CYExpression *increment_;
968 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
969 initialiser_(initialiser),
971 increment_(increment),
976 virtual void Output(CYOutput &out, CYFlags flags) const;
982 CYForInInitialiser *initialiser_;
986 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
987 initialiser_(initialiser),
993 virtual void Output(CYOutput &out, CYFlags flags) const;
999 CYForInInitialiser *initialiser_;
1003 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1004 initialiser_(initialiser),
1010 virtual void Output(CYOutput &out, CYFlags flags) const;
1016 CYPropertyName *name_;
1017 CYExpression *value_;
1019 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
1020 CYNext<CYProperty>(next),
1026 virtual void Output(CYOutput &out) const;
1032 CYProperty *property_;
1034 CYObject(CYProperty *property) :
1039 void Output(CYOutput &out, CYFlags flags) const;
1045 CYIdentifier *name_;
1048 CYCatch(CYIdentifier *name, CYStatement *code) :
1054 virtual void Output(CYOutput &out) const;
1060 CYExpression *self_;
1061 CYArgument *arguments_;
1063 CYSend(CYExpression *self, CYArgument *arguments) :
1065 arguments_(arguments)
1071 virtual void Output(CYOutput &out, CYFlags flags) const;
1077 CYExpression *object_;
1078 CYExpression *property_;
1080 CYMember(CYExpression *object, CYExpression *property) :
1086 void SetLeft(CYExpression *object) {
1091 struct CYDirectMember :
1094 CYDirectMember(CYExpression *object, CYExpression *property) :
1095 CYMember(object, property)
1102 virtual void Output(CYOutput &out, CYFlags flags) const;
1105 struct CYIndirectMember :
1108 CYIndirectMember(CYExpression *object, CYExpression *property) :
1109 CYMember(object, property)
1116 virtual void Output(CYOutput &out, CYFlags flags) const;
1122 CYExpression *constructor_;
1123 CYArgument *arguments_;
1125 CYNew(CYExpression *constructor, CYArgument *arguments) :
1126 constructor_(constructor),
1127 arguments_(arguments)
1131 virtual unsigned Precedence() const {
1132 return arguments_ == NULL ? 2 : 1;
1137 virtual void Output(CYOutput &out, CYFlags flags) const;
1143 CYExpression *function_;
1144 CYArgument *arguments_;
1146 CYCall(CYExpression *function, CYArgument *arguments) :
1147 function_(function),
1148 arguments_(arguments)
1155 virtual void Output(CYOutput &out, CYFlags flags) const;
1161 CYExpression *test_;
1163 CYStatement *false_;
1165 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1172 virtual void Output(CYOutput &out, CYFlags flags) const;
1178 CYExpression *test_;
1181 CYDoWhile(CYExpression *test, CYStatement *code) :
1187 virtual void Output(CYOutput &out, CYFlags flags) const;
1193 CYExpression *test_;
1196 CYWhile(CYExpression *test, CYStatement *code) :
1202 virtual void Output(CYOutput &out, CYFlags flags) const;
1206 CYIdentifier *name_;
1207 CYFunctionParameter *parameters_;
1210 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1212 parameters_(parameters),
1217 virtual void Output(CYOutput &out, CYFlags flags) const;
1220 struct CYFunctionExpression :
1224 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1225 CYFunction(name, parameters, body)
1232 virtual void Output(CYOutput &out, CYFlags flags) const;
1235 struct CYFunctionStatement :
1239 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1240 CYFunction(name, parameters, body)
1244 virtual void Output(CYOutput &out, CYFlags flags) const;
1250 CYExpression *expression_;
1252 CYExpress(CYExpression *expression) :
1253 expression_(expression)
1257 virtual void Output(CYOutput &out, CYFlags flags) const;
1263 CYIdentifier *label_;
1265 CYContinue(CYIdentifier *label) :
1270 virtual void Output(CYOutput &out, CYFlags flags) const;
1276 CYIdentifier *label_;
1278 CYBreak(CYIdentifier *label) :
1283 virtual void Output(CYOutput &out, CYFlags flags) const;
1289 CYExpression *value_;
1291 CYReturn(CYExpression *value) :
1296 virtual void Output(CYOutput &out, CYFlags flags) const;
1302 virtual void Output(CYOutput &out, CYFlags flags) const;
1308 CYFinally(CYStatement *code) :
1313 virtual void Output(CYOutput &out) const;
1321 CYFinally *finally_;
1323 CYTry(CYStatement *code, CYCatch *_catch, CYFinally *finally) :
1330 virtual void Output(CYOutput &out, CYFlags flags) const;
1336 CYExpression *value_;
1338 CYThrow(CYExpression *value) :
1343 virtual void Output(CYOutput &out, CYFlags flags) const;
1349 CYExpression *scope_;
1352 CYWith(CYExpression *scope, CYStatement *code) :
1358 virtual void Output(CYOutput &out, CYFlags flags) const;
1364 CYExpression *value_;
1367 CYSwitch(CYExpression *value, CYClause *clauses) :
1373 virtual void Output(CYOutput &out, CYFlags flags) const;
1376 struct CYCondition :
1379 CYExpression *test_;
1380 CYExpression *true_;
1381 CYExpression *false_;
1383 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1392 virtual void Output(CYOutput &out, CYFlags flags) const;
1395 struct CYAddressOf :
1398 CYAddressOf(CYExpression *rhs) :
1403 virtual const char *Operator() const {
1410 virtual void Output(CYOutput &out, CYFlags flags) const;
1416 CYIndirect(CYExpression *rhs) :
1421 virtual const char *Operator() const {
1428 virtual void Output(CYOutput &out, CYFlags flags) const;
1431 #define CYPostfix_(op, name) \
1432 struct CY ## name : \
1435 CY ## name(CYExpression *lhs) : \
1442 virtual const char *Operator() const { \
1447 #define CYPrefix_(alphabetic, op, name) \
1448 struct CY ## name : \
1451 CY ## name(CYExpression *rhs) : \
1456 CYAlphabetic(alphabetic) \
1459 virtual const char *Operator() const { \
1464 #define CYInfix_(alphabetic, precedence, op, name) \
1465 struct CY ## name : \
1468 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1473 CYAlphabetic(alphabetic) \
1474 CYPrecedence(precedence) \
1476 virtual const char *Operator() const { \
1481 #define CYAssignment_(op, name) \
1482 struct CY ## name ## Assign : \
1485 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1486 CYAssignment(lhs, rhs) \
1492 virtual const char *Operator() const { \
1497 CYPostfix_("++", PostIncrement)
1498 CYPostfix_("--", PostDecrement)
1500 CYPrefix_(true, "delete", Delete)
1501 CYPrefix_(true, "void", Void)
1502 CYPrefix_(true, "typeof", TypeOf)
1503 CYPrefix_(false, "++", PreIncrement)
1504 CYPrefix_(false, "--", PreDecrement)
1505 CYPrefix_(false, "-", Negate)
1506 CYPrefix_(false, "~", BitwiseNot)
1507 CYPrefix_(false, "!", LogicalNot)
1509 CYInfix_(false, 5, "*", Multiply)
1510 CYInfix_(false, 5, "/", Divide)
1511 CYInfix_(false, 5, "%", Modulus)
1512 CYInfix_(false, 6, "+", Add)
1513 CYInfix_(false, 6, "-", Subtract)
1514 CYInfix_(false, 7, "<<", ShiftLeft)
1515 CYInfix_(false, 7, ">>", ShiftRightSigned)
1516 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1517 CYInfix_(false, 8, "<", Less)
1518 CYInfix_(false, 8, ">", Greater)
1519 CYInfix_(false, 8, "<=", LessOrEqual)
1520 CYInfix_(false, 8, ">=", GreaterOrEqual)
1521 CYInfix_(true, 8, "instanceof", InstanceOf)
1522 CYInfix_(true, 8, "in", In)
1523 CYInfix_(false, 9, "==", Equal)
1524 CYInfix_(false, 9, "!=", NotEqual)
1525 CYInfix_(false, 9, "===", Identical)
1526 CYInfix_(false, 9, "!==", NotIdentical)
1527 CYInfix_(false, 10, "&", BitwiseAnd)
1528 CYInfix_(false, 11, "^", BitwiseXOr)
1529 CYInfix_(false, 12, "|", BitwiseOr)
1530 CYInfix_(false, 13, "&&", LogicalAnd)
1531 CYInfix_(false, 14, "||", LogicalOr)
1533 CYAssignment_("=", )
1534 CYAssignment_("*=", Multiply)
1535 CYAssignment_("/=", Divide)
1536 CYAssignment_("%=", Modulus)
1537 CYAssignment_("+=", Add)
1538 CYAssignment_("-=", Subtract)
1539 CYAssignment_("<<=", ShiftLeft)
1540 CYAssignment_(">>=", ShiftRightSigned)
1541 CYAssignment_(">>>=", ShiftRightUnsigned)
1542 CYAssignment_("&=", BitwiseAnd)
1543 CYAssignment_("^=", BitwiseXOr)
1544 CYAssignment_("|=", BitwiseOr)
1546 #endif/*CYPARSER_HPP*/