X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/c5fa2867a4a09bcc2b3933da72e820fb9f5ad8d1..1cf12770640ddf1ea2e0656fec69d61b7389dc3c:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 692be9b..0e4b51f 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,20 +1,20 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2010 Jay Freeman (saurik) + * Copyright (C) 2009-2013 Jay Freeman (saurik) */ -/* GNU Lesser General Public License, Version 3 {{{ */ +/* GNU General Public License, Version 3 {{{ */ /* - * Cycript is free software: you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. + * Cycript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. * - * Cycript is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for more details. + * Cycript is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License + * You should have received a copy of the GNU General Public License * along with Cycript. If not, see . **/ /* }}} */ @@ -32,13 +32,11 @@ #include #include -#include "location.hh" - #include "List.hpp" #include "Pooling.hpp" #include "Options.hpp" -class CYContext; +struct CYContext; struct CYThing { virtual ~CYThing() { @@ -98,6 +96,7 @@ struct CYPropertyName { }; struct CYExpression; +struct CYAssignment; enum CYNeeded { CYNever = -1, @@ -117,6 +116,34 @@ enum CYFlags { CYNoBF = (CYNoBrace | CYNoFunction), }; +_finline CYFlags operator ~(CYFlags rhs) { + return static_cast(~static_cast(rhs)); +} + +_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) { + return static_cast(static_cast(lhs) & static_cast(rhs)); +} + +_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) { + return static_cast(static_cast(lhs) | static_cast(rhs)); +} + +_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) { + return lhs = lhs | rhs; +} + +_finline CYFlags CYLeft(CYFlags flags) { + return flags & ~(CYNoDangle | CYNoInteger); +} + +_finline CYFlags CYRight(CYFlags flags) { + return flags & ~CYNoBF; +} + +_finline CYFlags CYCenter(CYFlags flags) { + return CYLeft(CYRight(flags)); +} + struct CYStatement : CYNext { @@ -126,7 +153,6 @@ struct CYStatement : void Single(CYOutput &out, CYFlags flags) const; void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const; - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context) = 0; private: @@ -284,15 +310,8 @@ struct CYIdentifierUsage { typedef std::vector CYIdentifierUsageVector; -// XXX: strategy pattern, maybe subclass -enum CYScopeType { - CYScopeCatch, - CYScopeFunction, - CYScopeProgram, -}; - struct CYScope { - CYScopeType type_; + bool transparent_; CYContext &context_; CYStatement *&statements_; @@ -302,7 +321,8 @@ struct CYScope { CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - CYScope(CYScopeType type, CYContext &context, CYStatement *&statements); + CYScope(bool transparent, CYContext &context, CYStatement *&statements); + virtual ~CYScope(); void Close(); @@ -327,11 +347,14 @@ struct CYProgram : }; struct CYNonLocal; +struct CYThisScope; struct CYContext { CYOptions &options_; CYScope *scope_; + CYThisScope *this_; + CYIdentifierUsageVector rename_; CYNonLocal *nonlocal_; @@ -341,6 +364,7 @@ struct CYContext { CYContext(CYOptions &options) : options_(options), scope_(NULL), + this_(NULL), nonlocal_(NULL), nextlocal_(NULL), unique_(0) @@ -354,8 +378,9 @@ struct CYContext { void ReplaceAll(Type_ *&values) { Type_ **last(&values); CYForEach (next, values) { - Replace(*last); - last = &(*last)->next_; + Replace(*last = next); + if (*last != NULL) + last = &(*last)->next_; } } @@ -390,6 +415,25 @@ struct CYNonLocal { } }; +struct CYThisScope : + CYNext +{ + CYIdentifier *identifier_; + + CYThisScope() : + identifier_(NULL) + { + } + + CYIdentifier *Identifier(CYContext &context) { + if (next_ != NULL) + return next_->Identifier(context); + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; + } +}; + struct CYBlock : CYStatement, CYThing @@ -406,7 +450,7 @@ struct CYBlock : } void AddPrev(CYStatement *statement) { - CYSetLast(statement, statements_); + CYSetLast(statement) = statements_; statements_ = statement; } @@ -416,92 +460,33 @@ struct CYBlock : virtual void Output(CYOutput &out, CYFlags flags) const; }; -enum CYState { - CYClear, - CYRestricted, - CYNewLine -}; - -class CYDriver { - public: - CYState state_; - void *scanner_; - - const char *data_; - size_t size_; - FILE *file_; - - bool strict_; - - enum Condition { - RegExpCondition, - XMLContentCondition, - XMLTagCondition, - }; - - std::string filename_; - - struct Error { - bool warning_; - cy::location location_; - std::string message_; - }; - - typedef std::vector Errors; - - CYProgram *program_; - Errors errors_; - - bool auto_; - - struct Context { - CYExpression *context_; - - Context(CYExpression *context) : - context_(context) - { - } - - typedef std::vector Words; - Words words_; - }; - - typedef std::vector Contexts; - Contexts contexts_; - - CYExpression *context_; - - enum Mode { - AutoNone, - AutoPrimary, - AutoDirect, - AutoIndirect, - AutoMessage - } mode_; - +class CYStream : + public std::istream +{ private: - void ScannerInit(); - void ScannerDestroy(); + class CYBuffer : + public std::streambuf + { + public: + CYBuffer(const char *start, const char *end) { + setg(const_cast(start), const_cast(start), const_cast(end)); + } + } buffer_; public: - CYDriver(const std::string &filename = ""); - ~CYDriver(); - - Condition GetCondition(); - void SetCondition(Condition condition); - - void PushCondition(Condition condition); - void PopCondition(); - - void Warning(const cy::location &location, const char *message); + CYStream(const char *start, const char *end) : + std::istream(&buffer_), + buffer_(start, end) + { + } }; struct CYForInitialiser { virtual ~CYForInitialiser() { } - virtual void For(CYOutput &out) const = 0; virtual CYExpression *Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYForInInitialiser { @@ -509,9 +494,12 @@ struct CYForInInitialiser { } virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; - virtual const char *ForEachIn() const = 0; - virtual CYExpression *ForEachIn(CYContext &out) = 0; + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0; + virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYAssignment *Assignment(CYContext &context) = 0; + + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYNumber; @@ -524,28 +512,26 @@ struct CYExpression : CYClassName, CYThing { - virtual unsigned Precedence() const = 0; + virtual int Precedence() const = 0; virtual bool RightHand() const { return true; } - virtual void For(CYOutput &out) const; virtual void ForIn(CYOutput &out, CYFlags flags) const; - - virtual const char *ForEachIn() const; - virtual CYExpression *ForEachIn(CYContext &out); + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const = 0; - void Output(CYOutput &out, unsigned precedence, CYFlags flags) const; + void Output(CYOutput &out, int precedence, CYFlags flags) const; virtual CYExpression *ClassName(CYContext &context, bool object); virtual void ClassName(CYOutput &out, bool object) const; virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYAssignment *Assignment(CYContext &context); virtual CYExpression *Primitive(CYContext &context) { return this; @@ -570,8 +556,8 @@ struct CYExpression : } #define CYPrecedence(value) \ - static const unsigned Precedence_ = value; \ - virtual unsigned Precedence() const { \ + static const int Precedence_ = value; \ + virtual int Precedence() const { \ return Precedence_; \ } @@ -591,7 +577,7 @@ struct CYCompound : } void AddPrev(CYExpression *expression) { - CYSetLast(expression, expressions_); + CYSetLast(expression) = expressions_; expressions_ = expression; } @@ -599,43 +585,42 @@ struct CYCompound : virtual CYExpression *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *Primitive(CYContext &context); }; +struct CYDeclaration; + struct CYFunctionParameter : CYNext, CYThing { - CYIdentifier *name_; + CYForInInitialiser *initialiser_; - CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) : + CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) : CYNext(next), - name_(name) + initialiser_(initialiser) { } - virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); - virtual void Output(CYOutput &out) const; + void Replace(CYContext &context, CYBlock &code); + void Output(CYOutput &out) const; }; -struct CYOptionalFunctionParameter : - CYFunctionParameter +struct CYComprehension : + CYNext, + CYThing { - CYExpression *initializer_; - - CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) : - CYFunctionParameter(name, next), - initializer_(initializer) + CYComprehension(CYComprehension *next = NULL) : + CYNext(next) { } - virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); - virtual void Output(CYOutput &out) const; -}; + CYComprehension *Modify(CYComprehension *next) { + next_ = next; + return this; + } -struct CYComprehension : - CYNext, - CYThing -{ virtual const char *Name() const = 0; virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; @@ -650,7 +635,8 @@ struct CYForInComprehension : CYIdentifier *name_; CYExpression *set_; - CYForInComprehension(CYIdentifier *name, CYExpression *set) : + CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYComprehension(next), name_(name), set_(set) { @@ -665,13 +651,14 @@ struct CYForInComprehension : virtual void Output(CYOutput &out) const; }; -struct CYForEachInComprehension : +struct CYForOfComprehension : CYComprehension { CYIdentifier *name_; CYExpression *set_; - CYForEachInComprehension(CYIdentifier *name, CYExpression *set) : + CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYComprehension(next), name_(name), set_(set) { @@ -691,7 +678,8 @@ struct CYIfComprehension : { CYExpression *test_; - CYIfComprehension(CYExpression *test) : + CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) : + CYComprehension(next), test_(test) { } @@ -1047,7 +1035,7 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; @@ -1138,20 +1126,19 @@ struct CYDeclaration : } virtual void ForIn(CYOutput &out, CYFlags flags) const; - - virtual const char *ForEachIn() const; - virtual CYExpression *ForEachIn(CYContext &out); + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); virtual CYExpression *Replace(CYContext &context); + virtual CYAssignment *Assignment(CYContext &context); + CYVariable *Variable(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYDeclarations : CYNext, - CYThing, - CYForInitialiser + CYThing { CYDeclaration *declaration_; @@ -1161,15 +1148,31 @@ struct CYDeclarations : { } - virtual void For(CYOutput &out) const; + void Replace(CYContext &context); - virtual CYCompound *Replace(CYContext &context); + CYCompound *Compound(CYContext &context); CYProperty *Property(CYContext &context); + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameter(CYContext &context); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYForDeclarations : + CYForInitialiser +{ + CYDeclarations *declarations_; + + CYForDeclarations(CYDeclarations *declarations) : + declarations_(declarations) + { + } + + virtual CYCompound *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYVar : CYStatement { @@ -1184,15 +1187,15 @@ struct CYVar : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYLet : +struct CYLetStatement : CYStatement { CYDeclarations *declarations_; - CYBlock code_; + CYStatement *code_; - CYLet(CYDeclarations *declarations, CYStatement *statements) : + CYLetStatement(CYDeclarations *declarations, CYStatement *code) : declarations_(declarations), - code_(statements) + code_(code) { } @@ -1238,14 +1241,14 @@ struct CYForIn : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYForEachIn : +struct CYForOf : CYStatement { CYForInInitialiser *initialiser_; CYExpression *set_; CYStatement *code_; - CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : initialiser_(initialiser), set_(set), code_(code) @@ -1285,8 +1288,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1334,7 +1335,7 @@ struct New : { } - virtual unsigned Precedence() const { + virtual int Precedence() const { return arguments_ == NULL ? 2 : 1; } @@ -1445,7 +1446,9 @@ struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; + CYNonLocal *nonlocal_; + CYThisScope this_; CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), @@ -1480,6 +1483,23 @@ struct CYFunctionExpression : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should derive from CYAnonymousFunction +struct CYFatArrow : + CYFunction, + CYExpression +{ + CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) : + CYFunction(NULL, parameters, statements) + { + } + + CYPrecedence(0) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + // XXX: this should derive from CYAnonymousFunctionExpression struct CYRubyProc : CYFunctionExpression @@ -1519,7 +1539,6 @@ struct CYExpress : throw; } - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1569,7 +1588,6 @@ struct CYReturn : struct CYEmpty : CYStatement { - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1588,6 +1606,216 @@ struct CYFinally : virtual void Output(CYOutput &out) const; }; +struct CYTypeModifier : + CYNext +{ + CYTypeModifier(CYTypeModifier *next) : + CYNext(next) + { + } + + virtual int Precedence() const = 0; + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0; + CYExpression *Replace(CYContext &context, CYExpression *type); + + virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0; + void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const; +}; + +struct CYTypeArrayOf : + CYTypeModifier +{ + CYExpression *size_; + + CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + size_(size) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeConstant : + CYTypeModifier +{ + CYTypeConstant(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypePointerTo : + CYTypeModifier +{ + CYTypePointerTo(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeVolatile : + CYTypeModifier +{ + CYTypeVolatile(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypedIdentifier : + CYNext, + CYThing +{ + CYIdentifier *identifier_; + CYExpression *type_; + CYTypeModifier *modifier_; + + CYTypedIdentifier(CYIdentifier *identifier = NULL) : + identifier_(identifier), + type_(NULL), + modifier_(NULL) + { + } + + CYTypedIdentifier(CYExpression *type, CYTypeModifier *modifier = NULL) : + identifier_(NULL), + type_(type), + modifier_(modifier) + { + } + + inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) { + CYSetLast(modifier_) = modifier; + return this; + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYEncodedType : + CYExpression +{ + CYTypedIdentifier *typed_; + + CYEncodedType(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypedParameter : + CYNext, + CYThing +{ + CYTypedIdentifier *typed_; + + CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : + CYNext(next), + typed_(typed) + { + } + + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameters(CYContext &context); + CYExpression *TypeSignature(CYContext &context, CYExpression *prefix); + + virtual void Output(CYOutput &out) const; +}; + +struct CYLambda : + CYExpression +{ + CYTypedIdentifier *typed_; + CYTypedParameter *parameters_; + CYStatement *statements_; + + CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) : + typed_(typed), + parameters_(parameters), + statements_(statements) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeDefinition : + CYStatement +{ + CYTypedIdentifier *typed_; + + CYTypeDefinition(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeBlockWith : + CYTypeModifier +{ + CYTypedParameter *parameters_; + + CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + parameters_(parameters) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeFunctionWith : + CYTypeModifier +{ + CYTypedParameter *parameters_; + + CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + parameters_(parameters) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + namespace cy { namespace Syntax { @@ -1673,6 +1901,17 @@ struct CYSwitch : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYDebugger : + CYStatement +{ + CYDebugger() + { + } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYCondition : CYExpression {