X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/15b88a33189cbd3da91900194d8e5c81252f8a4d..800811a0cfc03717e043632a9883c2aad9c5ab31:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index db4b1e6..9dbb9b9 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 . **/ /* }}} */ @@ -24,6 +24,7 @@ #include +#include #include #include #include @@ -38,7 +39,7 @@ #include "Pooling.hpp" #include "Options.hpp" -class CYContext; +struct CYContext; struct CYThing { virtual ~CYThing() { @@ -312,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_; @@ -330,7 +324,7 @@ struct CYScope { CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - CYScope(CYScopeType type, CYContext &context, CYStatement *&statements); + CYScope(bool transparent, CYContext &context, CYStatement *&statements); virtual ~CYScope(); void Close(); @@ -356,11 +350,14 @@ struct CYProgram : }; struct CYNonLocal; +struct CYThisScope; struct CYContext { CYOptions &options_; CYScope *scope_; + CYThisScope *this_; + CYIdentifierUsageVector rename_; CYNonLocal *nonlocal_; @@ -370,6 +367,7 @@ struct CYContext { CYContext(CYOptions &options) : options_(options), scope_(NULL), + this_(NULL), nonlocal_(NULL), nextlocal_(NULL), unique_(0) @@ -420,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 @@ -436,7 +453,7 @@ struct CYBlock : } void AddPrev(CYStatement *statement) { - CYSetLast(statement, statements_); + CYSetLast(statement) = statements_; statements_ = statement; } @@ -452,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, @@ -514,7 +559,7 @@ class CYDriver { void ScannerDestroy(); public: - CYDriver(const std::string &filename = ""); + CYDriver(std::istream &data, const std::string &filename = ""); ~CYDriver(); Condition GetCondition(); @@ -543,6 +588,8 @@ struct CYForInInitialiser { virtual CYExpression *Replace(CYContext &context) = 0; virtual CYAssignment *Assignment(CYContext &context) = 0; + + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYNumber; @@ -620,7 +667,7 @@ struct CYCompound : } void AddPrev(CYExpression *expression) { - CYSetLast(expression, expressions_); + CYSetLast(expression) = expressions_; expressions_ = expression; } @@ -628,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; @@ -679,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) { @@ -694,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) { @@ -1076,7 +1119,7 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; @@ -1228,13 +1271,13 @@ struct CYVar : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYLet : +struct CYLetStatement : CYStatement { CYDeclarations *declarations_; CYStatement *code_; - CYLet(CYDeclarations *declarations, CYStatement *code) : + CYLetStatement(CYDeclarations *declarations, CYStatement *code) : declarations_(declarations), code_(code) { @@ -1282,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) @@ -1329,8 +1372,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1489,7 +1530,9 @@ struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; + CYNonLocal *nonlocal_; + CYThisScope this_; CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), @@ -1524,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 @@ -1715,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 {