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(std::ostream &out) const = 0;
73 _finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) {
81 virtual bool IsBlock() const {
85 virtual void Show(std::ostream &out) const;
86 virtual void Output(std::ostream &out) const = 0;
87 virtual void Output(std::ostream &out, bool block) const;
88 virtual void Output_(std::ostream &out) const;
91 struct CYPropertyName {
92 virtual void PropertyName(std::ostream &out) const = 0;
96 virtual void ClassName(std::ostream &out, bool object) const = 0;
106 CYWord(const char *word) :
111 const char *Value() const {
115 virtual void Output(std::ostream &out) const;
117 virtual void ClassName(std::ostream &out, bool object) const;
118 virtual void PropertyName(std::ostream &out) const;
121 struct CYIdentifier :
124 CYIdentifier(const char *word) :
135 CYLabel(CYIdentifier *name, CYLabel *next) :
136 CYNext<CYLabel>(next),
152 void AddLabel(CYIdentifier *identifier) {
153 labels_ = new CYLabel(identifier, labels_);
156 virtual void Output_(std::ostream &out) const;
162 CYStatement *statements_;
164 CYBlock(CYStatement *statements) :
165 statements_(statements)
169 virtual bool IsBlock() const {
173 virtual void Output(std::ostream &out) const;
193 std::string filename_;
196 cy::location location_;
197 std::string message_;
200 typedef std::vector<Error> Errors;
207 void ScannerDestroy();
210 CYDriver(const std::string &filename);
215 virtual void Part(std::ostream &out) const = 0;
218 struct CYForInitialiser :
223 struct CYForInInitialiser :
230 CYNoBrace = (1 << 0),
231 CYNoFunction = (1 << 1),
232 CYNoLeader = (1 << 2),
233 CYNoTrailer = (1 << 3),
235 CYNoHyphen = (1 << 5),
238 struct CYExpression :
239 CYNext<CYExpression>,
244 virtual unsigned Precedence() const = 0;
245 virtual void Part(std::ostream &out) const;
246 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
247 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
249 virtual void ClassName(std::ostream &out, bool object) const;
251 virtual const char *Word() const {
256 #define CYAlphabetic(value) \
257 virtual bool Alphabetic() const { \
261 #define CYPrecedence(value) \
262 virtual unsigned Precedence() const { \
269 CYExpression *expressions_;
271 CYCompound(CYExpression *expressions) :
272 expressions_(expressions)
276 void AddPrev(CYExpression *expression) {
277 CYExpression *last(expression);
278 while (last->next_ != NULL)
280 last->SetNext(expressions_);
281 expressions_ = expression;
286 void Output(std::ostream &out, CYFlags flags) const;
301 struct CYSelectorPart :
302 CYNext<CYSelectorPart>
307 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
308 CYNext<CYSelectorPart>(next),
314 virtual void Output(std::ostream &out) const;
320 CYSelectorPart *name_;
322 CYSelector(CYSelectorPart *name) :
329 virtual void Output(std::ostream &out, CYFlags flags) const;
336 CYRange(uint64_t lo, uint64_t hi) :
341 bool operator [](uint8_t value) const {
342 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
345 void operator()(uint8_t value) {
348 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
352 extern CYRange DigitRange_;
353 extern CYRange WordStartRange_;
354 extern CYRange WordEndRange_;
363 CYString(const char *value, size_t size) :
369 CYString(const CYIdentifier *identifier) :
370 value_(identifier->Value()),
371 size_(strlen(value_))
375 const char *Value() const {
379 virtual const char *Word() const {
380 if (size_ == 0 || !WordStartRange_[value_[0]])
382 for (size_t i(1); i != size_; ++i)
383 if (!WordEndRange_[value_[i]])
388 virtual void Output(std::ostream &out) const {
389 return Output(out, CYNoFlags);
392 virtual void Output(std::ostream &out, CYFlags flags) const;
393 virtual void PropertyName(std::ostream &out) const;
402 CYNumber(double value) :
407 double Value() const {
411 virtual void Output(std::ostream &out) const {
412 return Output(out, CYNoFlags);
415 virtual void Output(std::ostream &out, CYFlags flags) const;
416 virtual void PropertyName(std::ostream &out) const;
428 virtual void Output(std::ostream &out, CYFlags flags) const;
440 virtual void Output(std::ostream &out, CYFlags flags) const;
446 virtual bool Value() const = 0;
447 virtual void Output(std::ostream &out, CYFlags flags) const;
459 virtual bool Value() const;
471 virtual bool Value() const;
479 CYVariable(CYIdentifier *name) :
486 virtual void Output(std::ostream &out, CYFlags flags) const;
494 CYPrefix(CYExpression *rhs) :
499 virtual bool Alphabetic() const = 0;
500 virtual const char *Operator() const = 0;
502 virtual void Output(std::ostream &out, CYFlags flags) const;
511 CYInfix(CYExpression *lhs, CYExpression *rhs) :
517 void SetLeft(CYExpression *lhs) {
521 virtual bool Alphabetic() const = 0;
522 virtual const char *Operator() const = 0;
524 virtual void Output(std::ostream &out, CYFlags flags) const;
532 CYPostfix(CYExpression *lhs) :
537 virtual const char *Operator() const = 0;
539 virtual void Output(std::ostream &out, CYFlags flags) const;
542 struct CYAssignment :
548 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
554 void SetLeft(CYExpression *lhs) {
558 virtual const char *Operator() const = 0;
560 virtual void Output(std::ostream &out, CYFlags flags) const;
567 CYExpression *value_;
569 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
570 CYNext<CYArgument>(next),
576 void Output(std::ostream &out) const;
595 CYClause(CYExpression *_case, CYStatement *code) :
601 virtual void Output(std::ostream &out) const;
607 CYExpression *value_;
609 CYElement(CYExpression *value, CYElement *next) :
610 CYNext<CYElement>(next),
615 void Output(std::ostream &out) const;
621 CYElement *elements_;
623 CYArray(CYElement *elements) :
628 virtual void Output(std::ostream &out, CYFlags flags) const;
631 struct CYDeclaration :
635 CYIdentifier *identifier_;
636 CYExpression *initialiser_;
638 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
639 identifier_(identifier),
640 initialiser_(initialiser)
644 virtual void Part(std::ostream &out) const;
645 virtual void Output(std::ostream &out) const;
648 struct CYDeclarations :
652 CYDeclaration *declaration_;
653 CYDeclarations *next_;
655 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
656 declaration_(declaration),
661 virtual void Part(std::ostream &out) const;
662 virtual void Output(std::ostream &out) const;
668 virtual void Output(std::ostream &out) const;
671 struct CYMessageParameter :
672 CYNext<CYMessageParameter>
678 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
691 CYMessageParameter *parameter_;
694 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
697 parameter_(parameter),
702 virtual void Output(std::ostream &out, bool replace) const;
709 CYExpression *super_;
711 CYMessage *messages_;
713 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
723 virtual void Output(std::ostream &out, CYFlags flags) const;
730 CYMessage *messages_;
732 CYCategory(CYClassName *name, CYMessage *messages) :
738 virtual void Output(std::ostream &out) const;
741 struct CYFunctionParameter :
742 CYNext<CYFunctionParameter>,
747 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
748 CYNext<CYFunctionParameter>(next),
753 virtual void Output(std::ostream &out) const;
759 CYForInitialiser *initialiser_;
761 CYExpression *increment_;
764 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
765 initialiser_(initialiser),
767 increment_(increment),
772 virtual void Output(std::ostream &out) const;
778 CYForInInitialiser *initialiser_;
782 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
783 initialiser_(initialiser),
789 virtual void Output(std::ostream &out) const;
795 CYPropertyName *name_;
796 CYExpression *value_;
798 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
799 CYNext<CYProperty>(next),
805 virtual void Output(std::ostream &out) const;
811 CYProperty *property_;
813 CYObject(CYProperty *property) :
818 void Output(std::ostream &out, CYFlags flags) const;
827 CYCatch(CYIdentifier *name, CYStatement *code) :
833 virtual void Output(std::ostream &out) const;
840 CYArgument *arguments_;
842 CYSend(CYExpression *self, CYArgument *arguments) :
844 arguments_(arguments)
850 virtual void Output(std::ostream &out, CYFlags flags) const;
856 CYExpression *object_;
857 CYExpression *property_;
859 CYMember(CYExpression *object, CYExpression *property) :
865 void SetLeft(CYExpression *object) {
870 struct CYDirectMember :
873 CYDirectMember(CYExpression *object, CYExpression *property) :
874 CYMember(object, property)
880 virtual void Output(std::ostream &out, CYFlags flags) const;
883 struct CYIndirectMember :
886 CYIndirectMember(CYExpression *object, CYExpression *property) :
887 CYMember(object, property)
893 virtual void Output(std::ostream &out, CYFlags flags) const;
899 CYExpression *constructor_;
900 CYArgument *arguments_;
902 CYNew(CYExpression *constructor, CYArgument *arguments) :
903 constructor_(constructor),
904 arguments_(arguments)
910 virtual void Output(std::ostream &out, CYFlags flags) const;
916 CYExpression *function_;
917 CYArgument *arguments_;
919 CYCall(CYExpression *function, CYArgument *arguments) :
921 arguments_(arguments)
927 virtual void Output(std::ostream &out, CYFlags flags) const;
937 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
944 virtual void Output(std::ostream &out) const;
953 CYDoWhile(CYExpression *test, CYStatement *code) :
959 virtual void Output(std::ostream &out) const;
968 CYWhile(CYExpression *test, CYStatement *code) :
974 virtual void Output(std::ostream &out) const;
981 CYFunctionParameter *parameters_;
984 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
986 parameters_(parameters),
993 virtual void Output(std::ostream &out, CYFlags flags) const;
1000 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1001 CYLambda(name, parameters, body)
1005 virtual void Output(std::ostream &out) const;
1011 CYExpression *expression_;
1013 CYExpress(CYExpression *expression) :
1014 expression_(expression)
1018 virtual void Output(std::ostream &out) const;
1024 CYIdentifier *label_;
1026 CYContinue(CYIdentifier *label) :
1031 virtual void Output(std::ostream &out) const;
1037 CYIdentifier *label_;
1039 CYBreak(CYIdentifier *label) :
1044 virtual void Output(std::ostream &out) const;
1050 CYExpression *value_;
1052 CYReturn(CYExpression *value) :
1057 virtual void Output(std::ostream &out) const;
1063 virtual void Output(std::ostream &out) const;
1064 virtual void Output(std::ostream &out, bool block) const;
1072 CYStatement *finally_;
1074 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1081 virtual void Output(std::ostream &out) const;
1087 CYExpression *value_;
1089 CYThrow(CYExpression *value) :
1094 virtual void Output(std::ostream &out) const;
1100 CYExpression *scope_;
1103 CYWith(CYExpression *scope, CYStatement *code) :
1109 virtual void Output(std::ostream &out) const;
1115 CYExpression *value_;
1118 CYSwitch(CYExpression *value, CYClause *clauses) :
1124 virtual void Output(std::ostream &out) const;
1127 struct CYCondition :
1130 CYExpression *test_;
1131 CYExpression *true_;
1132 CYExpression *false_;
1134 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1143 virtual void Output(std::ostream &out, CYFlags flags) const;
1146 struct CYAddressOf :
1149 CYAddressOf(CYExpression *rhs) :
1154 virtual const char *Operator() const {
1161 virtual void Output(std::ostream &out, CYFlags flags) const;
1167 CYIndirect(CYExpression *rhs) :
1172 virtual const char *Operator() const {
1179 virtual void Output(std::ostream &out, CYFlags flags) const;
1182 #define CYPostfix_(op, name) \
1183 struct CY ## name : \
1186 CY ## name(CYExpression *lhs) : \
1193 virtual const char *Operator() const { \
1198 #define CYPrefix_(alphabetic, op, name) \
1199 struct CY ## name : \
1202 CY ## name(CYExpression *rhs) : \
1207 CYAlphabetic(alphabetic) \
1210 virtual const char *Operator() const { \
1215 #define CYInfix_(alphabetic, precedence, op, name) \
1216 struct CY ## name : \
1219 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1224 CYAlphabetic(alphabetic) \
1225 CYPrecedence(precedence) \
1227 virtual const char *Operator() const { \
1232 #define CYAssignment_(op, name) \
1233 struct CY ## name ## Assign : \
1236 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1237 CYAssignment(lhs, rhs) \
1243 virtual const char *Operator() const { \
1248 CYPostfix_("++", PostIncrement)
1249 CYPostfix_("--", PostDecrement)
1251 CYPrefix_(true, "delete", Delete)
1252 CYPrefix_(true, "void", Void)
1253 CYPrefix_(true, "typeof", TypeOf)
1254 CYPrefix_(false, "++", PreIncrement)
1255 CYPrefix_(false, "--", PreDecrement)
1256 CYPrefix_(false, "-", Negate)
1257 CYPrefix_(false, "~", BitwiseNot)
1258 CYPrefix_(false, "!", LogicalNot)
1260 CYInfix_(false, 5, "*", Multiply)
1261 CYInfix_(false, 5, "/", Divide)
1262 CYInfix_(false, 5, "%", Modulus)
1263 CYInfix_(false, 6, "+", Add)
1264 CYInfix_(false, 6, "-", Subtract)
1265 CYInfix_(false, 7, "<<", ShiftLeft)
1266 CYInfix_(false, 7, ">>", ShiftRightSigned)
1267 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1268 CYInfix_(false, 8, "<", Less)
1269 CYInfix_(false, 8, ">", Greater)
1270 CYInfix_(false, 8, "<=", LessOrEqual)
1271 CYInfix_(false, 8, ">=", GreaterOrEqual)
1272 CYInfix_(true, 8, "instanceof", InstanceOf)
1273 CYInfix_(true, 8, "in", In)
1274 CYInfix_(false, 9, "==", Equal)
1275 CYInfix_(false, 9, "!=", NotEqual)
1276 CYInfix_(false, 9, "===", Identical)
1277 CYInfix_(false, 9, "!==", NotIdentical)
1278 CYInfix_(false, 10, "&", BitwiseAnd)
1279 CYInfix_(false, 11, "^", BitwiseXOr)
1280 CYInfix_(false, 12, "|", BitwiseOr)
1281 CYInfix_(false, 13, "&&", LogicalAnd)
1282 CYInfix_(false, 14, "||", LogicalOr)
1284 CYAssignment_("=", )
1285 CYAssignment_("*=", Multiply)
1286 CYAssignment_("/=", Divide)
1287 CYAssignment_("%=", Modulus)
1288 CYAssignment_("+=", Add)
1289 CYAssignment_("-=", Subtract)
1290 CYAssignment_("<<=", ShiftLeft)
1291 CYAssignment_(">>=", ShiftRightSigned)
1292 CYAssignment_(">>>=", ShiftRightUnsigned)
1293 CYAssignment_("&=", BitwiseAnd)
1294 CYAssignment_("^=", BitwiseXOr)
1295 CYAssignment_("|=", BitwiseOr)
1297 #endif/*CYPARSER_HPP*/