95 lines
2.1 KiB
C++
95 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include "libparse_global.h"
|
|
#include <ast_basic.h>
|
|
#include <ast_gen.h>
|
|
#include <ast_novel.h>
|
|
|
|
namespace lib_parse {
|
|
/**
|
|
* @brief 检查机制异常
|
|
*/
|
|
class LIBPARSE_EXPORT CheckException {
|
|
private:
|
|
QString msg_store;
|
|
|
|
public:
|
|
CheckException(const QString& msg);
|
|
virtual ~CheckException() = default;
|
|
|
|
virtual QString message() const;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief 校验机制提供类型
|
|
*/
|
|
class CheckProvider {
|
|
public:
|
|
virtual QString name() const = 0;
|
|
virtual void validCheck(std::shared_ptr<const ast_gen::ElementAccess> root) const = 0;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief 分析器对外接口
|
|
*/
|
|
class LIBPARSE_EXPORT Analyzer {
|
|
private:
|
|
QList<std::shared_ptr<const CheckProvider>> check_providers;
|
|
|
|
public:
|
|
/**
|
|
* @brief 构建AST分析器.
|
|
*
|
|
* \param providers
|
|
*/
|
|
Analyzer(const QList<std::shared_ptr<const CheckProvider>>& providers);
|
|
|
|
/**
|
|
* @brief 使用本分析器分析指定Ast.
|
|
*
|
|
* \param root
|
|
* \return
|
|
*/
|
|
std::shared_ptr<const ast_gen::ElementAccess> validCheckWith(std::shared_ptr<const ast_gen::ElementAccess> root) const;
|
|
};
|
|
|
|
/**
|
|
* @brief 遍历模式.
|
|
*/
|
|
enum class VisitMode {
|
|
FirstParent, // 先序遍历
|
|
LastParent, // 后序遍历
|
|
};
|
|
|
|
/**
|
|
* @brief Ast树遍历模式.
|
|
*/
|
|
class TreeVisitor {
|
|
public:
|
|
/**
|
|
* 获取访问器的遍历模式.
|
|
*
|
|
* \return
|
|
*/
|
|
virtual VisitMode mode() const = 0;
|
|
|
|
/**
|
|
* 对语法树执行节点遍历.
|
|
*
|
|
* \param syntax_element 当前访问节点
|
|
* \return 是否继续执行遍历
|
|
*/
|
|
virtual bool visit(std::shared_ptr<const ast_gen::ElementAccess> syntax_element) = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief Ast遍历控制接口.
|
|
*/
|
|
class LIBPARSE_EXPORT VisitEntry {
|
|
public:
|
|
bool visitWith(std::shared_ptr<const ast_gen::ElementAccess> syntax_elm, std::shared_ptr<TreeVisitor> visitor);
|
|
};
|
|
}
|