WsParser_VS/libSyntax/ast_basic.h

115 lines
3.6 KiB
C
Raw Normal View History

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;
2025-02-15 15:47:42 +00:00
/**
* @brief .
*
* \param pinst
*/
virtual void setParent(std::weak_ptr<const IExprInstance> pinst) = 0;
virtual std::weak_ptr<const IExprInstance> parentExpr() const = 0;
2025-02-07 15:26:20 +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:
2025-02-15 15:47:42 +00:00
std::weak_ptr<const IExprInstance > _parent_bind;
2025-02-07 15:26:20 +00:00
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;
2025-02-15 15:47:42 +00:00
virtual void setParent(std::weak_ptr<const IExprInstance> pinst) override;
virtual std::weak_ptr<const IExprInstance> parentExpr() const override;
2025-02-07 15:26:20 +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);
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;
2025-02-15 15:47:42 +00:00
void setParent(std::weak_ptr<const IExprInstance> pinst) override;
std::weak_ptr<const IExprInstance> parentExpr() const override;
2025-02-07 15:26:20 +00:00
};
2024-03-17 07:58:28 +00:00
}