X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/9561f209ba7d08b95e2bd3b86b6338bd2537ea55..7c4c728ddba1afa034d842723580e8ead8f71ac3:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index a76faaa..a7ac66a 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,88 +1,56 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2015 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU Affero 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: - * - * 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. - * - * 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. -*/ + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program 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 Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . +**/ /* }}} */ -#ifndef CYPARSER_HPP -#define CYPARSER_HPP - -// XXX: wtf is this here?! -#define CYPA 16 - -#include +#ifndef CYCRIPT_PARSER_HPP +#define CYCRIPT_PARSER_HPP +#include #include #include +#include +#include +#include #include -#include "location.hh" +#include "List.hpp" +#include "Location.hpp" #include "Pooling.hpp" +#include "Options.hpp" -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() { - } - virtual void Output(struct CYOutput &out) const = 0; }; struct CYOutput { - std::ostream &out_; + std::streambuf &out_; + CYPosition position_; + + CYOptions &options_; bool pretty_; unsigned indent_; + unsigned recent_; + bool right_; enum { NoMode, @@ -92,10 +60,13 @@ struct CYOutput { Terminated } mode_; - CYOutput(std::ostream &out) : + CYOutput(std::streambuf &out, CYOptions &options) : out_(out), + options_(options), pretty_(false), indent_(0), + recent_(0), + right_(false), mode_(NoMode) { } @@ -103,6 +74,25 @@ struct CYOutput { void Check(char value); void Terminate(); + _finline void operator ()(char value) { + _assert(out_.sputc(value) != EOF); + recent_ = indent_; + if (value == '\n') + position_.lines(1); + else + position_.columns(1); + } + + _finline void operator ()(const char *data, std::streamsize size) { + _assert(out_.sputn(data, size) == size); + recent_ = indent_; + position_.columns(size); + } + + _finline void operator ()(const char *data) { + return operator ()(data, strlen(data)); + } + CYOutput &operator <<(char rhs); CYOutput &operator <<(const char *rhs); @@ -120,12 +110,10 @@ struct CYOutput { struct CYPropertyName { virtual void PropertyName(CYOutput &out) const = 0; - - virtual ~CYPropertyName() { - } }; struct CYExpression; +struct CYAssignment; enum CYNeeded { CYNever = -1, @@ -145,35 +133,58 @@ enum CYFlags { CYNoBF = (CYNoBrace | CYNoFunction), }; -struct CYContext { - apr_pool_t *pool_; +_finline CYFlags operator ~(CYFlags rhs) { + return static_cast(~static_cast(rhs)); +} - CYContext(apr_pool_t *pool) : - pool_(pool) - { - } +_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) { + return static_cast(static_cast(lhs) & static_cast(rhs)); +} - template - void Replace(Type_ *&value) { - if (value != NULL) - while (Type_ *replace = value->Replace(*this)) - value = replace; - } +_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)); +} + +enum CYCompactType { + CYCompactNone, + CYCompactLong, + CYCompactShort, }; -struct CYStatement : - CYNext -{ - virtual ~CYStatement() { +#define CYCompact(type) \ + virtual CYCompactType Compact() const { \ + return CYCompact ## type; \ } - void Single(CYOutput &out, CYFlags flags) const; +struct CYStatement : + CYNext, + CYThing +{ + void Single(CYOutput &out, CYFlags flags, CYCompactType request) const; void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const; - - CYStatement *ReplaceAll(CYContext &context); + virtual void Output(CYOutput &out) const; virtual CYStatement *Replace(CYContext &context) = 0; + virtual CYCompactType Compact() const = 0; + virtual CYStatement *Return(); + private: virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; @@ -208,9 +219,6 @@ struct CYStatements { }; struct CYClassName { - virtual ~CYClassName() { - } - virtual CYExpression *ClassName(CYContext &context, bool object) = 0; virtual void ClassName(CYOutput &out, bool object) const = 0; }; @@ -227,10 +235,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 +248,44 @@ 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) { } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYLabel : @@ -263,154 +300,237 @@ struct CYLabel : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYProgram : - CYThing +struct CYCStringLess : + std::binary_function { - CYStatement *statements_; + _finline bool operator ()(const char *lhs, const char *rhs) const { + return strcmp(lhs, rhs) < 0; + } +}; - CYProgram(CYStatement *statements) : - statements_(statements) - { +struct CYIdentifierValueLess : + std::binary_function +{ + _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const { + return CYCStringLess()(lhs->Word(), rhs->Word()); } +}; - virtual void Replace(CYContext &context); +enum CYIdentifierFlags { + CYIdentifierArgument, + CYIdentifierVariable, + CYIdentifierOther, + CYIdentifierMagic, + CYIdentifierCatch, +}; - virtual void Output(CYOutput &out) const; +typedef std::set CYCStringSet; +typedef std::set CYIdentifierValueSet; +typedef std::map CYIdentifierAddressFlagsMap; + +struct CYIdentifierUsage { + CYIdentifier *identifier_; + size_t usage_; }; -struct CYBlock : - CYStatement, +typedef std::vector CYIdentifierUsageVector; + +struct CYScope { + bool transparent_; + CYScope *parent_; + + CYIdentifierAddressFlagsMap internal_; + CYIdentifierValueSet identifiers_; + + CYScope(bool transparent, CYContext &context); + + void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags); + virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier); + void Merge(CYContext &context, CYIdentifier *identifier); + void Close(CYContext &context, CYStatement *&statements); +}; + +struct CYProgram : CYThing { - CYStatement *statements_; + CYStatement *code_; - CYBlock(CYStatement *statements) : - statements_(statements) + CYProgram(CYStatement *code) : + code_(code) { } - operator CYStatement *() const { - return statements_; - } - - virtual CYStatement *Replace(CYContext &context); - + virtual void Replace(CYContext &context); virtual void Output(CYOutput &out) const; - virtual void Output(CYOutput &out, CYFlags flags) const; -}; - -enum CYState { - CYClear, - CYRestricted, - CYNewLine }; -class CYDriver { - public: - CYPool pool_; +struct CYNonLocal; +struct CYThisScope; - CYState state_; - void *scanner_; +struct CYContext { + CYOptions &options_; - const char *data_; - size_t size_; - FILE *file_; + CYScope *scope_; + CYThisScope *this_; - bool strict_; + CYIdentifierUsageVector rename_; - enum Condition { - RegExpCondition, - XMLContentCondition, - XMLTagCondition, - }; + CYNonLocal *nonlocal_; + CYNonLocal *nextlocal_; + unsigned unique_; - std::string filename_; + CYContext(CYOptions &options) : + options_(options), + scope_(NULL), + this_(NULL), + nonlocal_(NULL), + nextlocal_(NULL), + unique_(0) + { + } - struct Error { - bool warning_; - cy::location location_; - std::string message_; - }; + void ReplaceAll(CYStatement *&statement) { + if (statement == NULL) + return; + CYStatement *next(statement->next_); - typedef std::vector Errors; + Replace(statement); + ReplaceAll(next); - CYProgram *program_; - Errors errors_; + if (statement == NULL) + statement = next; + else + statement->SetNext(next); + } - private: - void ScannerInit(); - void ScannerDestroy(); + template + void Replace(Type_ *&value) { + for (;;) if (value == NULL) + break; + else { + Type_ *replace(value->Replace(*this)); + if (replace != value) + value = replace; + else break; + } + } - public: - CYDriver(const std::string &filename); - ~CYDriver(); + void NonLocal(CYStatement *&statements); + CYIdentifier *Unique(); +}; - Condition GetCondition(); - void SetCondition(Condition condition); +struct CYNonLocal { + CYIdentifier *identifier_; - void PushCondition(Condition condition); - void PopCondition(); + CYNonLocal() : + identifier_(NULL) + { + } - void Warning(const cy::location &location, const char *message); + CYIdentifier *Target(CYContext &context) { + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; + } }; -struct CYForInitialiser { - virtual ~CYForInitialiser() { +struct CYThisScope : + CYNext +{ + CYIdentifier *identifier_; + + CYThisScope() : + identifier_(NULL) + { } - virtual void For(CYOutput &out) const = 0; + CYIdentifier *Identifier(CYContext &context) { + if (next_ != NULL) + return next_->Identifier(context); + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; + } }; -struct CYForInInitialiser { - virtual ~CYForInInitialiser() { +struct CYBlock : + CYStatement +{ + CYStatement *code_; + + CYBlock(CYStatement *code) : + code_(code) + { } + CYCompact(Short) + + virtual CYStatement *Replace(CYContext &context); + + virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); +}; + +struct CYForInitialiser { + virtual CYExpression *Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out, CYFlags flags) const = 0; +}; + +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 CYFunctionParameter; + struct CYNumber; struct CYString; struct CYExpression : - CYNext, CYForInitialiser, CYForInInitialiser, 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 CYStatement *ForEachIn(CYContext &out, CYExpression *value); - 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; + void Output(CYOutput &out, int precedence, CYFlags flags) const; 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; + return NULL; } + virtual CYFunctionParameter *Parameter() const; + virtual CYNumber *Number(CYContext &context) { return NULL; } @@ -430,8 +550,9 @@ struct CYExpression : } #define CYPrecedence(value) \ - virtual unsigned Precedence() const { \ - return value; \ + static const int Precedence_ = value; \ + virtual int Precedence() const { \ + return Precedence_; \ } #define CYRightHand(value) \ @@ -442,46 +563,73 @@ struct CYExpression : struct CYCompound : CYExpression { - CYExpression *expressions_; + CYExpression *expression_; + CYExpression *next_; - CYCompound(CYExpression *expressions) : - expressions_(expressions) + CYCompound(CYExpression *expression, CYExpression *next) : + expression_(expression), + next_(next) { + _assert(expression_ != NULL); + _assert(next != NULL); } - void AddPrev(CYExpression *expression) { - CYExpression *last(expression); - while (last->next_ != NULL) - last = last->next_; - last->SetNext(expressions_); - expressions_ = expression; + CYPrecedence(17) + + virtual CYExpression *Replace(CYContext &context); + void Output(CYOutput &out, CYFlags flags) const; + + virtual CYFunctionParameter *Parameter() const; +}; + +struct CYParenthetical : + CYExpression +{ + CYExpression *expression_; + + CYParenthetical(CYExpression *expression) : + expression_(expression) + { } - CYPrecedence(17) + CYPrecedence(0) virtual CYExpression *Replace(CYContext &context); 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) + initialiser_(initialiser) { } - virtual void Output(CYOutput &out) const; + void Replace(CYContext &context, CYStatement *&statements); + void Output(CYOutput &out) const; }; struct CYComprehension : CYNext, CYThing { + CYComprehension(CYComprehension *next = NULL) : + CYNext(next) + { + } + + CYComprehension *Modify(CYComprehension *next) { + next_ = next; + return this; + } + virtual const char *Name() const = 0; virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; @@ -496,14 +644,15 @@ 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) { } virtual const char *Name() const { - return name_->Value(); + return name_->Word(); } virtual CYFunctionParameter *Parameter(CYContext &context) const; @@ -511,20 +660,21 @@ 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) { } virtual const char *Name() const { - return name_->Value(); + return name_->Word(); } virtual CYFunctionParameter *Parameter(CYContext &context) const; @@ -537,7 +687,8 @@ struct CYIfComprehension : { CYExpression *test_; - CYIfComprehension(CYExpression *test) : + CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) : + CYComprehension(next), test_(test) { } @@ -574,6 +725,10 @@ struct CYLiteral : { CYPrecedence(0) CYRightHand(false) + + virtual CYExpression *Primitive(CYContext &context) { + return this; + } }; struct CYTrivial : @@ -639,7 +794,7 @@ struct CYString : } CYString(const CYWord *word) : - value_(word->Value()), + value_(word->Word()), size_(strlen(value_)) { } @@ -698,14 +853,8 @@ struct CYRegEx : }; struct CYNull : - CYWord, CYTrivial { - CYNull() : - CYWord("null") - { - } - virtual CYNumber *Number(CYContext &context); virtual CYString *String(CYContext &context); @@ -713,14 +862,8 @@ struct CYNull : }; struct CYThis : - CYWord, CYMagic { - CYThis() : - CYWord("this") - { - } - virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -733,14 +876,8 @@ struct CYBoolean : }; struct CYFalse : - CYWord, CYBoolean { - CYFalse() : - CYWord("false") - { - } - virtual bool Value() const { return false; } @@ -750,14 +887,8 @@ struct CYFalse : }; struct CYTrue : - CYWord, CYBoolean { - CYTrue() : - CYWord("true") - { - } - virtual bool Value() const { return true; } @@ -776,11 +907,18 @@ struct CYVariable : { } + CYVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) + { + } + CYPrecedence(0) CYRightHand(false) virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYFunctionParameter *Parameter() const; }; struct CYPrefix : @@ -888,29 +1026,20 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; -struct CYBlank : - public CYWord -{ - CYBlank() : - CYWord("") - { - } -}; - struct CYClause : CYThing, CYNext { CYExpression *case_; - CYStatement *statements_; + CYStatement *code_; - CYClause(CYExpression *_case, CYStatement *statements) : + CYClause(CYExpression *_case, CYStatement *code) : case_(_case), - statements_(statements) + code_(code) { } @@ -979,18 +1108,18 @@ struct CYDeclaration : } virtual void ForIn(CYOutput &out, CYFlags flags) const; + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); - virtual const char *ForEachIn() const; - virtual CYExpression *ForEachIn(CYContext &out); + virtual CYExpression *Replace(CYContext &context); - void Replace(CYContext &context); + virtual CYAssignment *Assignment(CYContext &context); + CYVariable *Variable(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYDeclarations : CYNext, - CYForInitialiser, CYThing { CYDeclaration *declaration_; @@ -1001,15 +1130,31 @@ struct CYDeclarations : { } - virtual void For(CYOutput &out) const; - void Replace(CYContext &context); + + CYExpression *Expression(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 CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYVar : CYStatement { @@ -1020,22 +1165,26 @@ struct CYVar : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); 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) { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1056,6 +1205,8 @@ struct CYFor : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1074,24 +1225,28 @@ struct CYForIn : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); 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) { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1101,7 +1256,7 @@ struct CYObject : { CYProperty *properties_; - CYObject(CYProperty *properties) : + CYObject(CYProperty *properties = NULL) : properties_(properties) { } @@ -1125,8 +1280,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1159,19 +1312,22 @@ 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) { } - virtual unsigned Precedence() const { + virtual int Precedence() const { return arguments_ == NULL ? 2 : 1; } @@ -1179,8 +1335,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,14 +1358,37 @@ struct CYCall : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); }; -struct CYIf : - CYStatement -{ - CYExpression *test_; - CYStatement *true_; - CYStatement *false_; +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 : + CYStatement +{ + CYExpression *test_; + CYStatement *true_; + CYStatement *false_; CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) : test_(test), @@ -1214,8 +1397,12 @@ struct CYIf : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYDoWhile : @@ -1230,6 +1417,8 @@ struct CYDoWhile : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1246,35 +1435,60 @@ struct CYWhile : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); 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_; + CYStatement *code_; + + CYNonLocal *nonlocal_; + bool implicit_; + CYThisScope this_; - CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : + CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : name_(name), parameters_(parameters), - code_(statements) + code_(code), + nonlocal_(NULL), + implicit_(false) { } - virtual ~CYFunction() { + 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 +{ + CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : + CYFunction(name, parameters, code) + { } - virtual void Replace_(CYContext &context); + CYPrecedence(0) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYFunctionExpression : +// XXX: this should derive from CYAnonymousFunction +struct CYFatArrow : CYFunction, CYExpression { - CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : - CYFunction(name, parameters, statements) + CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) : + CYFunction(NULL, parameters, code) { } @@ -1285,15 +1499,31 @@ struct CYFunctionExpression : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should derive from CYAnonymousFunctionExpression +struct CYRubyProc : + CYFunctionExpression +{ + CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) : + CYFunctionExpression(NULL, parameters, code) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +// XXX: this should derive from CYNamedFunction struct CYFunctionStatement : CYFunction, CYStatement { - CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : - CYFunction(name, parameters, statements) + CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : + CYFunction(name, parameters, code) { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1306,10 +1536,16 @@ struct CYExpress : CYExpress(CYExpression *expression) : expression_(expression) { + if (expression_ == NULL) + throw; } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYContinue : @@ -1322,6 +1558,8 @@ struct CYContinue : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1336,6 +1574,8 @@ struct CYBreak : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1350,6 +1590,8 @@ struct CYReturn : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1357,6 +1599,8 @@ struct CYReturn : struct CYEmpty : CYStatement { + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1364,10 +1608,10 @@ struct CYEmpty : struct CYFinally : CYThing { - CYBlock code_; + CYStatement *code_; - CYFinally(CYStatement *statements) : - code_(statements) + CYFinally(CYStatement *code) : + code_(code) { } @@ -1375,6 +1619,379 @@ struct CYFinally : virtual void Output(CYOutput &out) const; }; +struct CYTypeSpecifier : + CYThing +{ + virtual CYExpression *Replace(CYContext &context) = 0; +}; + +struct CYTypeError : + CYTypeSpecifier +{ + CYTypeError() { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeVoid : + CYTypeSpecifier +{ + CYTypeVoid() { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeVariable : + CYTypeSpecifier +{ + CYIdentifier *name_; + + CYTypeVariable(CYIdentifier *name) : + name_(name) + { + } + + CYTypeVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeUnsigned : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeUnsigned(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeSigned : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeSigned(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeLong : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeLong(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeShort : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeShort(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeFunctionWith; + +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; + + virtual CYTypeFunctionWith *Function() { return NULL; } +}; + +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 +{ + CYLocation location_; + CYIdentifier *identifier_; + CYTypeSpecifier *specifier_; + CYTypeModifier *modifier_; + + CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) : + location_(location), + identifier_(identifier), + specifier_(NULL), + modifier_(NULL) + { + } + + CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) : + identifier_(NULL), + specifier_(specifier), + modifier_(modifier) + { + } + + inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) { + CYSetLast(modifier_) = modifier; + return this; + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; + + CYTypeFunctionWith *Function(); +}; + +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 *code_; + + CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) : + typed_(typed), + parameters_(parameters), + code_(code) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYModule : + CYNext, + CYThing +{ + CYWord *part_; + + CYModule(CYWord *part, CYModule *next = NULL) : + CYNext(next), + part_(part) + { + } + + CYString *Replace(CYContext &context, const char *separator) const; + void Output(CYOutput &out) const; +}; + +struct CYImport : + CYStatement +{ + CYModule *module_; + + CYImport(CYModule *module) : + module_(module) + { + } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYExternal : + CYStatement +{ + CYString *abi_; + CYTypedIdentifier *typed_; + + CYExternal(CYString *abi, CYTypedIdentifier *typed) : + abi_(abi), + typed_(typed) + { + } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeDefinition : + CYStatement +{ + CYTypedIdentifier *typed_; + + CYTypeDefinition(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + CYCompact(None) + + 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; + + virtual CYTypeFunctionWith *Function() { return this; } +}; + namespace cy { namespace Syntax { @@ -1382,11 +1999,11 @@ struct Catch : CYThing { CYIdentifier *name_; - CYBlock code_; + CYStatement *code_; - Catch(CYIdentifier *name, CYStatement *statements) : + Catch(CYIdentifier *name, CYStatement *code) : name_(name), - code_(statements) + code_(code) { } @@ -1397,17 +2014,19 @@ struct Catch : struct Try : CYStatement { - CYBlock code_; + CYStatement *code_; Catch *catch_; CYFinally *finally_; - Try(CYStatement *statements, Catch *_catch, CYFinally *finally) : - code_(statements), + Try(CYStatement *code, Catch *_catch, CYFinally *finally) : + code_(code), catch_(_catch), finally_(finally) { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1417,11 +2036,13 @@ struct Throw : { CYExpression *value_; - Throw(CYExpression *value) : + Throw(CYExpression *value = NULL) : value_(value) { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1440,6 +2061,8 @@ struct CYWith : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1456,6 +2079,21 @@ struct CYSwitch : { } + CYCompact(Long) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYDebugger : + CYStatement +{ + CYDebugger() + { + } + + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1591,7 +2229,7 @@ CYPrefix_(false, "-", Negate) CYPrefix_(false, "~", BitwiseNot) CYPrefix_(false, "!", LogicalNot) -CYInfix_(false, 5, "*", Multiply) +CYInfix_(false, 5, "*", Multiply, CYReplace) CYInfix_(false, 5, "/", Divide) CYInfix_(false, 5, "%", Modulus) CYInfix_(false, 6, "+", Add, CYReplace) @@ -1628,4 +2266,4 @@ CYAssignment_("&=", BitwiseAnd) CYAssignment_("^=", BitwiseXOr) CYAssignment_("|=", BitwiseOr) -#endif/*CYPARSER_HPP*/ +#endif/*CYCRIPT_PARSER_HPP*/