203 lines
5.4 KiB
C++
203 lines
5.4 KiB
C++
#pragma once
|
|
|
|
#include "libsyntax_global.h"
|
|
#include <libtoken.h>
|
|
#include <memory>
|
|
#include <tuple>
|
|
#include <functional>
|
|
namespace ast_basic {
|
|
class IExprInst;
|
|
class ExpressionElement;
|
|
}
|
|
|
|
namespace lib_syntax {
|
|
class IBasicRule;
|
|
|
|
/**
|
|
* @brief 语法异常
|
|
*/
|
|
class LIBSYNTAX_EXPORT SyntaxException {
|
|
private:
|
|
QString msg_store;
|
|
|
|
public:
|
|
SyntaxException(const QString& message);
|
|
virtual ~SyntaxException() = default;
|
|
|
|
virtual QString message() const;
|
|
};
|
|
|
|
// 基础语法解析接口 ===================================================================================================
|
|
/**
|
|
* @brief 修正类型
|
|
*/
|
|
enum class ErrDeals {
|
|
None, // 无错误
|
|
Surplus, // 多一个词
|
|
Absence, // 少一个词
|
|
Replace, // 错一个词
|
|
Abort, // 中断解析过程
|
|
TerminalAOT, // 输入提前终止
|
|
};
|
|
|
|
/**
|
|
* @brief 解析分支
|
|
*/
|
|
struct LIBSYNTAX_EXPORT ParseFork {
|
|
ErrDeals occurs = ErrDeals::None; // 解析过程中的错误
|
|
QList<QString> error_messages; // 累计错误信息
|
|
QList<std::shared_ptr<const ast_basic::IExprInst>> mbrs_list; // 成员存储
|
|
QList<std::shared_ptr<const lib_token::IToken>> tokens_list; // token列表
|
|
std::shared_ptr<const lib_token::IWordBase> next = nullptr; // 下一个解析词
|
|
|
|
ParseFork();
|
|
ParseFork(const ParseFork& other);
|
|
};
|
|
|
|
/**
|
|
* @brief 基础语法匹配规则接口
|
|
*/
|
|
class IBasicRule {
|
|
public:
|
|
virtual ~IBasicRule() = default;
|
|
/**
|
|
* @brief 子规则
|
|
* @return
|
|
*/
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const = 0;
|
|
|
|
|
|
/**
|
|
* @brief 解析
|
|
* @param context 累计上下文
|
|
* @return 返回结果<解析分支>
|
|
*/
|
|
virtual QList<ParseFork> parse(const ParseFork& context) const = 0;
|
|
|
|
/**
|
|
* 返回匹配语法规则的词法序列表达
|
|
*
|
|
* \return 词法表达序列
|
|
*/
|
|
virtual QString token_present() const = 0;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief token匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch> {
|
|
private:
|
|
std::shared_ptr<const lib_token::ITokenDefine> define_peer;
|
|
|
|
public:
|
|
TokenMatch(std::shared_ptr<const lib_token::ITokenDefine> define);
|
|
|
|
// IBasicRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
virtual QList<ParseFork> parse(const ParseFork& context) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法规则或匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT Any : public IBasicRule, public std::enable_shared_from_this<Any> {
|
|
private:
|
|
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
|
|
|
|
public:
|
|
Any(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
|
|
|
// IBasicRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
virtual QList<ParseFork> parse(const ParseFork& context) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法规则序列匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT Seqs : public IBasicRule, public std::enable_shared_from_this<Seqs> {
|
|
private:
|
|
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
|
|
|
|
public:
|
|
Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
|
|
|
// IBasicRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
virtual QList<ParseFork> parse(const ParseFork& context) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法规则重复匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT Rept : public IBasicRule, public std::enable_shared_from_this<Rept> {
|
|
private:
|
|
std::shared_ptr<const IBasicRule> rule_peer;
|
|
int min_match, max_match;
|
|
|
|
public:
|
|
Rept(std::shared_ptr<const IBasicRule> rule, int min, int max);
|
|
|
|
// IBasicRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
virtual QList<ParseFork> parse(const ParseFork& context) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
// 组合语法实体解析 ===================================================================================================
|
|
/**
|
|
* @brief 对应语法表达式解析规则
|
|
*/
|
|
class LIBSYNTAX_EXPORT ExprRule : public IBasicRule, public std::enable_shared_from_this<ExprRule> {
|
|
public:
|
|
typedef QList<std::shared_ptr<const lib_token::IToken>> TokenSeqs;
|
|
ExprRule(const QString& rule_name, int expr_mark);
|
|
|
|
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const IBasicRule> rule);
|
|
virtual QString name() const;
|
|
virtual int typeMark() const;
|
|
|
|
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const = 0;
|
|
virtual std::shared_ptr<ExprRule> makeCopy() const = 0;
|
|
|
|
// IBasicRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
virtual QList<ParseFork> parse(const ParseFork& context) const override;
|
|
virtual QString token_present() const override;
|
|
|
|
protected:
|
|
std::shared_ptr<const IBasicRule> child_store;
|
|
|
|
private:
|
|
QString name_store;
|
|
int mark_store;
|
|
};
|
|
|
|
/**
|
|
* 语法元素解析规则.
|
|
*/
|
|
template<class ExprType>
|
|
class ElementRule : public ExprRule {
|
|
public:
|
|
ElementRule(const QString& rule_name, int expr_mark)
|
|
:ExprRule(rule_name, expr_mark) {}
|
|
|
|
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const {
|
|
return std::dynamic_pointer_cast<ast_basic::IExprInst>(
|
|
std::make_shared<ExprType>(this->shared_from_this()));
|
|
}
|
|
|
|
virtual std::shared_ptr<ExprRule> makeCopy() const {
|
|
return std::make_shared<ElementRule<ExprType>>(name(), typeMark());
|
|
}
|
|
};
|
|
} // namespace lib_syntax
|