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 void Show(std::ostream &out) const;
82 virtual void Output(std::ostream &out) const = 0;
83 virtual void Output(std::ostream &out, bool block) const;
86 struct CYPropertyName {
87 virtual void PropertyName(std::ostream &out) const = 0;
91 virtual void ClassName(std::ostream &out) const = 0;
101 CYWord(const char *word) :
106 const char *Value() const {
110 virtual void Output(std::ostream &out) const;
112 virtual void ClassName(std::ostream &out) const;
113 virtual void PropertyName(std::ostream &out) const;
116 struct CYIdentifier :
119 CYIdentifier(const char *word) :
128 CYIdentifier *identifier_;
130 CYLabel(CYIdentifier *identifier, CYLabel *next) :
131 CYNext<CYLabel>(next),
132 identifier_(identifier)
142 void AddLabel(CYIdentifier *identifier) {
143 label_ = new CYLabel(identifier, label_);
164 std::string filename_;
167 cy::location location_;
168 std::string message_;
171 typedef std::vector<Error> Errors;
178 void ScannerDestroy();
181 CYDriver(const std::string &filename);
186 virtual void Part(std::ostream &out) const = 0;
189 struct CYForInitialiser :
194 struct CYForInInitialiser :
201 CYNoBrace = (1 << 0),
202 CYNoFunction = (1 << 1),
203 CYNoLeader = (1 << 2),
204 CYNoTrailer = (1 << 3),
206 CYNoHyphen = (1 << 5),
209 struct CYExpression :
210 CYNext<CYExpression>,
215 virtual unsigned Precedence() const = 0;
216 virtual void Part(std::ostream &out) const;
217 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
218 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
220 virtual void ClassName(std::ostream &out) const;
222 virtual const char *Word() const {
227 #define CYAlphabetic(value) \
228 virtual bool Alphabetic() const { \
232 #define CYPrecedence(value) \
233 virtual unsigned Precedence() const { \
240 CYExpression *expressions_;
242 CYCompound(CYExpression *expressions) :
243 expressions_(expressions)
247 void AddPrev(CYExpression *expression) {
248 CYExpression *last(expression);
249 while (last->next_ != NULL)
251 last->SetNext(expressions_);
252 expressions_ = expression;
257 void Output(std::ostream &out, CYFlags flags) const;
272 struct CYSelectorPart :
273 CYNext<CYSelectorPart>
278 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
279 CYNext<CYSelectorPart>(next),
285 virtual void Output(std::ostream &out) const;
291 CYSelectorPart *name_;
293 CYSelector(CYSelectorPart *name) :
300 virtual void Output(std::ostream &out, CYFlags flags) const;
307 CYRange(uint64_t lo, uint64_t hi) :
312 bool operator [](uint8_t value) const {
313 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
316 void operator()(uint8_t value) {
319 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
323 extern CYRange DigitRange_;
324 extern CYRange WordStartRange_;
325 extern CYRange WordEndRange_;
334 CYString(const char *value, size_t size) :
340 CYString(const CYIdentifier *identifier) :
341 value_(identifier->Value()),
342 size_(strlen(value_))
346 const char *Value() const {
350 virtual const char *Word() const {
351 if (size_ == 0 || !WordStartRange_[value_[0]])
353 for (size_t i(1); i != size_; ++i)
354 if (!WordEndRange_[value_[i]])
359 virtual void Output(std::ostream &out) const {
360 return Output(out, CYNoFlags);
363 virtual void Output(std::ostream &out, CYFlags flags) const;
364 virtual void PropertyName(std::ostream &out) const;
373 CYNumber(double value) :
378 double Value() const {
382 virtual void Output(std::ostream &out) const {
383 return Output(out, CYNoFlags);
386 virtual void Output(std::ostream &out, CYFlags flags) const;
387 virtual void PropertyName(std::ostream &out) const;
399 virtual void Output(std::ostream &out, CYFlags flags) const;
411 virtual void Output(std::ostream &out, CYFlags flags) const;
417 virtual bool Value() const = 0;
418 virtual void Output(std::ostream &out, CYFlags flags) const;
430 virtual bool Value() const;
442 virtual bool Value() const;
450 CYVariable(CYIdentifier *name) :
457 virtual void Output(std::ostream &out, CYFlags flags) const;
465 CYPrefix(CYExpression *rhs) :
470 virtual bool Alphabetic() const = 0;
471 virtual const char *Operator() const = 0;
473 virtual void Output(std::ostream &out, CYFlags flags) const;
482 CYInfix(CYExpression *lhs, CYExpression *rhs) :
488 void SetLeft(CYExpression *lhs) {
492 virtual bool Alphabetic() const = 0;
493 virtual const char *Operator() const = 0;
495 virtual void Output(std::ostream &out, CYFlags flags) const;
503 CYPostfix(CYExpression *lhs) :
508 virtual const char *Operator() const = 0;
510 virtual void Output(std::ostream &out, CYFlags flags) const;
513 struct CYAssignment :
519 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
525 void SetLeft(CYExpression *lhs) {
529 virtual const char *Operator() const = 0;
531 virtual void Output(std::ostream &out, CYFlags flags) const;
538 CYExpression *value_;
540 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
541 CYNext<CYArgument>(next),
547 void Output(std::ostream &out) const;
566 CYClause(CYExpression *_case, CYStatement *code) :
572 virtual void Output(std::ostream &out) const;
578 CYExpression *value_;
580 CYElement(CYExpression *value, CYElement *next) :
581 CYNext<CYElement>(next),
586 void Output(std::ostream &out) const;
592 CYElement *elements_;
594 CYArray(CYElement *elements) :
599 virtual void Output(std::ostream &out, CYFlags flags) const;
602 struct CYDeclaration :
606 CYIdentifier *identifier_;
607 CYExpression *initialiser_;
609 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
610 identifier_(identifier),
611 initialiser_(initialiser)
615 virtual void Part(std::ostream &out) const;
616 virtual void Output(std::ostream &out) const;
619 struct CYDeclarations :
623 CYDeclaration *declaration_;
624 CYDeclarations *next_;
626 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
627 declaration_(declaration),
632 virtual void Part(std::ostream &out) const;
633 virtual void Output(std::ostream &out) const;
639 virtual void Output(std::ostream &out) const;
642 struct CYMessageParameter :
643 CYNext<CYMessageParameter>
649 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
662 CYMessageParameter *parameter_;
665 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
668 parameter_(parameter),
673 virtual void Output(std::ostream &out, bool replace) const;
680 CYExpression *super_;
682 CYMessage *messages_;
684 CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) :
692 virtual void Output(std::ostream &out) const;
699 CYMessage *messages_;
701 CYCategory(CYClassName *name, CYMessage *messages) :
707 virtual void Output(std::ostream &out) const;
710 struct CYFunctionParameter :
711 CYNext<CYFunctionParameter>,
716 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
717 CYNext<CYFunctionParameter>(next),
722 virtual void Output(std::ostream &out) const;
728 CYForInitialiser *initialiser_;
730 CYExpression *increment_;
733 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
734 initialiser_(initialiser),
736 increment_(increment),
741 virtual void Output(std::ostream &out) const;
747 CYForInInitialiser *initialiser_;
751 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
752 initialiser_(initialiser),
758 virtual void Output(std::ostream &out) const;
764 CYPropertyName *name_;
765 CYExpression *value_;
767 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
768 CYNext<CYProperty>(next),
774 virtual void Output(std::ostream &out) const;
780 CYProperty *property_;
782 CYObject(CYProperty *property) :
787 void Output(std::ostream &out, CYFlags flags) const;
796 CYCatch(CYIdentifier *name, CYStatement *code) :
802 virtual void Output(std::ostream &out) const;
809 CYArgument *arguments_;
811 CYSend(CYExpression *self, CYArgument *arguments) :
813 arguments_(arguments)
819 virtual void Output(std::ostream &out, CYFlags flags) const;
825 CYExpression *object_;
826 CYExpression *property_;
828 CYMember(CYExpression *object, CYExpression *property) :
834 void SetLeft(CYExpression *object) {
839 struct CYDirectMember :
842 CYDirectMember(CYExpression *object, CYExpression *property) :
843 CYMember(object, property)
849 virtual void Output(std::ostream &out, CYFlags flags) const;
852 struct CYIndirectMember :
855 CYIndirectMember(CYExpression *object, CYExpression *property) :
856 CYMember(object, property)
862 virtual void Output(std::ostream &out, CYFlags flags) const;
868 CYExpression *constructor_;
869 CYArgument *arguments_;
871 CYNew(CYExpression *constructor, CYArgument *arguments) :
872 constructor_(constructor),
873 arguments_(arguments)
879 virtual void Output(std::ostream &out, CYFlags flags) const;
885 CYExpression *function_;
886 CYArgument *arguments_;
888 CYCall(CYExpression *function, CYArgument *arguments) :
890 arguments_(arguments)
896 virtual void Output(std::ostream &out, CYFlags flags) const;
906 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
913 virtual void Output(std::ostream &out) const;
922 CYDoWhile(CYExpression *test, CYStatement *code) :
928 virtual void Output(std::ostream &out) const;
937 CYWhile(CYExpression *test, CYStatement *code) :
943 virtual void Output(std::ostream &out) const;
950 CYFunctionParameter *parameters_;
953 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
955 parameters_(parameters),
962 virtual void Output(std::ostream &out, CYFlags flags) const;
969 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
970 CYLambda(name, parameters, body)
974 virtual void Output(std::ostream &out) const;
980 CYExpression *expression_;
982 CYExpress(CYExpression *expression) :
983 expression_(expression)
987 virtual void Output(std::ostream &out) const;
993 CYIdentifier *label_;
995 CYContinue(CYIdentifier *label) :
1000 virtual void Output(std::ostream &out) const;
1006 CYIdentifier *label_;
1008 CYBreak(CYIdentifier *label) :
1013 virtual void Output(std::ostream &out) const;
1019 CYExpression *value_;
1021 CYReturn(CYExpression *value) :
1026 virtual void Output(std::ostream &out) const;
1032 virtual void Output(std::ostream &out) const;
1033 virtual void Output(std::ostream &out, bool block) const;
1041 CYStatement *finally_;
1043 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1050 virtual void Output(std::ostream &out) const;
1056 CYExpression *value_;
1058 CYThrow(CYExpression *value) :
1063 virtual void Output(std::ostream &out) const;
1069 CYExpression *scope_;
1072 CYWith(CYExpression *scope, CYStatement *code) :
1078 virtual void Output(std::ostream &out) const;
1084 CYExpression *value_;
1087 CYSwitch(CYExpression *value, CYClause *clauses) :
1093 virtual void Output(std::ostream &out) const;
1096 struct CYCondition :
1099 CYExpression *test_;
1100 CYExpression *true_;
1101 CYExpression *false_;
1103 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1112 virtual void Output(std::ostream &out, CYFlags flags) const;
1115 struct CYAddressOf :
1118 CYAddressOf(CYExpression *rhs) :
1123 virtual const char *Operator() const {
1130 virtual void Output(std::ostream &out, CYFlags flags) const;
1136 CYIndirect(CYExpression *rhs) :
1141 virtual const char *Operator() const {
1148 virtual void Output(std::ostream &out, CYFlags flags) const;
1151 #define CYPostfix_(op, name) \
1152 struct CY ## name : \
1155 CY ## name(CYExpression *lhs) : \
1162 virtual const char *Operator() const { \
1167 #define CYPrefix_(alphabetic, op, name) \
1168 struct CY ## name : \
1171 CY ## name(CYExpression *rhs) : \
1176 CYAlphabetic(alphabetic) \
1179 virtual const char *Operator() const { \
1184 #define CYInfix_(alphabetic, precedence, op, name) \
1185 struct CY ## name : \
1188 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1193 CYAlphabetic(alphabetic) \
1194 CYPrecedence(precedence) \
1196 virtual const char *Operator() const { \
1201 #define CYAssignment_(op, name) \
1202 struct CY ## name ## Assign : \
1205 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1206 CYAssignment(lhs, rhs) \
1212 virtual const char *Operator() const { \
1217 CYPostfix_("++", PostIncrement)
1218 CYPostfix_("--", PostDecrement)
1220 CYPrefix_(true, "delete", Delete)
1221 CYPrefix_(true, "void", Void)
1222 CYPrefix_(true, "typeof", TypeOf)
1223 CYPrefix_(false, "++", PreIncrement)
1224 CYPrefix_(false, "--", PreDecrement)
1225 CYPrefix_(false, "-", Negate)
1226 CYPrefix_(false, "~", BitwiseNot)
1227 CYPrefix_(false, "!", LogicalNot)
1229 CYInfix_(false, 5, "*", Multiply)
1230 CYInfix_(false, 5, "/", Divide)
1231 CYInfix_(false, 5, "%", Modulus)
1232 CYInfix_(false, 6, "+", Add)
1233 CYInfix_(false, 6, "-", Subtract)
1234 CYInfix_(false, 7, "<<", ShiftLeft)
1235 CYInfix_(false, 7, ">>", ShiftRightSigned)
1236 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1237 CYInfix_(false, 8, "<", Less)
1238 CYInfix_(false, 8, ">", Greater)
1239 CYInfix_(false, 8, "<=", LessOrEqual)
1240 CYInfix_(false, 8, ">=", GreaterOrEqual)
1241 CYInfix_(true, 8, "instanceof", InstanceOf)
1242 CYInfix_(true, 8, "in", In)
1243 CYInfix_(false, 9, "==", Equal)
1244 CYInfix_(false, 9, "!=", NotEqual)
1245 CYInfix_(false, 9, "===", Identical)
1246 CYInfix_(false, 9, "!==", NotIdentical)
1247 CYInfix_(false, 10, "&", BitwiseAnd)
1248 CYInfix_(false, 11, "^", BitwiseXOr)
1249 CYInfix_(false, 12, "|", BitwiseOr)
1250 CYInfix_(false, 13, "&&", LogicalAnd)
1251 CYInfix_(false, 14, "||", LogicalOr)
1253 CYAssignment_("=", )
1254 CYAssignment_("*=", Multiply)
1255 CYAssignment_("/=", Divide)
1256 CYAssignment_("%=", Modulus)
1257 CYAssignment_("+=", Add)
1258 CYAssignment_("-=", Subtract)
1259 CYAssignment_("<<=", ShiftLeft)
1260 CYAssignment_(">>=", ShiftRightSigned)
1261 CYAssignment_(">>>=", ShiftRightUnsigned)
1262 CYAssignment_("&=", BitwiseAnd)
1263 CYAssignment_("^=", BitwiseXOr)
1264 CYAssignment_("|=", BitwiseOr)
1266 #endif/*CYPARSER_HPP*/