Toshihiro Shimizu 890ddd
#ifndef TGRAMMAR_INCLUDED
Toshihiro Shimizu 890ddd
#define TGRAMMAR_INCLUDED
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "ttokenizer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TNZBASE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//    Forward declarations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TDoubleParam;
Toshihiro Shimizu 890ddd
class TUnit;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TSyntax
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
class Token;
Toshihiro Shimizu 890ddd
class Calculator;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// Calculator & calculator nodes
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TSyntax
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI CalculatorNodeVisitor
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	CalculatorNodeVisitor() {}
Toshihiro Shimizu 890ddd
	virtual ~CalculatorNodeVisitor() {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI CalculatorNode
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Calculator *m_calculator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	CalculatorNode(Calculator *calculator) : m_calculator(calculator) {}
Toshihiro Shimizu 890ddd
	virtual ~CalculatorNode() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Calculator *getCalculator() const { return m_calculator; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum { T,
Toshihiro Shimizu 890ddd
		   FRAME,
Toshihiro Shimizu 890ddd
		   RFRAME };
Toshihiro Shimizu 890ddd
	virtual double compute(double vars[3]) const = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void accept(CalculatorNodeVisitor &visitor) = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// Non-copyable
Toshihiro Shimizu 890ddd
	CalculatorNode(const CalculatorNode &);
Toshihiro Shimizu 890ddd
	CalculatorNode &operator=(const CalculatorNode &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI Calculator
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	CalculatorNode *m_rootNode; //!< (owned) Root calculator node
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDoubleParam *m_param; //!< (not owned) Owner of the calculator object
Toshihiro Shimizu 890ddd
	const TUnit *m_unit;   //!< (not owned)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Calculator();
Toshihiro Shimizu 890ddd
	virtual ~Calculator();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setRootNode(CalculatorNode *node);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double compute(double t, double frame, double rframe)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		double vars[3];
Toshihiro Shimizu 890ddd
		vars[0] = t, vars[1] = frame, vars[2] = rframe;
Toshihiro Shimizu 890ddd
		return m_rootNode->compute(vars);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void accept(CalculatorNodeVisitor &visitor) { m_rootNode->accept(visitor); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typedef double Calculator::*Variable;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setOwnerParameter(TDoubleParam *param) { m_param = param; }
Toshihiro Shimizu 890ddd
	TDoubleParam *getOwnerParameter() const { return m_param; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const TUnit *getUnit() const { return m_unit; }
Toshihiro Shimizu 890ddd
	void setUnit(const TUnit *unit) { m_unit = unit; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not copyable
Toshihiro Shimizu 890ddd
	Calculator(const Calculator &);
Toshihiro Shimizu 890ddd
	Calculator &operator=(const Calculator &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI NumberNode : public CalculatorNode
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double m_value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	NumberNode(Calculator *calc, double value) : CalculatorNode(calc), m_value(value) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double compute(double vars[3]) const { return m_value; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void accept(CalculatorNodeVisitor &visitor) {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI VariableNode : public CalculatorNode
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int m_varIdx;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	VariableNode(Calculator *calc, int varIdx)
Toshihiro Shimizu 890ddd
		: CalculatorNode(calc), m_varIdx(varIdx) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double compute(double vars[3]) const { return vars[m_varIdx]; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void accept(CalculatorNodeVisitor &visitor) {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// Pattern
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
enum TokenType {
Toshihiro Shimizu 890ddd
	Unknown = 0,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Number,
Toshihiro Shimizu 890ddd
	Constant,
Toshihiro Shimizu 890ddd
	Variable,
Toshihiro Shimizu 890ddd
	Operator,
Toshihiro Shimizu 890ddd
	Parenthesis,
Toshihiro Shimizu 890ddd
	Function,
Toshihiro Shimizu 890ddd
	Comma,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UnexpectedToken = -100,
Toshihiro Shimizu 890ddd
	Eos,
Toshihiro Shimizu 890ddd
	Mismatch,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	InternalError = -200
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI Pattern
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::string m_description;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Pattern() {}
Toshihiro Shimizu 890ddd
	virtual ~Pattern() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	virtual std::string getFirstKeyword() const { return ""; }
Toshihiro Shimizu 890ddd
	virtual void getAcceptableKeywords(std::vector<std::string> &keywords) const {}</std::string>
Toshihiro Shimizu 890ddd
	virtual int getPriority() const { return 0; }
Toshihiro Shimizu 890ddd
	virtual bool expressionExpected(const std::vector<token> &previousTokens) const { return false; }</token>
Toshihiro Shimizu 890ddd
	virtual bool matchToken(const std::vector<token> &previousTokens, const Token &token) const = 0;</token>
Toshihiro Shimizu 890ddd
	virtual bool isFinished(const std::vector<token> &previousTokens, const Token &token) const = 0;</token>
Toshihiro Shimizu 890ddd
	virtual bool isComplete(const std::vector<token> &previousTokens, const Token &token) const { return isFinished(previousTokens, token); }</token>
Toshihiro Shimizu 890ddd
	virtual TokenType getTokenType(const std::vector<token> &previousTokens, const Token &token) const = 0; // see also SyntaxToken in tparser.h</token>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void createNode(
Toshihiro Shimizu 890ddd
		Calculator *calc,
Toshihiro Shimizu 890ddd
		std::vector<calculatornode *=""> &stack,</calculatornode>
Toshihiro Shimizu 890ddd
		const std::vector<token> &tokens) const = 0;</token>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::string getDescription() const { return m_description; }
Shinya Kitaoka 3bfa54
	void setDescription(std::string description) { m_description = description; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// helper methods
Toshihiro Shimizu 890ddd
	CalculatorNode *popNode(std::vector<calculatornode *=""> &stack) const;</calculatornode>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI Grammar
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	class Imp;
Shinya Kitaoka 262a92
	std::unique_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Grammar();
Toshihiro Shimizu 890ddd
	~Grammar();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addPattern(Pattern *pattern); // take ownership
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum Position { ExpressionStart,
Toshihiro Shimizu 890ddd
					ExpressionEnd };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// note: returns a matching pattern (or 0 if no pattern matches)
Toshihiro Shimizu 890ddd
	const Pattern *getPattern(Position position, const Token &token) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// returns matching <keywords, comment=""></keywords,>
Shinya Kitaoka 3bfa54
	typedef std::vector<std::pair<std::string, std::string="">> Suggestions;</std::pair<std::string,>
Toshihiro Shimizu 890ddd
	void getSuggestions(Suggestions &suggetsions, Position position) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	Grammar(const Grammar &);
Toshihiro Shimizu 890ddd
	Grammar &operator=(const Grammar &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TSyntax
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // TGRAMMAR_INCLUDED