#define CYPARSER_HPP
#include <cstdlib>
+#include <string>
+#include <vector>
-class CYParser {
- public:
- void *scanner_;
+#include "location.hh"
+#include "Pooling.hpp"
- private:
- void ScannerInit();
- void ScannerDestroy();
+template <typename Type_>
+struct CYNext {
+ Type_ *next_;
- public:
- CYParser();
- ~CYParser();
-};
-
-struct CYSource {
- CYSource *next_;
+ CYNext() :
+ next_(NULL)
+ {
+ }
- void SetNext(CYSource *next) {
+ void SetNext(Type_ *next) {
next_ = next;
}
};
-struct CYName {
- virtual const char *Name() const = 0;
+struct CYThing {
+ virtual void Output(std::ostream &out) const = 0;
};
-struct CYToken {
- virtual const char *Text() const = 0;
+_finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) {
+ rhs.Output(out);
+ return out;
+}
+
+struct CYPart {
+ virtual void Part(std::ostream &out) const = 0;
+};
+
+struct CYSource :
+ CYNext<CYSource>
+{
+ virtual void Show(std::ostream &out) const;
+ virtual void Output(std::ostream &out) const = 0;
+ virtual void Output(std::ostream &out, bool block) const;
+};
+
+struct CYName :
+ CYThing
+{
+ virtual const char *Name() const = 0;
};
struct CYWord :
- virtual CYToken,
CYName
{
const char *word_;
{
}
- virtual const char *Text() const {
+ const char *Value() const {
return word_;
}
virtual const char *Name() const {
- return Text();
+ return Value();
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYIdentifier :
CYWord
{
- const char *word_;
-
- virtual const char *Text() const {
- return word_;
+ CYIdentifier(const char *word) :
+ CYWord(word)
+ {
}
};
}
};
-struct CYForInitialiser {
+enum CYState {
+ CYClear,
+ CYRestricted,
+ CYNewLine
+};
+
+class CYDriver {
+ public:
+ CYPool pool_;
+
+ CYState state_;
+ void *scanner_;
+
+ const char *data_;
+ size_t size_;
+
+ std::string filename_;
+
+ struct Error {
+ cy::location location_;
+ std::string message_;
+ };
+
+ typedef std::vector<Error> Errors;
+
+ CYSource *source_;
+ Errors errors_;
+
+ private:
+ void ScannerInit();
+ void ScannerDestroy();
+
+ public:
+ CYDriver(const std::string &filename);
+ ~CYDriver();
+};
+
+struct CYForInitialiser :
+ CYPart
+{
};
-struct CYForInInitialiser {
+struct CYForInInitialiser :
+ CYPart
+{
};
struct CYExpression :
- CYStatement,
+ CYNext<CYExpression>,
CYForInitialiser,
CYForInInitialiser
{
+ virtual void Part(std::ostream &out) const;
+ virtual void Output(std::ostream &out) const = 0;
+ void Output(std::ostream &out, bool raw) const;
};
+_finline std::ostream &operator <<(std::ostream &out, const CYExpression &rhs) {
+ rhs.Output(out, false);
+ return out;
+}
+
struct CYLiteral :
CYExpression
{
};
+struct CYSelector :
+ CYLiteral
+{
+ CYWord *name_;
+ bool value_;
+ CYSelector *after_;
+
+ CYSelector(CYWord *name, bool value, CYSelector *after) :
+ name_(name),
+ value_(value),
+ after_(after)
+ {
+ }
+
+ virtual void Output(std::ostream &out) const;
+};
+
struct CYString :
CYLiteral,
CYName
{
const char *value_;
+ size_t size_;
- CYString(const char *value) :
- value_(value)
+ CYString(const char *value, size_t size) :
+ value_(value),
+ size_(size)
{
}
CYString(const CYIdentifier *identifier) :
- value_(identifier->Text())
+ value_(identifier->Value()),
+ size_(strlen(value_))
{
}
- const char *String() const {
+ const char *Value() const {
return value_;
}
virtual const char *Name() const {
- return String();
+ return Value();
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYNumber :
- virtual CYToken,
CYLiteral,
CYName
{
- double Number() const {
- throw;
+ double value_;
+
+ CYNumber(double value) :
+ value_(value)
+ {
+ }
+
+ double Value() const {
+ return value_;
}
virtual const char *Name() const {
throw;
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYNull :
CYWord("null")
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYThis :
CYWord("this")
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYBoolean :
CYLiteral
{
+ virtual bool Value() const = 0;
+ virtual void Output(std::ostream &out) const;
};
struct CYFalse :
CYWord("false")
{
}
+
+ virtual bool Value() const {
+ return false;
+ }
};
struct CYTrue :
CYWord("true")
{
}
+
+ virtual bool Value() const {
+ return true;
+ }
};
struct CYVariable :
name_(name)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYPrefix :
rhs_(rhs)
{
}
+
+ virtual const char *Operator() const = 0;
+
+ virtual void Output(std::ostream &out) const;
};
struct CYInfix :
rhs_(rhs)
{
}
+
+ virtual const char *Operator() const = 0;
+
+ virtual void Output(std::ostream &out) const;
};
struct CYPostfix :
lhs_(lhs)
{
}
+
+ virtual const char *Operator() const = 0;
+
+ virtual void Output(std::ostream &out) const;
};
struct CYAssignment :
CYInfix(lhs, rhs)
{
}
+
+ virtual const char *Operator() const = 0;
};
struct CYArgument {
next_(next)
{
}
+
+ void Output(std::ostream &out, bool send) const;
};
struct CYBlank :
}
};
-struct CYClause {
+struct CYClause :
+ CYThing,
+ CYNext<CYClause>
+{
CYExpression *case_;
CYStatement *code_;
- CYClause *next_;
CYClause(CYExpression *_case, CYStatement *code) :
case_(_case),
{
}
- void SetNext(CYClause *next) {
- next_ = next;
- }
+ virtual void Output(std::ostream &out) const;
};
-struct CYElement :
- CYLiteral
-{
+struct CYElement {
CYExpression *value_;
CYElement *next_;
next_(next)
{
}
+
+ void Output(std::ostream &out) const;
+};
+
+struct CYArray :
+ CYLiteral
+{
+ CYElement *elements_;
+
+ CYArray(CYElement *elements) :
+ elements_(elements)
+ {
+ }
+
+ virtual void Output(std::ostream &out) const;
};
struct CYDeclaration :
+ CYThing,
CYForInInitialiser
{
CYIdentifier *identifier_;
initialiser_(initialiser)
{
}
+
+ virtual void Part(std::ostream &out) const;
+ virtual void Output(std::ostream &out) const;
};
struct CYDeclarations :
next_(next)
{
}
+
+ virtual void Part(std::ostream &out) const;
+ virtual void Output(std::ostream &out) const;
};
-struct CYParameter {
+struct CYParameter :
+ CYThing
+{
CYIdentifier *name_;
CYParameter *next_;
next_(next)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYFor :
code_(code)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYForIn :
code_(code)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
-struct CYProperty :
- CYLiteral
-{
+struct CYProperty {
CYName *name_;
CYExpression *value_;
CYProperty *next_;
next_(next)
{
}
+
+ virtual void Output(std::ostream &out) const;
+};
+
+struct CYObject :
+ CYLiteral
+{
+ CYProperty *property_;
+
+ CYObject(CYProperty *property) :
+ property_(property)
+ {
+ }
+
+ void Output(std::ostream &out) const;
};
-struct CYCatch {
+struct CYCatch :
+ CYThing
+{
CYIdentifier *name_;
CYStatement *code_;
code_(code)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYMessage :
arguments_(arguments)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYMember :
property_(property)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYNew :
arguments_(arguments)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYCall :
arguments_(arguments)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYIf :
false_(_false)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYDoWhile :
code_(code)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYWhile :
code_(code)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYLambda :
body_(body)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYFunction :
- CYLambda
+ CYLambda,
+ CYSource
{
CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
CYLambda(name, parameters, body)
{
}
+
+ virtual void Output(std::ostream &out) const;
+};
+
+struct CYExpress :
+ CYStatement
+{
+ CYExpression *expression_;
+
+ CYExpress(CYExpression *expression) :
+ expression_(expression)
+ {
+ }
+
+ virtual void Output(std::ostream &out) const;
};
struct CYContinue :
label_(label)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYBreak :
label_(label)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYReturn :
value_(value)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYEmpty :
CYStatement
{
+ virtual void Output(std::ostream &out) const;
+ virtual void Output(std::ostream &out, bool block) const;
};
struct CYTry :
finally_(finally)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYThrow :
value_(value)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYWith :
code_(code)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYSwitch :
clauses_(clauses)
{
}
+
+ virtual void Output(std::ostream &out) const;
};
struct CYCondition :
false_(_false)
{
}
+
+ virtual void Output(std::ostream &out) const;
+};
+
+struct CYAddressOf :
+ CYPrefix
+{
+ CYAddressOf(CYExpression *rhs) :
+ CYPrefix(rhs)
+ {
+ }
+
+ virtual const char *Operator() const {
+ return "&";
+ }
+
+ virtual void Output(std::ostream &out) const;
+};
+
+struct CYIndirect :
+ CYPrefix
+{
+ CYIndirect(CYExpression *rhs) :
+ CYPrefix(rhs)
+ {
+ }
+
+ virtual const char *Operator() const {
+ return "*";
+ }
+
+ virtual void Output(std::ostream &out) const;
};
#define CYPostfix_(op, name) \
CYPostfix(lhs) \
{ \
} \
+ \
+ virtual const char *Operator() const { \
+ return op; \
+ } \
};
#define CYPrefix_(op, name) \
CYPrefix(rhs) \
{ \
} \
+ \
+ virtual const char *Operator() const { \
+ return op; \
+ } \
};
#define CYInfix_(op, name) \
CYInfix(lhs, rhs) \
{ \
} \
+ \
+ virtual const char *Operator() const { \
+ return op; \
+ } \
};
#define CYAssignment_(op, name) \
CYAssignment(lhs, rhs) \
{ \
} \
+ \
+ virtual const char *Operator() const { \
+ return op; \
+ } \
};
CYPostfix_("++", PostIncrement)
CYPrefix_("-", Negate)
CYPrefix_("~", BitwiseNot)
CYPrefix_("!", LogicalNot)
-CYPrefix_("*", Indirect)
-CYPrefix_("&", AddressOf)
CYInfix_("*", Multiply)
CYInfix_("/", Divide)