239 lines
7.4 KiB
C++
239 lines
7.4 KiB
C++
#pragma once
|
|
|
|
#include "libsyntax_global.h"
|
|
#include <libtoken.h>
|
|
#include <memory>
|
|
#include <tuple>
|
|
#include <functional>
|
|
namespace ast_basic {
|
|
class Expression;
|
|
class ExpressionElement;
|
|
}
|
|
|
|
namespace lib_syntax {
|
|
class BaseRule;
|
|
|
|
/**
|
|
* @brief 语法异常
|
|
*/
|
|
class LIBSYNTAX_EXPORT SyntaxException {
|
|
private:
|
|
QString msg_store;
|
|
|
|
public:
|
|
SyntaxException(const QString& message);
|
|
virtual ~SyntaxException() = default;
|
|
|
|
virtual QString message() const;
|
|
};
|
|
|
|
// 基础语法解析接口 ===================================================================================================
|
|
/**
|
|
* @brief 解析上下文接口
|
|
*/
|
|
class ParseContext {
|
|
public:
|
|
virtual ~ParseContext() = default;
|
|
|
|
virtual void setCurrentFile(const QString &path) = 0;
|
|
virtual QString currentFile() const = 0;
|
|
|
|
virtual void appendParseErrors(int start, const QString &error_msg) = 0;
|
|
virtual QStringList errors() const = 0;
|
|
virtual void clearErrors(int start) = 0;
|
|
|
|
/**
|
|
* \brief 当前表达式元素.
|
|
*
|
|
* \return 返回当前表达式
|
|
*/
|
|
virtual std::shared_ptr<ast_basic::Expression> currentInst() const = 0;
|
|
virtual void pushInst(std::shared_ptr<ast_basic::Expression> current_inst) = 0;
|
|
virtual std::shared_ptr<ast_basic::Expression> popInst() = 0;
|
|
|
|
virtual std::shared_ptr<const BaseRule> currentExpressionRule() const = 0;
|
|
virtual void pushExpressionRule(std::shared_ptr<const BaseRule> inst) = 0;
|
|
virtual std::shared_ptr<const BaseRule> popExpressionRule() = 0;
|
|
virtual QList<std::shared_ptr<const BaseRule>> currentExpressionRuleStack() const = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief 基础语法匹配规则接口
|
|
*/
|
|
class BaseRule {
|
|
public:
|
|
virtual ~BaseRule() = default;
|
|
/**
|
|
* @brief 子规则
|
|
* @return
|
|
*/
|
|
virtual QList<std::shared_ptr<const BaseRule>> children() const = 0;
|
|
|
|
/**
|
|
* @brief 匹配结果
|
|
*/
|
|
enum class MatchResult {
|
|
Success, // 符合匹配条件
|
|
Part, // 部分匹配
|
|
Fail // 从第一个词起完全不匹配
|
|
};
|
|
|
|
/**
|
|
* @brief 解析
|
|
* @param rt_inst 解析上下文
|
|
* @param head 列表头
|
|
* @return 返回结果<匹配完成新列表头,匹配长度>
|
|
*/
|
|
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
|
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0;
|
|
|
|
/**
|
|
* 返回匹配语法规则的词法序列表达
|
|
*
|
|
* \return 词法表达序列
|
|
*/
|
|
virtual QString token_present() const = 0;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief token匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT TokenMatch : public BaseRule, 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);
|
|
|
|
// BaseRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
|
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
|
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法规则或匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT Any : public BaseRule, public std::enable_shared_from_this<Any> {
|
|
private:
|
|
QList<std::shared_ptr<const BaseRule>> mbrs_store;
|
|
|
|
std::tuple<MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const lib_token::IWordBase>> rule_select(std::shared_ptr<const lib_token::IWordBase> head) const;
|
|
|
|
public:
|
|
Any(const QList<std::shared_ptr<const BaseRule>> mbrs);
|
|
|
|
// BaseRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
|
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
|
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法规则序列匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT Seqs : public BaseRule, public std::enable_shared_from_this<Seqs> {
|
|
private:
|
|
QList<std::shared_ptr<const BaseRule>> mbrs_store;
|
|
|
|
public:
|
|
Seqs(const QList<std::shared_ptr<const BaseRule>> mbrs);
|
|
|
|
// BaseRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
|
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
|
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
/**
|
|
* @brief 语法规则重复匹配
|
|
*/
|
|
class LIBSYNTAX_EXPORT Rept : public BaseRule, public std::enable_shared_from_this<Rept> {
|
|
private:
|
|
std::shared_ptr<const BaseRule> rule_peer;
|
|
int min_match, max_match;
|
|
|
|
public:
|
|
Rept(std::shared_ptr<const BaseRule> rule, int min, int max);
|
|
|
|
// BaseRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
|
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
|
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
|
virtual QString token_present() const override;
|
|
};
|
|
|
|
// 组合语法实体解析 ===================================================================================================
|
|
/**
|
|
* @brief 对应语法表达式解析规则
|
|
*/
|
|
class LIBSYNTAX_EXPORT ExpressionRule : public lib_syntax::BaseRule, public std::enable_shared_from_this<ExpressionRule> {
|
|
public:
|
|
typedef QList<std::shared_ptr<const lib_token::IToken>> TokenSeqs;
|
|
ExpressionRule(const QString& rule_name, int expr_mark);
|
|
|
|
virtual std::shared_ptr<const ExpressionRule> reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule);
|
|
virtual QString name() const;
|
|
virtual int typeMark() const;
|
|
|
|
virtual std::shared_ptr<ast_basic::Expression> newEmptyInstance() const = 0;
|
|
virtual std::shared_ptr<ExpressionRule> makeCopy() const = 0;
|
|
|
|
// BaseRule interface
|
|
public:
|
|
virtual QList<std::shared_ptr<const lib_syntax::BaseRule>> children() const override;
|
|
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
|
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
|
virtual QString token_present() const override;
|
|
|
|
private:
|
|
std::function<TokenSeqs(const TokenSeqs&)> filter_proc;
|
|
std::shared_ptr<const lib_syntax::BaseRule> child_store;
|
|
QString name_store;
|
|
int mark_store;
|
|
};
|
|
|
|
/**
|
|
* 语法元素解析规则.
|
|
*/
|
|
template<class ExprType>
|
|
class ElementRule : public ExpressionRule {
|
|
public:
|
|
ElementRule(const QString& rule_name, int expr_mark)
|
|
:ExpressionRule(rule_name, expr_mark){}
|
|
|
|
virtual std::shared_ptr<ast_basic::Expression> newEmptyInstance() const {
|
|
return std::dynamic_pointer_cast<ast_basic::Expression>(
|
|
std::make_shared<ExprType>(this->shared_from_this()));
|
|
}
|
|
|
|
virtual std::shared_ptr<ExpressionRule> makeCopy() const {
|
|
return std::make_shared<ElementRule<ExprType>>(name(), typeMark());
|
|
}
|
|
};
|
|
|
|
|
|
class MismatchException : public SyntaxException {
|
|
private:
|
|
std::shared_ptr<const lib_token::IWordBase> target;
|
|
|
|
public:
|
|
MismatchException(std::shared_ptr<const lib_token::IWordBase> inst);
|
|
virtual ~MismatchException() = default;
|
|
|
|
virtual std::shared_ptr<const lib_token::IWordBase> targetWord() const;
|
|
};
|
|
|
|
class InputTerminal : public SyntaxException {
|
|
public:
|
|
InputTerminal(const QString &file_path);
|
|
};
|
|
} // namespace lib_syntax
|