X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/12e37ba3b9b322dd5b0483b45008e78e12aaa670..7c4c728ddba1afa034d842723580e8ead8f71ac3:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 2c534cc..a7ac66a 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -22,8 +22,7 @@ #ifndef CYCRIPT_PARSER_HPP #define CYCRIPT_PARSER_HPP -#include - +#include #include #include #include @@ -40,17 +39,17 @@ 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 { @@ -61,11 +60,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) { @@ -74,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); @@ -91,9 +110,6 @@ struct CYOutput { struct CYPropertyName { virtual void PropertyName(CYOutput &out) const = 0; - - virtual ~CYPropertyName() { - } }; struct CYExpression; @@ -145,19 +161,28 @@ _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, 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: @@ -194,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; }; @@ -260,6 +282,8 @@ struct CYComment : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -276,6 +300,8 @@ struct CYLabel : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -369,9 +395,6 @@ struct CYContext { { } - virtual ~CYContext() { - } - void ReplaceAll(CYStatement *&statement) { if (statement == NULL) return; @@ -446,6 +469,8 @@ struct CYBlock : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; @@ -454,17 +479,11 @@ struct CYBlock : }; struct CYForInitialiser { - virtual ~CYForInitialiser() { - } - virtual CYExpression *Replace(CYContext &context) = 0; virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYForInInitialiser { - virtual ~CYForInInitialiser() { - } - virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0; @@ -834,14 +853,8 @@ struct CYRegEx : }; struct CYNull : - CYWord, CYTrivial { - CYNull() : - CYWord("null") - { - } - virtual CYNumber *Number(CYContext &context); virtual CYString *String(CYContext &context); @@ -849,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; }; @@ -869,14 +876,8 @@ struct CYBoolean : }; struct CYFalse : - CYWord, CYBoolean { - CYFalse() : - CYWord("false") - { - } - virtual bool Value() const { return false; } @@ -886,14 +887,8 @@ struct CYFalse : }; struct CYTrue : - CYWord, CYBoolean { - CYTrue() : - CYWord("true") - { - } - virtual bool Value() const { return true; } @@ -1035,15 +1030,6 @@ struct CYArgument : void Output(CYOutput &out) const; }; -struct CYBlank : - public CYWord -{ - CYBlank() : - CYWord("") - { - } -}; - struct CYClause : CYThing, CYNext @@ -1179,6 +1165,8 @@ struct CYVar : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1195,6 +1183,8 @@ struct CYLetStatement : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1215,6 +1205,8 @@ struct CYFor : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1233,6 +1225,8 @@ struct CYForIn : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1251,6 +1245,8 @@ struct CYForOf : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1401,6 +1397,8 @@ struct CYIf : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; @@ -1419,6 +1417,8 @@ struct CYDoWhile : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1435,6 +1435,8 @@ struct CYWhile : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1458,9 +1460,6 @@ struct CYFunction { { } - virtual ~CYFunction() { - } - void Inject(CYContext &context); virtual void Replace_(CYContext &context, bool outer); virtual void Output(CYOutput &out, CYFlags flags) const; @@ -1523,6 +1522,8 @@ struct CYFunctionStatement : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1539,6 +1540,8 @@ struct CYExpress : throw; } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; @@ -1555,6 +1558,8 @@ struct CYContinue : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1569,6 +1574,8 @@ struct CYBreak : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1583,6 +1590,8 @@ struct CYReturn : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1590,6 +1599,8 @@ struct CYReturn : struct CYEmpty : CYStatement { + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1905,6 +1916,8 @@ struct CYImport : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1921,6 +1934,8 @@ struct CYExternal : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1935,6 +1950,8 @@ struct CYTypeDefinition : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2008,6 +2025,8 @@ struct Try : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2022,6 +2041,8 @@ struct Throw : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2040,6 +2061,8 @@ struct CYWith : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2056,6 +2079,8 @@ struct CYSwitch : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2067,6 +2092,8 @@ struct CYDebugger : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; };