#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));
}
-struct CYStatement :
- CYNext<CYStatement>
-{
- virtual ~CYStatement() {
+enum CYCompactType {
+ CYCompactNone,
+ CYCompactLong,
+ CYCompactShort,
+};
+
+#define CYCompact(type) \
+ virtual CYCompactType Compact() const { \
+ return CYCompact ## type; \
}
- void Single(CYOutput &out, CYFlags flags) const;
+struct CYStatement :
+ CYNext<CYStatement>,
+ CYThing
+{
+ 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;
};
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 :
+struct CYScript :
CYThing
{
- CYStatement *statements_;
+ CYStatement *code_;
- CYProgram(CYStatement *statements) :
- statements_(statements)
+ CYScript(CYStatement *code) :
+ code_(code)
{
}
{
}
- virtual ~CYContext() {
- }
+ void ReplaceAll(CYStatement *&statement) {
+ if (statement == NULL)
+ return;
+ CYStatement *next(statement->next_);
- template <typename Type_>
- void ReplaceAll(Type_ *&values) {
- Type_ **last(&values);
- CYForEach (next, values) {
- Replace(*last = next);
- if (*last != NULL)
- last = &(*last)->next_;
- }
+ Replace(statement);
+ ReplaceAll(next);
+
+ if (statement == NULL)
+ statement = next;
+ else
+ statement->SetNext(next);
}
template <typename Type_>
};
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;
-};
-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;
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
+struct CYFunctionParameter;
+
struct CYNumber;
struct CYString;
struct CYExpression :
- CYForInitialiser,
- CYForInInitialiser,
+ CYForInitializer,
+ CYForInInitializer,
CYClassName,
CYThing
{
return NULL;
}
+ virtual CYFunctionParameter *Parameter() const;
+
virtual CYNumber *Number(CYContext &context) {
return NULL;
}
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)
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;
CYNext<CYFunctionParameter>,
CYThing
{
- CYForInInitialiser *initialiser_;
+ CYForInInitializer *initialiser_;
- CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
+ CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
CYNext<CYFunctionParameter>(next),
initialiser_(initialiser)
{
}
- void Replace(CYContext &context, CYBlock &code);
+ void Replace(CYContext &context, CYStatement *&statements);
void Output(CYOutput &out) const;
};
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;
}
virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYFunctionParameter *Parameter() const;
};
struct CYPrefix :
void Output(CYOutput &out) const;
};
-struct CYBlank :
- public CYWord
-{
- CYBlank() :
- CYWord("")
- {
- }
-};
-
struct CYClause :
CYThing,
CYNext<CYClause>
{
CYExpression *case_;
- CYStatement *statements_;
+ CYStatement *code_;
- CYClause(CYExpression *_case, CYStatement *statements) :
+ CYClause(CYExpression *_case, CYStatement *code) :
case_(_case),
- statements_(statements)
+ code_(code)
{
}
};
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_;
void Replace(CYContext &context);
- CYCompound *Compound(CYContext &context);
+ CYExpression *Expression(CYContext &context);
CYProperty *Property(CYContext &context);
CYArgument *Argument(CYContext &context);
CYFunctionParameter *Parameter(CYContext &context);
};
struct CYForDeclarations :
- CYForInitialiser
+ CYForInitializer
{
CYDeclarations *declarations_;
{
}
- virtual CYCompound *Replace(CYContext &context);
+ virtual CYExpression *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;
};
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;
};
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)
{
}
- virtual ~CYFunction() {
- }
-
void Inject(CYContext &context);
virtual void Replace_(CYContext &context, bool outer);
virtual void Output(CYOutput &out, CYFlags flags) const;
CYFunction,
CYExpression
{
- CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunction(name, parameters, statements)
+ CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunction(name, parameters, code)
{
}
CYFunction,
CYExpression
{
- CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunction(NULL, parameters, statements)
+ CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunction(NULL, parameters, code)
{
}
struct CYRubyProc :
CYFunctionExpression
{
- CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunctionExpression(NULL, parameters, statements)
+ CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunctionExpression(NULL, parameters, code)
{
}
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;
};
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;
};
struct CYFinally :
CYThing
{
- CYBlock code_;
+ CYStatement *code_;
- CYFinally(CYStatement *statements) :
- code_(statements)
+ CYFinally(CYStatement *code) :
+ code_(code)
{
}
{
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)
{
}
{
}
+ 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;
};
CYThing
{
CYIdentifier *name_;
- CYBlock code_;
+ CYStatement *code_;
- Catch(CYIdentifier *name, CYStatement *statements) :
+ Catch(CYIdentifier *name, CYStatement *code) :
name_(name),
- code_(statements)
+ code_(code)
{
}
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;
};
{
}
+ 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;
};