2024-03-17 07:58:28 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <QList>
|
2025-02-02 12:54:32 +00:00
|
|
|
|
#include "libtokens.h"
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
namespace lib_syntax {
|
|
|
|
|
class ExprRule;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 抽象语法树结构 ====================================================================
|
2024-03-17 07:58:28 +00:00
|
|
|
|
namespace ast_basic {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 抽象语法树集合节点/表达式节点
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
class IExprInstance {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 获取表达式的解析规则
|
2025-02-07 15:26:20 +00:00
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual std::shared_ptr<const lib_syntax::ExprRule> definedRule() const = 0;
|
|
|
|
|
//=====================================================
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* 获取语法节点的源码文件路径.
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* \return 文件路径
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
virtual QString filePath() const = 0;
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* 获取语法节点关联token序列.
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* \return token序列
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
virtual QList<std::shared_ptr<const lib_token::IToken>> tokens() const = 0;
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* 解析过程中,向表达式内部添加token实例.
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* \param token_inst 实例
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
virtual void addToken(std::shared_ptr<const lib_token::IToken> token_inst) = 0;
|
|
|
|
|
//=====================================================
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 子表达式集合
|
2025-02-07 15:26:20 +00:00
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual QList<std::shared_ptr<const IExprInstance>> children() const = 0;
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 添加子表达式.
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* \param inst 子表达式实例
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
virtual void addChild(std::shared_ptr<const IExprInstance> inst) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 基础语法树构成 ==========================================================================================
|
2025-02-07 15:26:20 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 表达式节点
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
class LIBSYNTAX_EXPORT ExprInstance : public IExprInstance, public std::enable_shared_from_this<ExprInstance> {
|
|
|
|
|
private:
|
|
|
|
|
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;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
public:
|
|
|
|
|
ExprInstance(std::shared_ptr<const lib_syntax::ExprRule> bind);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 通过 Expression 继承
|
2025-02-07 15:26:20 +00:00
|
|
|
|
std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override;
|
|
|
|
|
QString filePath() const override;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const lib_token::IToken>> tokens() const override;
|
|
|
|
|
void addToken(std::shared_ptr<const lib_token::IToken> token_inst) override;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const IExprInstance>> children() const override;
|
|
|
|
|
void addChild(std::shared_ptr<const IExprInstance> inst) override;
|
|
|
|
|
};
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 表达式树根节点
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
|
|
|
|
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);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 通过 IExprInstance 继承
|
2025-02-07 15:26:20 +00:00
|
|
|
|
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;
|
|
|
|
|
};
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|