WsParser_VS/libSyntax/libsyntax.h

327 lines
11 KiB
C
Raw Normal View History

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>
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 {
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
class LIBSYNTAX_EXPORT SyntaxException {
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
// 基础语法解析接口 ====================================================================================
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
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:
void addError(const QString& msg);
QList<QString> errors() const;
uint64_t errorCount() const;
private:
QList<QString> _error_collection;
};
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-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-07 15:26:20 +00:00
virtual void enterExprs();
2025-02-04 14:26:34 +00:00
virtual void logExprsError(const QString& msg);
virtual void quitExprs();
virtual bool mustStop() const;
virtual int exprsErrorCount() const;
virtual int totalErrorCount() const;
virtual QList<QString> totalErrors() const;
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);
virtual std::shared_ptr<const lib_token::IActionToken> currentToken() const;
virtual std::shared_ptr<const lib_words::IPrimitiveWord> currentWords() const;
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-11 14:32:10 +00:00
QList<QString> _total_errors; // 所有解析错误
QList<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误
std::shared_ptr<const lib_token::IActionToken> _current_token = nullptr; // 当前Token
std::shared_ptr<const lib_words::IPrimitiveWord> _remains_word = nullptr; // 剩余词语
2025-02-08 05:40:36 +00:00
bool parse_stop() const;
};
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
2024-07-12 21:52:32 +00:00
class IBasicRule {
public:
/**
2025-02-11 14:32:10 +00:00
* @brief
2025-02-04 14:26:34 +00:00
* @return
*/
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
* @return <>
*/
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
};
2025-02-11 14:32:10 +00:00
// 组合语法实体解析 =====================================================================================
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
2024-07-12 21:52:32 +00:00
class LIBSYNTAX_EXPORT Any : public IBasicRule, public std::enable_shared_from_this<Any> {
private:
2024-07-12 21:52:32 +00:00
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
public:
2024-07-12 21:52:32 +00:00
Any(const QList<std::shared_ptr<const IBasicRule>> mbrs);
2024-07-12 21:52:32 +00:00
// IBasicRule interface
public:
2024-07-12 21:52:32 +00:00
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
2025-02-07 15:26:20 +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;
};
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
2024-07-12 21:52:32 +00:00
class LIBSYNTAX_EXPORT Seqs : public IBasicRule, public std::enable_shared_from_this<Seqs> {
private:
2024-07-12 21:52:32 +00:00
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
public:
2024-07-12 21:52:32 +00:00
Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs);
2024-07-12 21:52:32 +00:00
// IBasicRule interface
public:
2024-07-12 21:52:32 +00:00
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
2025-02-07 15:26:20 +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;
};
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
2024-07-12 21:52:32 +00:00
class LIBSYNTAX_EXPORT Rept : public IBasicRule, public std::enable_shared_from_this<Rept> {
private:
2024-07-12 21:52:32 +00:00
std::shared_ptr<const IBasicRule> rule_peer;
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-07-12 21:52:32 +00:00
// IBasicRule interface
public:
2024-07-12 21:52:32 +00:00
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
2025-02-07 15:26:20 +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;
};
2025-02-08 05:40:36 +00:00
/**
2025-02-11 14:32:10 +00:00
* @brief
*/
2025-02-07 15:26:20 +00:00
class LIBSYNTAX_EXPORT ExprRule : public IBasicRule, public std::enable_shared_from_this<ExprRule> {
public:
ExprRule(const QString& rule_name, int expr_mark);
virtual QString name() const;
virtual int typeMark() const;
2024-07-13 05:17:56 +00:00
protected:
2025-02-11 14:32:10 +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:
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-11 14:32:10 +00:00
class TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch<ELEM, XProc>> {
2025-02-07 15:26:20 +00:00
private:
std::shared_ptr<const lib_token::ITokenProcess> _define_peers;
public:
TokenMatch(std::shared_ptr<const lib_token::ITokenProcess> define) : _define_peers(define) { }
// 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
}
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> current) const override {
auto w_this = current->currentWords();
2025-02-12 13:50:45 +00:00
// 如果提前结束,记录错误并返回
2025-02-08 05:40:36 +00:00
if (!w_this) {
auto clone_ins = std::make_shared<MatchCursor>(current);
2025-02-11 14:32:10 +00:00
clone_ins->logExprsError(QString("Syntax[0x00001]语法匹配错误,缺失\"%1\"<file<%2>>")
2025-02-08 05:40:36 +00:00
.arg(this->_define_peers->reviseWords()).arg(current->filePath()));
2025-02-11 14:32:10 +00:00
clone_ins->logExprsError(QString("Syntax[0x00001]输入流提前结束,<%1>").arg(current->filePath()));
2025-02-08 05:40:36 +00:00
return QList<std::shared_ptr<const MatchCursor>>() << clone_ins;
}
2025-02-07 15:26:20 +00:00
auto t_this = current->currentToken();
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);
short_one->logExprsError(QString("Syntax[0x00001]语法匹配错误,缺失\"%1\"<row:%2,col:%3,file<%4>>")
.arg(this->_define_peers->reviseWords())
.arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
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);
error_one->logExprsError(QString("Syntax[0x00001]语法匹配错误,请修正\"%1\"<row:%2,col:%3,file<%4>>")
.arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
auto tkins = std::make_shared<lib_token::TokenContent>(
w_this->row(), w_this->column(), w_this->position(),
QString("%2_%1").arg((uint64_t) error_one.get()).arg(this->_define_peers->reviseWords()),
w_this->file(), this->_define_peers);
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-08 05:40:36 +00:00
if (w_this->nextWord()) {
2025-02-07 15:26:20 +00:00
auto nx_word = w_this->nextWord();
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-11 14:32:10 +00:00
clone_ins->logExprsError(QString("Syntax[0x00001]语法匹配错误,请删除\"%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>>();
}
};
/**
2025-02-11 14:32:10 +00:00
* @brief .
*/
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 {
public:
2025-02-12 05:44:35 +00:00
ElementRule(const QString& rule_name)
: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();
}
2025-02-07 15:26:20 +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-07 15:26:20 +00:00
if (cursor->mustStop())
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
auto t_this = cursor->currentToken();
auto w_this = cursor->currentWords();
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();
2025-02-12 13:50:45 +00:00
// 表达式语法解析
2025-02-11 14:32:10 +00:00
auto nbranch = this->expr_rule_parse(ncursor);
2025-02-12 13:50:45 +00:00
decltype(nbranch) branch_procs;
std::for_each(nbranch.begin(), nbranch.end(), [&](std::shared_ptr<const MatchCursor> curs) {
2025-02-07 15:26:20 +00:00
if (curs->mustStop()) {
branch_procs.append(curs);
}
else {
auto t_end = curs->currentToken();
auto w_end = curs->currentWords();
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);
branch_procs.append(ecursor);
}
2025-02-11 14:32:10 +00:00
});
2025-02-07 15:26:20 +00:00
return branch_procs;
}
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-03-17 07:58:28 +00:00
} // namespace lib_syntax