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),
226 virtual void Part(std::ostream &out, CYFlags flags) const = 0;
229 struct CYForInitialiser :
234 struct CYForInInitialiser :
237 virtual const char *ForEachIn() const = 0;
238 virtual void ForEachIn(std::ostream &out) const = 0;
241 struct CYExpression :
242 CYNext<CYExpression>,
247 virtual unsigned Precedence() const = 0;
248 virtual void Part(std::ostream &out, CYFlags flags) const;
250 virtual const char *ForEachIn() const {
254 virtual void ForEachIn(std::ostream &out) const;
256 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
257 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
259 virtual void ClassName(std::ostream &out, bool object) const;
261 virtual const char *Word() const {
266 #define CYAlphabetic(value) \
267 virtual bool Alphabetic() const { \
271 #define CYPrecedence(value) \
272 virtual unsigned Precedence() const { \
279 CYExpression *expressions_;
281 CYCompound(CYExpression *expressions) :
282 expressions_(expressions)
286 void AddPrev(CYExpression *expression) {
287 CYExpression *last(expression);
288 while (last->next_ != NULL)
290 last->SetNext(expressions_);
291 expressions_ = expression;
296 void Output(std::ostream &out, CYFlags flags) const;
299 struct CYComprehension :
300 CYNext<CYComprehension>
302 void Output(std::ostream &out) const;
303 virtual const char *Name() const = 0;
305 virtual void Begin_(std::ostream &out) const = 0;
307 virtual void End_(std::ostream &out) const {
311 struct CYForInComprehension :
317 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
323 virtual const char *Name() const {
324 return name_->Value();
327 virtual void Begin_(std::ostream &out) const;
330 struct CYForEachInComprehension :
336 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
342 virtual const char *Name() const {
343 return name_->Value();
346 virtual void Begin_(std::ostream &out) const;
347 virtual void End_(std::ostream &out) const;
350 struct CYIfComprehension :
355 CYIfComprehension(CYExpression *test) :
360 virtual const char *Name() const {
364 virtual void Begin_(std::ostream &out) const;
367 struct CYArrayComprehension :
370 CYExpression *expression_;
371 CYComprehension *comprehensions_;
373 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
374 expression_(expression),
375 comprehensions_(comprehensions)
381 virtual void Output(std::ostream &out, CYFlags flags) const;
396 struct CYSelectorPart :
397 CYNext<CYSelectorPart>
402 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
403 CYNext<CYSelectorPart>(next),
409 virtual void Output(std::ostream &out) const;
415 CYSelectorPart *name_;
417 CYSelector(CYSelectorPart *name) :
424 virtual void Output(std::ostream &out, CYFlags flags) const;
431 CYRange(uint64_t lo, uint64_t hi) :
436 bool operator [](uint8_t value) const {
437 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
440 void operator()(uint8_t value) {
443 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
447 extern CYRange DigitRange_;
448 extern CYRange WordStartRange_;
449 extern CYRange WordEndRange_;
458 CYString(const char *value, size_t size) :
464 CYString(const CYIdentifier *identifier) :
465 value_(identifier->Value()),
466 size_(strlen(value_))
470 const char *Value() const {
474 virtual const char *Word() const {
475 if (size_ == 0 || !WordStartRange_[value_[0]])
477 for (size_t i(1); i != size_; ++i)
478 if (!WordEndRange_[value_[i]])
483 virtual void Output(std::ostream &out) const {
484 return Output(out, CYNoFlags);
487 virtual void Output(std::ostream &out, CYFlags flags) const;
488 virtual void PropertyName(std::ostream &out) const;
497 CYNumber(double value) :
502 double Value() const {
506 virtual void Output(std::ostream &out) const {
507 return Output(out, CYNoFlags);
510 virtual void Output(std::ostream &out, CYFlags flags) const;
511 virtual void PropertyName(std::ostream &out) const;
523 virtual void Output(std::ostream &out, CYFlags flags) const;
535 virtual void Output(std::ostream &out, CYFlags flags) const;
541 virtual bool Value() const = 0;
542 virtual void Output(std::ostream &out, CYFlags flags) const;
554 virtual bool Value() const;
566 virtual bool Value() const;
574 CYVariable(CYIdentifier *name) :
581 virtual void Output(std::ostream &out, CYFlags flags) const;
589 CYPrefix(CYExpression *rhs) :
594 virtual bool Alphabetic() const = 0;
595 virtual const char *Operator() const = 0;
597 virtual void Output(std::ostream &out, CYFlags flags) const;
606 CYInfix(CYExpression *lhs, CYExpression *rhs) :
612 void SetLeft(CYExpression *lhs) {
616 virtual bool Alphabetic() const = 0;
617 virtual const char *Operator() const = 0;
619 virtual void Output(std::ostream &out, CYFlags flags) const;
627 CYPostfix(CYExpression *lhs) :
632 virtual const char *Operator() const = 0;
634 virtual void Output(std::ostream &out, CYFlags flags) const;
637 struct CYAssignment :
643 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
649 void SetLeft(CYExpression *lhs) {
653 virtual const char *Operator() const = 0;
655 virtual void Output(std::ostream &out, CYFlags flags) const;
662 CYExpression *value_;
664 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
665 CYNext<CYArgument>(next),
671 void Output(std::ostream &out) const;
690 CYClause(CYExpression *_case, CYStatement *code) :
696 virtual void Output(std::ostream &out) const;
702 CYExpression *value_;
704 CYElement(CYExpression *value, CYElement *next) :
705 CYNext<CYElement>(next),
710 void Output(std::ostream &out) const;
716 CYElement *elements_;
718 CYArray(CYElement *elements) :
723 virtual void Output(std::ostream &out, CYFlags flags) const;
726 struct CYDeclaration :
729 CYIdentifier *identifier_;
730 CYExpression *initialiser_;
732 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
733 identifier_(identifier),
734 initialiser_(initialiser)
738 virtual void Part(std::ostream &out, CYFlags flags) const;
740 virtual const char *ForEachIn() const {
741 return identifier_->Value();
744 virtual void ForEachIn(std::ostream &out) const;
746 virtual void Output(std::ostream &out, CYFlags flags) const;
749 struct CYDeclarations :
753 CYDeclaration *declaration_;
754 CYDeclarations *next_;
756 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
757 declaration_(declaration),
762 virtual void Part(std::ostream &out, CYFlags flags) const;
763 virtual void Output(std::ostream &out) const;
769 virtual void Output(std::ostream &out) const;
772 struct CYMessageParameter :
773 CYNext<CYMessageParameter>
779 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
792 CYMessageParameter *parameter_;
795 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
798 parameter_(parameter),
803 virtual void Output(std::ostream &out, bool replace) const;
811 CYExpression *super_;
813 CYMessage *messages_;
815 CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) :
825 virtual void Output(std::ostream &out) const;
826 virtual void Output(std::ostream &out, CYFlags flags) const;
833 CYMessage *messages_;
835 CYCategory(CYClassName *name, CYMessage *messages) :
841 virtual void Output(std::ostream &out) const;
844 struct CYFunctionParameter :
845 CYNext<CYFunctionParameter>,
850 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
851 CYNext<CYFunctionParameter>(next),
856 virtual void Output(std::ostream &out) const;
862 CYForInitialiser *initialiser_;
864 CYExpression *increment_;
867 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
868 initialiser_(initialiser),
870 increment_(increment),
875 virtual void Output(std::ostream &out) const;
881 CYForInInitialiser *initialiser_;
885 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
886 initialiser_(initialiser),
892 virtual void Output(std::ostream &out) const;
898 CYForInInitialiser *initialiser_;
902 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
903 initialiser_(initialiser),
909 virtual void Output(std::ostream &out) const;
915 CYPropertyName *name_;
916 CYExpression *value_;
918 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
919 CYNext<CYProperty>(next),
925 virtual void Output(std::ostream &out) const;
931 CYProperty *property_;
933 CYObject(CYProperty *property) :
938 void Output(std::ostream &out, CYFlags flags) const;
947 CYCatch(CYIdentifier *name, CYStatement *code) :
953 virtual void Output(std::ostream &out) const;
960 CYArgument *arguments_;
962 CYSend(CYExpression *self, CYArgument *arguments) :
964 arguments_(arguments)
970 virtual void Output(std::ostream &out, CYFlags flags) const;
976 CYExpression *object_;
977 CYExpression *property_;
979 CYMember(CYExpression *object, CYExpression *property) :
985 void SetLeft(CYExpression *object) {
990 struct CYDirectMember :
993 CYDirectMember(CYExpression *object, CYExpression *property) :
994 CYMember(object, property)
1000 virtual void Output(std::ostream &out, CYFlags flags) const;
1003 struct CYIndirectMember :
1006 CYIndirectMember(CYExpression *object, CYExpression *property) :
1007 CYMember(object, property)
1013 virtual void Output(std::ostream &out, CYFlags flags) const;
1019 CYExpression *constructor_;
1020 CYArgument *arguments_;
1022 CYNew(CYExpression *constructor, CYArgument *arguments) :
1023 constructor_(constructor),
1024 arguments_(arguments)
1030 virtual void Output(std::ostream &out, CYFlags flags) const;
1036 CYExpression *function_;
1037 CYArgument *arguments_;
1039 CYCall(CYExpression *function, CYArgument *arguments) :
1040 function_(function),
1041 arguments_(arguments)
1047 virtual void Output(std::ostream &out, CYFlags flags) const;
1053 CYExpression *test_;
1055 CYStatement *false_;
1057 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
1064 virtual void Output(std::ostream &out) const;
1070 CYExpression *test_;
1073 CYDoWhile(CYExpression *test, CYStatement *code) :
1079 virtual void Output(std::ostream &out) const;
1085 CYExpression *test_;
1088 CYWhile(CYExpression *test, CYStatement *code) :
1094 virtual void Output(std::ostream &out) const;
1100 CYIdentifier *name_;
1101 CYFunctionParameter *parameters_;
1104 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1106 parameters_(parameters),
1113 virtual void Output(std::ostream &out, CYFlags flags) const;
1120 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
1121 CYLambda(name, parameters, body)
1125 virtual void Output(std::ostream &out) const;
1131 CYExpression *expression_;
1133 CYExpress(CYExpression *expression) :
1134 expression_(expression)
1138 virtual void Output(std::ostream &out) const;
1144 CYIdentifier *label_;
1146 CYContinue(CYIdentifier *label) :
1151 virtual void Output(std::ostream &out) const;
1157 CYIdentifier *label_;
1159 CYBreak(CYIdentifier *label) :
1164 virtual void Output(std::ostream &out) const;
1170 CYExpression *value_;
1172 CYReturn(CYExpression *value) :
1177 virtual void Output(std::ostream &out) const;
1183 virtual void Output(std::ostream &out) const;
1184 virtual void Output(std::ostream &out, bool block) const;
1192 CYStatement *finally_;
1194 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1201 virtual void Output(std::ostream &out) const;
1207 CYExpression *value_;
1209 CYThrow(CYExpression *value) :
1214 virtual void Output(std::ostream &out) const;
1220 CYExpression *scope_;
1223 CYWith(CYExpression *scope, CYStatement *code) :
1229 virtual void Output(std::ostream &out) const;
1235 CYExpression *value_;
1238 CYSwitch(CYExpression *value, CYClause *clauses) :
1244 virtual void Output(std::ostream &out) const;
1247 struct CYCondition :
1250 CYExpression *test_;
1251 CYExpression *true_;
1252 CYExpression *false_;
1254 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1263 virtual void Output(std::ostream &out, CYFlags flags) const;
1266 struct CYAddressOf :
1269 CYAddressOf(CYExpression *rhs) :
1274 virtual const char *Operator() const {
1281 virtual void Output(std::ostream &out, CYFlags flags) const;
1287 CYIndirect(CYExpression *rhs) :
1292 virtual const char *Operator() const {
1299 virtual void Output(std::ostream &out, CYFlags flags) const;
1302 #define CYPostfix_(op, name) \
1303 struct CY ## name : \
1306 CY ## name(CYExpression *lhs) : \
1313 virtual const char *Operator() const { \
1318 #define CYPrefix_(alphabetic, op, name) \
1319 struct CY ## name : \
1322 CY ## name(CYExpression *rhs) : \
1327 CYAlphabetic(alphabetic) \
1330 virtual const char *Operator() const { \
1335 #define CYInfix_(alphabetic, precedence, op, name) \
1336 struct CY ## name : \
1339 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1344 CYAlphabetic(alphabetic) \
1345 CYPrecedence(precedence) \
1347 virtual const char *Operator() const { \
1352 #define CYAssignment_(op, name) \
1353 struct CY ## name ## Assign : \
1356 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1357 CYAssignment(lhs, rhs) \
1363 virtual const char *Operator() const { \
1368 CYPostfix_("++", PostIncrement)
1369 CYPostfix_("--", PostDecrement)
1371 CYPrefix_(true, "delete", Delete)
1372 CYPrefix_(true, "void", Void)
1373 CYPrefix_(true, "typeof", TypeOf)
1374 CYPrefix_(false, "++", PreIncrement)
1375 CYPrefix_(false, "--", PreDecrement)
1376 CYPrefix_(false, "-", Negate)
1377 CYPrefix_(false, "~", BitwiseNot)
1378 CYPrefix_(false, "!", LogicalNot)
1380 CYInfix_(false, 5, "*", Multiply)
1381 CYInfix_(false, 5, "/", Divide)
1382 CYInfix_(false, 5, "%", Modulus)
1383 CYInfix_(false, 6, "+", Add)
1384 CYInfix_(false, 6, "-", Subtract)
1385 CYInfix_(false, 7, "<<", ShiftLeft)
1386 CYInfix_(false, 7, ">>", ShiftRightSigned)
1387 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1388 CYInfix_(false, 8, "<", Less)
1389 CYInfix_(false, 8, ">", Greater)
1390 CYInfix_(false, 8, "<=", LessOrEqual)
1391 CYInfix_(false, 8, ">=", GreaterOrEqual)
1392 CYInfix_(true, 8, "instanceof", InstanceOf)
1393 CYInfix_(true, 8, "in", In)
1394 CYInfix_(false, 9, "==", Equal)
1395 CYInfix_(false, 9, "!=", NotEqual)
1396 CYInfix_(false, 9, "===", Identical)
1397 CYInfix_(false, 9, "!==", NotIdentical)
1398 CYInfix_(false, 10, "&", BitwiseAnd)
1399 CYInfix_(false, 11, "^", BitwiseXOr)
1400 CYInfix_(false, 12, "|", BitwiseOr)
1401 CYInfix_(false, 13, "&&", LogicalAnd)
1402 CYInfix_(false, 14, "||", LogicalOr)
1404 CYAssignment_("=", )
1405 CYAssignment_("*=", Multiply)
1406 CYAssignment_("/=", Divide)
1407 CYAssignment_("%=", Modulus)
1408 CYAssignment_("+=", Add)
1409 CYAssignment_("-=", Subtract)
1410 CYAssignment_("<<=", ShiftLeft)
1411 CYAssignment_(">>=", ShiftRightSigned)
1412 CYAssignment_(">>>=", ShiftRightUnsigned)
1413 CYAssignment_("&=", BitwiseAnd)
1414 CYAssignment_("^=", BitwiseXOr)
1415 CYAssignment_("|=", BitwiseOr)
1417 #endif/*CYPARSER_HPP*/