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;
216 std::string filename_;
219 cy::location location_;
220 std::string message_;
223 typedef std::vector<Error> Errors;
230 void ScannerDestroy();
233 CYDriver(const std::string &filename);
239 CYNoBrace = (1 << 0),
240 CYNoFunction = (1 << 1),
241 CYNoLeader = (1 << 2),
242 CYNoTrailer = (1 << 3),
244 CYNoHyphen = (1 << 5),
245 CYNoBF = (CYNoBrace | CYNoFunction),
248 struct CYForInitialiser {
249 virtual void For(CYOutput &out) const = 0;
252 struct CYForInInitialiser {
253 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
254 virtual const char *ForEachIn() const = 0;
255 virtual void ForEachIn(CYOutput &out) const = 0;
258 struct CYExpression :
259 CYNext<CYExpression>,
264 virtual unsigned Precedence() const = 0;
266 virtual void For(CYOutput &out) const;
267 virtual void ForIn(CYOutput &out, CYFlags flags) const;
269 virtual const char *ForEachIn() const;
270 virtual void ForEachIn(CYOutput &out) const;
272 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
273 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
275 virtual void ClassName(CYOutput &out, bool object) const;
277 virtual const char *Word() const {
282 #define CYAlphabetic(value) \
283 virtual bool Alphabetic() const { \
287 #define CYPrecedence(value) \
288 virtual unsigned Precedence() const { \
295 CYExpression *expressions_;
297 CYCompound(CYExpression *expressions) :
298 expressions_(expressions)
302 void AddPrev(CYExpression *expression) {
303 CYExpression *last(expression);
304 while (last->next_ != NULL)
306 last->SetNext(expressions_);
307 expressions_ = expression;
312 void Output(CYOutput &out, CYFlags flags) const;
315 struct CYComprehension :
316 CYNext<CYComprehension>
318 void Output(CYOutput &out) const;
319 virtual const char *Name() const = 0;
321 virtual void Begin_(CYOutput &out) const = 0;
323 virtual void End_(CYOutput &out) const {
327 struct CYForInComprehension :
333 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
339 virtual const char *Name() const {
340 return name_->Value();
343 virtual void Begin_(CYOutput &out) const;
346 struct CYForEachInComprehension :
352 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
358 virtual const char *Name() const {
359 return name_->Value();
362 virtual void Begin_(CYOutput &out) const;
363 virtual void End_(CYOutput &out) const;
366 struct CYIfComprehension :
371 CYIfComprehension(CYExpression *test) :
376 virtual const char *Name() const {
380 virtual void Begin_(CYOutput &out) const;
383 struct CYArrayComprehension :
386 CYExpression *expression_;
387 CYComprehension *comprehensions_;
389 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
390 expression_(expression),
391 comprehensions_(comprehensions)
397 virtual void Output(CYOutput &out, CYFlags flags) const;
412 struct CYSelectorPart :
413 CYNext<CYSelectorPart>
418 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
419 CYNext<CYSelectorPart>(next),
425 virtual void Output(CYOutput &out) const;
431 CYSelectorPart *name_;
433 CYSelector(CYSelectorPart *name) :
440 virtual void Output(CYOutput &out, CYFlags flags) const;
447 CYRange(uint64_t lo, uint64_t hi) :
452 bool operator [](uint8_t value) const {
453 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
456 void operator()(uint8_t value) {
459 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
463 extern CYRange DigitRange_;
464 extern CYRange WordStartRange_;
465 extern CYRange WordEndRange_;
474 CYString(const char *value, size_t size) :
480 CYString(const CYIdentifier *identifier) :
481 value_(identifier->Value()),
482 size_(strlen(value_))
486 const char *Value() const {
490 virtual const char *Word() const {
491 if (size_ == 0 || !WordStartRange_[value_[0]])
493 for (size_t i(1); i != size_; ++i)
494 if (!WordEndRange_[value_[i]])
499 virtual void Output(CYOutput &out) const {
500 return Output(out, CYNoFlags);
503 virtual void Output(CYOutput &out, CYFlags flags) const;
504 virtual void PropertyName(CYOutput &out) const;
513 CYNumber(double value) :
518 double Value() const {
522 virtual void Output(CYOutput &out) const {
523 return Output(out, CYNoFlags);
526 virtual void Output(CYOutput &out, CYFlags flags) const;
527 virtual void PropertyName(CYOutput &out) const;
539 virtual void Output(CYOutput &out, CYFlags flags) const;
551 virtual void Output(CYOutput &out, CYFlags flags) const;
557 virtual bool Value() const = 0;
558 virtual void Output(CYOutput &out, CYFlags flags) const;
570 virtual bool Value() const;
582 virtual bool Value() const;
590 CYVariable(CYIdentifier *name) :
597 virtual void Output(CYOutput &out, CYFlags flags) const;
605 CYPrefix(CYExpression *rhs) :
610 virtual bool Alphabetic() const = 0;
611 virtual const char *Operator() const = 0;
613 virtual void Output(CYOutput &out, CYFlags flags) const;
622 CYInfix(CYExpression *lhs, CYExpression *rhs) :
628 void SetLeft(CYExpression *lhs) {
632 virtual bool Alphabetic() const = 0;
633 virtual const char *Operator() const = 0;
635 virtual void Output(CYOutput &out, CYFlags flags) const;
643 CYPostfix(CYExpression *lhs) :
648 virtual const char *Operator() const = 0;
650 virtual void Output(CYOutput &out, CYFlags flags) const;
653 struct CYAssignment :
659 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
665 void SetLeft(CYExpression *lhs) {
669 virtual const char *Operator() const = 0;
671 virtual void Output(CYOutput &out, CYFlags flags) const;
678 CYExpression *value_;
680 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
681 CYNext<CYArgument>(next),
687 void Output(CYOutput &out) const;
706 CYClause(CYExpression *_case, CYStatement *code) :
712 virtual void Output(CYOutput &out) const;
718 CYExpression *value_;
720 CYElement(CYExpression *value, CYElement *next) :
721 CYNext<CYElement>(next),
726 void Output(CYOutput &out) const;
732 CYElement *elements_;
734 CYArray(CYElement *elements) :
739 virtual void Output(CYOutput &out, CYFlags flags) const;
742 struct CYDeclaration :
745 CYIdentifier *identifier_;
746 CYExpression *initialiser_;
748 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
749 identifier_(identifier),
750 initialiser_(initialiser)
754 virtual void ForIn(CYOutput &out, CYFlags flags) const;
756 virtual const char *ForEachIn() const;
757 virtual void ForEachIn(CYOutput &out) const;
759 virtual void Output(CYOutput &out, CYFlags flags) const;
762 struct CYDeclarations :
763 CYNext<CYDeclarations>,
766 CYDeclaration *declaration_;
768 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
769 CYNext<CYDeclarations>(next),
770 declaration_(declaration)
774 virtual void For(CYOutput &out) const;
775 virtual void Output(CYOutput &out, CYFlags flags) const;
781 CYDeclarations *declarations_;
783 CYVar(CYDeclarations *declarations) :
784 declarations_(declarations)
788 virtual void Output(CYOutput &out) const;
794 CYDeclarations *declarations_;
795 CYStatement *statements_;
797 CYLet(CYDeclarations *declarations, CYStatement *statements) :
798 declarations_(declarations),
799 statements_(statements)
803 virtual void Output(CYOutput &out) const;
809 virtual void Output(CYOutput &out) const;
812 struct CYMessageParameter :
813 CYNext<CYMessageParameter>
819 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
832 CYMessageParameter *parameter_;
835 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
838 parameter_(parameter),
843 virtual void Output(CYOutput &out, bool replace) const;
851 CYExpression *super_;
853 CYMessage *messages_;
855 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
865 virtual void Output(CYOutput &out) const;
866 virtual void Output(CYOutput &out, CYFlags flags) const;
873 CYMessage *messages_;
875 CYCategory(CYClassName *name, CYMessage *messages) :
881 virtual void Output(CYOutput &out) const;
884 struct CYFunctionParameter :
885 CYNext<CYFunctionParameter>,
890 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
891 CYNext<CYFunctionParameter>(next),
896 virtual void Output(CYOutput &out) const;
902 CYForInitialiser *initialiser_;
904 CYExpression *increment_;
907 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
908 initialiser_(initialiser),
910 increment_(increment),
915 virtual void Output(CYOutput &out) const;
921 CYForInInitialiser *initialiser_;
925 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
926 initialiser_(initialiser),
932 virtual void Output(CYOutput &out) const;
938 CYForInInitialiser *initialiser_;
942 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
943 initialiser_(initialiser),
949 virtual void Output(CYOutput &out) const;
955 CYPropertyName *name_;
956 CYExpression *value_;
958 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
959 CYNext<CYProperty>(next),
965 virtual void Output(CYOutput &out) const;
971 CYProperty *property_;
973 CYObject(CYProperty *property) :
978 void Output(CYOutput &out, CYFlags flags) const;
987 CYCatch(CYIdentifier *name, CYStatement *code) :
993 virtual void Output(CYOutput &out) const;
1000 CYArgument *arguments_;
1002 CYSend(CYExpression *self, CYArgument *arguments) :
1004 arguments_(arguments)
1010 virtual void Output(CYOutput &out, CYFlags flags) const;
1016 CYExpression *object_;
1017 CYExpression *property_;
1019 CYMember(CYExpression *object, CYExpression *property) :
1025 void SetLeft(CYExpression *object) {
1030 struct CYDirectMember :
1033 CYDirectMember(CYExpression *object, CYExpression *property) :
1034 CYMember(object, property)
1040 virtual void Output(CYOutput &out, CYFlags flags) const;
1043 struct CYIndirectMember :
1046 CYIndirectMember(CYExpression *object, CYExpression *property) :
1047 CYMember(object, property)
1053 virtual void Output(CYOutput &out, CYFlags flags) const;
1059 CYExpression *constructor_;
1060 CYArgument *arguments_;
1062 CYNew(CYExpression *constructor, CYArgument *arguments) :
1063 constructor_(constructor),
1064 arguments_(arguments)
1070 virtual void Output(CYOutput &out, CYFlags flags) const;
1076 CYExpression *function_;
1077 CYArgument *arguments_;
1079 CYCall(CYExpression *function, CYArgument *arguments) :
1080 function_(function),
1081 arguments_(arguments)
1087 virtual void Output(CYOutput &out, CYFlags flags) const;
1093 CYExpression *test_;
1095 CYStatement *false_;
1097 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1104 virtual void Output(CYOutput &out) const;
1110 CYExpression *test_;
1113 CYDoWhile(CYExpression *test, CYStatement *code) :
1119 virtual void Output(CYOutput &out) const;
1125 CYExpression *test_;
1128 CYWhile(CYExpression *test, CYStatement *code) :
1134 virtual void Output(CYOutput &out) const;
1140 CYIdentifier *name_;
1141 CYFunctionParameter *parameters_;
1144 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1146 parameters_(parameters),
1153 virtual void Output(CYOutput &out, CYFlags flags) const;
1160 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1161 CYLambda(name, parameters, body)
1165 virtual void Output(CYOutput &out) const;
1171 CYExpression *expression_;
1173 CYExpress(CYExpression *expression) :
1174 expression_(expression)
1178 virtual void Output(CYOutput &out) const;
1184 CYIdentifier *label_;
1186 CYContinue(CYIdentifier *label) :
1191 virtual void Output(CYOutput &out) const;
1197 CYIdentifier *label_;
1199 CYBreak(CYIdentifier *label) :
1204 virtual void Output(CYOutput &out) const;
1210 CYExpression *value_;
1212 CYReturn(CYExpression *value) :
1217 virtual void Output(CYOutput &out) const;
1223 virtual void Output(CYOutput &out) const;
1224 virtual void Output(CYOutput &out, bool block) const;
1232 CYStatement *finally_;
1234 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1241 virtual void Output(CYOutput &out) const;
1247 CYExpression *value_;
1249 CYThrow(CYExpression *value) :
1254 virtual void Output(CYOutput &out) const;
1260 CYExpression *scope_;
1263 CYWith(CYExpression *scope, CYStatement *code) :
1269 virtual void Output(CYOutput &out) const;
1275 CYExpression *value_;
1278 CYSwitch(CYExpression *value, CYClause *clauses) :
1284 virtual void Output(CYOutput &out) const;
1287 struct CYCondition :
1290 CYExpression *test_;
1291 CYExpression *true_;
1292 CYExpression *false_;
1294 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1303 virtual void Output(CYOutput &out, CYFlags flags) const;
1306 struct CYAddressOf :
1309 CYAddressOf(CYExpression *rhs) :
1314 virtual const char *Operator() const {
1321 virtual void Output(CYOutput &out, CYFlags flags) const;
1327 CYIndirect(CYExpression *rhs) :
1332 virtual const char *Operator() const {
1339 virtual void Output(CYOutput &out, CYFlags flags) const;
1342 #define CYPostfix_(op, name) \
1343 struct CY ## name : \
1346 CY ## name(CYExpression *lhs) : \
1353 virtual const char *Operator() const { \
1358 #define CYPrefix_(alphabetic, op, name) \
1359 struct CY ## name : \
1362 CY ## name(CYExpression *rhs) : \
1367 CYAlphabetic(alphabetic) \
1370 virtual const char *Operator() const { \
1375 #define CYInfix_(alphabetic, precedence, op, name) \
1376 struct CY ## name : \
1379 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1384 CYAlphabetic(alphabetic) \
1385 CYPrecedence(precedence) \
1387 virtual const char *Operator() const { \
1392 #define CYAssignment_(op, name) \
1393 struct CY ## name ## Assign : \
1396 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1397 CYAssignment(lhs, rhs) \
1403 virtual const char *Operator() const { \
1408 CYPostfix_("++", PostIncrement)
1409 CYPostfix_("--", PostDecrement)
1411 CYPrefix_(true, "delete", Delete)
1412 CYPrefix_(true, "void", Void)
1413 CYPrefix_(true, "typeof", TypeOf)
1414 CYPrefix_(false, "++", PreIncrement)
1415 CYPrefix_(false, "--", PreDecrement)
1416 CYPrefix_(false, "-", Negate)
1417 CYPrefix_(false, "~", BitwiseNot)
1418 CYPrefix_(false, "!", LogicalNot)
1420 CYInfix_(false, 5, "*", Multiply)
1421 CYInfix_(false, 5, "/", Divide)
1422 CYInfix_(false, 5, "%", Modulus)
1423 CYInfix_(false, 6, "+", Add)
1424 CYInfix_(false, 6, "-", Subtract)
1425 CYInfix_(false, 7, "<<", ShiftLeft)
1426 CYInfix_(false, 7, ">>", ShiftRightSigned)
1427 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1428 CYInfix_(false, 8, "<", Less)
1429 CYInfix_(false, 8, ">", Greater)
1430 CYInfix_(false, 8, "<=", LessOrEqual)
1431 CYInfix_(false, 8, ">=", GreaterOrEqual)
1432 CYInfix_(true, 8, "instanceof", InstanceOf)
1433 CYInfix_(true, 8, "in", In)
1434 CYInfix_(false, 9, "==", Equal)
1435 CYInfix_(false, 9, "!=", NotEqual)
1436 CYInfix_(false, 9, "===", Identical)
1437 CYInfix_(false, 9, "!==", NotIdentical)
1438 CYInfix_(false, 10, "&", BitwiseAnd)
1439 CYInfix_(false, 11, "^", BitwiseXOr)
1440 CYInfix_(false, 12, "|", BitwiseOr)
1441 CYInfix_(false, 13, "&&", LogicalAnd)
1442 CYInfix_(false, 14, "||", LogicalOr)
1444 CYAssignment_("=", )
1445 CYAssignment_("*=", Multiply)
1446 CYAssignment_("/=", Divide)
1447 CYAssignment_("%=", Modulus)
1448 CYAssignment_("+=", Add)
1449 CYAssignment_("-=", Subtract)
1450 CYAssignment_("<<=", ShiftLeft)
1451 CYAssignment_(">>=", ShiftRightSigned)
1452 CYAssignment_(">>>=", ShiftRightUnsigned)
1453 CYAssignment_("&=", BitwiseAnd)
1454 CYAssignment_("^=", BitwiseXOr)
1455 CYAssignment_("|=", BitwiseOr)
1457 #endif/*CYPARSER_HPP*/