X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/ab2aa221129d1936a866b25679e18445b6c20a77..3ea7eed0e5ab3447a2cc73016594971b507a2344:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 2d3a59f..d809e77 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,50 +1,30 @@ -/* 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 +#ifndef CYCRIPT_PARSER_HPP +#define CYCRIPT_PARSER_HPP #include +#include #include #include #include @@ -54,30 +34,13 @@ #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 CYThing { virtual ~CYThing() { } @@ -136,6 +99,7 @@ struct CYPropertyName { }; struct CYExpression; +struct CYAssignment; enum CYNeeded { CYNever = -1, @@ -155,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 { @@ -164,9 +156,6 @@ 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: @@ -310,6 +299,7 @@ enum CYIdentifierFlags { CYIdentifierVariable, CYIdentifierOther, CYIdentifierMagic, + CYIdentifierCatch, }; typedef std::set CYCStringSet; @@ -324,18 +314,20 @@ struct CYIdentifierUsage { typedef std::vector CYIdentifierUsageVector; struct CYScope { + bool transparent_; + + CYContext &context_; + CYStatement *&statements_; + CYScope *parent_; CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - CYScope() : - parent_(NULL) - { - } + CYScope(bool transparent, CYContext &context, CYStatement *&statements); + virtual ~CYScope(); - virtual ~CYScope() { - } + void Close(); void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags); virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier); @@ -360,20 +352,20 @@ struct CYProgram : struct CYNonLocal; struct CYContext { - apr_pool_t *pool_; CYOptions &options_; CYScope *scope_; CYIdentifierUsageVector rename_; CYNonLocal *nonlocal_; + CYNonLocal *nextlocal_; unsigned unique_; - CYContext(apr_pool_t *pool, CYOptions &options) : - pool_(pool), + CYContext(CYOptions &options) : options_(options), scope_(NULL), nonlocal_(NULL), + nextlocal_(NULL), unique_(0) { } @@ -381,6 +373,16 @@ struct CYContext { virtual ~CYContext() { } + template + void ReplaceAll(Type_ *&values) { + Type_ **last(&values); + CYForEach (next, values) { + Replace(*last = next); + if (*last != NULL) + last = &(*last)->next_; + } + } + template void Replace(Type_ *&value) { for (;;) if (value == NULL) @@ -428,10 +430,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; } @@ -449,10 +448,11 @@ enum CYState { class CYDriver { public: - CYPool pool_; + void *scanner_; CYState state_; - void *scanner_; + bool statement_; + std::stack in_; const char *data_; size_t size_; @@ -511,7 +511,7 @@ class CYDriver { void ScannerDestroy(); public: - CYDriver(apr_pool_t *pool = NULL, const std::string &filename = ""); + CYDriver(const std::string &filename = ""); ~CYDriver(); Condition GetCondition(); @@ -527,8 +527,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 { @@ -536,9 +536,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; @@ -557,11 +560,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); @@ -572,9 +572,8 @@ 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 CYAssignment *Assignment(CYContext &context); virtual CYExpression *Primitive(CYContext &context) { return this; @@ -599,8 +598,9 @@ struct CYExpression : } #define CYPrecedence(value) \ + static const unsigned Precedence_ = value; \ virtual unsigned Precedence() const { \ - return value; \ + return Precedence_; \ } #define CYRightHand(value) \ @@ -619,10 +619,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; } @@ -632,35 +629,22 @@ struct CYCompound : void Output(CYOutput &out, CYFlags flags) const; }; +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 : @@ -961,6 +945,11 @@ struct CYVariable : { } + CYVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) + { + } + CYPrecedence(0) CYRightHand(false) @@ -1073,7 +1062,7 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; @@ -1164,20 +1153,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_; @@ -1187,15 +1175,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 { @@ -1210,15 +1214,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) { } @@ -1311,8 +1315,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1345,13 +1347,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) { @@ -1369,6 +1374,8 @@ struct CYNew : virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); }; +} } + struct CYCall : CYExpression { @@ -1540,7 +1547,6 @@ struct CYExpress : throw; } - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1590,7 +1596,6 @@ struct CYReturn : struct CYEmpty : CYStatement { - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1694,6 +1699,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 { @@ -1862,4 +1878,4 @@ CYAssignment_("&=", BitwiseAnd) CYAssignment_("^=", BitwiseXOr) CYAssignment_("|=", BitwiseOr) -#endif/*CYPARSER_HPP*/ +#endif/*CYCRIPT_PARSER_HPP*/