X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/9561f209ba7d08b95e2bd3b86b6338bd2537ea55..da2af935087da0e425bbfb06fe1cc24107e3f32d:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index a76faaa..d853939 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,76 +1,44 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2010 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU Lesser General Public License, Version 3 {{{ */ /* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: + * 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. * - * 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. + * 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. * - * 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. -*/ + * You should have received a copy of the GNU Lesser General Public License + * along with Cycript. If not, see . +**/ /* }}} */ #ifndef CYPARSER_HPP #define CYPARSER_HPP -// XXX: wtf is this here?! -#define CYPA 16 - #include #include #include +#include +#include +#include #include #include "location.hh" -#include "Pooling.hpp" - -template -struct CYNext { - Type_ *next_; - - CYNext() : - next_(NULL) - { - } - CYNext(Type_ *next) : - next_(next) - { - } +#include "List.hpp" +#include "Pooling.hpp" +#include "Options.hpp" - void SetNext(Type_ *next) { - next_ = next; - } -}; +class CYContext; struct CYThing { virtual ~CYThing() { @@ -81,8 +49,10 @@ struct CYThing { struct CYOutput { std::ostream &out_; + CYOptions &options_; bool pretty_; unsigned indent_; + bool right_; enum { NoMode, @@ -92,10 +62,12 @@ struct CYOutput { Terminated } mode_; - CYOutput(std::ostream &out) : + CYOutput(std::ostream &out, CYOptions &options) : out_(out), + options_(options), pretty_(false), indent_(0), + right_(false), mode_(NoMode) { } @@ -145,22 +117,6 @@ enum CYFlags { CYNoBF = (CYNoBrace | CYNoFunction), }; -struct CYContext { - apr_pool_t *pool_; - - CYContext(apr_pool_t *pool) : - pool_(pool) - { - } - - template - void Replace(Type_ *&value) { - if (value != NULL) - while (Type_ *replace = value->Replace(*this)) - value = replace; - } -}; - struct CYStatement : CYNext { @@ -170,8 +126,7 @@ struct CYStatement : void Single(CYOutput &out, CYFlags flags) const; void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const; - CYStatement *ReplaceAll(CYContext &context); - + virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context) = 0; private: @@ -227,10 +182,11 @@ struct CYWord : { } - const char *Value() const { - return word_; + void Set(const char *value) { + word_ = value; } + virtual const char *Word() const; virtual void Output(CYOutput &out) const; virtual CYExpression *ClassName(CYContext &context, bool object); @@ -239,16 +195,42 @@ struct CYWord : }; _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) { - return lhs << rhs.Value(); + lhs << &rhs << '='; + return lhs << rhs.Word(); } struct CYIdentifier : + CYNext, CYWord { + CYIdentifier *replace_; + size_t offset_; + size_t usage_; + CYIdentifier(const char *word) : - CYWord(word) + CYWord(word), + replace_(NULL), + offset_(0), + usage_(0) + { + } + + virtual const char *Word() const; + CYIdentifier *Replace(CYContext &context); +}; + +struct CYComment : + CYStatement +{ + const char *value_; + + CYComment(const char *value) : + value_(value) { } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYLabel : @@ -267,6 +249,69 @@ struct CYLabel : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYCStringLess : + std::binary_function +{ + _finline bool operator ()(const char *lhs, const char *rhs) const { + return strcmp(lhs, rhs) < 0; + } +}; + +struct CYIdentifierValueLess : + std::binary_function +{ + _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const { + return CYCStringLess()(lhs->Word(), rhs->Word()); + } +}; + +enum CYIdentifierFlags { + CYIdentifierArgument, + CYIdentifierVariable, + CYIdentifierOther, + CYIdentifierMagic, + CYIdentifierCatch, +}; + +typedef std::set CYCStringSet; +typedef std::set CYIdentifierValueSet; +typedef std::map CYIdentifierAddressFlagsMap; + +struct CYIdentifierUsage { + CYIdentifier *identifier_; + size_t usage_; +}; + +typedef std::vector CYIdentifierUsageVector; + +// XXX: strategy pattern, maybe subclass +enum CYScopeType { + CYScopeCatch, + CYScopeFunction, + CYScopeProgram, +}; + +struct CYScope { + CYScopeType type_; + + CYContext &context_; + CYStatement *&statements_; + + CYScope *parent_; + + CYIdentifierAddressFlagsMap internal_; + CYIdentifierValueSet identifiers_; + + CYScope(CYScopeType type, CYContext &context, CYStatement *&statements); + + void Close(); + + void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags); + virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier); + void Merge(CYContext &context, CYIdentifier *identifier); + void Scope(CYContext &context, CYStatement *&statements); +}; + struct CYProgram : CYThing { @@ -278,10 +323,73 @@ struct CYProgram : } virtual void Replace(CYContext &context); - virtual void Output(CYOutput &out) const; }; +struct CYNonLocal; + +struct CYContext { + CYOptions &options_; + + CYScope *scope_; + CYIdentifierUsageVector rename_; + + CYNonLocal *nonlocal_; + CYNonLocal *nextlocal_; + unsigned unique_; + + CYContext(CYOptions &options) : + options_(options), + scope_(NULL), + nonlocal_(NULL), + nextlocal_(NULL), + unique_(0) + { + } + + virtual ~CYContext() { + } + + template + void ReplaceAll(Type_ *&values) { + Type_ **last(&values); + CYForEach (next, values) { + Replace(*last); + last = &(*last)->next_; + } + } + + template + void Replace(Type_ *&value) { + for (;;) if (value == NULL) + break; + else { + Type_ *replace(value->Replace(*this)); + if (replace != value) + value = replace; + else break; + } + } + + void NonLocal(CYStatement *&statements); + CYIdentifier *Unique(); +}; + +struct CYNonLocal { + CYIdentifier *identifier_; + + CYNonLocal() : + identifier_(NULL) + { + } + + CYIdentifier *Target(CYContext &context) { + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; + } +}; + struct CYBlock : CYStatement, CYThing @@ -297,6 +405,11 @@ struct CYBlock : return statements_; } + void AddPrev(CYStatement *statement) { + CYSetLast(statement, statements_); + statements_ = statement; + } + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out) const; @@ -311,8 +424,6 @@ enum CYState { class CYDriver { public: - CYPool pool_; - CYState state_; void *scanner_; @@ -341,12 +452,39 @@ class CYDriver { 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_; + private: void ScannerInit(); void ScannerDestroy(); public: - CYDriver(const std::string &filename); + CYDriver(const std::string &filename = ""); ~CYDriver(); Condition GetCondition(); @@ -363,6 +501,7 @@ struct CYForInitialiser { } virtual void For(CYOutput &out) const = 0; + virtual CYExpression *Replace(CYContext &context) = 0; }; struct CYForInInitialiser { @@ -372,6 +511,7 @@ struct CYForInInitialiser { virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; virtual const char *ForEachIn() const = 0; virtual CYExpression *ForEachIn(CYContext &out) = 0; + virtual CYExpression *Replace(CYContext &context) = 0; }; struct CYNumber; @@ -396,6 +536,8 @@ struct CYExpression : virtual const char *ForEachIn() const; virtual CYExpression *ForEachIn(CYContext &out); + 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; @@ -403,8 +545,6 @@ struct CYExpression : virtual CYExpression *ClassName(CYContext &context, bool object); virtual void ClassName(CYOutput &out, bool object) const; - CYExpression *ReplaceAll(CYContext &context); - virtual CYExpression *Replace(CYContext &context) = 0; virtual CYExpression *Primitive(CYContext &context) { @@ -430,8 +570,9 @@ struct CYExpression : } #define CYPrecedence(value) \ + static const unsigned Precedence_ = value; \ virtual unsigned Precedence() const { \ - return value; \ + return Precedence_; \ } #define CYRightHand(value) \ @@ -444,16 +585,13 @@ struct CYCompound : { CYExpression *expressions_; - CYCompound(CYExpression *expressions) : + CYCompound(CYExpression *expressions = NULL) : expressions_(expressions) { } void AddPrev(CYExpression *expression) { - CYExpression *last(expression); - while (last->next_ != NULL) - last = last->next_; - last->SetNext(expressions_); + CYSetLast(expression, expressions_); expressions_ = expression; } @@ -475,6 +613,22 @@ struct CYFunctionParameter : { } + 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) + { + } + + virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); virtual void Output(CYOutput &out) const; }; @@ -503,7 +657,7 @@ struct CYForInComprehension : } virtual const char *Name() const { - return name_->Value(); + return name_->Word(); } virtual CYFunctionParameter *Parameter(CYContext &context) const; @@ -524,7 +678,7 @@ struct CYForEachInComprehension : } virtual const char *Name() const { - return name_->Value(); + return name_->Word(); } virtual CYFunctionParameter *Parameter(CYContext &context) const; @@ -639,7 +793,7 @@ struct CYString : } CYString(const CYWord *word) : - value_(word->Value()), + value_(word->Word()), size_(strlen(value_)) { } @@ -776,6 +930,11 @@ struct CYVariable : { } + CYVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) + { + } + CYPrecedence(0) CYRightHand(false) @@ -983,15 +1142,16 @@ struct CYDeclaration : virtual const char *ForEachIn() const; virtual CYExpression *ForEachIn(CYContext &out); - void Replace(CYContext &context); + virtual CYExpression *Replace(CYContext &context); + virtual CYAssignment *Assignment(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYDeclarations : CYNext, - CYForInitialiser, - CYThing + CYThing, + CYForInitialiser { CYDeclaration *declaration_; @@ -1003,7 +1163,7 @@ struct CYDeclarations : virtual void For(CYOutput &out) const; - void Replace(CYContext &context); + virtual CYCompound *Replace(CYContext &context); CYProperty *Property(CYContext &context); virtual void Output(CYOutput &out) const; @@ -1101,7 +1261,7 @@ struct CYObject : { CYProperty *properties_; - CYObject(CYProperty *properties) : + CYObject(CYProperty *properties = NULL) : properties_(properties) { } @@ -1159,13 +1319,16 @@ struct CYIndirectMember : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYNew : +namespace cy { +namespace Syntax { + +struct New : CYExpression { CYExpression *constructor_; CYArgument *arguments_; - CYNew(CYExpression *constructor, CYArgument *arguments) : + New(CYExpression *constructor, CYArgument *arguments) : constructor_(constructor), arguments_(arguments) { @@ -1179,8 +1342,12 @@ struct CYNew : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); }; +} } + struct CYCall : CYExpression { @@ -1198,6 +1365,29 @@ struct CYCall : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); +}; + +struct CYRubyProc; + +struct CYRubyBlock : + CYExpression +{ + CYExpression *call_; + CYRubyProc *proc_; + + CYRubyBlock(CYExpression *call, CYRubyProc *proc) : + call_(call), + proc_(proc) + { + } + + CYPrecedence(1) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYIf : @@ -1250,25 +1440,30 @@ struct CYWhile : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass) struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; + CYNonLocal *nonlocal_; CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), parameters_(parameters), - code_(statements) + code_(statements), + nonlocal_(NULL) { } virtual ~CYFunction() { } - virtual void Replace_(CYContext &context); + void Inject(CYContext &context); + virtual void Replace_(CYContext &context, bool outer); virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression struct CYFunctionExpression : CYFunction, CYExpression @@ -1285,6 +1480,20 @@ struct CYFunctionExpression : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should derive from CYAnonymousFunctionExpression +struct CYRubyProc : + CYFunctionExpression +{ + CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) : + CYFunctionExpression(NULL, parameters, statements) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +// XXX: this should derive from CYNamedFunction struct CYFunctionStatement : CYFunction, CYStatement @@ -1306,8 +1515,11 @@ struct CYExpress : CYExpress(CYExpression *expression) : expression_(expression) { + if (expression == NULL) + throw; } + virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1357,6 +1569,7 @@ struct CYReturn : struct CYEmpty : CYStatement { + virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1417,7 +1630,7 @@ struct Throw : { CYExpression *value_; - Throw(CYExpression *value) : + Throw(CYExpression *value = NULL) : value_(value) { }