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