115 lines
3.6 KiB
C++
115 lines
3.6 KiB
C++
#pragma once
|
||
|
||
#include <memory>
|
||
#include <QList>
|
||
#include "libtokens.h"
|
||
|
||
|
||
namespace lib_syntax {
|
||
class ExprRule;
|
||
}
|
||
|
||
// 抽象语法树结构 ====================================================================
|
||
namespace ast_basic {
|
||
/**
|
||
* @brief 抽象语法树集合节点/表达式节点
|
||
*/
|
||
class IExprInstance {
|
||
public:
|
||
/**
|
||
* @brief 获取表达式的解析规则
|
||
* @return
|
||
*/
|
||
virtual std::shared_ptr<const lib_syntax::ExprRule> definedRule() const = 0;
|
||
//=====================================================
|
||
/**
|
||
* 获取语法节点的源码文件路径.
|
||
*
|
||
* \return 文件路径
|
||
*/
|
||
virtual QString filePath() const = 0;
|
||
/**
|
||
* 获取语法节点关联token序列.
|
||
*
|
||
* \return token序列
|
||
*/
|
||
virtual QList<std::shared_ptr<const lib_token::IToken>> tokens() const = 0;
|
||
/**
|
||
* 解析过程中,向表达式内部添加token实例.
|
||
*
|
||
* \param token_inst 实例
|
||
*/
|
||
virtual void addToken(std::shared_ptr<const lib_token::IToken> token_inst) = 0;
|
||
//=====================================================
|
||
/**
|
||
* @brief 子表达式集合
|
||
* @return
|
||
*/
|
||
virtual QList<std::shared_ptr<const IExprInstance>> children() const = 0;
|
||
/**
|
||
* @brief 添加子表达式.
|
||
*
|
||
* \param inst 子表达式实例
|
||
*/
|
||
virtual void addChild(std::shared_ptr<const IExprInstance> inst) = 0;
|
||
/**
|
||
* @brief 设置父表达式.
|
||
*
|
||
* \param pinst
|
||
*/
|
||
virtual void setParent(std::weak_ptr<const IExprInstance> pinst) = 0;
|
||
virtual std::weak_ptr<const IExprInstance> parentExpr() const = 0;
|
||
};
|
||
|
||
|
||
// 基础语法树构成 ==========================================================================================
|
||
/**
|
||
* @brief 表达式节点
|
||
*/
|
||
class LIBSYNTAX_EXPORT ExprInstance : public IExprInstance, public std::enable_shared_from_this<ExprInstance> {
|
||
private:
|
||
std::weak_ptr<const IExprInstance > _parent_bind;
|
||
|
||
std::shared_ptr<const lib_syntax::ExprRule> _expr_rule;
|
||
QList<std::shared_ptr<const IExprInstance>> children_store;
|
||
QList<std::shared_ptr<const lib_token::IToken>> tokens_bind;
|
||
|
||
public:
|
||
ExprInstance(std::shared_ptr<const lib_syntax::ExprRule> bind);
|
||
|
||
// 通过 Expression 继承
|
||
std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override;
|
||
QString filePath() const override;
|
||
|
||
QList<std::shared_ptr<const lib_token::IToken>> tokens() const override;
|
||
void addToken(std::shared_ptr<const lib_token::IToken> token_inst) override;
|
||
|
||
QList<std::shared_ptr<const IExprInstance>> children() const override;
|
||
void addChild(std::shared_ptr<const IExprInstance> inst) override;
|
||
|
||
virtual void setParent(std::weak_ptr<const IExprInstance> pinst) override;
|
||
virtual std::weak_ptr<const IExprInstance> parentExpr() const override;
|
||
};
|
||
|
||
/**
|
||
* @brief 表达式树根节点
|
||
*/
|
||
class LIBSYNTAX_EXPORT ExprProgram : public IExprInstance, public std::enable_shared_from_this<ExprProgram> {
|
||
private:
|
||
QList<std::shared_ptr<const IExprInstance>> _children_store;
|
||
QString _program_root;
|
||
|
||
public:
|
||
ExprProgram(const QString &root);
|
||
|
||
// 通过 IExprInstance 继承
|
||
std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override;
|
||
QString filePath() const override;
|
||
QList<std::shared_ptr<const lib_token::IToken>> tokens() const override;
|
||
void addToken(std::shared_ptr<const lib_token::IToken> token_inst) override;
|
||
QList<std::shared_ptr<const IExprInstance>> children() const override;
|
||
void addChild(std::shared_ptr<const IExprInstance> inst) override;
|
||
void setParent(std::weak_ptr<const IExprInstance> pinst) override;
|
||
std::weak_ptr<const IExprInstance> parentExpr() const override;
|
||
};
|
||
} |