#ifndef CYCRIPT_PARSER_HPP
#define CYCRIPT_PARSER_HPP
-#include <iostream>
-
+#include <streambuf>
#include <string>
#include <vector>
#include <map>
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 {
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)
{
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);
struct CYPropertyName {
virtual void PropertyName(CYOutput &out) const = 0;
-
- virtual ~CYPropertyName() {
- }
};
struct CYExpression;
return CYLeft(CYRight(flags));
}
+enum CYCompactType {
+ CYCompactNone,
+ CYCompactLong,
+ CYCompactShort,
+};
+
+#define CYCompact(type) \
+ virtual CYCompactType Compact() const { \
+ return CYCompact ## type; \
+ }
+
struct CYStatement :
CYNext<CYStatement>,
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;
};
};
struct CYClassName {
- virtual ~CYClassName() {
- }
-
virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
virtual void ClassName(CYOutput &out, bool object) const = 0;
};
CYIdentifier *Replace(CYContext &context);
};
-struct CYComment :
- CYStatement
-{
- const char *value_;
-
- CYComment(const char *value) :
- value_(value)
- {
- }
-
- virtual CYStatement *Replace(CYContext &context);
- virtual void Output(CYOutput &out, CYFlags flags) const;
-};
-
struct CYLabel :
CYStatement
{
{
}
+ CYCompact(Short)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
void Close(CYContext &context, CYStatement *&statements);
};
-struct CYProgram :
+struct CYScript :
CYThing
{
CYStatement *code_;
- CYProgram(CYStatement *code) :
+ CYScript(CYStatement *code) :
code_(code)
{
}
{
}
- virtual ~CYContext() {
- }
-
void ReplaceAll(CYStatement *&statement) {
if (statement == NULL)
return;
{
}
+ CYCompact(Short)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
-};
-struct CYForInitialiser {
- virtual ~CYForInitialiser() {
- }
+ virtual CYStatement *Return();
+};
+struct CYForInitializer {
virtual CYExpression *Replace(CYContext &context) = 0;
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
-struct CYForInInitialiser {
- virtual ~CYForInInitialiser() {
- }
-
+struct CYForInInitializer {
virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
struct CYString;
struct CYExpression :
- CYForInitialiser,
- CYForInInitialiser,
+ CYForInitializer,
+ CYForInInitializer,
CYClassName,
CYThing
{
CYNext<CYFunctionParameter>,
CYThing
{
- CYForInInitialiser *initialiser_;
+ CYForInInitializer *initialiser_;
- CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
+ CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
CYNext<CYFunctionParameter>(next),
initialiser_(initialiser)
{
virtual void PropertyName(CYOutput &out) const;
};
+struct CYElementValue;
+
+struct CYSpan :
+ CYNext<CYSpan>
+{
+ CYExpression *expression_;
+ CYString *string_;
+
+ CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
+ CYNext<CYSpan>(next),
+ expression_(expression),
+ string_(string)
+ {
+ }
+
+ CYElementValue *Replace(CYContext &context);
+};
+
+struct CYTemplate :
+ CYExpression
+{
+ CYString *string_;
+ CYSpan *spans_;
+
+ CYTemplate(CYString *string, CYSpan *spans) :
+ string_(string),
+ spans_(spans)
+ {
+ }
+
+ CYPrecedence(0)
+ CYRightHand(false)
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYNumber :
CYTrivial,
CYPropertyName
CYTrivial
{
const char *value_;
+ size_t size_;
- CYRegEx(const char *value) :
- value_(value)
+ CYRegEx(const char *value, size_t size) :
+ value_(value),
+ size_(size)
{
}
};
struct CYNull :
- CYWord,
CYTrivial
{
- CYNull() :
- CYWord("null")
- {
- }
-
virtual CYNumber *Number(CYContext &context);
virtual CYString *String(CYContext &context);
};
struct CYThis :
- CYWord,
CYMagic
{
- CYThis() :
- CYWord("this")
- {
- }
-
virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
};
struct CYFalse :
- CYWord,
CYBoolean
{
- CYFalse() :
- CYWord("false")
- {
- }
-
virtual bool Value() const {
return false;
}
};
struct CYTrue :
- CYWord,
CYBoolean
{
- CYTrue() :
- CYWord("true")
- {
- }
-
virtual bool Value() const {
return true;
}
void Output(CYOutput &out) const;
};
-struct CYBlank :
- public CYWord
-{
- CYBlank() :
- CYWord("")
- {
- }
-};
-
struct CYClause :
CYThing,
CYNext<CYClause>
};
struct CYElement :
- CYNext<CYElement>,
CYThing
+{
+ virtual bool Elision() const = 0;
+
+ virtual void Replace(CYContext &context) = 0;
+};
+
+struct CYElementValue :
+ CYNext<CYElement>,
+ CYElement
{
CYExpression *value_;
- CYElement(CYExpression *value, CYElement *next) :
+ CYElementValue(CYExpression *value, CYElement *next) :
CYNext<CYElement>(next),
value_(value)
{
}
- void Replace(CYContext &context);
- void Output(CYOutput &out) const;
+ virtual bool Elision() const {
+ return value_ == NULL;
+ }
+
+ virtual void Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYElementSpread :
+ CYElement
+{
+ CYExpression *value_;
+
+ CYElementSpread(CYExpression *value) :
+ value_(value)
+ {
+ }
+
+ virtual bool Elision() const {
+ return false;
+ }
+
+ virtual void Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
};
struct CYArray :
};
struct CYDeclaration :
- CYForInInitialiser
+ CYForInInitializer
{
CYIdentifier *identifier_;
CYExpression *initialiser_;
};
struct CYForDeclarations :
- CYForInitialiser
+ CYForInitializer
{
CYDeclarations *declarations_;
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(Long)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYFor :
CYStatement
{
- CYForInitialiser *initialiser_;
+ CYForInitializer *initialiser_;
CYExpression *test_;
CYExpression *increment_;
CYStatement *code_;
- CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
+ CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
initialiser_(initialiser),
test_(test),
increment_(increment),
{
}
+ CYCompact(Long)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYForIn :
CYStatement
{
- CYForInInitialiser *initialiser_;
+ CYForInInitializer *initialiser_;
CYExpression *set_;
CYStatement *code_;
- CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
+ CYForIn(CYForInInitializer *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;
};
struct CYForOf :
CYStatement
{
- CYForInInitialiser *initialiser_;
+ CYForInInitializer *initialiser_;
CYExpression *set_;
CYStatement *code_;
- CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
+ CYForOf(CYForInInitializer *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;
};
{
}
+ CYCompact(Long)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYStatement *Return();
};
struct CYDoWhile :
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(Long)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYStatement *code_;
CYNonLocal *nonlocal_;
+ bool implicit_;
CYThisScope this_;
CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
name_(name),
parameters_(parameters),
code_(code),
- nonlocal_(NULL)
+ 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;
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
throw;
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYStatement *Return();
};
struct CYContinue :
{
}
+ CYCompact(Short)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(Short)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYYieldGenerator :
+ CYExpression
+{
+ CYExpression *value_;
+
+ CYYieldGenerator(CYExpression *value) :
+ value_(value)
+ {
+ }
+
+ CYPrecedence(0)
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYYieldValue :
+ CYExpression
+{
+ CYExpression *value_;
+
+ CYYieldValue(CYExpression *value) :
+ value_(value)
+ {
+ }
+
+ CYPrecedence(0)
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYEmpty :
CYStatement
{
+ CYCompact(Short)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(Short)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(Long)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(Long)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
+ CYCompact(None)
+
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};