X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/01d0f64d1f396362fa532d04926bd90d3aa231c3..4f8fca8fa217661b5f75b1bb4c610c345d87fe98:/Parser.hpp?ds=sidebyside diff --git a/Parser.hpp b/Parser.hpp index c9614ed..0e4b51f 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,8 +32,6 @@ #include #include -#include "location.hh" - #include "List.hpp" #include "Pooling.hpp" #include "Options.hpp" @@ -463,12 +460,6 @@ struct CYBlock : virtual void Output(CYOutput &out, CYFlags flags) const; }; -enum CYState { - CYClear, - CYRestricted, - CYNewLine -}; - class CYStream : public std::istream { @@ -490,87 +481,6 @@ class CYStream : } }; -class CYDriver { - public: - void *scanner_; - - CYState state_; - std::stack in_; - - struct { - bool AtImplementation; - bool Function; - bool OpenBrace; - } no_; - - std::istream &data_; - - bool strict_; - bool commented_; - - 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_; - - private: - void ScannerInit(); - void ScannerDestroy(); - - public: - CYDriver(std::istream &data, 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); -}; - struct CYForInitialiser { virtual ~CYForInitialiser() { } @@ -602,7 +512,7 @@ struct CYExpression : CYClassName, CYThing { - virtual unsigned Precedence() const = 0; + virtual int Precedence() const = 0; virtual bool RightHand() const { return true; @@ -615,7 +525,7 @@ struct CYExpression : 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; @@ -646,8 +556,8 @@ struct CYExpression : } #define CYPrecedence(value) \ - static const unsigned Precedence_ = value; \ - virtual unsigned Precedence() const { \ + static const int Precedence_ = value; \ + virtual int Precedence() const { \ return Precedence_; \ } @@ -706,6 +616,11 @@ struct CYComprehension : { } + CYComprehension *Modify(CYComprehension *next) { + next_ = next; + return this; + } + virtual const char *Name() const = 0; virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; @@ -763,7 +678,8 @@ struct CYIfComprehension : { CYExpression *test_; - CYIfComprehension(CYExpression *test) : + CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) : + CYComprehension(next), test_(test) { } @@ -1419,7 +1335,7 @@ struct New : { } - virtual unsigned Precedence() const { + virtual int Precedence() const { return arguments_ == NULL ? 2 : 1; } @@ -1690,6 +1606,216 @@ struct CYFinally : virtual void Output(CYOutput &out) const; }; +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; +}; + +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 +{ + CYIdentifier *identifier_; + CYExpression *type_; + CYTypeModifier *modifier_; + + CYTypedIdentifier(CYIdentifier *identifier = NULL) : + identifier_(identifier), + type_(NULL), + modifier_(NULL) + { + } + + CYTypedIdentifier(CYExpression *type, CYTypeModifier *modifier = NULL) : + identifier_(NULL), + type_(type), + modifier_(modifier) + { + } + + inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) { + CYSetLast(modifier_) = modifier; + return this; + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +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 *statements_; + + CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) : + typed_(typed), + 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; +}; + +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; +}; + namespace cy { namespace Syntax {