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;
76 CYOutput(std::ostream &out) :
81 _finline CYOutput &operator <<(char rhs) {
86 _finline CYOutput &operator <<(const char *rhs) {
91 _finline CYOutput &operator <<(const CYThing &rhs) {
97 struct CYPropertyName {
98 virtual void PropertyName(CYOutput &out) const = 0;
102 virtual void ClassName(CYOutput &out, bool object) const = 0;
112 CYWord(const char *word) :
117 const char *Value() const {
121 virtual void Output(CYOutput &out) const;
123 virtual void ClassName(CYOutput &out, bool object) const;
124 virtual void PropertyName(CYOutput &out) const;
127 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
128 return lhs << rhs.Value();
131 struct CYIdentifier :
134 CYIdentifier(const char *word) :
145 CYLabel(CYIdentifier *name, CYLabel *next) :
146 CYNext<CYLabel>(next),
162 void AddLabel(CYIdentifier *identifier) {
163 labels_ = new CYLabel(identifier, labels_);
166 virtual bool IsBlock() const {
167 return next_ != NULL;
170 virtual void Show(CYOutput &out) const;
171 virtual void Output(CYOutput &out) const = 0;
172 virtual void Output(CYOutput &out, bool block) const;
173 virtual void Output_(CYOutput &out) const;
179 CYStatement *statements_;
181 CYBlock(CYStatement *statements) :
182 statements_(statements)
186 virtual bool IsBlock() const {
190 virtual void Output(CYOutput &out) const;
217 std::string filename_;
221 cy::location location_;
222 std::string message_;
225 typedef std::vector<Error> Errors;
227 CYStatement *program_;
232 void ScannerDestroy();
235 CYDriver(const std::string &filename);
238 void SetCondition(Condition condition);
240 void Warning(const cy::location &location, const char *message);
245 CYNoBrace = (1 << 0),
246 CYNoFunction = (1 << 1),
247 CYNoLeader = (1 << 2),
248 CYNoTrailer = (1 << 3),
250 CYNoHyphen = (1 << 5),
251 CYNoBF = (CYNoBrace | CYNoFunction),
254 struct CYForInitialiser {
255 virtual void For(CYOutput &out) const = 0;
258 struct CYForInInitialiser {
259 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
260 virtual const char *ForEachIn() const = 0;
261 virtual void ForEachIn(CYOutput &out) const = 0;
264 struct CYExpression :
265 CYNext<CYExpression>,
270 virtual unsigned Precedence() const = 0;
272 virtual void For(CYOutput &out) const;
273 virtual void ForIn(CYOutput &out, CYFlags flags) const;
275 virtual const char *ForEachIn() const;
276 virtual void ForEachIn(CYOutput &out) const;
278 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
279 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
281 virtual void ClassName(CYOutput &out, bool object) const;
283 virtual const char *Word() const {
288 #define CYAlphabetic(value) \
289 virtual bool Alphabetic() const { \
293 #define CYPrecedence(value) \
294 virtual unsigned Precedence() const { \
301 CYExpression *expressions_;
303 CYCompound(CYExpression *expressions) :
304 expressions_(expressions)
308 void AddPrev(CYExpression *expression) {
309 CYExpression *last(expression);
310 while (last->next_ != NULL)
312 last->SetNext(expressions_);
313 expressions_ = expression;
318 void Output(CYOutput &out, CYFlags flags) const;
321 struct CYComprehension :
322 CYNext<CYComprehension>
324 void Output(CYOutput &out) const;
325 virtual const char *Name() const = 0;
327 virtual void Begin_(CYOutput &out) const = 0;
329 virtual void End_(CYOutput &out) const {
333 struct CYForInComprehension :
339 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
345 virtual const char *Name() const {
346 return name_->Value();
349 virtual void Begin_(CYOutput &out) const;
352 struct CYForEachInComprehension :
358 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
364 virtual const char *Name() const {
365 return name_->Value();
368 virtual void Begin_(CYOutput &out) const;
369 virtual void End_(CYOutput &out) const;
372 struct CYIfComprehension :
377 CYIfComprehension(CYExpression *test) :
382 virtual const char *Name() const {
386 virtual void Begin_(CYOutput &out) const;
389 struct CYArrayComprehension :
392 CYExpression *expression_;
393 CYComprehension *comprehensions_;
395 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
396 expression_(expression),
397 comprehensions_(comprehensions)
403 virtual void Output(CYOutput &out, CYFlags flags) const;
418 struct CYSelectorPart :
419 CYNext<CYSelectorPart>
424 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
425 CYNext<CYSelectorPart>(next),
431 virtual void Output(CYOutput &out) const;
437 CYSelectorPart *name_;
439 CYSelector(CYSelectorPart *name) :
446 virtual void Output(CYOutput &out, CYFlags flags) const;
453 CYRange(uint64_t lo, uint64_t hi) :
458 bool operator [](uint8_t value) const {
459 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
462 void operator()(uint8_t value) {
465 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
469 extern CYRange DigitRange_;
470 extern CYRange WordStartRange_;
471 extern CYRange WordEndRange_;
480 CYString(const char *value, size_t size) :
486 CYString(const CYIdentifier *identifier) :
487 value_(identifier->Value()),
488 size_(strlen(value_))
492 const char *Value() const {
496 virtual const char *Word() const {
497 if (size_ == 0 || !WordStartRange_[value_[0]])
499 for (size_t i(1); i != size_; ++i)
500 if (!WordEndRange_[value_[i]])
505 virtual void Output(CYOutput &out) const {
506 return Output(out, CYNoFlags);
509 virtual void Output(CYOutput &out, CYFlags flags) const;
510 virtual void PropertyName(CYOutput &out) const;
519 CYNumber(double value) :
524 double Value() const {
528 virtual void Output(CYOutput &out) const {
529 return Output(out, CYNoFlags);
532 virtual void Output(CYOutput &out, CYFlags flags) const;
533 virtual void PropertyName(CYOutput &out) const;
541 CYRegEx(const char *value) :
546 const char *Value() const {
550 virtual void Output(CYOutput &out, CYFlags flags) const;
562 virtual void Output(CYOutput &out, CYFlags flags) const;
574 virtual void Output(CYOutput &out, CYFlags flags) const;
580 virtual bool Value() const = 0;
581 virtual void Output(CYOutput &out, CYFlags flags) const;
593 virtual bool Value() const;
605 virtual bool Value() const;
613 CYVariable(CYIdentifier *name) :
620 virtual void Output(CYOutput &out, CYFlags flags) const;
628 CYPrefix(CYExpression *rhs) :
633 virtual bool Alphabetic() const = 0;
634 virtual const char *Operator() const = 0;
636 virtual void Output(CYOutput &out, CYFlags flags) const;
645 CYInfix(CYExpression *lhs, CYExpression *rhs) :
651 void SetLeft(CYExpression *lhs) {
655 virtual bool Alphabetic() const = 0;
656 virtual const char *Operator() const = 0;
658 virtual void Output(CYOutput &out, CYFlags flags) const;
666 CYPostfix(CYExpression *lhs) :
671 virtual const char *Operator() const = 0;
673 virtual void Output(CYOutput &out, CYFlags flags) const;
676 struct CYAssignment :
682 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
688 void SetLeft(CYExpression *lhs) {
692 virtual const char *Operator() const = 0;
694 virtual void Output(CYOutput &out, CYFlags flags) const;
701 CYExpression *value_;
703 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
704 CYNext<CYArgument>(next),
710 void Output(CYOutput &out) const;
729 CYClause(CYExpression *_case, CYStatement *code) :
735 virtual void Output(CYOutput &out) const;
741 CYExpression *value_;
743 CYElement(CYExpression *value, CYElement *next) :
744 CYNext<CYElement>(next),
749 void Output(CYOutput &out) const;
755 CYElement *elements_;
757 CYArray(CYElement *elements) :
762 virtual void Output(CYOutput &out, CYFlags flags) const;
765 struct CYDeclaration :
768 CYIdentifier *identifier_;
769 CYExpression *initialiser_;
771 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
772 identifier_(identifier),
773 initialiser_(initialiser)
777 virtual void ForIn(CYOutput &out, CYFlags flags) const;
779 virtual const char *ForEachIn() const;
780 virtual void ForEachIn(CYOutput &out) const;
782 virtual void Output(CYOutput &out, CYFlags flags) const;
785 struct CYDeclarations :
786 CYNext<CYDeclarations>,
789 CYDeclaration *declaration_;
791 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
792 CYNext<CYDeclarations>(next),
793 declaration_(declaration)
797 virtual void For(CYOutput &out) const;
798 virtual void Output(CYOutput &out, CYFlags flags) const;
804 CYDeclarations *declarations_;
806 CYVar(CYDeclarations *declarations) :
807 declarations_(declarations)
811 virtual void Output(CYOutput &out) const;
817 CYDeclarations *declarations_;
818 CYStatement *statements_;
820 CYLet(CYDeclarations *declarations, CYStatement *statements) :
821 declarations_(declarations),
822 statements_(statements)
826 virtual void Output(CYOutput &out) const;
832 virtual void Output(CYOutput &out) const;
835 struct CYMessageParameter :
836 CYNext<CYMessageParameter>
842 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
855 CYMessageParameter *parameter_;
858 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYStatement *body) :
861 parameter_(parameter),
866 virtual void Output(CYOutput &out, bool replace) const;
874 CYExpression *super_;
876 CYMessage *messages_;
878 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
888 virtual void Output(CYOutput &out) const;
889 virtual void Output(CYOutput &out, CYFlags flags) const;
896 CYMessage *messages_;
898 CYCategory(CYClassName *name, CYMessage *messages) :
904 virtual void Output(CYOutput &out) const;
907 struct CYFunctionParameter :
908 CYNext<CYFunctionParameter>,
913 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
914 CYNext<CYFunctionParameter>(next),
919 virtual void Output(CYOutput &out) const;
925 CYForInitialiser *initialiser_;
927 CYExpression *increment_;
930 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
931 initialiser_(initialiser),
933 increment_(increment),
938 virtual void Output(CYOutput &out) const;
944 CYForInInitialiser *initialiser_;
948 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
949 initialiser_(initialiser),
955 virtual void Output(CYOutput &out) const;
961 CYForInInitialiser *initialiser_;
965 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
966 initialiser_(initialiser),
972 virtual void Output(CYOutput &out) const;
978 CYPropertyName *name_;
979 CYExpression *value_;
981 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
982 CYNext<CYProperty>(next),
988 virtual void Output(CYOutput &out) const;
994 CYProperty *property_;
996 CYObject(CYProperty *property) :
1001 void Output(CYOutput &out, CYFlags flags) const;
1007 CYIdentifier *name_;
1010 CYCatch(CYIdentifier *name, CYStatement *code) :
1016 virtual void Output(CYOutput &out) const;
1022 CYExpression *self_;
1023 CYArgument *arguments_;
1025 CYSend(CYExpression *self, CYArgument *arguments) :
1027 arguments_(arguments)
1033 virtual void Output(CYOutput &out, CYFlags flags) const;
1039 CYExpression *object_;
1040 CYExpression *property_;
1042 CYMember(CYExpression *object, CYExpression *property) :
1048 void SetLeft(CYExpression *object) {
1053 struct CYDirectMember :
1056 CYDirectMember(CYExpression *object, CYExpression *property) :
1057 CYMember(object, property)
1063 virtual void Output(CYOutput &out, CYFlags flags) const;
1066 struct CYIndirectMember :
1069 CYIndirectMember(CYExpression *object, CYExpression *property) :
1070 CYMember(object, property)
1076 virtual void Output(CYOutput &out, CYFlags flags) const;
1082 CYExpression *constructor_;
1083 CYArgument *arguments_;
1085 CYNew(CYExpression *constructor, CYArgument *arguments) :
1086 constructor_(constructor),
1087 arguments_(arguments)
1093 virtual void Output(CYOutput &out, CYFlags flags) const;
1099 CYExpression *function_;
1100 CYArgument *arguments_;
1102 CYCall(CYExpression *function, CYArgument *arguments) :
1103 function_(function),
1104 arguments_(arguments)
1110 virtual void Output(CYOutput &out, CYFlags flags) const;
1116 CYExpression *test_;
1118 CYStatement *false_;
1120 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1127 virtual void Output(CYOutput &out) const;
1133 CYExpression *test_;
1136 CYDoWhile(CYExpression *test, CYStatement *code) :
1142 virtual void Output(CYOutput &out) const;
1148 CYExpression *test_;
1151 CYWhile(CYExpression *test, CYStatement *code) :
1157 virtual void Output(CYOutput &out) const;
1163 CYIdentifier *name_;
1164 CYFunctionParameter *parameters_;
1167 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1169 parameters_(parameters),
1176 virtual void Output(CYOutput &out, CYFlags flags) const;
1183 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *body) :
1184 CYLambda(name, parameters, body)
1188 virtual void Output(CYOutput &out) const;
1194 CYExpression *expression_;
1196 CYExpress(CYExpression *expression) :
1197 expression_(expression)
1201 virtual void Output(CYOutput &out) const;
1207 CYIdentifier *label_;
1209 CYContinue(CYIdentifier *label) :
1214 virtual void Output(CYOutput &out) const;
1220 CYIdentifier *label_;
1222 CYBreak(CYIdentifier *label) :
1227 virtual void Output(CYOutput &out) const;
1233 CYExpression *value_;
1235 CYReturn(CYExpression *value) :
1240 virtual void Output(CYOutput &out) const;
1246 virtual void Output(CYOutput &out) const;
1247 virtual void Output(CYOutput &out, bool block) const;
1253 CYFinally(CYStatement *code) :
1258 virtual void Output(CYOutput &out) const;
1266 CYFinally *finally_;
1268 CYTry(CYStatement *code, CYCatch *_catch, CYFinally *finally) :
1275 virtual void Output(CYOutput &out) const;
1281 CYExpression *value_;
1283 CYThrow(CYExpression *value) :
1288 virtual void Output(CYOutput &out) const;
1294 CYExpression *scope_;
1297 CYWith(CYExpression *scope, CYStatement *code) :
1303 virtual void Output(CYOutput &out) const;
1309 CYExpression *value_;
1312 CYSwitch(CYExpression *value, CYClause *clauses) :
1318 virtual void Output(CYOutput &out) const;
1321 struct CYCondition :
1324 CYExpression *test_;
1325 CYExpression *true_;
1326 CYExpression *false_;
1328 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1337 virtual void Output(CYOutput &out, CYFlags flags) const;
1340 struct CYAddressOf :
1343 CYAddressOf(CYExpression *rhs) :
1348 virtual const char *Operator() const {
1355 virtual void Output(CYOutput &out, CYFlags flags) const;
1361 CYIndirect(CYExpression *rhs) :
1366 virtual const char *Operator() const {
1373 virtual void Output(CYOutput &out, CYFlags flags) const;
1376 #define CYPostfix_(op, name) \
1377 struct CY ## name : \
1380 CY ## name(CYExpression *lhs) : \
1387 virtual const char *Operator() const { \
1392 #define CYPrefix_(alphabetic, op, name) \
1393 struct CY ## name : \
1396 CY ## name(CYExpression *rhs) : \
1401 CYAlphabetic(alphabetic) \
1404 virtual const char *Operator() const { \
1409 #define CYInfix_(alphabetic, precedence, op, name) \
1410 struct CY ## name : \
1413 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1418 CYAlphabetic(alphabetic) \
1419 CYPrecedence(precedence) \
1421 virtual const char *Operator() const { \
1426 #define CYAssignment_(op, name) \
1427 struct CY ## name ## Assign : \
1430 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1431 CYAssignment(lhs, rhs) \
1437 virtual const char *Operator() const { \
1442 CYPostfix_("++", PostIncrement)
1443 CYPostfix_("--", PostDecrement)
1445 CYPrefix_(true, "delete", Delete)
1446 CYPrefix_(true, "void", Void)
1447 CYPrefix_(true, "typeof", TypeOf)
1448 CYPrefix_(false, "++", PreIncrement)
1449 CYPrefix_(false, "--", PreDecrement)
1450 CYPrefix_(false, "-", Negate)
1451 CYPrefix_(false, "~", BitwiseNot)
1452 CYPrefix_(false, "!", LogicalNot)
1454 CYInfix_(false, 5, "*", Multiply)
1455 CYInfix_(false, 5, "/", Divide)
1456 CYInfix_(false, 5, "%", Modulus)
1457 CYInfix_(false, 6, "+", Add)
1458 CYInfix_(false, 6, "-", Subtract)
1459 CYInfix_(false, 7, "<<", ShiftLeft)
1460 CYInfix_(false, 7, ">>", ShiftRightSigned)
1461 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1462 CYInfix_(false, 8, "<", Less)
1463 CYInfix_(false, 8, ">", Greater)
1464 CYInfix_(false, 8, "<=", LessOrEqual)
1465 CYInfix_(false, 8, ">=", GreaterOrEqual)
1466 CYInfix_(true, 8, "instanceof", InstanceOf)
1467 CYInfix_(true, 8, "in", In)
1468 CYInfix_(false, 9, "==", Equal)
1469 CYInfix_(false, 9, "!=", NotEqual)
1470 CYInfix_(false, 9, "===", Identical)
1471 CYInfix_(false, 9, "!==", NotIdentical)
1472 CYInfix_(false, 10, "&", BitwiseAnd)
1473 CYInfix_(false, 11, "^", BitwiseXOr)
1474 CYInfix_(false, 12, "|", BitwiseOr)
1475 CYInfix_(false, 13, "&&", LogicalAnd)
1476 CYInfix_(false, 14, "||", LogicalOr)
1478 CYAssignment_("=", )
1479 CYAssignment_("*=", Multiply)
1480 CYAssignment_("/=", Divide)
1481 CYAssignment_("%=", Modulus)
1482 CYAssignment_("+=", Add)
1483 CYAssignment_("-=", Subtract)
1484 CYAssignment_("<<=", ShiftLeft)
1485 CYAssignment_(">>=", ShiftRightSigned)
1486 CYAssignment_(">>>=", ShiftRightUnsigned)
1487 CYAssignment_("&=", BitwiseAnd)
1488 CYAssignment_("^=", BitwiseXOr)
1489 CYAssignment_("|=", BitwiseOr)
1491 #endif/*CYPARSER_HPP*/