X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/478d4ed0925f2e61fbcdc4672104620342c52c4c..6f926cee9d6b259845cd91069cb7c6f488d1eed6:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 1599959..66bd872 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,3 +1,42 @@ +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + #ifndef CYPARSER_HPP #define CYPARSER_HPP @@ -39,19 +78,28 @@ _finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) { struct CYSource : CYNext { + virtual bool IsBlock() const { + return next_ != NULL; + } + virtual void Show(std::ostream &out) const; virtual void Output(std::ostream &out) const = 0; virtual void Output(std::ostream &out, bool block) const; + virtual void Output_(std::ostream &out) const; }; -struct CYName : - CYThing -{ - virtual const char *Name() const = 0; +struct CYPropertyName { + virtual void PropertyName(std::ostream &out) const = 0; +}; + +struct CYClassName { + virtual void ClassName(std::ostream &out, bool object) const = 0; }; struct CYWord : - CYName + CYThing, + CYPropertyName, + CYClassName { const char *word_; @@ -64,11 +112,10 @@ struct CYWord : return word_; } - virtual const char *Name() const { - return Value(); - } - virtual void Output(std::ostream &out) const; + + virtual void ClassName(std::ostream &out, bool object) const; + virtual void PropertyName(std::ostream &out) const; }; struct CYIdentifier : @@ -83,11 +130,11 @@ struct CYIdentifier : struct CYLabel : CYNext { - CYIdentifier *identifier_; + CYIdentifier *name_; - CYLabel(CYIdentifier *identifier, CYLabel *next) : + CYLabel(CYIdentifier *name, CYLabel *next) : CYNext(next), - identifier_(identifier) + name_(name) { } }; @@ -95,11 +142,35 @@ struct CYLabel : struct CYStatement : CYSource { - CYLabel *label_; + CYLabel *labels_; + + CYStatement() : + labels_(NULL) + { + } void AddLabel(CYIdentifier *identifier) { - label_ = new CYLabel(identifier, label_); + labels_ = new CYLabel(identifier, labels_); } + + virtual void Output_(std::ostream &out) const; +}; + +struct CYBlock : + CYStatement +{ + CYStatement *statements_; + + CYBlock(CYStatement *statements) : + statements_(statements) + { + } + + virtual bool IsBlock() const { + return true; + } + + virtual void Output(std::ostream &out) const; }; enum CYState { @@ -117,6 +188,7 @@ class CYDriver { const char *data_; size_t size_; + FILE *file_; std::string filename_; @@ -139,20 +211,6 @@ class CYDriver { ~CYDriver(); }; -struct CYPart { - virtual void Part(std::ostream &out) const = 0; -}; - -struct CYForInitialiser : - CYPart -{ -}; - -struct CYForInInitialiser : - CYPart -{ -}; - enum CYFlags { CYNoFlags = 0, CYNoBrace = (1 << 0), @@ -160,18 +218,39 @@ enum CYFlags { CYNoLeader = (1 << 2), CYNoTrailer = (1 << 3), CYNoIn = (1 << 4), + CYNoHyphen = (1 << 5), + CYNoBF = (CYNoBrace | CYNoFunction), +}; + +struct CYForInitialiser { + virtual void For(std::ostream &out) const = 0; +}; + +struct CYForInInitialiser { + virtual void ForIn(std::ostream &out, CYFlags flags) const = 0; + virtual const char *ForEachIn() const = 0; + virtual void ForEachIn(std::ostream &out) const = 0; }; struct CYExpression : CYNext, CYForInitialiser, - CYForInInitialiser + CYForInInitialiser, + CYClassName { virtual unsigned Precedence() const = 0; - virtual void Part(std::ostream &out) const; + + virtual void For(std::ostream &out) const; + virtual void ForIn(std::ostream &out, CYFlags flags) const; + + virtual const char *ForEachIn() const; + virtual void ForEachIn(std::ostream &out) const; + virtual void Output(std::ostream &out, CYFlags flags) const = 0; void Output(std::ostream &out, unsigned precedence, CYFlags flags) const; + virtual void ClassName(std::ostream &out, bool object) const; + virtual const char *Word() const { return NULL; } @@ -210,6 +289,91 @@ struct CYCompound : void Output(std::ostream &out, CYFlags flags) const; }; +struct CYComprehension : + CYNext +{ + void Output(std::ostream &out) const; + virtual const char *Name() const = 0; + + virtual void Begin_(std::ostream &out) const = 0; + + virtual void End_(std::ostream &out) const { + } +}; + +struct CYForInComprehension : + CYComprehension +{ + CYIdentifier *name_; + CYExpression *set_; + + CYForInComprehension(CYIdentifier *name, CYExpression *set) : + name_(name), + set_(set) + { + } + + virtual const char *Name() const { + return name_->Value(); + } + + virtual void Begin_(std::ostream &out) const; +}; + +struct CYForEachInComprehension : + CYComprehension +{ + CYIdentifier *name_; + CYExpression *set_; + + CYForEachInComprehension(CYIdentifier *name, CYExpression *set) : + name_(name), + set_(set) + { + } + + virtual const char *Name() const { + return name_->Value(); + } + + virtual void Begin_(std::ostream &out) const; + virtual void End_(std::ostream &out) const; +}; + +struct CYIfComprehension : + CYComprehension +{ + CYExpression *test_; + + CYIfComprehension(CYExpression *test) : + test_(test) + { + } + + virtual const char *Name() const { + return NULL; + } + + virtual void Begin_(std::ostream &out) const; +}; + +struct CYArrayComprehension : + CYExpression +{ + CYExpression *expression_; + CYComprehension *comprehensions_; + + CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) : + expression_(expression), + comprehensions_(comprehensions) + { + } + + CYPrecedence(0) + + virtual void Output(std::ostream &out, CYFlags flags) const; +}; + struct CYLiteral : CYExpression { @@ -273,12 +437,13 @@ struct CYRange { } }; +extern CYRange DigitRange_; extern CYRange WordStartRange_; extern CYRange WordEndRange_; struct CYString : CYLiteral, - CYName + CYPropertyName { const char *value_; size_t size_; @@ -299,10 +464,6 @@ struct CYString : return value_; } - virtual const char *Name() const { - return Value(); - } - virtual const char *Word() const { if (size_ == 0 || !WordStartRange_[value_[0]]) return NULL; @@ -317,11 +478,12 @@ struct CYString : } virtual void Output(std::ostream &out, CYFlags flags) const; + virtual void PropertyName(std::ostream &out) const; }; struct CYNumber : CYLiteral, - CYName + CYPropertyName { double value_; @@ -334,15 +496,12 @@ struct CYNumber : return value_; } - virtual const char *Name() const { - throw; - } - virtual void Output(std::ostream &out) const { return Output(out, CYNoFlags); } virtual void Output(std::ostream &out, CYFlags flags) const; + virtual void PropertyName(std::ostream &out) const; }; struct CYNull : @@ -443,6 +602,10 @@ struct CYInfix : { } + void SetLeft(CYExpression *lhs) { + lhs_ = lhs; + } + virtual bool Alphabetic() const = 0; virtual const char *Operator() const = 0; @@ -476,6 +639,10 @@ struct CYAssignment : { } + void SetLeft(CYExpression *lhs) { + lhs_ = lhs; + } + virtual const char *Operator() const = 0; virtual void Output(std::ostream &out, CYFlags flags) const; @@ -550,7 +717,6 @@ struct CYArray : }; struct CYDeclaration : - CYThing, CYForInInitialiser { CYIdentifier *identifier_; @@ -562,24 +728,55 @@ struct CYDeclaration : { } - virtual void Part(std::ostream &out) const; - virtual void Output(std::ostream &out) const; + virtual void ForIn(std::ostream &out, CYFlags flags) const; + + virtual const char *ForEachIn() const; + virtual void ForEachIn(std::ostream &out) const; + + virtual void Output(std::ostream &out, CYFlags flags) const; }; struct CYDeclarations : - CYStatement, + CYNext, CYForInitialiser { CYDeclaration *declaration_; - CYDeclarations *next_; CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) : - declaration_(declaration), - next_(next) + CYNext(next), + declaration_(declaration) + { + } + + virtual void For(std::ostream &out) const; + virtual void Output(std::ostream &out, CYFlags flags) const; +}; + +struct CYVar : + CYStatement +{ + CYDeclarations *declarations_; + + CYVar(CYDeclarations *declarations) : + declarations_(declarations) + { + } + + virtual void Output(std::ostream &out) const; +}; + +struct CYLet : + CYStatement +{ + CYDeclarations *declarations_; + CYStatement *statements_; + + CYLet(CYDeclarations *declarations, CYStatement *statements) : + declarations_(declarations), + statements_(statements) { } - virtual void Part(std::ostream &out) const; virtual void Output(std::ostream &out) const; }; @@ -605,7 +802,7 @@ struct CYMessageParameter : }; struct CYMessage : - CYSource + CYNext { bool instance_; CYExpression *type_; @@ -620,18 +817,19 @@ struct CYMessage : { } - virtual void Output(std::ostream &out) const; + virtual void Output(std::ostream &out, bool replace) const; }; struct CYClass : - CYSource + CYExpression, + CYStatement { - CYIdentifier *name_; + CYClassName *name_; CYExpression *super_; CYField *fields_; CYMessage *messages_; - CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) : + CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) : name_(name), super_(super), fields_(fields), @@ -639,6 +837,24 @@ struct CYClass : { } + CYPrecedence(0) + + virtual void Output(std::ostream &out) const; + virtual void Output(std::ostream &out, CYFlags flags) const; +}; + +struct CYCategory : + CYStatement +{ + CYClassName *name_; + CYMessage *messages_; + + CYCategory(CYClassName *name, CYMessage *messages) : + name_(name), + messages_(messages) + { + } + virtual void Output(std::ostream &out) const; }; @@ -693,13 +909,30 @@ struct CYForIn : virtual void Output(std::ostream &out) const; }; +struct CYForEachIn : + CYStatement +{ + CYForInInitialiser *initialiser_; + CYExpression *set_; + CYStatement *code_; + + CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + initialiser_(initialiser), + set_(set), + code_(code) + { + } + + virtual void Output(std::ostream &out) const; +}; + struct CYProperty : CYNext { - CYName *name_; + CYPropertyName *name_; CYExpression *value_; - CYProperty(CYName *name, CYExpression *value, CYProperty *next) : + CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) : CYNext(next), name_(name), value_(value) @@ -766,6 +999,32 @@ struct CYMember : { } + void SetLeft(CYExpression *object) { + object_ = object; + } +}; + +struct CYDirectMember : + CYMember +{ + CYDirectMember(CYExpression *object, CYExpression *property) : + CYMember(object, property) + { + } + + CYPrecedence(1) + + virtual void Output(std::ostream &out, CYFlags flags) const; +}; + +struct CYIndirectMember : + CYMember +{ + CYIndirectMember(CYExpression *object, CYExpression *property) : + CYMember(object, property) + { + } + CYPrecedence(1) virtual void Output(std::ostream &out, CYFlags flags) const;