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);
216 CYNoBrace = (1 << 0),
217 CYNoFunction = (1 << 1),
218 CYNoLeader = (1 << 2),
219 CYNoTrailer = (1 << 3),
221 CYNoHyphen = (1 << 5),
222 CYNoBF = (CYNoBrace | CYNoFunction),
225 struct CYForInitialiser {
226 virtual void For(std::ostream &out) const = 0;
229 struct CYForInInitialiser {
230 virtual void ForIn(std::ostream &out, CYFlags flags) const = 0;
231 virtual const char *ForEachIn() const = 0;
232 virtual void ForEachIn(std::ostream &out) const = 0;
235 struct CYExpression :
236 CYNext<CYExpression>,
241 virtual unsigned Precedence() const = 0;
243 virtual void For(std::ostream &out) const;
244 virtual void ForIn(std::ostream &out, CYFlags flags) const;
246 virtual const char *ForEachIn() const;
247 virtual void ForEachIn(std::ostream &out) const;
249 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
250 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
252 virtual void ClassName(std::ostream &out, bool object) const;
254 virtual const char *Word() const {
259 #define CYAlphabetic(value) \
260 virtual bool Alphabetic() const { \
264 #define CYPrecedence(value) \
265 virtual unsigned Precedence() const { \
272 CYExpression *expressions_;
274 CYCompound(CYExpression *expressions) :
275 expressions_(expressions)
279 void AddPrev(CYExpression *expression) {
280 CYExpression *last(expression);
281 while (last->next_ != NULL)
283 last->SetNext(expressions_);
284 expressions_ = expression;
289 void Output(std::ostream &out, CYFlags flags) const;
292 struct CYComprehension :
293 CYNext<CYComprehension>
295 void Output(std::ostream &out) const;
296 virtual const char *Name() const = 0;
298 virtual void Begin_(std::ostream &out) const = 0;
300 virtual void End_(std::ostream &out) const {
304 struct CYForInComprehension :
310 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
316 virtual const char *Name() const {
317 return name_->Value();
320 virtual void Begin_(std::ostream &out) const;
323 struct CYForEachInComprehension :
329 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
335 virtual const char *Name() const {
336 return name_->Value();
339 virtual void Begin_(std::ostream &out) const;
340 virtual void End_(std::ostream &out) const;
343 struct CYIfComprehension :
348 CYIfComprehension(CYExpression *test) :
353 virtual const char *Name() const {
357 virtual void Begin_(std::ostream &out) const;
360 struct CYArrayComprehension :
363 CYExpression *expression_;
364 CYComprehension *comprehensions_;
366 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
367 expression_(expression),
368 comprehensions_(comprehensions)
374 virtual void Output(std::ostream &out, CYFlags flags) const;
389 struct CYSelectorPart :
390 CYNext<CYSelectorPart>
395 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
396 CYNext<CYSelectorPart>(next),
402 virtual void Output(std::ostream &out) const;
408 CYSelectorPart *name_;
410 CYSelector(CYSelectorPart *name) :
417 virtual void Output(std::ostream &out, CYFlags flags) const;
424 CYRange(uint64_t lo, uint64_t hi) :
429 bool operator [](uint8_t value) const {
430 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
433 void operator()(uint8_t value) {
436 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
440 extern CYRange DigitRange_;
441 extern CYRange WordStartRange_;
442 extern CYRange WordEndRange_;
451 CYString(const char *value, size_t size) :
457 CYString(const CYIdentifier *identifier) :
458 value_(identifier->Value()),
459 size_(strlen(value_))
463 const char *Value() const {
467 virtual const char *Word() const {
468 if (size_ == 0 || !WordStartRange_[value_[0]])
470 for (size_t i(1); i != size_; ++i)
471 if (!WordEndRange_[value_[i]])
476 virtual void Output(std::ostream &out) const {
477 return Output(out, CYNoFlags);
480 virtual void Output(std::ostream &out, CYFlags flags) const;
481 virtual void PropertyName(std::ostream &out) const;
490 CYNumber(double value) :
495 double Value() const {
499 virtual void Output(std::ostream &out) const {
500 return Output(out, CYNoFlags);
503 virtual void Output(std::ostream &out, CYFlags flags) const;
504 virtual void PropertyName(std::ostream &out) const;
516 virtual void Output(std::ostream &out, CYFlags flags) const;
528 virtual void Output(std::ostream &out, CYFlags flags) const;
534 virtual bool Value() const = 0;
535 virtual void Output(std::ostream &out, CYFlags flags) const;
547 virtual bool Value() const;
559 virtual bool Value() const;
567 CYVariable(CYIdentifier *name) :
574 virtual void Output(std::ostream &out, CYFlags flags) const;
582 CYPrefix(CYExpression *rhs) :
587 virtual bool Alphabetic() const = 0;
588 virtual const char *Operator() const = 0;
590 virtual void Output(std::ostream &out, CYFlags flags) const;
599 CYInfix(CYExpression *lhs, CYExpression *rhs) :
605 void SetLeft(CYExpression *lhs) {
609 virtual bool Alphabetic() const = 0;
610 virtual const char *Operator() const = 0;
612 virtual void Output(std::ostream &out, CYFlags flags) const;
620 CYPostfix(CYExpression *lhs) :
625 virtual const char *Operator() const = 0;
627 virtual void Output(std::ostream &out, CYFlags flags) const;
630 struct CYAssignment :
636 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
642 void SetLeft(CYExpression *lhs) {
646 virtual const char *Operator() const = 0;
648 virtual void Output(std::ostream &out, CYFlags flags) const;
655 CYExpression *value_;
657 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
658 CYNext<CYArgument>(next),
664 void Output(std::ostream &out) const;
683 CYClause(CYExpression *_case, CYStatement *code) :
689 virtual void Output(std::ostream &out) const;
695 CYExpression *value_;
697 CYElement(CYExpression *value, CYElement *next) :
698 CYNext<CYElement>(next),
703 void Output(std::ostream &out) const;
709 CYElement *elements_;
711 CYArray(CYElement *elements) :
716 virtual void Output(std::ostream &out, CYFlags flags) const;
719 struct CYDeclaration :
722 CYIdentifier *identifier_;
723 CYExpression *initialiser_;
725 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
726 identifier_(identifier),
727 initialiser_(initialiser)
731 virtual void ForIn(std::ostream &out, CYFlags flags) const;
733 virtual const char *ForEachIn() const;
734 virtual void ForEachIn(std::ostream &out) const;
736 virtual void Output(std::ostream &out, CYFlags flags) const;
739 struct CYDeclarations :
740 CYNext<CYDeclarations>,
743 CYDeclaration *declaration_;
745 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
746 CYNext<CYDeclarations>(next),
747 declaration_(declaration)
751 virtual void For(std::ostream &out) const;
752 virtual void Output(std::ostream &out, CYFlags flags) const;
758 CYDeclarations *declarations_;
760 CYVar(CYDeclarations *declarations) :
761 declarations_(declarations)
765 virtual void Output(std::ostream &out) const;
771 CYDeclarations *declarations_;
772 CYStatement *statements_;
774 CYLet(CYDeclarations *declarations, CYStatement *statements) :
775 declarations_(declarations),
776 statements_(statements)
780 virtual void Output(std::ostream &out) const;
786 virtual void Output(std::ostream &out) const;
789 struct CYMessageParameter :
790 CYNext<CYMessageParameter>
796 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
809 CYMessageParameter *parameter_;
812 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
815 parameter_(parameter),
820 virtual void Output(std::ostream &out, bool replace) const;
828 CYExpression *super_;
830 CYMessage *messages_;
832 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
842 virtual void Output(std::ostream &out) const;
843 virtual void Output(std::ostream &out, CYFlags flags) const;
850 CYMessage *messages_;
852 CYCategory(CYClassName *name, CYMessage *messages) :
858 virtual void Output(std::ostream &out) const;
861 struct CYFunctionParameter :
862 CYNext<CYFunctionParameter>,
867 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
868 CYNext<CYFunctionParameter>(next),
873 virtual void Output(std::ostream &out) const;
879 CYForInitialiser *initialiser_;
881 CYExpression *increment_;
884 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
885 initialiser_(initialiser),
887 increment_(increment),
892 virtual void Output(std::ostream &out) const;
898 CYForInInitialiser *initialiser_;
902 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
903 initialiser_(initialiser),
909 virtual void Output(std::ostream &out) const;
915 CYForInInitialiser *initialiser_;
919 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
920 initialiser_(initialiser),
926 virtual void Output(std::ostream &out) const;
932 CYPropertyName *name_;
933 CYExpression *value_;
935 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
936 CYNext<CYProperty>(next),
942 virtual void Output(std::ostream &out) const;
948 CYProperty *property_;
950 CYObject(CYProperty *property) :
955 void Output(std::ostream &out, CYFlags flags) const;
964 CYCatch(CYIdentifier *name, CYStatement *code) :
970 virtual void Output(std::ostream &out) const;
977 CYArgument *arguments_;
979 CYSend(CYExpression *self, CYArgument *arguments) :
981 arguments_(arguments)
987 virtual void Output(std::ostream &out, CYFlags flags) const;
993 CYExpression *object_;
994 CYExpression *property_;
996 CYMember(CYExpression *object, CYExpression *property) :
1002 void SetLeft(CYExpression *object) {
1007 struct CYDirectMember :
1010 CYDirectMember(CYExpression *object, CYExpression *property) :
1011 CYMember(object, property)
1017 virtual void Output(std::ostream &out, CYFlags flags) const;
1020 struct CYIndirectMember :
1023 CYIndirectMember(CYExpression *object, CYExpression *property) :
1024 CYMember(object, property)
1030 virtual void Output(std::ostream &out, CYFlags flags) const;
1036 CYExpression *constructor_;
1037 CYArgument *arguments_;
1039 CYNew(CYExpression *constructor, CYArgument *arguments) :
1040 constructor_(constructor),
1041 arguments_(arguments)
1047 virtual void Output(std::ostream &out, CYFlags flags) const;
1053 CYExpression *function_;
1054 CYArgument *arguments_;
1056 CYCall(CYExpression *function, CYArgument *arguments) :
1057 function_(function),
1058 arguments_(arguments)
1064 virtual void Output(std::ostream &out, CYFlags flags) const;
1070 CYExpression *test_;
1072 CYStatement *false_;
1074 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1081 virtual void Output(std::ostream &out) const;
1087 CYExpression *test_;
1090 CYDoWhile(CYExpression *test, CYStatement *code) :
1096 virtual void Output(std::ostream &out) const;
1102 CYExpression *test_;
1105 CYWhile(CYExpression *test, CYStatement *code) :
1111 virtual void Output(std::ostream &out) const;
1117 CYIdentifier *name_;
1118 CYFunctionParameter *parameters_;
1121 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1123 parameters_(parameters),
1130 virtual void Output(std::ostream &out, CYFlags flags) const;
1137 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1138 CYLambda(name, parameters, body)
1142 virtual void Output(std::ostream &out) const;
1148 CYExpression *expression_;
1150 CYExpress(CYExpression *expression) :
1151 expression_(expression)
1155 virtual void Output(std::ostream &out) const;
1161 CYIdentifier *label_;
1163 CYContinue(CYIdentifier *label) :
1168 virtual void Output(std::ostream &out) const;
1174 CYIdentifier *label_;
1176 CYBreak(CYIdentifier *label) :
1181 virtual void Output(std::ostream &out) const;
1187 CYExpression *value_;
1189 CYReturn(CYExpression *value) :
1194 virtual void Output(std::ostream &out) const;
1200 virtual void Output(std::ostream &out) const;
1201 virtual void Output(std::ostream &out, bool block) const;
1209 CYStatement *finally_;
1211 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1218 virtual void Output(std::ostream &out) const;
1224 CYExpression *value_;
1226 CYThrow(CYExpression *value) :
1231 virtual void Output(std::ostream &out) const;
1237 CYExpression *scope_;
1240 CYWith(CYExpression *scope, CYStatement *code) :
1246 virtual void Output(std::ostream &out) const;
1252 CYExpression *value_;
1255 CYSwitch(CYExpression *value, CYClause *clauses) :
1261 virtual void Output(std::ostream &out) const;
1264 struct CYCondition :
1267 CYExpression *test_;
1268 CYExpression *true_;
1269 CYExpression *false_;
1271 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1280 virtual void Output(std::ostream &out, CYFlags flags) const;
1283 struct CYAddressOf :
1286 CYAddressOf(CYExpression *rhs) :
1291 virtual const char *Operator() const {
1298 virtual void Output(std::ostream &out, CYFlags flags) const;
1304 CYIndirect(CYExpression *rhs) :
1309 virtual const char *Operator() const {
1316 virtual void Output(std::ostream &out, CYFlags flags) const;
1319 #define CYPostfix_(op, name) \
1320 struct CY ## name : \
1323 CY ## name(CYExpression *lhs) : \
1330 virtual const char *Operator() const { \
1335 #define CYPrefix_(alphabetic, op, name) \
1336 struct CY ## name : \
1339 CY ## name(CYExpression *rhs) : \
1344 CYAlphabetic(alphabetic) \
1347 virtual const char *Operator() const { \
1352 #define CYInfix_(alphabetic, precedence, op, name) \
1353 struct CY ## name : \
1356 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1361 CYAlphabetic(alphabetic) \
1362 CYPrecedence(precedence) \
1364 virtual const char *Operator() const { \
1369 #define CYAssignment_(op, name) \
1370 struct CY ## name ## Assign : \
1373 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1374 CYAssignment(lhs, rhs) \
1380 virtual const char *Operator() const { \
1385 CYPostfix_("++", PostIncrement)
1386 CYPostfix_("--", PostDecrement)
1388 CYPrefix_(true, "delete", Delete)
1389 CYPrefix_(true, "void", Void)
1390 CYPrefix_(true, "typeof", TypeOf)
1391 CYPrefix_(false, "++", PreIncrement)
1392 CYPrefix_(false, "--", PreDecrement)
1393 CYPrefix_(false, "-", Negate)
1394 CYPrefix_(false, "~", BitwiseNot)
1395 CYPrefix_(false, "!", LogicalNot)
1397 CYInfix_(false, 5, "*", Multiply)
1398 CYInfix_(false, 5, "/", Divide)
1399 CYInfix_(false, 5, "%", Modulus)
1400 CYInfix_(false, 6, "+", Add)
1401 CYInfix_(false, 6, "-", Subtract)
1402 CYInfix_(false, 7, "<<", ShiftLeft)
1403 CYInfix_(false, 7, ">>", ShiftRightSigned)
1404 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1405 CYInfix_(false, 8, "<", Less)
1406 CYInfix_(false, 8, ">", Greater)
1407 CYInfix_(false, 8, "<=", LessOrEqual)
1408 CYInfix_(false, 8, ">=", GreaterOrEqual)
1409 CYInfix_(true, 8, "instanceof", InstanceOf)
1410 CYInfix_(true, 8, "in", In)
1411 CYInfix_(false, 9, "==", Equal)
1412 CYInfix_(false, 9, "!=", NotEqual)
1413 CYInfix_(false, 9, "===", Identical)
1414 CYInfix_(false, 9, "!==", NotIdentical)
1415 CYInfix_(false, 10, "&", BitwiseAnd)
1416 CYInfix_(false, 11, "^", BitwiseXOr)
1417 CYInfix_(false, 12, "|", BitwiseOr)
1418 CYInfix_(false, 13, "&&", LogicalAnd)
1419 CYInfix_(false, 14, "||", LogicalOr)
1421 CYAssignment_("=", )
1422 CYAssignment_("*=", Multiply)
1423 CYAssignment_("/=", Divide)
1424 CYAssignment_("%=", Modulus)
1425 CYAssignment_("+=", Add)
1426 CYAssignment_("-=", Subtract)
1427 CYAssignment_("<<=", ShiftLeft)
1428 CYAssignment_(">>=", ShiftRightSigned)
1429 CYAssignment_(">>>=", ShiftRightUnsigned)
1430 CYAssignment_("&=", BitwiseAnd)
1431 CYAssignment_("^=", BitwiseXOr)
1432 CYAssignment_("|=", BitwiseOr)
1434 #endif/*CYPARSER_HPP*/