#pragma once #include "libsyntax_global.h" #include #include #include #include namespace ast_basic { class TokenNode; class ExprNode; } namespace lib_syntax { /** * @brief 语法异常 */ class SyntaxException { private: QString msg_store; public: SyntaxException(const QString& message); virtual ~SyntaxException() = default; virtual QString message() const; }; // 基础语法解析接口 =================================================================================================== /** * @brief 基础语法匹配规则接口 */ class BaseRule { public: /** * @brief 子规则 * @return */ virtual QList> children() const = 0; /** * @brief 匹配结果 */ enum class MatchResult { Success, // 符合匹配条件 Part, // 部分匹配 Fail // 从第一个词起完全不匹配 }; /** * @brief token流匹配 * @return 首部对齐,匹配token集合 */ virtual std::tuple match(const QList>& stream) const = 0; /** * @brief 解析 * @param stream * @return */ virtual QList> parse(const QList>& stream) const = 0; /** * 返回匹配语法规则的词法序列表达 * * \return 词法表达序列 */ virtual QString token_present() const = 0; }; /** * @brief token匹配 */ class LIBSYNTAX_EXPORT TokenMatch : public BaseRule { private: std::shared_ptr define_peer; public: TokenMatch(std::shared_ptr define); // BaseRule interface public: virtual QList> children() const override; virtual std::tuple match(const QList>& stream) const override; virtual QList> parse(const QList>& stream) const override; virtual QString token_present() const override; }; /** * @brief 语法规则或匹配 */ class LIBSYNTAX_EXPORT Any : public BaseRule { private: QList> mbrs_store; std::tuple> rule_select(const QList>& stream) const; public: Any(const QList> mbrs); // BaseRule interface public: virtual QList> children() const override; virtual std::tuple match(const QList>& stream) const override; virtual QList> parse(const QList>& stream) const override; virtual QString token_present() const override; }; /** * @brief 语法规则序列匹配 */ class LIBSYNTAX_EXPORT Seqs : public BaseRule { private: QList> mbrs_store; public: Seqs(const QList> mbrs); // BaseRule interface public: virtual QList> children() const override; virtual std::tuple match(const QList>& stream) const override; virtual QList> parse(const QList>& stream) const override; virtual QString token_present() const override; }; /** * @brief 语法规则重复匹配 */ class LIBSYNTAX_EXPORT Rept : public BaseRule { private: std::shared_ptr rule_peer; int min_match, max_match; public: Rept(std::shared_ptr rule, int min, int max); // BaseRule interface public: virtual QList> children() const override; virtual std::tuple match(const QList>& stream) const override; virtual QList> parse(const QList>& stream) const override; virtual QString token_present() const override; }; // 组合语法实体解析 =================================================================================================== /** * @brief 对应语法表达式解析规则 */ class LIBSYNTAX_EXPORT ExprRule : public lib_syntax::BaseRule, public std::enable_shared_from_this { private: std::shared_ptr child_store; QString name_store; int mark_store; std::function>(const QList>& primary)> filter_proc; public: typedef QList> TokenSeqs; ExprRule(const QString& rule_name, int expr_mark); virtual std::shared_ptr reloadRule(std::function filter, std::shared_ptr rule); virtual QString name() const; virtual int typeMark() const; // BaseRule interface public: virtual QList> children() const override; virtual std::tuple match(const QList>& stream) const override; virtual QList> parse(const QList>& stream) const override; virtual QString token_present() const override; }; /** * @brief 语法检查核对工具 */ class LIBSYNTAX_EXPORT ExprsChecker { public: ExprsChecker(std::shared_ptr parse_tree); QList> parseFrom(const QList>& all_tokens); private: std::shared_ptr syntax_tree_root; }; } // namespace lib_syntax