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;
89 virtual const char *Name() const = 0;
97 CYWord(const char *word) :
102 const char *Value() const {
106 virtual const char *Name() const {
110 virtual void Output(std::ostream &out) const;
113 struct CYIdentifier :
116 CYIdentifier(const char *word) :
125 CYIdentifier *identifier_;
127 CYLabel(CYIdentifier *identifier, CYLabel *next) :
128 CYNext<CYLabel>(next),
129 identifier_(identifier)
139 void AddLabel(CYIdentifier *identifier) {
140 label_ = new CYLabel(identifier, label_);
160 std::string filename_;
163 cy::location location_;
164 std::string message_;
167 typedef std::vector<Error> Errors;
174 void ScannerDestroy();
177 CYDriver(const std::string &filename);
182 virtual void Part(std::ostream &out) const = 0;
185 struct CYForInitialiser :
190 struct CYForInInitialiser :
197 CYNoBrace = (1 << 0),
198 CYNoFunction = (1 << 1),
199 CYNoLeader = (1 << 2),
200 CYNoTrailer = (1 << 3),
204 struct CYExpression :
205 CYNext<CYExpression>,
209 virtual unsigned Precedence() const = 0;
210 virtual void Part(std::ostream &out) const;
211 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
212 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
214 virtual const char *Word() const {
219 #define CYAlphabetic(value) \
220 virtual bool Alphabetic() const { \
224 #define CYPrecedence(value) \
225 virtual unsigned Precedence() const { \
232 CYExpression *expressions_;
234 CYCompound(CYExpression *expressions) :
235 expressions_(expressions)
239 void AddPrev(CYExpression *expression) {
240 CYExpression *last(expression);
241 while (last->next_ != NULL)
243 last->SetNext(expressions_);
244 expressions_ = expression;
249 void Output(std::ostream &out, CYFlags flags) const;
264 struct CYSelectorPart :
265 CYNext<CYSelectorPart>
270 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
271 CYNext<CYSelectorPart>(next),
277 virtual void Output(std::ostream &out) const;
283 CYSelectorPart *name_;
285 CYSelector(CYSelectorPart *name) :
292 virtual void Output(std::ostream &out, CYFlags flags) const;
299 CYRange(uint64_t lo, uint64_t hi) :
304 bool operator [](uint8_t value) const {
305 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
308 void operator()(uint8_t value) {
311 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
315 extern CYRange DigitRange_;
316 extern CYRange WordStartRange_;
317 extern CYRange WordEndRange_;
326 CYString(const char *value, size_t size) :
332 CYString(const CYIdentifier *identifier) :
333 value_(identifier->Value()),
334 size_(strlen(value_))
338 const char *Value() const {
342 virtual const char *Name() const {
346 virtual const char *Word() const {
347 if (size_ == 0 || !WordStartRange_[value_[0]])
349 for (size_t i(1); i != size_; ++i)
350 if (!WordEndRange_[value_[i]])
355 virtual void Output(std::ostream &out) const {
356 return Output(out, CYNoFlags);
359 virtual void Output(std::ostream &out, CYFlags flags) const;
368 CYNumber(double value) :
373 double Value() const {
377 virtual const char *Name() const {
381 virtual void Output(std::ostream &out) const {
382 return Output(out, CYNoFlags);
385 virtual void Output(std::ostream &out, CYFlags flags) const;
397 virtual void Output(std::ostream &out, CYFlags flags) const;
409 virtual void Output(std::ostream &out, CYFlags flags) const;
415 virtual bool Value() const = 0;
416 virtual void Output(std::ostream &out, CYFlags flags) const;
428 virtual bool Value() const;
440 virtual bool Value() const;
448 CYVariable(CYIdentifier *name) :
455 virtual void Output(std::ostream &out, CYFlags flags) const;
463 CYPrefix(CYExpression *rhs) :
468 virtual bool Alphabetic() const = 0;
469 virtual const char *Operator() const = 0;
471 virtual void Output(std::ostream &out, CYFlags flags) const;
480 CYInfix(CYExpression *lhs, CYExpression *rhs) :
486 virtual bool Alphabetic() const = 0;
487 virtual const char *Operator() const = 0;
489 virtual void Output(std::ostream &out, CYFlags flags) const;
497 CYPostfix(CYExpression *lhs) :
502 virtual const char *Operator() const = 0;
504 virtual void Output(std::ostream &out, CYFlags flags) const;
507 struct CYAssignment :
513 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
519 virtual const char *Operator() const = 0;
521 virtual void Output(std::ostream &out, CYFlags flags) const;
528 CYExpression *value_;
530 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
531 CYNext<CYArgument>(next),
537 void Output(std::ostream &out) const;
556 CYClause(CYExpression *_case, CYStatement *code) :
562 virtual void Output(std::ostream &out) const;
568 CYExpression *value_;
570 CYElement(CYExpression *value, CYElement *next) :
571 CYNext<CYElement>(next),
576 void Output(std::ostream &out) const;
582 CYElement *elements_;
584 CYArray(CYElement *elements) :
589 virtual void Output(std::ostream &out, CYFlags flags) const;
592 struct CYDeclaration :
596 CYIdentifier *identifier_;
597 CYExpression *initialiser_;
599 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
600 identifier_(identifier),
601 initialiser_(initialiser)
605 virtual void Part(std::ostream &out) const;
606 virtual void Output(std::ostream &out) const;
609 struct CYDeclarations :
613 CYDeclaration *declaration_;
614 CYDeclarations *next_;
616 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
617 declaration_(declaration),
622 virtual void Part(std::ostream &out) const;
623 virtual void Output(std::ostream &out) const;
629 virtual void Output(std::ostream &out) const;
632 struct CYMessageParameter :
633 CYNext<CYMessageParameter>
639 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
652 CYMessageParameter *parameter_;
655 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
658 parameter_(parameter),
663 virtual void Output(std::ostream &out) const;
670 CYExpression *super_;
672 CYMessage *messages_;
674 CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) :
682 virtual void Output(std::ostream &out) const;
685 struct CYFunctionParameter :
686 CYNext<CYFunctionParameter>,
691 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
692 CYNext<CYFunctionParameter>(next),
697 virtual void Output(std::ostream &out) const;
703 CYForInitialiser *initialiser_;
705 CYExpression *increment_;
708 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
709 initialiser_(initialiser),
711 increment_(increment),
716 virtual void Output(std::ostream &out) const;
722 CYForInInitialiser *initialiser_;
726 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
727 initialiser_(initialiser),
733 virtual void Output(std::ostream &out) const;
740 CYExpression *value_;
742 CYProperty(CYName *name, CYExpression *value, CYProperty *next) :
743 CYNext<CYProperty>(next),
749 virtual void Output(std::ostream &out) const;
755 CYProperty *property_;
757 CYObject(CYProperty *property) :
762 void Output(std::ostream &out, CYFlags flags) const;
771 CYCatch(CYIdentifier *name, CYStatement *code) :
777 virtual void Output(std::ostream &out) const;
784 CYArgument *arguments_;
786 CYSend(CYExpression *self, CYArgument *arguments) :
788 arguments_(arguments)
794 virtual void Output(std::ostream &out, CYFlags flags) const;
800 CYExpression *object_;
801 CYExpression *property_;
803 CYMember(CYExpression *object, CYExpression *property) :
811 virtual void Output(std::ostream &out, CYFlags flags) const;
817 CYExpression *constructor_;
818 CYArgument *arguments_;
820 CYNew(CYExpression *constructor, CYArgument *arguments) :
821 constructor_(constructor),
822 arguments_(arguments)
828 virtual void Output(std::ostream &out, CYFlags flags) const;
834 CYExpression *function_;
835 CYArgument *arguments_;
837 CYCall(CYExpression *function, CYArgument *arguments) :
839 arguments_(arguments)
845 virtual void Output(std::ostream &out, CYFlags flags) const;
855 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
862 virtual void Output(std::ostream &out) const;
871 CYDoWhile(CYExpression *test, CYStatement *code) :
877 virtual void Output(std::ostream &out) const;
886 CYWhile(CYExpression *test, CYStatement *code) :
892 virtual void Output(std::ostream &out) const;
899 CYFunctionParameter *parameters_;
902 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
904 parameters_(parameters),
911 virtual void Output(std::ostream &out, CYFlags flags) const;
918 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
919 CYLambda(name, parameters, body)
923 virtual void Output(std::ostream &out) const;
929 CYExpression *expression_;
931 CYExpress(CYExpression *expression) :
932 expression_(expression)
936 virtual void Output(std::ostream &out) const;
942 CYIdentifier *label_;
944 CYContinue(CYIdentifier *label) :
949 virtual void Output(std::ostream &out) const;
955 CYIdentifier *label_;
957 CYBreak(CYIdentifier *label) :
962 virtual void Output(std::ostream &out) const;
968 CYExpression *value_;
970 CYReturn(CYExpression *value) :
975 virtual void Output(std::ostream &out) const;
981 virtual void Output(std::ostream &out) const;
982 virtual void Output(std::ostream &out, bool block) const;
990 CYStatement *finally_;
992 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
999 virtual void Output(std::ostream &out) const;
1005 CYExpression *value_;
1007 CYThrow(CYExpression *value) :
1012 virtual void Output(std::ostream &out) const;
1018 CYExpression *scope_;
1021 CYWith(CYExpression *scope, CYStatement *code) :
1027 virtual void Output(std::ostream &out) const;
1033 CYExpression *value_;
1036 CYSwitch(CYExpression *value, CYClause *clauses) :
1042 virtual void Output(std::ostream &out) const;
1045 struct CYCondition :
1048 CYExpression *test_;
1049 CYExpression *true_;
1050 CYExpression *false_;
1052 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1061 virtual void Output(std::ostream &out, CYFlags flags) const;
1064 struct CYAddressOf :
1067 CYAddressOf(CYExpression *rhs) :
1072 virtual const char *Operator() const {
1079 virtual void Output(std::ostream &out, CYFlags flags) const;
1085 CYIndirect(CYExpression *rhs) :
1090 virtual const char *Operator() const {
1097 virtual void Output(std::ostream &out, CYFlags flags) const;
1100 #define CYPostfix_(op, name) \
1101 struct CY ## name : \
1104 CY ## name(CYExpression *lhs) : \
1111 virtual const char *Operator() const { \
1116 #define CYPrefix_(alphabetic, op, name) \
1117 struct CY ## name : \
1120 CY ## name(CYExpression *rhs) : \
1125 CYAlphabetic(alphabetic) \
1128 virtual const char *Operator() const { \
1133 #define CYInfix_(alphabetic, precedence, op, name) \
1134 struct CY ## name : \
1137 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1142 CYAlphabetic(alphabetic) \
1143 CYPrecedence(precedence) \
1145 virtual const char *Operator() const { \
1150 #define CYAssignment_(op, name) \
1151 struct CY ## name ## Assign : \
1154 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1155 CYAssignment(lhs, rhs) \
1161 virtual const char *Operator() const { \
1166 CYPostfix_("++", PostIncrement)
1167 CYPostfix_("--", PostDecrement)
1169 CYPrefix_(true, "delete", Delete)
1170 CYPrefix_(true, "void", Void)
1171 CYPrefix_(true, "typeof", TypeOf)
1172 CYPrefix_(false, "++", PreIncrement)
1173 CYPrefix_(false, "--", PreDecrement)
1174 CYPrefix_(false, "-", Negate)
1175 CYPrefix_(false, "~", BitwiseNot)
1176 CYPrefix_(false, "!", LogicalNot)
1178 CYInfix_(false, 5, "*", Multiply)
1179 CYInfix_(false, 5, "/", Divide)
1180 CYInfix_(false, 5, "%", Modulus)
1181 CYInfix_(false, 6, "+", Add)
1182 CYInfix_(false, 6, "-", Subtract)
1183 CYInfix_(false, 7, "<<", ShiftLeft)
1184 CYInfix_(false, 7, ">>", ShiftRightSigned)
1185 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1186 CYInfix_(false, 8, "<", Less)
1187 CYInfix_(false, 8, ">", Greater)
1188 CYInfix_(false, 8, "<=", LessOrEqual)
1189 CYInfix_(false, 8, ">=", GreaterOrEqual)
1190 CYInfix_(true, 8, "instanceof", InstanceOf)
1191 CYInfix_(true, 8, "in", In)
1192 CYInfix_(false, 9, "==", Equal)
1193 CYInfix_(false, 9, "!=", NotEqual)
1194 CYInfix_(false, 9, "===", Identical)
1195 CYInfix_(false, 9, "!==", NotIdentical)
1196 CYInfix_(false, 10, "&", BitwiseAnd)
1197 CYInfix_(false, 11, "^", BitwiseXOr)
1198 CYInfix_(false, 12, "|", BitwiseOr)
1199 CYInfix_(false, 13, "&&", LogicalAnd)
1200 CYInfix_(false, 14, "||", LogicalOr)
1202 CYAssignment_("=", )
1203 CYAssignment_("*=", Multiply)
1204 CYAssignment_("/=", Divide)
1205 CYAssignment_("%=", Modulus)
1206 CYAssignment_("+=", Add)
1207 CYAssignment_("-=", Subtract)
1208 CYAssignment_("<<=", ShiftLeft)
1209 CYAssignment_(">>=", ShiftRightSigned)
1210 CYAssignment_(">>>=", ShiftRightUnsigned)
1211 CYAssignment_("&=", BitwiseAnd)
1212 CYAssignment_("^=", BitwiseXOr)
1213 CYAssignment_("|=", BitwiseOr)
1215 #endif/*CYPARSER_HPP*/