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) 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) 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) 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(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) :
721 virtual void Output(std::ostream &out) const;
728 CYMessage *messages_;
730 CYCategory(CYClassName *name, CYMessage *messages) :
736 virtual void Output(std::ostream &out) const;
739 struct CYFunctionParameter :
740 CYNext<CYFunctionParameter>,
745 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
746 CYNext<CYFunctionParameter>(next),
751 virtual void Output(std::ostream &out) const;
757 CYForInitialiser *initialiser_;
759 CYExpression *increment_;
762 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
763 initialiser_(initialiser),
765 increment_(increment),
770 virtual void Output(std::ostream &out) const;
776 CYForInInitialiser *initialiser_;
780 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
781 initialiser_(initialiser),
787 virtual void Output(std::ostream &out) const;
793 CYPropertyName *name_;
794 CYExpression *value_;
796 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
797 CYNext<CYProperty>(next),
803 virtual void Output(std::ostream &out) const;
809 CYProperty *property_;
811 CYObject(CYProperty *property) :
816 void Output(std::ostream &out, CYFlags flags) const;
825 CYCatch(CYIdentifier *name, CYStatement *code) :
831 virtual void Output(std::ostream &out) const;
838 CYArgument *arguments_;
840 CYSend(CYExpression *self, CYArgument *arguments) :
842 arguments_(arguments)
848 virtual void Output(std::ostream &out, CYFlags flags) const;
854 CYExpression *object_;
855 CYExpression *property_;
857 CYMember(CYExpression *object, CYExpression *property) :
863 void SetLeft(CYExpression *object) {
868 struct CYDirectMember :
871 CYDirectMember(CYExpression *object, CYExpression *property) :
872 CYMember(object, property)
878 virtual void Output(std::ostream &out, CYFlags flags) const;
881 struct CYIndirectMember :
884 CYIndirectMember(CYExpression *object, CYExpression *property) :
885 CYMember(object, property)
891 virtual void Output(std::ostream &out, CYFlags flags) const;
897 CYExpression *constructor_;
898 CYArgument *arguments_;
900 CYNew(CYExpression *constructor, CYArgument *arguments) :
901 constructor_(constructor),
902 arguments_(arguments)
908 virtual void Output(std::ostream &out, CYFlags flags) const;
914 CYExpression *function_;
915 CYArgument *arguments_;
917 CYCall(CYExpression *function, CYArgument *arguments) :
919 arguments_(arguments)
925 virtual void Output(std::ostream &out, CYFlags flags) const;
935 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
942 virtual void Output(std::ostream &out) const;
951 CYDoWhile(CYExpression *test, CYStatement *code) :
957 virtual void Output(std::ostream &out) const;
966 CYWhile(CYExpression *test, CYStatement *code) :
972 virtual void Output(std::ostream &out) const;
979 CYFunctionParameter *parameters_;
982 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
984 parameters_(parameters),
991 virtual void Output(std::ostream &out, CYFlags flags) const;
998 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
999 CYLambda(name, parameters, body)
1003 virtual void Output(std::ostream &out) const;
1009 CYExpression *expression_;
1011 CYExpress(CYExpression *expression) :
1012 expression_(expression)
1016 virtual void Output(std::ostream &out) const;
1022 CYIdentifier *label_;
1024 CYContinue(CYIdentifier *label) :
1029 virtual void Output(std::ostream &out) const;
1035 CYIdentifier *label_;
1037 CYBreak(CYIdentifier *label) :
1042 virtual void Output(std::ostream &out) const;
1048 CYExpression *value_;
1050 CYReturn(CYExpression *value) :
1055 virtual void Output(std::ostream &out) const;
1061 virtual void Output(std::ostream &out) const;
1062 virtual void Output(std::ostream &out, bool block) const;
1070 CYStatement *finally_;
1072 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1079 virtual void Output(std::ostream &out) const;
1085 CYExpression *value_;
1087 CYThrow(CYExpression *value) :
1092 virtual void Output(std::ostream &out) const;
1098 CYExpression *scope_;
1101 CYWith(CYExpression *scope, CYStatement *code) :
1107 virtual void Output(std::ostream &out) const;
1113 CYExpression *value_;
1116 CYSwitch(CYExpression *value, CYClause *clauses) :
1122 virtual void Output(std::ostream &out) const;
1125 struct CYCondition :
1128 CYExpression *test_;
1129 CYExpression *true_;
1130 CYExpression *false_;
1132 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1141 virtual void Output(std::ostream &out, CYFlags flags) const;
1144 struct CYAddressOf :
1147 CYAddressOf(CYExpression *rhs) :
1152 virtual const char *Operator() const {
1159 virtual void Output(std::ostream &out, CYFlags flags) const;
1165 CYIndirect(CYExpression *rhs) :
1170 virtual const char *Operator() const {
1177 virtual void Output(std::ostream &out, CYFlags flags) const;
1180 #define CYPostfix_(op, name) \
1181 struct CY ## name : \
1184 CY ## name(CYExpression *lhs) : \
1191 virtual const char *Operator() const { \
1196 #define CYPrefix_(alphabetic, op, name) \
1197 struct CY ## name : \
1200 CY ## name(CYExpression *rhs) : \
1205 CYAlphabetic(alphabetic) \
1208 virtual const char *Operator() const { \
1213 #define CYInfix_(alphabetic, precedence, op, name) \
1214 struct CY ## name : \
1217 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1222 CYAlphabetic(alphabetic) \
1223 CYPrecedence(precedence) \
1225 virtual const char *Operator() const { \
1230 #define CYAssignment_(op, name) \
1231 struct CY ## name ## Assign : \
1234 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1235 CYAssignment(lhs, rhs) \
1241 virtual const char *Operator() const { \
1246 CYPostfix_("++", PostIncrement)
1247 CYPostfix_("--", PostDecrement)
1249 CYPrefix_(true, "delete", Delete)
1250 CYPrefix_(true, "void", Void)
1251 CYPrefix_(true, "typeof", TypeOf)
1252 CYPrefix_(false, "++", PreIncrement)
1253 CYPrefix_(false, "--", PreDecrement)
1254 CYPrefix_(false, "-", Negate)
1255 CYPrefix_(false, "~", BitwiseNot)
1256 CYPrefix_(false, "!", LogicalNot)
1258 CYInfix_(false, 5, "*", Multiply)
1259 CYInfix_(false, 5, "/", Divide)
1260 CYInfix_(false, 5, "%", Modulus)
1261 CYInfix_(false, 6, "+", Add)
1262 CYInfix_(false, 6, "-", Subtract)
1263 CYInfix_(false, 7, "<<", ShiftLeft)
1264 CYInfix_(false, 7, ">>", ShiftRightSigned)
1265 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1266 CYInfix_(false, 8, "<", Less)
1267 CYInfix_(false, 8, ">", Greater)
1268 CYInfix_(false, 8, "<=", LessOrEqual)
1269 CYInfix_(false, 8, ">=", GreaterOrEqual)
1270 CYInfix_(true, 8, "instanceof", InstanceOf)
1271 CYInfix_(true, 8, "in", In)
1272 CYInfix_(false, 9, "==", Equal)
1273 CYInfix_(false, 9, "!=", NotEqual)
1274 CYInfix_(false, 9, "===", Identical)
1275 CYInfix_(false, 9, "!==", NotIdentical)
1276 CYInfix_(false, 10, "&", BitwiseAnd)
1277 CYInfix_(false, 11, "^", BitwiseXOr)
1278 CYInfix_(false, 12, "|", BitwiseOr)
1279 CYInfix_(false, 13, "&&", LogicalAnd)
1280 CYInfix_(false, 14, "||", LogicalOr)
1282 CYAssignment_("=", )
1283 CYAssignment_("*=", Multiply)
1284 CYAssignment_("/=", Divide)
1285 CYAssignment_("%=", Modulus)
1286 CYAssignment_("+=", Add)
1287 CYAssignment_("-=", Subtract)
1288 CYAssignment_("<<=", ShiftLeft)
1289 CYAssignment_(">>=", ShiftRightSigned)
1290 CYAssignment_(">>>=", ShiftRightUnsigned)
1291 CYAssignment_("&=", BitwiseAnd)
1292 CYAssignment_("^=", BitwiseXOr)
1293 CYAssignment_("|=", BitwiseOr)
1295 #endif/*CYPARSER_HPP*/