111 lines
3.4 KiB
C++
111 lines
3.4 KiB
C++
#pragma once
|
||
|
||
#include <memory>
|
||
#include <QList>
|
||
#include <libtoken.h>
|
||
#include "libsyntax.h"
|
||
|
||
namespace ast_basic {
|
||
// 抽象语法树 ================================================================================================
|
||
|
||
/**
|
||
* @brief 抽象语法树节点/叶节点/词法节点
|
||
*/
|
||
class TokenNode {
|
||
public:
|
||
/**
|
||
* @brief 获取文件路径
|
||
* @return
|
||
*/
|
||
virtual QString filePath() const = 0;
|
||
|
||
/**
|
||
* @brief 是否属于叶子节点
|
||
* @return
|
||
*/
|
||
virtual bool isLeaf() const = 0;
|
||
/**
|
||
* @brief 获取节点定义包含Token集合
|
||
* @return
|
||
*/
|
||
virtual QList<std::shared_ptr<const lib_token::Token>> tokens() const = 0;
|
||
/**
|
||
* @brief 重置节点内子节点
|
||
* @param tokens
|
||
* @return
|
||
*/
|
||
virtual std::shared_ptr<const TokenNode> filledWith(const QList<std::shared_ptr<const TokenNode>>& child_nodes) const = 0;
|
||
};
|
||
|
||
/**
|
||
* @brief 抽象语法树集合节点/表达式节点
|
||
*/
|
||
class ExprNode : public TokenNode {
|
||
public:
|
||
|
||
/**
|
||
* @brief 表达式定义规则
|
||
* @return
|
||
*/
|
||
virtual std::shared_ptr<const lib_syntax::ExprRule> defined() const = 0;
|
||
/**
|
||
* @brief 直接子表达式集合
|
||
* @return
|
||
*/
|
||
virtual QList<std::shared_ptr<const ExprNode>> exprNodes() const = 0;
|
||
|
||
/**
|
||
* @brief 获取TokenNode,以直接子元素计算
|
||
* @param index 起始元素索引 0起始
|
||
* @param length 长度
|
||
* @return
|
||
*/
|
||
virtual QList<std::shared_ptr<const TokenNode>> childTokensMid(int index, int length = 1) const = 0;
|
||
};
|
||
|
||
|
||
// 基础语法树构成 ==========================================================================================
|
||
|
||
/**
|
||
* @brief 词法定义节点
|
||
*/
|
||
class LIBSYNTAX_EXPORT TokenNodeImpl : public ast_basic::TokenNode {
|
||
private:
|
||
std::shared_ptr<const lib_token::Token> word_store;
|
||
|
||
public:
|
||
TokenNodeImpl(std::shared_ptr<const lib_token::Token> word);
|
||
|
||
// TokenNode interface
|
||
public:
|
||
virtual QString filePath() const override;
|
||
virtual bool isLeaf() const override;
|
||
virtual QList<std::shared_ptr<const lib_token::Token>> tokens() const override;
|
||
virtual std::shared_ptr<const TokenNode> filledWith(const QList<std::shared_ptr<const TokenNode> >& tokens) const override;
|
||
};
|
||
|
||
/**
|
||
* @brief 表达式节点
|
||
*/
|
||
class LIBSYNTAX_EXPORT ExprNodeImpl : public ast_basic::ExprNode {
|
||
private:
|
||
std::shared_ptr<const lib_syntax::ExprRule> _expr_rule;
|
||
QList<std::shared_ptr<const TokenNode>> children_store;
|
||
|
||
public:
|
||
ExprNodeImpl(std::shared_ptr<const lib_syntax::ExprRule> bind);
|
||
|
||
// TokenNode interface
|
||
public:
|
||
virtual QString filePath() const override;
|
||
virtual bool isLeaf() const override;
|
||
virtual QList<std::shared_ptr<const lib_token::Token>> tokens() const override;
|
||
virtual std::shared_ptr<const TokenNode> filledWith(const QList<std::shared_ptr<const TokenNode> >& child_nodes) const override;
|
||
|
||
// ExprNode interface
|
||
public:
|
||
virtual std::shared_ptr<const lib_syntax::ExprRule> defined() const override;
|
||
virtual QList<std::shared_ptr<const ExprNode>> exprNodes() const override;
|
||
virtual QList<std::shared_ptr<const TokenNode> > childTokensMid(int index, int length) const override;
|
||
};
|
||
} |