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;
86 CYOutput(std::ostream &out) :
94 void Check(char value);
97 CYOutput &operator <<(char rhs);
98 CYOutput &operator <<(const char *rhs);
100 _finline CYOutput &operator <<(const CYThing *rhs) {
106 _finline CYOutput &operator <<(const CYThing &rhs) {
112 struct CYPropertyName {
113 virtual void PropertyName(CYOutput &out) const = 0;
117 virtual void ClassName(CYOutput &out, bool object) const = 0;
127 CYWord(const char *word) :
132 const char *Value() const {
136 virtual void Output(CYOutput &out) const;
138 virtual void ClassName(CYOutput &out, bool object) const;
139 virtual void PropertyName(CYOutput &out) const;
142 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
143 return lhs << rhs.Value();
146 struct CYIdentifier :
149 CYIdentifier(const char *word) :
160 CYLabel(CYIdentifier *name, CYLabel *next) :
161 CYNext<CYLabel>(next),
175 CYNoBrace = (1 << 0),
176 CYNoFunction = (1 << 1),
179 CYNoRightHand = (1 << 4),
180 CYNoDangle = (1 << 5),
181 CYNoBF = (CYNoBrace | CYNoFunction),
194 void AddLabel(CYIdentifier *identifier) {
195 labels_ = new CYLabel(identifier, labels_);
198 bool Single(CYOutput &out, CYFlags flags) const;
199 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
202 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
208 CYStatement *statements_;
210 CYBlock(CYStatement *statements) :
211 statements_(statements)
215 virtual void Output(CYOutput &out, CYFlags flags) const;
242 std::string filename_;
246 cy::location location_;
247 std::string message_;
250 typedef std::vector<Error> Errors;
252 CYStatement *program_;
257 void ScannerDestroy();
260 CYDriver(const std::string &filename);
263 void SetCondition(Condition condition);
265 void Warning(const cy::location &location, const char *message);
268 struct CYForInitialiser {
269 virtual void For(CYOutput &out) const = 0;
272 struct CYForInInitialiser {
273 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
274 virtual const char *ForEachIn() const = 0;
275 virtual void ForEachIn(CYOutput &out) const = 0;
278 struct CYExpression :
279 CYNext<CYExpression>,
285 virtual unsigned Precedence() const = 0;
287 virtual bool RightHand() const {
291 virtual void For(CYOutput &out) const;
292 virtual void ForIn(CYOutput &out, CYFlags flags) const;
294 virtual const char *ForEachIn() const;
295 virtual void ForEachIn(CYOutput &out) const;
297 virtual void Output(CYOutput &out) const;
298 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
299 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
301 virtual void ClassName(CYOutput &out, bool object) const;
303 virtual const char *Word() const {
308 #define CYAlphabetic(value) \
309 virtual bool Alphabetic() const { \
313 #define CYPrecedence(value) \
314 virtual unsigned Precedence() const { \
318 #define CYRightHand(value) \
319 virtual bool RightHand() const { \
326 CYExpression *expressions_;
328 CYCompound(CYExpression *expressions) :
329 expressions_(expressions)
333 void AddPrev(CYExpression *expression) {
334 CYExpression *last(expression);
335 while (last->next_ != NULL)
337 last->SetNext(expressions_);
338 expressions_ = expression;
343 void Output(CYOutput &out, CYFlags flags) const;
346 struct CYComprehension :
347 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>,
453 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
454 CYNext<CYSelectorPart>(next),
460 virtual void Output(CYOutput &out) const;
466 CYSelectorPart *name_;
468 CYSelector(CYSelectorPart *name) :
475 virtual void Output(CYOutput &out, CYFlags flags) const;
482 CYRange(uint64_t lo, uint64_t hi) :
487 bool operator [](uint8_t value) const {
488 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
491 void operator()(uint8_t value) {
494 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
498 extern CYRange DigitRange_;
499 extern CYRange WordStartRange_;
500 extern CYRange WordEndRange_;
509 CYString(const char *value, size_t size) :
515 CYString(const CYIdentifier *identifier) :
516 value_(identifier->Value()),
517 size_(strlen(value_))
521 const char *Value() const {
525 virtual const char *Word() const;
527 virtual void Output(CYOutput &out, CYFlags flags) const;
528 virtual void PropertyName(CYOutput &out) const;
537 CYNumber(double value) :
542 double Value() const {
546 virtual void Output(CYOutput &out, CYFlags flags) const;
547 virtual void PropertyName(CYOutput &out) const;
555 CYRegEx(const char *value) :
560 const char *Value() const {
564 virtual void Output(CYOutput &out, CYFlags flags) const;
576 virtual void Output(CYOutput &out, CYFlags flags) const;
588 virtual void Output(CYOutput &out, CYFlags flags) const;
594 virtual bool Value() const = 0;
595 virtual void Output(CYOutput &out, CYFlags flags) const;
607 virtual bool Value() const {
621 virtual bool Value() const {
631 CYVariable(CYIdentifier *name) :
639 virtual void Output(CYOutput &out, CYFlags flags) const;
647 CYPrefix(CYExpression *rhs) :
652 virtual bool Alphabetic() const = 0;
653 virtual const char *Operator() const = 0;
655 virtual void Output(CYOutput &out, CYFlags flags) const;
664 CYInfix(CYExpression *lhs, CYExpression *rhs) :
670 void SetLeft(CYExpression *lhs) {
674 virtual bool Alphabetic() const = 0;
675 virtual const char *Operator() const = 0;
677 virtual void Output(CYOutput &out, CYFlags flags) const;
685 CYPostfix(CYExpression *lhs) :
690 virtual const char *Operator() const = 0;
692 virtual void Output(CYOutput &out, CYFlags flags) const;
695 struct CYAssignment :
701 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
707 void SetLeft(CYExpression *lhs) {
711 virtual const char *Operator() const = 0;
713 virtual void Output(CYOutput &out, CYFlags flags) const;
721 CYExpression *value_;
723 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
724 CYNext<CYArgument>(next),
730 void Output(CYOutput &out) const;
749 CYClause(CYExpression *_case, CYStatement *code) :
755 virtual void Output(CYOutput &out) const;
762 CYExpression *value_;
764 CYElement(CYExpression *value, CYElement *next) :
765 CYNext<CYElement>(next),
770 void Output(CYOutput &out) const;
776 CYElement *elements_;
778 CYArray(CYElement *elements) :
783 virtual void Output(CYOutput &out, CYFlags flags) const;
786 struct CYDeclaration :
789 CYIdentifier *identifier_;
790 CYExpression *initialiser_;
792 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
793 identifier_(identifier),
794 initialiser_(initialiser)
798 virtual void ForIn(CYOutput &out, CYFlags flags) const;
800 virtual const char *ForEachIn() const;
801 virtual void ForEachIn(CYOutput &out) const;
803 virtual void Output(CYOutput &out, CYFlags flags) const;
806 struct CYDeclarations :
807 CYNext<CYDeclarations>,
811 CYDeclaration *declaration_;
813 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
814 CYNext<CYDeclarations>(next),
815 declaration_(declaration)
819 virtual void For(CYOutput &out) const;
821 virtual void Output(CYOutput &out) const;
822 virtual void Output(CYOutput &out, CYFlags flags) const;
828 CYDeclarations *declarations_;
830 CYVar(CYDeclarations *declarations) :
831 declarations_(declarations)
835 virtual void Output(CYOutput &out, CYFlags flags) const;
841 CYDeclarations *declarations_;
842 CYStatement *statements_;
844 CYLet(CYDeclarations *declarations, CYStatement *statements) :
845 declarations_(declarations),
846 statements_(statements)
850 virtual void Output(CYOutput &out, CYFlags flags) const;
856 virtual void Output(CYOutput &out) const;
859 struct CYMessageParameter :
860 CYNext<CYMessageParameter>
866 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
879 CYMessageParameter *parameter_;
882 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYStatement *body) :
885 parameter_(parameter),
890 virtual void Output(CYOutput &out, bool replace) const;
895 CYExpression *super_;
897 CYMessage *messages_;
899 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
907 virtual void Output(CYOutput &out, CYFlags flags) const;
910 struct CYClassExpression :
914 CYClassExpression(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
915 CYClass(name, super, fields, messages)
921 virtual void Output(CYOutput &out, CYFlags flags) const;
924 struct CYClassStatement :
928 CYClassStatement(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
929 CYClass(name, super, fields, messages)
933 virtual void Output(CYOutput &out, CYFlags flags) const;
940 CYMessage *messages_;
942 CYCategory(CYClassName *name, CYMessage *messages) :
948 virtual void Output(CYOutput &out, CYFlags flags) const;
951 struct CYFunctionParameter :
952 CYNext<CYFunctionParameter>,
957 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
958 CYNext<CYFunctionParameter>(next),
963 virtual void Output(CYOutput &out) const;
969 CYForInitialiser *initialiser_;
971 CYExpression *increment_;
974 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
975 initialiser_(initialiser),
977 increment_(increment),
982 virtual void Output(CYOutput &out, CYFlags flags) const;
988 CYForInInitialiser *initialiser_;
992 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
993 initialiser_(initialiser),
999 virtual void Output(CYOutput &out, CYFlags flags) const;
1002 struct CYForEachIn :
1005 CYForInInitialiser *initialiser_;
1009 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1010 initialiser_(initialiser),
1016 virtual void Output(CYOutput &out, CYFlags flags) const;
1023 CYPropertyName *name_;
1024 CYExpression *value_;
1026 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
1027 CYNext<CYProperty>(next),
1033 virtual void Output(CYOutput &out) const;
1039 CYProperty *property_;
1041 CYObject(CYProperty *property) :
1046 void Output(CYOutput &out, CYFlags flags) const;
1052 CYIdentifier *name_;
1055 CYCatch(CYIdentifier *name, CYStatement *code) :
1061 virtual void Output(CYOutput &out) const;
1067 CYExpression *self_;
1068 CYArgument *arguments_;
1070 CYSend(CYExpression *self, CYArgument *arguments) :
1072 arguments_(arguments)
1078 virtual void Output(CYOutput &out, CYFlags flags) const;
1084 CYExpression *object_;
1085 CYExpression *property_;
1087 CYMember(CYExpression *object, CYExpression *property) :
1093 void SetLeft(CYExpression *object) {
1098 struct CYDirectMember :
1101 CYDirectMember(CYExpression *object, CYExpression *property) :
1102 CYMember(object, property)
1109 virtual void Output(CYOutput &out, CYFlags flags) const;
1112 struct CYIndirectMember :
1115 CYIndirectMember(CYExpression *object, CYExpression *property) :
1116 CYMember(object, property)
1123 virtual void Output(CYOutput &out, CYFlags flags) const;
1129 CYExpression *constructor_;
1130 CYArgument *arguments_;
1132 CYNew(CYExpression *constructor, CYArgument *arguments) :
1133 constructor_(constructor),
1134 arguments_(arguments)
1138 virtual unsigned Precedence() const {
1139 return arguments_ == NULL ? 2 : 1;
1144 virtual void Output(CYOutput &out, CYFlags flags) const;
1150 CYExpression *function_;
1151 CYArgument *arguments_;
1153 CYCall(CYExpression *function, CYArgument *arguments) :
1154 function_(function),
1155 arguments_(arguments)
1162 virtual void Output(CYOutput &out, CYFlags flags) const;
1168 CYExpression *test_;
1170 CYStatement *false_;
1172 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1179 virtual void Output(CYOutput &out, CYFlags flags) const;
1185 CYExpression *test_;
1188 CYDoWhile(CYExpression *test, CYStatement *code) :
1194 virtual void Output(CYOutput &out, CYFlags flags) const;
1200 CYExpression *test_;
1203 CYWhile(CYExpression *test, CYStatement *code) :
1209 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 CYIdentifier *name_;
1214 CYFunctionParameter *parameters_;
1217 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1219 parameters_(parameters),
1224 virtual void Output(CYOutput &out, CYFlags flags) const;
1227 struct CYFunctionExpression :
1231 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1232 CYFunction(name, parameters, body)
1239 virtual void Output(CYOutput &out, CYFlags flags) const;
1242 struct CYFunctionStatement :
1246 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1247 CYFunction(name, parameters, body)
1251 virtual void Output(CYOutput &out, CYFlags flags) const;
1257 CYExpression *expression_;
1259 CYExpress(CYExpression *expression) :
1260 expression_(expression)
1264 virtual void Output(CYOutput &out, CYFlags flags) const;
1270 CYIdentifier *label_;
1272 CYContinue(CYIdentifier *label) :
1277 virtual void Output(CYOutput &out, CYFlags flags) const;
1283 CYIdentifier *label_;
1285 CYBreak(CYIdentifier *label) :
1290 virtual void Output(CYOutput &out, CYFlags flags) const;
1296 CYExpression *value_;
1298 CYReturn(CYExpression *value) :
1303 virtual void Output(CYOutput &out, CYFlags flags) const;
1309 virtual void Output(CYOutput &out, CYFlags flags) const;
1317 CYFinally(CYStatement *code) :
1322 virtual void Output(CYOutput &out) const;
1330 CYFinally *finally_;
1332 CYTry(CYStatement *code, CYCatch *_catch, CYFinally *finally) :
1339 virtual void Output(CYOutput &out, CYFlags flags) const;
1345 CYExpression *value_;
1347 CYThrow(CYExpression *value) :
1352 virtual void Output(CYOutput &out, CYFlags flags) const;
1358 CYExpression *scope_;
1361 CYWith(CYExpression *scope, CYStatement *code) :
1367 virtual void Output(CYOutput &out, CYFlags flags) const;
1373 CYExpression *value_;
1376 CYSwitch(CYExpression *value, CYClause *clauses) :
1382 virtual void Output(CYOutput &out, CYFlags flags) const;
1385 struct CYCondition :
1388 CYExpression *test_;
1389 CYExpression *true_;
1390 CYExpression *false_;
1392 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1401 virtual void Output(CYOutput &out, CYFlags flags) const;
1404 struct CYAddressOf :
1407 CYAddressOf(CYExpression *rhs) :
1412 virtual const char *Operator() const {
1419 virtual void Output(CYOutput &out, CYFlags flags) const;
1425 CYIndirect(CYExpression *rhs) :
1430 virtual const char *Operator() const {
1437 virtual void Output(CYOutput &out, CYFlags flags) const;
1440 #define CYPostfix_(op, name) \
1441 struct CY ## name : \
1444 CY ## name(CYExpression *lhs) : \
1451 virtual const char *Operator() const { \
1456 #define CYPrefix_(alphabetic, op, name) \
1457 struct CY ## name : \
1460 CY ## name(CYExpression *rhs) : \
1465 CYAlphabetic(alphabetic) \
1468 virtual const char *Operator() const { \
1473 #define CYInfix_(alphabetic, precedence, op, name) \
1474 struct CY ## name : \
1477 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1482 CYAlphabetic(alphabetic) \
1483 CYPrecedence(precedence) \
1485 virtual const char *Operator() const { \
1490 #define CYAssignment_(op, name) \
1491 struct CY ## name ## Assign : \
1494 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1495 CYAssignment(lhs, rhs) \
1501 virtual const char *Operator() const { \
1506 CYPostfix_("++", PostIncrement)
1507 CYPostfix_("--", PostDecrement)
1509 CYPrefix_(true, "delete", Delete)
1510 CYPrefix_(true, "void", Void)
1511 CYPrefix_(true, "typeof", TypeOf)
1512 CYPrefix_(false, "++", PreIncrement)
1513 CYPrefix_(false, "--", PreDecrement)
1514 CYPrefix_(false, "+", Affirm)
1515 CYPrefix_(false, "-", Negate)
1516 CYPrefix_(false, "~", BitwiseNot)
1517 CYPrefix_(false, "!", LogicalNot)
1519 CYInfix_(false, 5, "*", Multiply)
1520 CYInfix_(false, 5, "/", Divide)
1521 CYInfix_(false, 5, "%", Modulus)
1522 CYInfix_(false, 6, "+", Add)
1523 CYInfix_(false, 6, "-", Subtract)
1524 CYInfix_(false, 7, "<<", ShiftLeft)
1525 CYInfix_(false, 7, ">>", ShiftRightSigned)
1526 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1527 CYInfix_(false, 8, "<", Less)
1528 CYInfix_(false, 8, ">", Greater)
1529 CYInfix_(false, 8, "<=", LessOrEqual)
1530 CYInfix_(false, 8, ">=", GreaterOrEqual)
1531 CYInfix_(true, 8, "instanceof", InstanceOf)
1532 CYInfix_(true, 8, "in", In)
1533 CYInfix_(false, 9, "==", Equal)
1534 CYInfix_(false, 9, "!=", NotEqual)
1535 CYInfix_(false, 9, "===", Identical)
1536 CYInfix_(false, 9, "!==", NotIdentical)
1537 CYInfix_(false, 10, "&", BitwiseAnd)
1538 CYInfix_(false, 11, "^", BitwiseXOr)
1539 CYInfix_(false, 12, "|", BitwiseOr)
1540 CYInfix_(false, 13, "&&", LogicalAnd)
1541 CYInfix_(false, 14, "||", LogicalOr)
1543 CYAssignment_("=", )
1544 CYAssignment_("*=", Multiply)
1545 CYAssignment_("/=", Divide)
1546 CYAssignment_("%=", Modulus)
1547 CYAssignment_("+=", Add)
1548 CYAssignment_("-=", Subtract)
1549 CYAssignment_("<<=", ShiftLeft)
1550 CYAssignment_(">>=", ShiftRightSigned)
1551 CYAssignment_(">>>=", ShiftRightUnsigned)
1552 CYAssignment_("&=", BitwiseAnd)
1553 CYAssignment_("^=", BitwiseXOr)
1554 CYAssignment_("|=", BitwiseOr)
1556 #endif/*CYPARSER_HPP*/