2024-03-17 07:58:28 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "libsyntax_global.h"
|
2025-02-02 12:54:32 +00:00
|
|
|
|
#include "libtokens.h"
|
2025-02-07 15:26:20 +00:00
|
|
|
|
#include "tokens_impl.h"
|
2024-03-17 07:58:28 +00:00
|
|
|
|
#include <memory>
|
2025-02-02 12:54:32 +00:00
|
|
|
|
#include <QtCore/QList>
|
2025-02-14 01:26:42 +00:00
|
|
|
|
#include <QtCore/QDebug>
|
2025-02-24 07:02:23 +00:00
|
|
|
|
#include <QtCore/QHash>
|
2024-03-17 07:58:28 +00:00
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <functional>
|
2025-02-07 15:26:20 +00:00
|
|
|
|
#include "ast_basic.h"
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
|
|
namespace lib_syntax {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 语法异常
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 基础语法解析接口 ====================================================================================
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 解析上下文接口
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-07 15:26:20 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT MatchCursor {
|
2025-02-04 14:26:34 +00:00
|
|
|
|
public:
|
2025-02-07 15:26:20 +00:00
|
|
|
|
class ErrsPack {
|
|
|
|
|
public:
|
2025-02-14 07:33:23 +00:00
|
|
|
|
ErrsPack();
|
|
|
|
|
ErrsPack(const ErrsPack& other);
|
|
|
|
|
|
2025-02-24 07:02:23 +00:00
|
|
|
|
void addError(std::shared_ptr<const lib_words::IPrimitiveWord>, const QString& msg);
|
|
|
|
|
QList<std::pair<std::shared_ptr<const lib_words::IPrimitiveWord>, QString>> errors() const;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
uint64_t errorCount() const;
|
|
|
|
|
|
|
|
|
|
private:
|
2025-02-24 07:02:23 +00:00
|
|
|
|
QList<std::pair<std::shared_ptr<const lib_words::IPrimitiveWord>, QString>> _error_collection;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2025-02-08 05:40:36 +00:00
|
|
|
|
MatchCursor(const QString& path);
|
2025-02-07 15:26:20 +00:00
|
|
|
|
MatchCursor(std::shared_ptr<const MatchCursor> other_ptr);
|
2025-02-04 14:26:34 +00:00
|
|
|
|
virtual ~MatchCursor() = default;
|
|
|
|
|
|
2025-02-19 05:43:34 +00:00
|
|
|
|
bool operator>(const MatchCursor& other) const;
|
2025-02-17 05:59:25 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
virtual std::shared_ptr<const MatchCursor> previous() const;
|
2025-02-08 05:40:36 +00:00
|
|
|
|
virtual QString filePath() const;
|
2025-02-14 17:48:14 +00:00
|
|
|
|
virtual QString parseSyntax() const;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
virtual void enterExprs();
|
2025-02-24 07:02:23 +00:00
|
|
|
|
virtual void logExprsError(std::shared_ptr<const lib_words::IPrimitiveWord> t, const QString& msg);
|
2025-02-04 14:26:34 +00:00
|
|
|
|
virtual void quitExprs();
|
|
|
|
|
virtual int exprsErrorCount() const;
|
|
|
|
|
virtual int totalErrorCount() const;
|
|
|
|
|
virtual QList<QString> totalErrors() const;
|
2025-02-24 07:02:23 +00:00
|
|
|
|
virtual void mergeWith(const MatchCursor &other);
|
2025-02-04 14:26:34 +00:00
|
|
|
|
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual bool parseFailure() const;
|
|
|
|
|
virtual void setFailure(bool mark = true);
|
|
|
|
|
virtual bool parseComplete() const;
|
|
|
|
|
virtual void setComplete(bool mark = true);
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
virtual void setCurrent(std::shared_ptr<const lib_token::IActionToken> t, std::shared_ptr<const lib_words::IPrimitiveWord> remains);
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual std::shared_ptr<const lib_token::IActionToken> token() const;
|
|
|
|
|
virtual std::shared_ptr<const lib_words::IPrimitiveWord> words() const;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2025-02-08 05:40:36 +00:00
|
|
|
|
QString _file_path;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
std::shared_ptr<const MatchCursor> _prev_cursor = nullptr;
|
2025-02-15 04:57:53 +00:00
|
|
|
|
bool _parse_stop_with_errors = false, _parse_complete = false;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
2025-02-24 07:02:23 +00:00
|
|
|
|
QHash<qulonglong, QStringList> _total_errors; // 所有解析错误
|
2025-02-11 14:32:10 +00:00
|
|
|
|
QList<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误
|
2025-02-14 17:48:14 +00:00
|
|
|
|
std::shared_ptr<const lib_token::IActionToken> _current_token = nullptr; // 当前Token
|
2025-02-11 14:32:10 +00:00
|
|
|
|
std::shared_ptr<const lib_words::IPrimitiveWord> _remains_word = nullptr; // 剩余词语
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 基础语法匹配规则接口
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2024-07-12 21:52:32 +00:00
|
|
|
|
class IBasicRule {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 子规则
|
2025-02-04 14:26:34 +00:00
|
|
|
|
* @return
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-04 14:26:34 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const = 0;
|
2025-02-11 10:31:39 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* 返回匹配语法规则的词法序列表达
|
|
|
|
|
*
|
|
|
|
|
* \return 词法表达序列
|
|
|
|
|
*/
|
|
|
|
|
virtual QString present() const = 0;
|
2025-02-11 10:31:39 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 解析
|
|
|
|
|
* @param cursor 解析游标
|
2025-02-14 17:48:14 +00:00
|
|
|
|
* @param out 解析结束的分支存储集合
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @return 返回结果<匹配分支>
|
|
|
|
|
*/
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 组合语法实体解析 =====================================================================================
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 语法规则或匹配
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-14 01:26:42 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT __anyone_impl : public IBasicRule, public std::enable_shared_from_this<__anyone_impl> {
|
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:
|
2025-02-14 01:26:42 +00:00
|
|
|
|
__anyone_impl(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;
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
virtual QString present() const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 语法规则序列匹配
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-14 01:26:42 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT __sequence_impl : public IBasicRule, public std::enable_shared_from_this<__sequence_impl> {
|
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:
|
2025-02-14 01:26:42 +00:00
|
|
|
|
__sequence_impl(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;
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
virtual QString present() const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 语法规则重复匹配
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-14 01:26:42 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT __repeat_impl : public IBasicRule, public std::enable_shared_from_this<__repeat_impl> {
|
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:
|
2025-02-14 01:26:42 +00:00
|
|
|
|
__repeat_impl(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;
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
virtual QString present() const override;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
2025-02-08 05:40:36 +00:00
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 对应语法表达式解析规则
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-07 15:26:20 +00:00
|
|
|
|
class LIBSYNTAX_EXPORT ExprRule : public IBasicRule, public std::enable_shared_from_this<ExprRule> {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2024-07-12 04:53:43 +00:00
|
|
|
|
ExprRule(const QString& rule_name, int expr_mark);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
virtual QString name() const;
|
|
|
|
|
virtual int typeMark() const;
|
|
|
|
|
|
2024-07-13 05:17:56 +00:00
|
|
|
|
protected:
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
|
2024-07-13 05:17:56 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2024-06-18 03:54:36 +00:00
|
|
|
|
QString name_store;
|
|
|
|
|
int mark_store;
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief token匹配
|
2025-02-07 15:26:20 +00:00
|
|
|
|
*/
|
2025-02-12 02:13:35 +00:00
|
|
|
|
template<typename ELEM, lib_token::TokenProc<ELEM> XProc = nullptr>
|
2025-02-11 15:36:30 +00:00
|
|
|
|
requires std::derived_from<ELEM, ast_basic::IExprInstance>
|
2025-02-14 01:26:42 +00:00
|
|
|
|
class __token_match_impl : public IBasicRule, public std::enable_shared_from_this<__token_match_impl<ELEM, XProc>> {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
private:
|
|
|
|
|
std::shared_ptr<const lib_token::ITokenProcess> _define_peers;
|
|
|
|
|
|
|
|
|
|
public:
|
2025-02-19 05:43:34 +00:00
|
|
|
|
__token_match_impl(std::shared_ptr<const lib_token::ITokenProcess> define) : _define_peers(define) {
|
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
// IBasicRule interface
|
|
|
|
|
public:
|
|
|
|
|
virtual QString present() const override {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
return QString("%1").arg(this->_define_peers->reviseWords());
|
2025-02-07 15:26:20 +00:00
|
|
|
|
}
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> current) const override {
|
2025-02-12 13:50:45 +00:00
|
|
|
|
// 如果提前结束,记录错误并返回
|
2025-02-15 04:57:53 +00:00
|
|
|
|
if (!current->words()) {
|
|
|
|
|
// 只有在表达式的起始点遇到nullptr,才是正常结束。
|
|
|
|
|
if (current->token()->tokenType() != lib_token::IActionToken::Type::ElementBegin) {
|
|
|
|
|
auto ncurs = std::make_shared<MatchCursor>(current);
|
2025-02-24 07:02:23 +00:00
|
|
|
|
ncurs->logExprsError(nullptr, QString("SyntaxError[0x00001]输入错误,程序提前结束:%1。").arg(current->filePath()));
|
2025-02-15 04:57:53 +00:00
|
|
|
|
ncurs->setFailure();
|
|
|
|
|
return QList<std::shared_ptr<const MatchCursor>>() << ncurs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (current->token()->tokenType() == lib_token::IActionToken::Type::ElementBegin)
|
|
|
|
|
current = current->previous();
|
2025-02-14 17:48:14 +00:00
|
|
|
|
|
2025-02-15 04:57:53 +00:00
|
|
|
|
auto ncurs = std::make_shared<MatchCursor>(current);
|
|
|
|
|
ncurs->setComplete();
|
|
|
|
|
return QList<std::shared_ptr<const MatchCursor>>() << ncurs;
|
2025-02-14 01:26:42 +00:00
|
|
|
|
}
|
2025-02-08 05:40:36 +00:00
|
|
|
|
|
2025-02-15 04:57:53 +00:00
|
|
|
|
auto w_this = current->words();
|
|
|
|
|
auto t_this = current->token();
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto match_result = _define_peers->analysis(w_this);
|
2025-02-12 13:50:45 +00:00
|
|
|
|
// 解析成功
|
2025-02-07 15:26:20 +00:00
|
|
|
|
if (std::get<0>(match_result)) {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
auto chain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(std::get<0>(match_result), t_this);
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto remains = w_this->nextWord();
|
|
|
|
|
if (std::get<1>(match_result)) {
|
|
|
|
|
remains = std::make_shared<lib_words::WordImpl>(std::get<1>(match_result), remains);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto clone_ins = std::make_shared<MatchCursor>(current);
|
|
|
|
|
clone_ins->setCurrent(chain, remains);
|
|
|
|
|
return QList<std::shared_ptr<const MatchCursor>>() << clone_ins;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> retvals;
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 少一个
|
2025-02-12 13:50:45 +00:00
|
|
|
|
auto short_one = std::make_shared<MatchCursor>(current);
|
2025-02-24 07:02:23 +00:00
|
|
|
|
short_one->logExprsError(w_this, QString("SyntaxError[0x00002]语法匹配错误,缺失\"%1\"<row:%2,col:%3,file<%4>>")
|
2025-02-17 05:59:25 +00:00
|
|
|
|
.arg(this->_define_peers->reviseWords()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
|
|
|
|
|
auto tkins0 = std::make_shared<lib_token::TokenContent>(w_this->row(), w_this->column(), w_this->position(),
|
|
|
|
|
QString("<+%1>").arg(this->_define_peers->reviseWords()), w_this->file(), this->_define_peers);
|
|
|
|
|
auto tkchain0 = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(tkins0, t_this);
|
|
|
|
|
short_one->setCurrent(tkchain0, w_this);
|
2025-02-12 13:50:45 +00:00
|
|
|
|
retvals << short_one;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 错一个
|
2025-02-12 13:50:45 +00:00
|
|
|
|
auto error_one = std::make_shared<MatchCursor>(current);
|
2025-02-24 07:02:23 +00:00
|
|
|
|
error_one->logExprsError(w_this, QString("SyntaxError[0x00003]语法匹配错误,请修正\"%1\"<row:%2,col:%3,file<%4>>")
|
2025-02-12 13:50:45 +00:00
|
|
|
|
.arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
|
2025-02-17 05:59:25 +00:00
|
|
|
|
auto tkins = std::make_shared<lib_token::TokenContent>(w_this->row(), w_this->column(), w_this->position(),
|
|
|
|
|
QString("<%1(%2)>").arg(w_this->content()).arg(this->_define_peers->reviseWords()), w_this->file(), this->_define_peers);
|
2025-02-12 13:50:45 +00:00
|
|
|
|
auto tkchain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(tkins, t_this);
|
|
|
|
|
error_one->setCurrent(tkchain, w_this->nextWord());
|
|
|
|
|
retvals << error_one;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// 多一个
|
2025-02-15 04:57:53 +00:00
|
|
|
|
auto nx_word = w_this->nextWord();
|
|
|
|
|
if (nx_word) {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto nx_result = this->_define_peers->analysis(nx_word);
|
|
|
|
|
if (std::get<0>(nx_result)) {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
auto chain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(std::get<0>(nx_result), t_this);
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto remains = nx_word->nextWord();
|
|
|
|
|
if (std::get<1>(nx_result)) {
|
|
|
|
|
remains = std::make_shared<lib_words::WordImpl>(std::get<1>(nx_result), remains);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto clone_ins = std::make_shared<MatchCursor>(current);
|
2025-02-24 07:02:23 +00:00
|
|
|
|
clone_ins->logExprsError(w_this, QString("SyntaxError[0x00004]语法匹配错误,请删除\"%1\"<row:%2,col:%3,file<%4>>")
|
2025-02-08 05:40:36 +00:00
|
|
|
|
.arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
|
2025-02-07 15:26:20 +00:00
|
|
|
|
clone_ins->setCurrent(chain, remains);
|
|
|
|
|
retvals << clone_ins;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retvals;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const override {
|
|
|
|
|
return QList<std::shared_ptr<const IBasicRule>>();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
/**
|
2025-02-11 14:32:10 +00:00
|
|
|
|
* @brief 基础模板化语法元素解析规则.
|
2024-06-18 03:54:36 +00:00
|
|
|
|
*/
|
2025-02-12 05:44:35 +00:00
|
|
|
|
template<class ExprType, int mark, typename R>
|
|
|
|
|
requires std::derived_from<ExprType, ast_basic::IExprInstance>&& std::derived_from<R, lib_syntax::IBasicRule>
|
2025-02-11 15:36:30 +00:00
|
|
|
|
class ElementRule : public ExprRule {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
ElementRule(const QString& rule_name)
|
2025-02-19 05:43:34 +00:00
|
|
|
|
:ExprRule(rule_name, mark), _children_store(std::make_shared<R>()) {
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
virtual QList<std::shared_ptr<const IBasicRule>> children() const {
|
|
|
|
|
return QList<std::shared_ptr<const IBasicRule>>() << this->_children_store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual QString present() const {
|
|
|
|
|
return this->_children_store->present();
|
|
|
|
|
}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-15 04:57:53 +00:00
|
|
|
|
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
2025-02-12 13:50:45 +00:00
|
|
|
|
// 提前结束,直接返回
|
2025-02-24 07:02:23 +00:00
|
|
|
|
if (cursor->parseFailure() || cursor->parseComplete())
|
2025-02-07 15:26:20 +00:00
|
|
|
|
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
|
|
|
|
|
2025-02-14 07:33:23 +00:00
|
|
|
|
auto syntax = present();
|
|
|
|
|
|
2025-02-15 04:57:53 +00:00
|
|
|
|
auto t_this = cursor->token();
|
|
|
|
|
auto w_this = cursor->words();
|
2025-02-12 13:50:45 +00:00
|
|
|
|
// 起始Token打点
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto split_begin = std::make_shared<lib_token::ExprBeginToken<ExprType>>(shared_from_this(), t_this);
|
|
|
|
|
|
|
|
|
|
auto ncursor = std::make_shared<MatchCursor>(cursor);
|
|
|
|
|
ncursor->setCurrent(split_begin, w_this);
|
|
|
|
|
ncursor->enterExprs();
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-12 13:50:45 +00:00
|
|
|
|
// 表达式语法解析
|
2025-02-15 04:57:53 +00:00
|
|
|
|
auto nbranch = this->expr_rule_parse(ncursor);
|
2025-02-14 01:26:42 +00:00
|
|
|
|
|
|
|
|
|
// 语法完全匹配的分支
|
2025-02-12 13:50:45 +00:00
|
|
|
|
decltype(nbranch) branch_procs;
|
2025-02-14 01:26:42 +00:00
|
|
|
|
std::copy_if(nbranch.begin(), nbranch.end(),
|
|
|
|
|
std::back_inserter(branch_procs),
|
|
|
|
|
[&](std::shared_ptr<const MatchCursor> ins) {
|
2025-02-15 04:57:53 +00:00
|
|
|
|
return ins->totalErrorCount() == cursor->totalErrorCount() || ins->parseComplete();
|
2025-02-14 01:26:42 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 语法修正后能匹配的分支
|
|
|
|
|
if (!branch_procs.size()) {
|
|
|
|
|
std::copy_if(nbranch.begin(), nbranch.end(),
|
|
|
|
|
std::back_inserter(branch_procs),
|
2025-02-15 04:57:53 +00:00
|
|
|
|
[](std::shared_ptr<const MatchCursor> ins) { return !ins->parseFailure(); });
|
2025-02-24 07:02:23 +00:00
|
|
|
|
|
|
|
|
|
if(branch_procs.size()){
|
|
|
|
|
auto first_cursor = branch_procs.first();
|
|
|
|
|
for (auto curr = ++branch_procs.begin(); curr != branch_procs.end(); curr++) {
|
|
|
|
|
std::const_pointer_cast<lib_syntax::MatchCursor>(first_cursor)->mergeWith(**curr);
|
|
|
|
|
}
|
|
|
|
|
branch_procs = { first_cursor };
|
|
|
|
|
}
|
2025-02-14 01:26:42 +00:00
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
2025-02-19 05:43:34 +00:00
|
|
|
|
// 表达式匹配结尾
|
|
|
|
|
if (branch_procs.size()) {
|
|
|
|
|
decltype(nbranch) results_fnl;
|
|
|
|
|
for (auto curs : branch_procs) {
|
2025-02-15 04:57:53 +00:00
|
|
|
|
auto t_end = curs->token();
|
|
|
|
|
auto w_end = curs->words();
|
2025-02-14 07:33:23 +00:00
|
|
|
|
|
|
|
|
|
auto ecursor = std::make_shared<MatchCursor>(curs);
|
|
|
|
|
ecursor->quitExprs();
|
|
|
|
|
|
|
|
|
|
auto split_end = std::make_shared<lib_token::ExprEndToken<ExprType>>(split_begin, t_end);
|
|
|
|
|
ecursor->setCurrent(split_end, w_end);
|
|
|
|
|
results_fnl.append(ecursor);
|
2025-02-17 05:59:25 +00:00
|
|
|
|
|
|
|
|
|
if (curs->totalErrorCount() == cursor->totalErrorCount()) {
|
|
|
|
|
results_fnl.clear();
|
|
|
|
|
results_fnl.append(ecursor);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-02-14 07:33:23 +00:00
|
|
|
|
}
|
2025-02-19 05:43:34 +00:00
|
|
|
|
return results_fnl;
|
2025-02-14 07:33:23 +00:00
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
2025-02-19 05:43:34 +00:00
|
|
|
|
// 匹配失败
|
2025-02-24 07:02:23 +00:00
|
|
|
|
return nbranch;
|
2025-02-07 15:26:20 +00:00
|
|
|
|
}
|
2025-02-08 05:40:36 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
protected:
|
2025-02-11 14:32:10 +00:00
|
|
|
|
std::shared_ptr<const IBasicRule> _children_store;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
};
|
2024-03-17 07:58:28 +00:00
|
|
|
|
} // namespace lib_syntax
|