X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/bf45251b6ef1230ffdbd79319b9c4a60c5ae6913..f1b5a47ffa92e12d17571d350a567e2035d75039:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index c26e6d2..110cf0d 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,20 +1,20 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2012 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,7 +24,6 @@ #include -#include #include #include #include @@ -33,13 +32,11 @@ #include #include -#include "location.hh" - #include "List.hpp" #include "Pooling.hpp" #include "Options.hpp" -class CYContext; +struct CYContext; struct CYThing { virtual ~CYThing() { @@ -350,11 +347,14 @@ struct CYProgram : }; struct CYNonLocal; +struct CYThisScope; struct CYContext { CYOptions &options_; CYScope *scope_; + CYThisScope *this_; + CYIdentifierUsageVector rename_; CYNonLocal *nonlocal_; @@ -364,6 +364,7 @@ struct CYContext { CYContext(CYOptions &options) : options_(options), scope_(NULL), + this_(NULL), nonlocal_(NULL), nextlocal_(NULL), unique_(0) @@ -414,6 +415,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 @@ -440,87 +460,25 @@ struct CYBlock : virtual void Output(CYOutput &out, CYFlags flags) const; }; -enum CYState { - CYClear, - CYRestricted, - CYNewLine -}; - -class CYDriver { - public: - void *scanner_; - - CYState state_; - bool nobrace_; - std::stack in_; - - const char *data_; - size_t size_; - FILE *file_; - - bool strict_; - - enum Condition { - RegExpCondition, - XMLContentCondition, - XMLTagCondition, - }; - - std::string filename_; - - struct Error { - bool warning_; - cy::location location_; - std::string message_; - }; - - typedef std::vector Errors; - - 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_; - +class CYStream : + public std::istream +{ private: - void ScannerInit(); - void ScannerDestroy(); + 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: - CYDriver(const std::string &filename = ""); - ~CYDriver(); - - Condition GetCondition(); - void SetCondition(Condition condition); - - void PushCondition(Condition condition); - void PopCondition(); - - void Warning(const cy::location &location, const char *message); + CYStream(const char *start, const char *end) : + std::istream(&buffer_), + buffer_(start, end) + { + } }; struct CYForInitialiser { @@ -1482,7 +1440,9 @@ struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; + CYNonLocal *nonlocal_; + CYThisScope this_; CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), @@ -1517,6 +1477,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 @@ -1623,6 +1600,140 @@ struct CYFinally : virtual void Output(CYOutput &out) const; }; +struct CYTypeModifier : + CYNext +{ + CYTypeModifier(CYTypeModifier *next) : + CYNext(next) + { + } + + virtual CYExpression *Replace(CYContext &context) = 0; +}; + +struct CYTypeArrayOf : + CYTypeModifier +{ + CYExpression *size_; + + CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + size_(size) + { + } + + CYPrecedence(2) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypeConstant : + CYTypeModifier +{ + CYTypeConstant(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(3) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypePointerTo : + CYTypeModifier +{ + CYTypePointerTo(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(3) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypeVariable : + CYTypeModifier +{ + CYExpression *expression_; + + CYTypeVariable(CYExpression *expression) : + CYTypeModifier(NULL), + expression_(expression) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypedIdentifier : + CYNext, + CYThing +{ + CYIdentifier *identifier_; + CYTypeModifier *type_; + + CYTypedIdentifier(CYIdentifier *identifier) : + identifier_(identifier), + type_(NULL) + { + } + + virtual void Output(CYOutput &out) const; +}; + +struct CYTypedParameter : + CYNext +{ + CYTypedIdentifier *typed_; + + CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : + CYNext(next), + typed_(typed) + { + } + + CYFunctionParameter *Parameters(CYContext &context); + CYExpression *TypeSignature(CYContext &context, CYExpression *prefix); +}; + +struct CYLambda : + CYExpression +{ + CYTypeModifier *type_; + CYTypedParameter *parameters_; + CYStatement *statements_; + + CYLambda(CYTypeModifier *type, CYTypedParameter *parameters, CYStatement *statements) : + type_(type), + parameters_(parameters), + statements_(statements) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeDefinition : + CYStatement +{ + CYTypedIdentifier *typed_; + + CYTypeDefinition(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + namespace cy { namespace Syntax {