X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/cde20a5add55cd85f55405acf6d60c2eafc2b575..afdeb40444392e6b538d1bb46454e017724e6775:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 5d86018..c9614ed 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,5 +1,5 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2010 Jay Freeman (saurik) + * Copyright (C) 2009-2012 Jay Freeman (saurik) */ /* GNU Lesser General Public License, Version 3 {{{ */ @@ -19,11 +19,12 @@ **/ /* }}} */ -#ifndef CYPARSER_HPP -#define CYPARSER_HPP +#ifndef CYCRIPT_PARSER_HPP +#define CYCRIPT_PARSER_HPP #include +#include #include #include #include @@ -33,29 +34,12 @@ #include #include "location.hh" + +#include "List.hpp" #include "Pooling.hpp" #include "Options.hpp" -class CYContext; - -template -struct CYNext { - Type_ *next_; - - CYNext() : - next_(NULL) - { - } - - CYNext(Type_ *next) : - next_(next) - { - } - - void SetNext(Type_ *next) { - next_ = next; - } -}; +struct CYContext; struct CYThing { virtual ~CYThing() { @@ -115,6 +99,7 @@ struct CYPropertyName { }; struct CYExpression; +struct CYAssignment; enum CYNeeded { CYNever = -1, @@ -134,6 +119,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 { @@ -143,7 +156,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: @@ -301,15 +313,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_; @@ -319,7 +324,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(); @@ -344,11 +350,14 @@ struct CYProgram : }; struct CYNonLocal; +struct CYThisScope; struct CYContext { CYOptions &options_; CYScope *scope_; + CYThisScope *this_; + CYIdentifierUsageVector rename_; CYNonLocal *nonlocal_; @@ -358,6 +367,7 @@ struct CYContext { CYContext(CYOptions &options) : options_(options), scope_(NULL), + this_(NULL), nonlocal_(NULL), nextlocal_(NULL), unique_(0) @@ -370,9 +380,10 @@ struct CYContext { template void ReplaceAll(Type_ *&values) { Type_ **last(&values); - for (Type_ *next(values); next != NULL; next = next->next_) { - Replace(*last); - last = &(*last)->next_; + CYForEach (next, values) { + Replace(*last = next); + if (*last != NULL) + last = &(*last)->next_; } } @@ -407,6 +418,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 @@ -423,10 +453,7 @@ struct CYBlock : } void AddPrev(CYStatement *statement) { - CYStatement *last(statement); - while (last->next_ != NULL) - last = last->next_; - last->SetNext(statements_); + CYSetLast(statement) = statements_; statements_ = statement; } @@ -442,16 +469,44 @@ enum CYState { CYNewLine }; +class CYStream : + public std::istream +{ + private: + 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: + CYStream(const char *start, const char *end) : + std::istream(&buffer_), + buffer_(start, end) + { + } +}; + class CYDriver { public: - CYState state_; void *scanner_; - const char *data_; - size_t size_; - FILE *file_; + CYState state_; + std::stack in_; + + struct { + bool AtImplementation; + bool Function; + bool OpenBrace; + } no_; + + std::istream &data_; bool strict_; + bool commented_; enum Condition { RegExpCondition, @@ -504,7 +559,7 @@ class CYDriver { void ScannerDestroy(); public: - CYDriver(const std::string &filename = ""); + CYDriver(std::istream &data, const std::string &filename = ""); ~CYDriver(); Condition GetCondition(); @@ -520,8 +575,8 @@ 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 { @@ -529,9 +584,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; @@ -550,11 +608,8 @@ struct CYExpression : 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); @@ -566,6 +621,7 @@ struct CYExpression : 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; @@ -611,10 +667,7 @@ struct CYCompound : } void AddPrev(CYExpression *expression) { - CYExpression *last(expression); - while (last->next_ != NULL) - last = last->next_; - last->SetNext(expressions_); + CYSetLast(expression) = expressions_; expressions_ = expression; } @@ -622,43 +675,37 @@ 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) - { - } - - virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); - virtual void Output(CYOutput &out) const; -}; - -struct CYOptionalFunctionParameter : - CYFunctionParameter -{ - CYExpression *initializer_; - - CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) : - CYFunctionParameter(name, next), - initializer_(initializer) + 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 CYComprehension : CYNext, CYThing { + CYComprehension(CYComprehension *next = NULL) : + CYNext(next) + { + } + virtual const char *Name() const = 0; virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; @@ -673,7 +720,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) { @@ -688,13 +736,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) { @@ -1070,7 +1119,7 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; @@ -1161,20 +1210,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_; @@ -1184,15 +1232,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 { @@ -1207,15 +1271,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) { } @@ -1261,14 +1325,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) @@ -1308,8 +1372,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1468,7 +1530,9 @@ struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; + CYNonLocal *nonlocal_; + CYThisScope this_; CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), @@ -1503,6 +1567,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 @@ -1542,7 +1623,6 @@ struct CYExpress : throw; } - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1592,7 +1672,6 @@ struct CYReturn : struct CYEmpty : CYStatement { - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1696,6 +1775,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 { @@ -1864,4 +1954,4 @@ CYAssignment_("&=", BitwiseAnd) CYAssignment_("^=", BitwiseXOr) CYAssignment_("|=", BitwiseOr) -#endif/*CYPARSER_HPP*/ +#endif/*CYCRIPT_PARSER_HPP*/