2024-03-17 07:58:28 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "libsyntax_global.h"
|
|
|
|
|
#include <libtoken.h>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <functional>
|
|
|
|
|
namespace ast_basic {
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class IExprInst;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
class ExpressionElement;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace lib_syntax {
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class IBasicRule;
|
2024-06-18 17:09:45 +00:00
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>쳣
|
|
|
|
|
*/
|
2024-06-20 13:36:46 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT SyntaxException {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
private:
|
|
|
|
|
QString msg_store;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SyntaxException(const QString& message);
|
|
|
|
|
virtual ~SyntaxException() = default;
|
|
|
|
|
|
|
|
|
|
virtual QString message() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFB7A8><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD> ===================================================================================================
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľӿ<EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class IContext {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual ~IContext() = default;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
virtual void setCurrentFile(const QString &path) = 0;
|
|
|
|
|
virtual QString currentFile() const = 0;
|
|
|
|
|
|
2024-07-12 09:35:35 +00:00
|
|
|
|
virtual void appendParseErrors(const QString & file_path, int start, const QString &error_msg) = 0;
|
2024-06-27 04:55:07 +00:00
|
|
|
|
virtual QStringList errors() const = 0;
|
2024-07-12 09:35:35 +00:00
|
|
|
|
virtual void clearErrors(const QString &file_path, int start) = 0;
|
2024-06-27 04:55:07 +00:00
|
|
|
|
|
2024-07-12 22:19:30 +00:00
|
|
|
|
virtual void appendDocInst(std::shared_ptr<ast_basic::IExprInst> inst) = 0;
|
|
|
|
|
virtual QList<std::shared_ptr<const ast_basic::IExprInst>> getDocInsts() const = 0;
|
2024-07-12 04:53:43 +00:00
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* \brief <EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽԪ<EFBFBD><EFBFBD>.
|
|
|
|
|
*
|
|
|
|
|
* \return <EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual std::shared_ptr<ast_basic::IExprInst> currentExprInst() const = 0;
|
|
|
|
|
virtual void pushExprInst(std::shared_ptr<ast_basic::IExprInst> current_inst) = 0;
|
|
|
|
|
virtual std::shared_ptr<ast_basic::IExprInst> popExprInst() = 0;
|
|
|
|
|
|
|
|
|
|
virtual std::shared_ptr<const IBasicRule> currentExprRule() const = 0;
|
|
|
|
|
virtual void pushExprRule(std::shared_ptr<const IBasicRule> inst) = 0;
|
|
|
|
|
virtual std::shared_ptr<const IBasicRule> popExprRule() = 0;
|
2024-07-12 22:19:30 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> currentExprRuleStack() const = 0;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class IBasicRule {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual ~IBasicRule() = default;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD>ӹ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const = 0;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
enum class MatchResult {
|
|
|
|
|
Success, // <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
Part, // <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
|
|
|
|
|
Fail // <20>ӵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ƥ<EFBFBD><C6A5>
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-06-18 14:59:41 +00:00
|
|
|
|
* @param rt_inst <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param head <EFBFBD>б<EFBFBD>ͷ
|
|
|
|
|
* @return <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>ͷ,ƥ<EFBFBD>䳤<EFBFBD><EFBFBD>>
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
|
|
|
|
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* \return <EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
virtual QString token_present() const = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief tokenƥ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch> {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
private:
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::shared_ptr<const lib_token::ITokenDefine> define_peer;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2024-06-20 13:36:46 +00:00
|
|
|
|
TokenMatch(std::shared_ptr<const lib_token::ITokenDefine> define);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
// IBasicRule interface
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
|
|
|
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
|
|
|
|
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
virtual QString token_present() const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT Any : public IBasicRule, public std::enable_shared_from_this<Any> {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
private:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
std::tuple<MatchResult, uint, std::shared_ptr<const IBasicRule>, std::shared_ptr<const lib_token::IWordBase>> rule_select(std::shared_ptr<const lib_token::IWordBase> head) const;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
Any(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
// IBasicRule interface
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
|
|
|
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
|
|
|
|
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
virtual QString token_present() const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT Seqs : public IBasicRule, public std::enable_shared_from_this<Seqs> {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
private:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
// IBasicRule interface
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
|
|
|
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
|
|
|
|
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
virtual QString token_present() const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ƥ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT Rept : public IBasicRule, public std::enable_shared_from_this<Rept> {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
private:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
std::shared_ptr<const IBasicRule> rule_peer;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
int min_match, max_match;
|
|
|
|
|
|
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
Rept(std::shared_ptr<const IBasicRule> rule, int min, int max);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
// IBasicRule interface
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
|
|
|
|
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
|
|
|
|
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
virtual QString token_present() const override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ʵ<EFB7A8><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ===================================================================================================
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT ExprRule : public lib_syntax::IBasicRule, public std::enable_shared_from_this<ExprRule> {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-06-20 13:36:46 +00:00
|
|
|
|
typedef QList<std::shared_ptr<const lib_token::IToken>> TokenSeqs;
|
2024-07-12 04:53:43 +00:00
|
|
|
|
ExprRule(const QString& rule_name, int expr_mark);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const IBasicRule> rule);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
virtual QString name() const;
|
|
|
|
|
virtual int typeMark() const;
|
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const = 0;
|
2024-07-12 04:53:43 +00:00
|
|
|
|
virtual std::shared_ptr<ExprRule> makeCopy() const = 0;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
// IBasicRule interface
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const lib_syntax::IBasicRule>> children() const override;
|
|
|
|
|
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
|
|
|
|
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
virtual QString token_present() const override;
|
|
|
|
|
|
|
|
|
|
private:
|
2024-07-12 21:52:32 +00:00
|
|
|
|
std::shared_ptr<const lib_syntax::IBasicRule> child_store;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
QString name_store;
|
|
|
|
|
int mark_store;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <EFBFBD>Ԫ<EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
*/
|
|
|
|
|
template<class ExprType>
|
2024-07-12 04:53:43 +00:00
|
|
|
|
class ElementRule : public ExprRule {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
|
|
|
|
ElementRule(const QString& rule_name, int expr_mark)
|
2024-07-12 04:53:43 +00:00
|
|
|
|
:ExprRule(rule_name, expr_mark){}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const {
|
|
|
|
|
return std::dynamic_pointer_cast<ast_basic::IExprInst>(
|
2024-06-18 03:54:36 +00:00
|
|
|
|
std::make_shared<ExprType>(this->shared_from_this()));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 04:53:43 +00:00
|
|
|
|
virtual std::shared_ptr<ExprRule> makeCopy() const {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
return std::make_shared<ElementRule<ExprType>>(name(), typeMark());
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-17 07:58:28 +00:00
|
|
|
|
} // namespace lib_syntax
|