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) {
100 virtual bool IsBlock() const {
101 return next_ != NULL;
104 virtual void Show(CYOutput &out) const;
105 virtual void Output(CYOutput &out) const = 0;
106 virtual void Output(CYOutput &out, bool block) const;
107 virtual void Output_(CYOutput &out) const;
110 struct CYPropertyName {
111 virtual void PropertyName(CYOutput &out) const = 0;
115 virtual void ClassName(CYOutput &out, bool object) const = 0;
125 CYWord(const char *word) :
130 const char *Value() const {
134 virtual void Output(CYOutput &out) const;
136 virtual void ClassName(CYOutput &out, bool object) const;
137 virtual void PropertyName(CYOutput &out) const;
140 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
141 return lhs << rhs.Value();
144 struct CYIdentifier :
147 CYIdentifier(const char *word) :
158 CYLabel(CYIdentifier *name, CYLabel *next) :
159 CYNext<CYLabel>(next),
175 void AddLabel(CYIdentifier *identifier) {
176 labels_ = new CYLabel(identifier, labels_);
179 virtual void Output_(CYOutput &out) const;
185 CYStatement *statements_;
187 CYBlock(CYStatement *statements) :
188 statements_(statements)
192 virtual bool IsBlock() const {
196 virtual void Output(CYOutput &out) const;
221 std::string filename_;
224 cy::location location_;
225 std::string message_;
228 typedef std::vector<Error> Errors;
235 void ScannerDestroy();
238 CYDriver(const std::string &filename);
241 void SetCondition(Condition condition);
246 CYNoBrace = (1 << 0),
247 CYNoFunction = (1 << 1),
248 CYNoLeader = (1 << 2),
249 CYNoTrailer = (1 << 3),
251 CYNoHyphen = (1 << 5),
252 CYNoBF = (CYNoBrace | CYNoFunction),
255 struct CYForInitialiser {
256 virtual void For(CYOutput &out) const = 0;
259 struct CYForInInitialiser {
260 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
261 virtual const char *ForEachIn() const = 0;
262 virtual void ForEachIn(CYOutput &out) const = 0;
265 struct CYExpression :
266 CYNext<CYExpression>,
271 virtual unsigned Precedence() const = 0;
273 virtual void For(CYOutput &out) const;
274 virtual void ForIn(CYOutput &out, CYFlags flags) const;
276 virtual const char *ForEachIn() const;
277 virtual void ForEachIn(CYOutput &out) const;
279 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
280 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
282 virtual void ClassName(CYOutput &out, bool object) const;
284 virtual const char *Word() const {
289 #define CYAlphabetic(value) \
290 virtual bool Alphabetic() const { \
294 #define CYPrecedence(value) \
295 virtual unsigned Precedence() const { \
302 CYExpression *expressions_;
304 CYCompound(CYExpression *expressions) :
305 expressions_(expressions)
309 void AddPrev(CYExpression *expression) {
310 CYExpression *last(expression);
311 while (last->next_ != NULL)
313 last->SetNext(expressions_);
314 expressions_ = expression;
319 void Output(CYOutput &out, CYFlags flags) const;
322 struct CYComprehension :
323 CYNext<CYComprehension>
325 void Output(CYOutput &out) const;
326 virtual const char *Name() const = 0;
328 virtual void Begin_(CYOutput &out) const = 0;
330 virtual void End_(CYOutput &out) const {
334 struct CYForInComprehension :
340 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
346 virtual const char *Name() const {
347 return name_->Value();
350 virtual void Begin_(CYOutput &out) const;
353 struct CYForEachInComprehension :
359 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
365 virtual const char *Name() const {
366 return name_->Value();
369 virtual void Begin_(CYOutput &out) const;
370 virtual void End_(CYOutput &out) const;
373 struct CYIfComprehension :
378 CYIfComprehension(CYExpression *test) :
383 virtual const char *Name() const {
387 virtual void Begin_(CYOutput &out) const;
390 struct CYArrayComprehension :
393 CYExpression *expression_;
394 CYComprehension *comprehensions_;
396 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
397 expression_(expression),
398 comprehensions_(comprehensions)
404 virtual void Output(CYOutput &out, CYFlags flags) const;
419 struct CYSelectorPart :
420 CYNext<CYSelectorPart>
425 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
426 CYNext<CYSelectorPart>(next),
432 virtual void Output(CYOutput &out) const;
438 CYSelectorPart *name_;
440 CYSelector(CYSelectorPart *name) :
447 virtual void Output(CYOutput &out, CYFlags flags) const;
454 CYRange(uint64_t lo, uint64_t hi) :
459 bool operator [](uint8_t value) const {
460 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
463 void operator()(uint8_t value) {
466 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
470 extern CYRange DigitRange_;
471 extern CYRange WordStartRange_;
472 extern CYRange WordEndRange_;
481 CYString(const char *value, size_t size) :
487 CYString(const CYIdentifier *identifier) :
488 value_(identifier->Value()),
489 size_(strlen(value_))
493 const char *Value() const {
497 virtual const char *Word() const {
498 if (size_ == 0 || !WordStartRange_[value_[0]])
500 for (size_t i(1); i != size_; ++i)
501 if (!WordEndRange_[value_[i]])
506 virtual void Output(CYOutput &out) const {
507 return Output(out, CYNoFlags);
510 virtual void Output(CYOutput &out, CYFlags flags) const;
511 virtual void PropertyName(CYOutput &out) const;
520 CYNumber(double value) :
525 double Value() const {
529 virtual void Output(CYOutput &out) const {
530 return Output(out, CYNoFlags);
533 virtual void Output(CYOutput &out, CYFlags flags) const;
534 virtual void PropertyName(CYOutput &out) const;
542 CYRegEx(const char *value) :
547 const char *Value() const {
551 virtual void Output(CYOutput &out, CYFlags flags) const;
563 virtual void Output(CYOutput &out, CYFlags flags) const;
575 virtual void Output(CYOutput &out, CYFlags flags) const;
581 virtual bool Value() const = 0;
582 virtual void Output(CYOutput &out, CYFlags flags) const;
594 virtual bool Value() const;
606 virtual bool Value() const;
614 CYVariable(CYIdentifier *name) :
621 virtual void Output(CYOutput &out, CYFlags flags) const;
629 CYPrefix(CYExpression *rhs) :
634 virtual bool Alphabetic() const = 0;
635 virtual const char *Operator() const = 0;
637 virtual void Output(CYOutput &out, CYFlags flags) const;
646 CYInfix(CYExpression *lhs, CYExpression *rhs) :
652 void SetLeft(CYExpression *lhs) {
656 virtual bool Alphabetic() const = 0;
657 virtual const char *Operator() const = 0;
659 virtual void Output(CYOutput &out, CYFlags flags) const;
667 CYPostfix(CYExpression *lhs) :
672 virtual const char *Operator() const = 0;
674 virtual void Output(CYOutput &out, CYFlags flags) const;
677 struct CYAssignment :
683 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
689 void SetLeft(CYExpression *lhs) {
693 virtual const char *Operator() const = 0;
695 virtual void Output(CYOutput &out, CYFlags flags) const;
702 CYExpression *value_;
704 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
705 CYNext<CYArgument>(next),
711 void Output(CYOutput &out) const;
730 CYClause(CYExpression *_case, CYStatement *code) :
736 virtual void Output(CYOutput &out) const;
742 CYExpression *value_;
744 CYElement(CYExpression *value, CYElement *next) :
745 CYNext<CYElement>(next),
750 void Output(CYOutput &out) const;
756 CYElement *elements_;
758 CYArray(CYElement *elements) :
763 virtual void Output(CYOutput &out, CYFlags flags) const;
766 struct CYDeclaration :
769 CYIdentifier *identifier_;
770 CYExpression *initialiser_;
772 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
773 identifier_(identifier),
774 initialiser_(initialiser)
778 virtual void ForIn(CYOutput &out, CYFlags flags) const;
780 virtual const char *ForEachIn() const;
781 virtual void ForEachIn(CYOutput &out) const;
783 virtual void Output(CYOutput &out, CYFlags flags) const;
786 struct CYDeclarations :
787 CYNext<CYDeclarations>,
790 CYDeclaration *declaration_;
792 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
793 CYNext<CYDeclarations>(next),
794 declaration_(declaration)
798 virtual void For(CYOutput &out) const;
799 virtual void Output(CYOutput &out, CYFlags flags) const;
805 CYDeclarations *declarations_;
807 CYVar(CYDeclarations *declarations) :
808 declarations_(declarations)
812 virtual void Output(CYOutput &out) const;
818 CYDeclarations *declarations_;
819 CYStatement *statements_;
821 CYLet(CYDeclarations *declarations, CYStatement *statements) :
822 declarations_(declarations),
823 statements_(statements)
827 virtual void Output(CYOutput &out) const;
833 virtual void Output(CYOutput &out) const;
836 struct CYMessageParameter :
837 CYNext<CYMessageParameter>
843 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
856 CYMessageParameter *parameter_;
859 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
862 parameter_(parameter),
867 virtual void Output(CYOutput &out, bool replace) const;
875 CYExpression *super_;
877 CYMessage *messages_;
879 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
889 virtual void Output(CYOutput &out) const;
890 virtual void Output(CYOutput &out, CYFlags flags) const;
897 CYMessage *messages_;
899 CYCategory(CYClassName *name, CYMessage *messages) :
905 virtual void Output(CYOutput &out) const;
908 struct CYFunctionParameter :
909 CYNext<CYFunctionParameter>,
914 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
915 CYNext<CYFunctionParameter>(next),
920 virtual void Output(CYOutput &out) const;
926 CYForInitialiser *initialiser_;
928 CYExpression *increment_;
931 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
932 initialiser_(initialiser),
934 increment_(increment),
939 virtual void Output(CYOutput &out) const;
945 CYForInInitialiser *initialiser_;
949 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
950 initialiser_(initialiser),
956 virtual void Output(CYOutput &out) const;
962 CYForInInitialiser *initialiser_;
966 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
967 initialiser_(initialiser),
973 virtual void Output(CYOutput &out) const;
979 CYPropertyName *name_;
980 CYExpression *value_;
982 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
983 CYNext<CYProperty>(next),
989 virtual void Output(CYOutput &out) const;
995 CYProperty *property_;
997 CYObject(CYProperty *property) :
1002 void Output(CYOutput &out, CYFlags flags) const;
1008 CYIdentifier *name_;
1011 CYCatch(CYIdentifier *name, CYStatement *code) :
1017 virtual void Output(CYOutput &out) const;
1023 CYExpression *self_;
1024 CYArgument *arguments_;
1026 CYSend(CYExpression *self, CYArgument *arguments) :
1028 arguments_(arguments)
1034 virtual void Output(CYOutput &out, CYFlags flags) const;
1040 CYExpression *object_;
1041 CYExpression *property_;
1043 CYMember(CYExpression *object, CYExpression *property) :
1049 void SetLeft(CYExpression *object) {
1054 struct CYDirectMember :
1057 CYDirectMember(CYExpression *object, CYExpression *property) :
1058 CYMember(object, property)
1064 virtual void Output(CYOutput &out, CYFlags flags) const;
1067 struct CYIndirectMember :
1070 CYIndirectMember(CYExpression *object, CYExpression *property) :
1071 CYMember(object, property)
1077 virtual void Output(CYOutput &out, CYFlags flags) const;
1083 CYExpression *constructor_;
1084 CYArgument *arguments_;
1086 CYNew(CYExpression *constructor, CYArgument *arguments) :
1087 constructor_(constructor),
1088 arguments_(arguments)
1094 virtual void Output(CYOutput &out, CYFlags flags) const;
1100 CYExpression *function_;
1101 CYArgument *arguments_;
1103 CYCall(CYExpression *function, CYArgument *arguments) :
1104 function_(function),
1105 arguments_(arguments)
1111 virtual void Output(CYOutput &out, CYFlags flags) const;
1117 CYExpression *test_;
1119 CYStatement *false_;
1121 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1128 virtual void Output(CYOutput &out) const;
1134 CYExpression *test_;
1137 CYDoWhile(CYExpression *test, CYStatement *code) :
1143 virtual void Output(CYOutput &out) const;
1149 CYExpression *test_;
1152 CYWhile(CYExpression *test, CYStatement *code) :
1158 virtual void Output(CYOutput &out) const;
1164 CYIdentifier *name_;
1165 CYFunctionParameter *parameters_;
1168 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1170 parameters_(parameters),
1177 virtual void Output(CYOutput &out, CYFlags flags) const;
1184 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1185 CYLambda(name, parameters, body)
1189 virtual void Output(CYOutput &out) const;
1195 CYExpression *expression_;
1197 CYExpress(CYExpression *expression) :
1198 expression_(expression)
1202 virtual void Output(CYOutput &out) const;
1208 CYIdentifier *label_;
1210 CYContinue(CYIdentifier *label) :
1215 virtual void Output(CYOutput &out) const;
1221 CYIdentifier *label_;
1223 CYBreak(CYIdentifier *label) :
1228 virtual void Output(CYOutput &out) const;
1234 CYExpression *value_;
1236 CYReturn(CYExpression *value) :
1241 virtual void Output(CYOutput &out) const;
1247 virtual void Output(CYOutput &out) const;
1248 virtual void Output(CYOutput &out, bool block) const;
1256 CYStatement *finally_;
1258 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1265 virtual void Output(CYOutput &out) const;
1271 CYExpression *value_;
1273 CYThrow(CYExpression *value) :
1278 virtual void Output(CYOutput &out) const;
1284 CYExpression *scope_;
1287 CYWith(CYExpression *scope, CYStatement *code) :
1293 virtual void Output(CYOutput &out) const;
1299 CYExpression *value_;
1302 CYSwitch(CYExpression *value, CYClause *clauses) :
1308 virtual void Output(CYOutput &out) const;
1311 struct CYCondition :
1314 CYExpression *test_;
1315 CYExpression *true_;
1316 CYExpression *false_;
1318 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1327 virtual void Output(CYOutput &out, CYFlags flags) const;
1330 struct CYAddressOf :
1333 CYAddressOf(CYExpression *rhs) :
1338 virtual const char *Operator() const {
1345 virtual void Output(CYOutput &out, CYFlags flags) const;
1351 CYIndirect(CYExpression *rhs) :
1356 virtual const char *Operator() const {
1363 virtual void Output(CYOutput &out, CYFlags flags) const;
1366 #define CYPostfix_(op, name) \
1367 struct CY ## name : \
1370 CY ## name(CYExpression *lhs) : \
1377 virtual const char *Operator() const { \
1382 #define CYPrefix_(alphabetic, op, name) \
1383 struct CY ## name : \
1386 CY ## name(CYExpression *rhs) : \
1391 CYAlphabetic(alphabetic) \
1394 virtual const char *Operator() const { \
1399 #define CYInfix_(alphabetic, precedence, op, name) \
1400 struct CY ## name : \
1403 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1408 CYAlphabetic(alphabetic) \
1409 CYPrecedence(precedence) \
1411 virtual const char *Operator() const { \
1416 #define CYAssignment_(op, name) \
1417 struct CY ## name ## Assign : \
1420 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1421 CYAssignment(lhs, rhs) \
1427 virtual const char *Operator() const { \
1432 CYPostfix_("++", PostIncrement)
1433 CYPostfix_("--", PostDecrement)
1435 CYPrefix_(true, "delete", Delete)
1436 CYPrefix_(true, "void", Void)
1437 CYPrefix_(true, "typeof", TypeOf)
1438 CYPrefix_(false, "++", PreIncrement)
1439 CYPrefix_(false, "--", PreDecrement)
1440 CYPrefix_(false, "-", Negate)
1441 CYPrefix_(false, "~", BitwiseNot)
1442 CYPrefix_(false, "!", LogicalNot)
1444 CYInfix_(false, 5, "*", Multiply)
1445 CYInfix_(false, 5, "/", Divide)
1446 CYInfix_(false, 5, "%", Modulus)
1447 CYInfix_(false, 6, "+", Add)
1448 CYInfix_(false, 6, "-", Subtract)
1449 CYInfix_(false, 7, "<<", ShiftLeft)
1450 CYInfix_(false, 7, ">>", ShiftRightSigned)
1451 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1452 CYInfix_(false, 8, "<", Less)
1453 CYInfix_(false, 8, ">", Greater)
1454 CYInfix_(false, 8, "<=", LessOrEqual)
1455 CYInfix_(false, 8, ">=", GreaterOrEqual)
1456 CYInfix_(true, 8, "instanceof", InstanceOf)
1457 CYInfix_(true, 8, "in", In)
1458 CYInfix_(false, 9, "==", Equal)
1459 CYInfix_(false, 9, "!=", NotEqual)
1460 CYInfix_(false, 9, "===", Identical)
1461 CYInfix_(false, 9, "!==", NotIdentical)
1462 CYInfix_(false, 10, "&", BitwiseAnd)
1463 CYInfix_(false, 11, "^", BitwiseXOr)
1464 CYInfix_(false, 12, "|", BitwiseOr)
1465 CYInfix_(false, 13, "&&", LogicalAnd)
1466 CYInfix_(false, 14, "||", LogicalOr)
1468 CYAssignment_("=", )
1469 CYAssignment_("*=", Multiply)
1470 CYAssignment_("/=", Divide)
1471 CYAssignment_("%=", Modulus)
1472 CYAssignment_("+=", Add)
1473 CYAssignment_("-=", Subtract)
1474 CYAssignment_("<<=", ShiftLeft)
1475 CYAssignment_(">>=", ShiftRightSigned)
1476 CYAssignment_(">>>=", ShiftRightUnsigned)
1477 CYAssignment_("&=", BitwiseAnd)
1478 CYAssignment_("^=", BitwiseXOr)
1479 CYAssignment_("|=", BitwiseOr)
1481 #endif/*CYPARSER_HPP*/