#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:
};
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;
virtual CYStatement *Return();
};
-struct CYForInitialiser {
- virtual ~CYForInitialiser() {
- }
-
+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
};
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;
{
}
+ 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;
};
{
}
- 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;
{
}
+ 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 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;
};