163 lines
4.1 KiB
C++
163 lines
4.1 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:
|
|
/**
|
|
* @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
|
|
*/
|
|
virtual std::shared_ptr<const SyntaxElement> parent() const = 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;
|
|
QList<std::shared_ptr<const ElementAccess>> children_store;
|
|
|
|
public:
|
|
ElementAccess(std::shared_ptr<const SyntaxElement> point);
|
|
|
|
std::shared_ptr<const SyntaxElement> element() const;
|
|
|
|
QList<std::shared_ptr<const ElementAccess>> children() const;
|
|
void appendChild(std::shared_ptr<const ElementAccess> inst);
|
|
|
|
|
|
/**
|
|
* @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 {
|
|
private:
|
|
QList<std::shared_ptr<const SyntaxElement>> children_store;
|
|
QString names_store;
|
|
QHash<QString, std::shared_ptr<const SyntaxElement>> node_cache;
|
|
|
|
public:
|
|
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;
|
|
|
|
// 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 QList<std::shared_ptr<const TokenAccess>> selfTokens() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法树节点访问器
|
|
*/
|
|
class ExprVisitor {
|
|
public:
|
|
/**
|
|
* @brief 解析表达式节点,转换成语法元素实体
|
|
* @param expr 表达式节点
|
|
* @param pnode 父节点
|
|
* @return 该节点转换成的语法元素节点
|
|
*/
|
|
virtual std::shared_ptr<const SyntaxElement> visit(std::shared_ptr<const ast_basic::ExprNode> expr, std::shared_ptr<const SyntaxElement> pnode) const = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief 抽象语法树解析器
|
|
*/
|
|
class LIBSYNTAX_EXPORT ExprTreeParser {
|
|
private:
|
|
std::shared_ptr<const ExprVisitor> visitor_store;
|
|
void tidy_branch(std::shared_ptr<ElementAccess> root, std::shared_ptr<const SyntaxElement> node) const;
|
|
|
|
public:
|
|
ExprTreeParser(std::shared_ptr<const ExprVisitor> vistor);
|
|
|
|
/**
|
|
* @brief 解析语法内容,生成倒立语法树
|
|
* @param name 生成产物名称
|
|
* @param forest 表达式森林,全部的表达式树
|
|
* @return 语法树的所有叶子节点
|
|
*/
|
|
QList<std::shared_ptr<const SyntaxElement>> parse(const QString& name, const QList<std::shared_ptr<const ast_basic::ExprNode>>& forest) const;
|
|
|
|
/**
|
|
* @brief 整理语法树节点,生成语法元素访问树
|
|
* @param nodes
|
|
* @return
|
|
*/
|
|
std::shared_ptr<const ElementAccess> tidy(const QList<std::shared_ptr<const SyntaxElement>>& nodes) const;
|
|
};
|
|
} |