/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2010 Jay Freeman (saurik)
+ * Copyright (C) 2009-2012 Jay Freeman (saurik)
*/
/* GNU Lesser General Public License, Version 3 {{{ */
**/
/* }}} */
-#ifndef CYPARSER_HPP
-#define CYPARSER_HPP
+#ifndef CYCRIPT_PARSER_HPP
+#define CYCRIPT_PARSER_HPP
#include <iostream>
+#include <stack>
#include <string>
#include <vector>
#include <map>
#include <cstdlib>
#include "location.hh"
+
+#include "List.hpp"
#include "Pooling.hpp"
#include "Options.hpp"
-class CYContext;
-
-template <typename Type_>
-struct CYNext {
- Type_ *next_;
-
- CYNext() :
- next_(NULL)
- {
- }
-
- CYNext(Type_ *next) :
- next_(next)
- {
- }
-
- void SetNext(Type_ *next) {
- next_ = next;
- }
-};
+struct CYContext;
struct CYThing {
virtual ~CYThing() {
};
struct CYExpression;
+struct CYAssignment;
enum CYNeeded {
CYNever = -1,
CYNoBF = (CYNoBrace | CYNoFunction),
};
+_finline CYFlags operator ~(CYFlags rhs) {
+ return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
+}
+
+_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
+ return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
+}
+
+_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
+ return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
+}
+
+_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
+ return lhs = lhs | rhs;
+}
+
+_finline CYFlags CYLeft(CYFlags flags) {
+ return flags & ~(CYNoDangle | CYNoInteger);
+}
+
+_finline CYFlags CYRight(CYFlags flags) {
+ return flags & ~CYNoBF;
+}
+
+_finline CYFlags CYCenter(CYFlags flags) {
+ return CYLeft(CYRight(flags));
+}
+
struct CYStatement :
CYNext<CYStatement>
{
void Single(CYOutput &out, CYFlags flags) const;
void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
- CYStatement *ReplaceAll(CYContext &context);
- virtual CYStatement *Collapse(CYContext &context);
-
virtual CYStatement *Replace(CYContext &context) = 0;
private:
typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
-// XXX: strategy pattern, maybe subclass
-enum CYScopeType {
- CYScopeCatch,
- CYScopeFunction,
- CYScopeProgram,
-};
-
struct CYScope {
- CYScopeType type_;
+ bool transparent_;
CYContext &context_;
CYStatement *&statements_;
CYIdentifierAddressFlagsMap internal_;
CYIdentifierValueSet identifiers_;
- CYScope(CYScopeType type, CYContext &context, CYStatement *&statements);
+ CYScope(bool transparent, CYContext &context, CYStatement *&statements);
+ virtual ~CYScope();
void Close();
};
struct CYNonLocal;
+struct CYThisScope;
struct CYContext {
CYOptions &options_;
CYScope *scope_;
+ CYThisScope *this_;
+
CYIdentifierUsageVector rename_;
CYNonLocal *nonlocal_;
CYContext(CYOptions &options) :
options_(options),
scope_(NULL),
+ this_(NULL),
nonlocal_(NULL),
nextlocal_(NULL),
unique_(0)
virtual ~CYContext() {
}
+ template <typename Type_>
+ void ReplaceAll(Type_ *&values) {
+ Type_ **last(&values);
+ CYForEach (next, values) {
+ Replace(*last = next);
+ if (*last != NULL)
+ last = &(*last)->next_;
+ }
+ }
+
template <typename Type_>
void Replace(Type_ *&value) {
for (;;) if (value == NULL)
}
};
+struct CYThisScope :
+ CYNext<CYThisScope>
+{
+ CYIdentifier *identifier_;
+
+ CYThisScope() :
+ identifier_(NULL)
+ {
+ }
+
+ CYIdentifier *Identifier(CYContext &context) {
+ if (next_ != NULL)
+ return next_->Identifier(context);
+ if (identifier_ == NULL)
+ identifier_ = context.Unique();
+ return identifier_;
+ }
+};
+
struct CYBlock :
CYStatement,
CYThing
}
void AddPrev(CYStatement *statement) {
- CYStatement *last(statement);
- while (last->next_ != NULL)
- last = last->next_;
- last->SetNext(statements_);
+ CYSetLast(statement) = statements_;
statements_ = statement;
}
CYNewLine
};
+class CYStream :
+ public std::istream
+{
+ private:
+ class CYBuffer :
+ public std::streambuf
+ {
+ public:
+ CYBuffer(const char *start, const char *end) {
+ setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
+ }
+ } buffer_;
+
+ public:
+ CYStream(const char *start, const char *end) :
+ std::istream(&buffer_),
+ buffer_(start, end)
+ {
+ }
+};
+
class CYDriver {
public:
- CYState state_;
void *scanner_;
- const char *data_;
- size_t size_;
- FILE *file_;
+ CYState state_;
+ std::stack<bool> in_;
+
+ struct {
+ bool AtImplementation;
+ bool Function;
+ bool OpenBrace;
+ } no_;
+
+ std::istream &data_;
bool strict_;
+ bool commented_;
enum Condition {
RegExpCondition,
void ScannerDestroy();
public:
- CYDriver(const std::string &filename = "");
+ CYDriver(std::istream &data, const std::string &filename = "");
~CYDriver();
Condition GetCondition();
virtual ~CYForInitialiser() {
}
- virtual void For(CYOutput &out) const = 0;
virtual CYExpression *Replace(CYContext &context) = 0;
+ virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
struct CYForInInitialiser {
}
virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
- virtual const char *ForEachIn() const = 0;
- virtual CYExpression *ForEachIn(CYContext &out) = 0;
+ virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
+
virtual CYExpression *Replace(CYContext &context) = 0;
+ virtual CYAssignment *Assignment(CYContext &context) = 0;
+
+ virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
struct CYNumber;
return true;
}
- virtual void For(CYOutput &out) const;
virtual void ForIn(CYOutput &out, CYFlags flags) const;
-
- virtual const char *ForEachIn() const;
- virtual CYExpression *ForEachIn(CYContext &out);
+ virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
virtual void ClassName(CYOutput &out, bool object) const;
virtual CYExpression *Replace(CYContext &context) = 0;
+ virtual CYAssignment *Assignment(CYContext &context);
virtual CYExpression *Primitive(CYContext &context) {
return this;
}
void AddPrev(CYExpression *expression) {
- CYExpression *last(expression);
- while (last->next_ != NULL)
- last = last->next_;
- last->SetNext(expressions_);
+ CYSetLast(expression) = expressions_;
expressions_ = expression;
}
virtual CYExpression *Replace(CYContext &context);
void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYExpression *Primitive(CYContext &context);
};
+struct CYDeclaration;
+
struct CYFunctionParameter :
CYNext<CYFunctionParameter>,
CYThing
{
- CYIdentifier *name_;
+ CYForInInitialiser *initialiser_;
- CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
+ CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
CYNext<CYFunctionParameter>(next),
- name_(name)
- {
- }
-
- virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
- virtual void Output(CYOutput &out) const;
-};
-
-struct CYOptionalFunctionParameter :
- CYFunctionParameter
-{
- CYExpression *initializer_;
-
- CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
- CYFunctionParameter(name, next),
- initializer_(initializer)
+ initialiser_(initialiser)
{
}
- virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
- virtual void Output(CYOutput &out) const;
+ void Replace(CYContext &context, CYBlock &code);
+ void Output(CYOutput &out) const;
};
struct CYComprehension :
CYNext<CYComprehension>,
CYThing
{
+ CYComprehension(CYComprehension *next = NULL) :
+ CYNext<CYComprehension>(next)
+ {
+ }
+
virtual const char *Name() const = 0;
virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
CYIdentifier *name_;
CYExpression *set_;
- CYForInComprehension(CYIdentifier *name, CYExpression *set) :
+ CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
+ CYComprehension(next),
name_(name),
set_(set)
{
virtual void Output(CYOutput &out) const;
};
-struct CYForEachInComprehension :
+struct CYForOfComprehension :
CYComprehension
{
CYIdentifier *name_;
CYExpression *set_;
- CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
+ CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
+ CYComprehension(next),
name_(name),
set_(set)
{
{
}
- void Replace(CYContext &context);
+ CYArgument *Replace(CYContext &context);
void Output(CYOutput &out) const;
};
}
virtual void ForIn(CYOutput &out, CYFlags flags) const;
-
- virtual const char *ForEachIn() const;
- virtual CYExpression *ForEachIn(CYContext &out);
+ virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
virtual CYExpression *Replace(CYContext &context);
+
virtual CYAssignment *Assignment(CYContext &context);
+ CYVariable *Variable(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYDeclarations :
CYNext<CYDeclarations>,
- CYThing,
- CYForInitialiser
+ CYThing
{
CYDeclaration *declaration_;
{
}
- virtual void For(CYOutput &out) const;
+ void Replace(CYContext &context);
- virtual CYCompound *Replace(CYContext &context);
+ CYCompound *Compound(CYContext &context);
CYProperty *Property(CYContext &context);
+ CYArgument *Argument(CYContext &context);
+ CYFunctionParameter *Parameter(CYContext &context);
virtual void Output(CYOutput &out) const;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYForDeclarations :
+ CYForInitialiser
+{
+ CYDeclarations *declarations_;
+
+ CYForDeclarations(CYDeclarations *declarations) :
+ declarations_(declarations)
+ {
+ }
+
+ virtual CYCompound *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYVar :
CYStatement
{
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-struct CYLet :
+struct CYLetStatement :
CYStatement
{
CYDeclarations *declarations_;
- CYBlock code_;
+ CYStatement *code_;
- CYLet(CYDeclarations *declarations, CYStatement *statements) :
+ CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
declarations_(declarations),
- code_(statements)
+ code_(code)
{
}
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-struct CYForEachIn :
+struct CYForOf :
CYStatement
{
CYForInInitialiser *initialiser_;
CYExpression *set_;
CYStatement *code_;
- CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
+ CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
initialiser_(initialiser),
set_(set),
code_(code)
void SetLeft(CYExpression *object) {
object_ = object;
}
-
- void Replace_(CYContext &context);
};
struct CYDirectMember :
CYIdentifier *name_;
CYFunctionParameter *parameters_;
CYBlock code_;
+
CYNonLocal *nonlocal_;
+ CYThisScope this_;
CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
name_(name),
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+// XXX: this should derive from CYAnonymousFunction
+struct CYFatArrow :
+ CYFunction,
+ CYExpression
+{
+ CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
+ CYFunction(NULL, parameters, statements)
+ {
+ }
+
+ CYPrecedence(0)
+ CYRightHand(false)
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
// XXX: this should derive from CYAnonymousFunctionExpression
struct CYRubyProc :
CYFunctionExpression
throw;
}
- virtual CYStatement *Collapse(CYContext &context);
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYEmpty :
CYStatement
{
- virtual CYStatement *Collapse(CYContext &context);
virtual CYStatement *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYDebugger :
+ CYStatement
+{
+ CYDebugger()
+ {
+ }
+
+ virtual CYStatement *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYCondition :
CYExpression
{
CYAssignment_("^=", BitwiseXOr)
CYAssignment_("|=", BitwiseOr)
-#endif/*CYPARSER_HPP*/
+#endif/*CYCRIPT_PARSER_HPP*/