X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/693d501bb3c190a047d02b897f84917ccb623db6..cde525f7e0bded621b0c3f39dfbbc5f49e067dac:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 7673ad0..2821a61 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -5,6 +5,7 @@ #include <string> #include <vector> +#include "location.hh" #include "Pooling.hpp" template <typename Type_> @@ -16,6 +17,11 @@ struct CYNext { { } + CYNext(Type_ *next) : + next_(next) + { + } + void SetNext(Type_ *next) { next_ = next; } @@ -78,13 +84,14 @@ struct CYIdentifier : } }; -struct CYLabel { +struct CYLabel : + CYNext<CYLabel> +{ CYIdentifier *identifier_; - CYLabel *next_; CYLabel(CYIdentifier *identifier, CYLabel *next) : - identifier_(identifier), - next_(next) + CYNext<CYLabel>(next), + identifier_(identifier) { } }; @@ -108,11 +115,25 @@ enum CYState { class CYDriver { public: CYPool pool_; + CYState state_; - std::string filename_; - std::vector<CYSource *> source_; void *scanner_; + const char *data_; + size_t size_; + + std::string filename_; + + struct Error { + cy::location location_; + std::string message_; + }; + + typedef std::vector<Error> Errors; + + CYSource *source_; + Errors errors_; + private: void ScannerInit(); void ScannerDestroy(); @@ -120,8 +141,6 @@ class CYDriver { public: CYDriver(const std::string &filename); ~CYDriver(); - - void Clear(); }; struct CYForInitialiser : @@ -139,19 +158,73 @@ struct CYExpression : CYForInitialiser, CYForInInitialiser { + virtual unsigned Precedence() const = 0; virtual void Part(std::ostream &out) const; virtual void Output(std::ostream &out) const = 0; - void Output(std::ostream &out, bool raw) const; + void Output(std::ostream &out, unsigned precedence) const; }; -_finline std::ostream &operator <<(std::ostream &out, const CYExpression &rhs) { - rhs.Output(out, false); - return out; -} +#define CYPrecedence(value) \ + virtual unsigned Precedence() const { \ + return value; \ + } + +struct CYCompound : + CYExpression +{ + CYExpression *expressions_; + + CYCompound(CYExpression *expressions) : + expressions_(expressions) + { + } + + void AddPrev(CYExpression *expression) { + CYExpression *last(expression); + while (last->next_ != NULL) + last = last->next_; + last->SetNext(expressions_); + expressions_ = expression; + } + + CYPrecedence(17) + + void Output(std::ostream &out) const; +}; struct CYLiteral : CYExpression { + CYPrecedence(0) +}; + +struct CYSelectorPart : + CYNext<CYSelectorPart> +{ + CYWord *name_; + bool value_; + + CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) : + CYNext<CYSelectorPart>(next), + name_(name), + value_(value) + { + } + + virtual void Output(std::ostream &out) const; +}; + +struct CYSelector : + CYLiteral +{ + CYSelectorPart *name_; + + CYSelector(CYSelectorPart *name) : + name_(name) + { + } + + virtual void Output(std::ostream &out) const; }; struct CYString : @@ -227,6 +300,8 @@ struct CYThis : { } + CYPrecedence(0) + virtual void Output(std::ostream &out) const; }; @@ -275,6 +350,8 @@ struct CYVariable : { } + CYPrecedence(0) + virtual void Output(std::ostream &out) const; }; @@ -326,29 +403,36 @@ struct CYPostfix : }; struct CYAssignment : - CYInfix + CYExpression { + CYExpression *lhs_; + CYExpression *rhs_; + CYAssignment(CYExpression *lhs, CYExpression *rhs) : - CYInfix(lhs, rhs) + lhs_(lhs), + rhs_(rhs) { } virtual const char *Operator() const = 0; + + virtual void Output(std::ostream &out) const; }; -struct CYArgument { +struct CYArgument : + CYNext<CYArgument> +{ CYWord *name_; CYExpression *value_; - CYArgument *next_; CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) : + CYNext<CYArgument>(next), name_(name), - value_(value), - next_(next) + value_(value) { } - void Output(std::ostream &out, bool send) const; + void Output(std::ostream &out) const; }; struct CYBlank : @@ -377,18 +461,29 @@ struct CYClause : }; struct CYElement : - CYLiteral + CYNext<CYElement> { CYExpression *value_; - CYElement *next_; CYElement(CYExpression *value, CYElement *next) : - value_(value), - next_(next) + CYNext<CYElement>(next), + value_(value) + { + } + + void Output(std::ostream &out) const; +}; + +struct CYArray : + CYLiteral +{ + CYElement *elements_; + + CYArray(CYElement *elements) : + elements_(elements) { } - void Output(std::ostream &out, bool raw) const; virtual void Output(std::ostream &out) const; }; @@ -427,14 +522,14 @@ struct CYDeclarations : }; struct CYParameter : + CYNext<CYParameter>, CYThing { CYIdentifier *name_; - CYParameter *next_; CYParameter(CYIdentifier *name, CYParameter *next) : - name_(name), - next_(next) + CYNext<CYParameter>(next), + name_(name) { } @@ -477,15 +572,16 @@ struct CYForIn : virtual void Output(std::ostream &out) const; }; -struct CYProperty { +struct CYProperty : + CYNext<CYProperty> +{ CYName *name_; CYExpression *value_; - CYProperty *next_; CYProperty(CYName *name, CYExpression *value, CYProperty *next) : + CYNext<CYProperty>(next), name_(name), - value_(value), - next_(next) + value_(value) { } @@ -532,6 +628,8 @@ struct CYMessage : { } + CYPrecedence(0) + virtual void Output(std::ostream &out) const; }; @@ -547,6 +645,8 @@ struct CYMember : { } + CYPrecedence(1) + virtual void Output(std::ostream &out) const; }; @@ -562,6 +662,8 @@ struct CYNew : { } + CYPrecedence(1) + virtual void Output(std::ostream &out) const; }; @@ -577,6 +679,8 @@ struct CYCall : { } + CYPrecedence(2) + virtual void Output(std::ostream &out) const; }; @@ -641,6 +745,8 @@ struct CYLambda : { } + CYPrecedence(0) + virtual void Output(std::ostream &out) const; }; @@ -783,11 +889,14 @@ struct CYCondition : CYExpression *false_; CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) : + test_(test), true_(_true), false_(_false) { } + CYPrecedence(15) + virtual void Output(std::ostream &out) const; }; @@ -803,6 +912,8 @@ struct CYAddressOf : return "&"; } + CYPrecedence(2) + virtual void Output(std::ostream &out) const; }; @@ -818,6 +929,8 @@ struct CYIndirect : return "*"; } + CYPrecedence(1) + virtual void Output(std::ostream &out) const; }; @@ -829,6 +942,8 @@ struct CYIndirect : CYPostfix(lhs) \ { \ } \ + \ + CYPrecedence(3) \ \ virtual const char *Operator() const { \ return op; \ @@ -843,13 +958,15 @@ struct CYIndirect : CYPrefix(rhs) \ { \ } \ + \ + CYPrecedence(4) \ \ virtual const char *Operator() const { \ return op; \ } \ }; -#define CYInfix_(op, name) \ +#define CYInfix_(precedence, op, name) \ struct CY ## name : \ CYInfix \ { \ @@ -857,6 +974,8 @@ struct CYIndirect : CYInfix(lhs, rhs) \ { \ } \ + \ + CYPrecedence(precedence) \ \ virtual const char *Operator() const { \ return op; \ @@ -871,6 +990,8 @@ struct CYIndirect : CYAssignment(lhs, rhs) \ { \ } \ + \ + CYPrecedence(16) \ \ virtual const char *Operator() const { \ return op; \ @@ -889,29 +1010,29 @@ CYPrefix_("-", Negate) CYPrefix_("~", BitwiseNot) CYPrefix_("!", LogicalNot) -CYInfix_("*", Multiply) -CYInfix_("/", Divide) -CYInfix_("%", Modulus) -CYInfix_("+", Add) -CYInfix_("-", Subtract) -CYInfix_("<<", ShiftLeft) -CYInfix_(">>", ShiftRightSigned) -CYInfix_(">>>", ShiftRightUnsigned) -CYInfix_("<", Less) -CYInfix_(">", Greater) -CYInfix_("<=", LessOrEqual) -CYInfix_(">=", GreaterOrEqual) -CYInfix_("instanceof", InstanceOf) -CYInfix_("in", In) -CYInfix_("==", Equal) -CYInfix_("!=", NotEqual) -CYInfix_("===", Identical) -CYInfix_("!==", NotIdentical) -CYInfix_("&", BitwiseAnd) -CYInfix_("^", BitwiseXOr) -CYInfix_("|", BitwiseOr) -CYInfix_("&&", LogicalAnd) -CYInfix_("||", LogicalOr) +CYInfix_(5, "*", Multiply) +CYInfix_(5, "/", Divide) +CYInfix_(5, "%", Modulus) +CYInfix_(6, "+", Add) +CYInfix_(6, "-", Subtract) +CYInfix_(7, "<<", ShiftLeft) +CYInfix_(7, ">>", ShiftRightSigned) +CYInfix_(7, ">>>", ShiftRightUnsigned) +CYInfix_(8, "<", Less) +CYInfix_(8, ">", Greater) +CYInfix_(8, "<=", LessOrEqual) +CYInfix_(8, ">=", GreaterOrEqual) +CYInfix_(8, "instanceof", InstanceOf) +CYInfix_(8, "in", In) +CYInfix_(9, "==", Equal) +CYInfix_(9, "!=", NotEqual) +CYInfix_(9, "===", Identical) +CYInfix_(9, "!==", NotIdentical) +CYInfix_(10, "&", BitwiseAnd) +CYInfix_(11, "^", BitwiseXOr) +CYInfix_(12, "|", BitwiseOr) +CYInfix_(13, "&&", LogicalAnd) +CYInfix_(14, "||", LogicalOr) CYAssignment_("=", ) CYAssignment_("*=", Multiply)