136 lines
3.4 KiB
C++
136 lines
3.4 KiB
C++
#pragma once
|
||
|
||
#include <QString>
|
||
#include <QList>
|
||
#include <QHash>
|
||
#include "ast_basic.h"
|
||
#include "libsyntax_global.h"
|
||
|
||
namespace ast_gen
|
||
{
|
||
class TokenAccess;
|
||
|
||
/**
|
||
* @brief 解析上下文
|
||
*/
|
||
class SyntaxElement {
|
||
public:
|
||
virtual ~SyntaxElement() = default;
|
||
/**
|
||
* 绑定表达式实体.
|
||
*
|
||
* \return 表达式实例
|
||
*/
|
||
virtual std::shared_ptr<const ast_basic::Expression> bindExpression() const = 0;
|
||
|
||
/**
|
||
* @brief 类型标记
|
||
* @return
|
||
*/
|
||
virtual int typeMark() const = 0;
|
||
|
||
/**
|
||
* 是否属于匿名节点.
|
||
*
|
||
* \return 匿名标志
|
||
*/
|
||
virtual bool isAnonymous() const = 0;
|
||
|
||
/**
|
||
* @brief 文件路径
|
||
* @return
|
||
*/
|
||
virtual QString path() const = 0;
|
||
|
||
/**
|
||
* @brief 元素签名,如果是匿名元素返回空字符串
|
||
* @return
|
||
*/
|
||
virtual QString signature() const = 0;
|
||
|
||
/**
|
||
* @brief 获取父元素
|
||
* @return 未设置parent,返回nullptr
|
||
*/
|
||
virtual std::shared_ptr<const SyntaxElement> parent() const = 0;
|
||
virtual void setParent(std::shared_ptr<const SyntaxElement> inst) = 0;
|
||
|
||
/**
|
||
* @brief 定义元素自身的Token集合
|
||
* @return
|
||
*/
|
||
virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const = 0;
|
||
};
|
||
|
||
/**
|
||
* @brief 语法元素整理访问接口
|
||
*/
|
||
class LIBSYNTAX_EXPORT ElementAccess {
|
||
private:
|
||
std::shared_ptr<const SyntaxElement> peers;
|
||
|
||
public:
|
||
ElementAccess(std::shared_ptr<const SyntaxElement> point);
|
||
|
||
std::shared_ptr<const SyntaxElement> element() const;
|
||
|
||
QList<std::shared_ptr<const ElementAccess>> children() const;
|
||
|
||
/**
|
||
* @brief 获取该元素下所有Token定义
|
||
* @return
|
||
*/
|
||
virtual QList<std::shared_ptr<const TokenAccess>> tokens() const;
|
||
};
|
||
|
||
class LIBSYNTAX_EXPORT TokenAccess {
|
||
private:
|
||
std::shared_ptr<const ast_gen::SyntaxElement> element_bind;
|
||
std::shared_ptr<const lib_token::Token> token_store;
|
||
|
||
public:
|
||
TokenAccess(std::shared_ptr<const ast_gen::SyntaxElement> elm_inst, std::shared_ptr<const lib_token::Token> token_inst);
|
||
virtual std::shared_ptr<const ast_gen::SyntaxElement> bind() const;
|
||
virtual std::shared_ptr<const lib_token::Token> token() const;
|
||
};
|
||
|
||
/**
|
||
* @brief 根元素定义
|
||
*/
|
||
class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement, public ast_basic::ExpressionContext {
|
||
private:
|
||
QString names_store;
|
||
QHash<QString, std::shared_ptr<const SyntaxElement>> node_cache;
|
||
|
||
std::shared_ptr<ast_basic::Expression> bind_exprs = std::make_shared<ast_basic::ExpressionElement>(nullptr);
|
||
|
||
public:
|
||
static GlobalElement* UniquePtr;
|
||
GlobalElement(const QString& name);
|
||
|
||
virtual void clearCache();
|
||
virtual void appendToCache(std::shared_ptr<const ast_gen::SyntaxElement> named_node);
|
||
/**
|
||
* @brief 通过节点签名获取定义节点
|
||
* @param signature 完全签名
|
||
* @return
|
||
* @throws 没有指定节点抛出异常
|
||
*/
|
||
virtual std::shared_ptr<const ast_gen::SyntaxElement> getNamedNodeBy(int type, const QString& signature) const;
|
||
|
||
virtual void addChild(std::shared_ptr<ast_gen::SyntaxElement> citem);
|
||
|
||
// ParseElement interface
|
||
public:
|
||
virtual int typeMark() const override;
|
||
virtual bool isAnonymous() const override;
|
||
virtual QString signature() const override;
|
||
virtual QString path() const override;
|
||
virtual std::shared_ptr<const SyntaxElement> parent() const override;
|
||
virtual void setParent(std::shared_ptr<const SyntaxElement> inst) override;
|
||
virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const override;
|
||
|
||
// 通过 SyntaxElement 继承
|
||
std::shared_ptr<const ast_basic::Expression> bindExpression() const override;
|
||
};
|
||
} |