X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/f95d2598051e347460e028286ff2d33e5825e548..efd689d86ad25be72d38cf012d90ffe0203850d1:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 697fbea..028ea0c 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,5 +1,5 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2014 Jay Freeman (saurik) + * Copyright (C) 2009-2015 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -22,8 +22,7 @@ #ifndef CYCRIPT_PARSER_HPP #define CYCRIPT_PARSER_HPP -#include - +#include #include #include #include @@ -33,6 +32,7 @@ #include #include "List.hpp" +#include "Location.hpp" #include "Pooling.hpp" #include "Options.hpp" @@ -46,10 +46,13 @@ struct CYThing { }; struct CYOutput { - std::ostream &out_; + std::streambuf &out_; + CYPosition position_; + CYOptions &options_; bool pretty_; unsigned indent_; + unsigned recent_; bool right_; enum { @@ -60,11 +63,12 @@ struct CYOutput { Terminated } mode_; - CYOutput(std::ostream &out, CYOptions &options) : + CYOutput(std::streambuf &out, CYOptions &options) : out_(out), options_(options), pretty_(false), indent_(0), + recent_(0), right_(false), mode_(NoMode) { @@ -73,6 +77,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); @@ -144,17 +167,33 @@ _finline CYFlags CYCenter(CYFlags flags) { return CYLeft(CYRight(flags)); } +enum CYCompactType { + CYCompactNone, + CYCompactLong, + CYCompactShort, +}; + +#define CYCompact(type) \ + virtual CYCompactType Compact() const { \ + return CYCompact ## type; \ + } + struct CYStatement : - CYNext + CYNext, + CYThing { virtual ~CYStatement() { } - void Single(CYOutput &out, CYFlags flags) const; + void Single(CYOutput &out, CYFlags flags, CYCompactType request) const; void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const; + 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; }; @@ -255,6 +294,8 @@ struct CYComment : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -271,6 +312,8 @@ struct CYLabel : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -312,33 +355,26 @@ typedef std::vector CYIdentifierUsageVector; struct CYScope { bool transparent_; - - CYContext &context_; - CYStatement *&statements_; - CYScope *parent_; CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - CYScope(bool transparent, CYContext &context, CYStatement *&statements); - virtual ~CYScope(); - - void Close(); + 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 Scope(CYContext &context, CYStatement *&statements); + void Close(CYContext &context, CYStatement *&statements); }; struct CYProgram : CYThing { - CYStatement *statements_; + CYStatement *code_; - CYProgram(CYStatement *statements) : - statements_(statements) + CYProgram(CYStatement *code) : + code_(code) { } @@ -374,14 +410,18 @@ struct CYContext { virtual ~CYContext() { } - template - void ReplaceAll(Type_ *&values) { - Type_ **last(&values); - CYForEach (next, values) { - Replace(*last = next); - if (*last != NULL) - last = &(*last)->next_; - } + void ReplaceAll(CYStatement *&statement) { + if (statement == NULL) + return; + CYStatement *next(statement->next_); + + Replace(statement); + ReplaceAll(next); + + if (statement == NULL) + statement = next; + else + statement->SetNext(next); } template @@ -435,29 +475,22 @@ struct CYThisScope : }; struct CYBlock : - CYStatement, - CYThing + CYStatement { - CYStatement *statements_; + CYStatement *code_; - CYBlock(CYStatement *statements) : - statements_(statements) + CYBlock(CYStatement *code) : + code_(code) { } - operator CYStatement *() const { - return statements_; - } - - void AddPrev(CYStatement *statement) { - CYSetLast(statement) = statements_; - statements_ = statement; - } + CYCompact(Short) virtual CYStatement *Replace(CYContext &context); - virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYForInitialiser { @@ -481,6 +514,8 @@ struct CYForInInitialiser { virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; +struct CYFunctionParameter; + struct CYNumber; struct CYString; @@ -515,6 +550,8 @@ struct CYExpression : return NULL; } + virtual CYFunctionParameter *Parameter() const; + virtual CYNumber *Number(CYContext &context) { return NULL; } @@ -550,13 +587,12 @@ struct CYCompound : CYExpression *expression_; CYExpression *next_; - CYCompound(CYExpression *expression, CYExpression *next = NULL) : + CYCompound(CYExpression *expression, CYExpression *next) : expression_(expression), next_(next) { - if (expression_ == NULL) - throw; _assert(expression_ != NULL); + _assert(next != NULL); } CYPrecedence(17) @@ -564,7 +600,23 @@ struct CYCompound : virtual CYExpression *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; - virtual CYExpression *Primitive(CYContext &context); + virtual CYFunctionParameter *Parameter() const; +}; + +struct CYParenthetical : + CYExpression +{ + CYExpression *expression_; + + CYParenthetical(CYExpression *expression) : + expression_(expression) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace(CYContext &context); + void Output(CYOutput &out, CYFlags flags) const; }; struct CYDeclaration; @@ -581,7 +633,7 @@ struct CYFunctionParameter : { } - void Replace(CYContext &context, CYBlock &code); + void Replace(CYContext &context, CYStatement *&statements); void Output(CYOutput &out) const; }; @@ -910,6 +962,8 @@ struct CYVariable : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYFunctionParameter *Parameter() const; }; struct CYPrefix : @@ -1035,11 +1089,11 @@ struct CYClause : CYNext { CYExpression *case_; - CYStatement *statements_; + CYStatement *code_; - CYClause(CYExpression *_case, CYStatement *statements) : + CYClause(CYExpression *_case, CYStatement *code) : case_(_case), - statements_(statements) + code_(code) { } @@ -1132,7 +1186,7 @@ struct CYDeclarations : void Replace(CYContext &context); - CYCompound *Compound(CYContext &context); + CYExpression *Expression(CYContext &context); CYProperty *Property(CYContext &context); CYArgument *Argument(CYContext &context); CYFunctionParameter *Parameter(CYContext &context); @@ -1151,7 +1205,7 @@ struct CYForDeclarations : { } - virtual CYCompound *Replace(CYContext &context); + virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1165,6 +1219,8 @@ struct CYVar : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1181,6 +1237,8 @@ struct CYLetStatement : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1201,6 +1259,8 @@ struct CYFor : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1219,6 +1279,8 @@ struct CYForIn : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1237,6 +1299,8 @@ struct CYForOf : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1387,8 +1451,12 @@ struct CYIf : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYDoWhile : @@ -1403,6 +1471,8 @@ struct CYDoWhile : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1419,6 +1489,8 @@ struct CYWhile : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1427,16 +1499,18 @@ struct CYWhile : 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), - nonlocal_(NULL) + code_(code), + nonlocal_(NULL), + implicit_(false) { } @@ -1453,8 +1527,8 @@ struct CYFunctionExpression : CYFunction, CYExpression { - CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : - CYFunction(name, parameters, statements) + CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : + CYFunction(name, parameters, code) { } @@ -1470,8 +1544,8 @@ struct CYFatArrow : CYFunction, CYExpression { - CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) : - CYFunction(NULL, parameters, statements) + CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) : + CYFunction(NULL, parameters, code) { } @@ -1486,8 +1560,8 @@ struct CYFatArrow : struct CYRubyProc : CYFunctionExpression { - CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) : - CYFunctionExpression(NULL, parameters, statements) + CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) : + CYFunctionExpression(NULL, parameters, code) { } @@ -1500,11 +1574,13 @@ 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; }; @@ -1521,8 +1597,12 @@ struct CYExpress : throw; } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYContinue : @@ -1535,6 +1615,8 @@ struct CYContinue : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1549,6 +1631,8 @@ struct CYBreak : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1563,6 +1647,8 @@ struct CYReturn : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1570,6 +1656,8 @@ struct CYReturn : struct CYEmpty : CYStatement { + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1577,10 +1665,10 @@ struct CYEmpty : struct CYFinally : CYThing { - CYBlock code_; + CYStatement *code_; - CYFinally(CYStatement *statements) : - code_(statements) + CYFinally(CYStatement *code) : + code_(code) { } @@ -1689,6 +1777,8 @@ struct CYTypeShort : virtual void Output(CYOutput &out) const; }; +struct CYTypeFunctionWith; + struct CYTypeModifier : CYNext { @@ -1704,6 +1794,8 @@ struct CYTypeModifier : 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 : @@ -1769,11 +1861,13 @@ struct CYTypedIdentifier : CYNext, CYThing { + CYLocation location_; CYIdentifier *identifier_; CYTypeSpecifier *specifier_; CYTypeModifier *modifier_; - CYTypedIdentifier(CYIdentifier *identifier = NULL) : + CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) : + location_(location), identifier_(identifier), specifier_(NULL), modifier_(NULL) @@ -1794,6 +1888,8 @@ struct CYTypedIdentifier : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out) const; + + CYTypeFunctionWith *Function(); }; struct CYEncodedType : @@ -1836,12 +1932,12 @@ struct CYLambda : { CYTypedIdentifier *typed_; CYTypedParameter *parameters_; - CYStatement *statements_; + CYStatement *code_; - CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) : + CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) : typed_(typed), parameters_(parameters), - statements_(statements) + code_(code) { } @@ -1877,6 +1973,26 @@ struct CYImport : { } + 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; }; @@ -1891,6 +2007,8 @@ struct CYTypeDefinition : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1927,6 +2045,8 @@ struct CYTypeFunctionWith : virtual CYExpression *Replace_(CYContext &context, CYExpression *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + + virtual CYTypeFunctionWith *Function() { return this; } }; namespace cy { @@ -1936,11 +2056,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) { } @@ -1951,17 +2071,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; }; @@ -1976,6 +2098,8 @@ struct Throw : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1994,6 +2118,8 @@ struct CYWith : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2010,6 +2136,8 @@ struct CYSwitch : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2021,6 +2149,8 @@ struct CYDebugger : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; };