2024-03-17 07:58:28 +00:00
|
|
|
|
#include "libsyntax.h"
|
|
|
|
|
#include "ast_basic.h"
|
2025-02-02 12:54:32 +00:00
|
|
|
|
#include <tuple>
|
2024-06-27 04:55:07 +00:00
|
|
|
|
#include <QDebug>
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
|
|
using namespace lib_syntax;
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace lib_token;
|
2025-02-02 12:54:32 +00:00
|
|
|
|
using namespace lib_words;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
using namespace ast_basic;
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
Any::Any(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) { }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> Any::children() const {
|
|
|
|
|
return mbrs_store;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> Any::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> result_list;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
if (cursor->mustStop())
|
|
|
|
|
result_list << cursor;
|
|
|
|
|
else
|
|
|
|
|
for (auto rx : this->children())
|
|
|
|
|
result_list.append(rx->parse(cursor));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
return result_list;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QString Any::present() const {
|
|
|
|
|
QString members_content;
|
|
|
|
|
for (auto& it : children()) {
|
|
|
|
|
members_content += it->present() + u8"|";
|
|
|
|
|
}
|
|
|
|
|
return members_content.mid(0, members_content.size() - 1);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
Seqs::Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) { }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> Seqs::children() const {
|
|
|
|
|
return mbrs_store;
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> Seqs::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> results;
|
|
|
|
|
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
|
|
|
|
|
for (auto rule : this->children()) {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> current_result;
|
|
|
|
|
std::for_each(bridge_list.begin(), bridge_list.end(),
|
|
|
|
|
[&](std::shared_ptr<const MatchCursor> vcurs) {
|
|
|
|
|
if (vcurs->mustStop())
|
|
|
|
|
results.push_back(vcurs);
|
|
|
|
|
else {
|
|
|
|
|
current_result.append(rule->parse(vcurs));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bridge_list = current_result;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
results.append(bridge_list);
|
|
|
|
|
return results;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
QString Seqs::present() const {
|
2024-03-17 07:58:28 +00:00
|
|
|
|
QString content;
|
|
|
|
|
for (auto& it : children())
|
2025-02-04 14:26:34 +00:00
|
|
|
|
content += it->present() + " ";
|
2024-07-25 03:54:40 +00:00
|
|
|
|
return content.mid(0, content.size() - 1);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
Rept::Rept(std::shared_ptr<const IBasicRule> rule, int min, int max)
|
|
|
|
|
: rule_peer(rule), min_match(min), max_match(max) { }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> Rept::children() const {
|
|
|
|
|
return QList<std::shared_ptr<const IBasicRule>>() << rule_peer;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> Rept::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> results;
|
|
|
|
|
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
|
|
|
|
|
// <20><>С<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
|
|
|
|
|
for (auto idx = 0; idx < min_match; ++idx) {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> current_list;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ÿһ<C3BF>ο<EFBFBD><CEBF><EFBFBD>ƥ<EFBFBD><C6A5>
|
|
|
|
|
std::for_each(bridge_list.begin(), bridge_list.end(),
|
|
|
|
|
[&](std::shared_ptr<const MatchCursor> curs) {
|
|
|
|
|
if (curs->mustStop())
|
|
|
|
|
results.push_back(curs);
|
|
|
|
|
else {
|
|
|
|
|
current_list.append(this->rule_peer->parse(curs));
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bridge_list = current_list;
|
2024-06-18 17:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
// <20>鲢ʧ<E9B2A2>ܷ<EFBFBD>֧
|
|
|
|
|
std::copy_if(bridge_list.begin(), bridge_list.end(), std::back_inserter(results),
|
|
|
|
|
[&](std::shared_ptr<const MatchCursor> ins) { return ins->mustStop(); });
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>ʧ<EFBFBD>ܷ<EFBFBD>֧
|
|
|
|
|
for (auto idx = 0; idx < bridge_list.size(); ++idx)
|
|
|
|
|
if (bridge_list.at(idx)->mustStop())
|
|
|
|
|
bridge_list.removeAt(idx--);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сƥ<D0A1><C6A5>
|
|
|
|
|
if (!bridge_list.size())
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (auto idx = min_match; idx < max_match; ++idx) {
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> current_list;
|
|
|
|
|
|
|
|
|
|
// ƥ<><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
std::for_each(bridge_list.begin(), bridge_list.end(),
|
|
|
|
|
[&](std::shared_ptr<const MatchCursor> ins) {
|
|
|
|
|
current_list.append(this->rule_peer->parse(ins));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// <20>Ƴ<EFBFBD>ʧ<EFBFBD>ܷ<EFBFBD>֧
|
|
|
|
|
for (auto idx = 0; idx<current_list.size(); ++idx) {
|
|
|
|
|
auto rst_branch = current_list.at(idx);
|
|
|
|
|
if (rst_branch->mustStop()) {
|
|
|
|
|
results.append(rst_branch->previous());
|
|
|
|
|
current_list.removeAt(idx--);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bridge_list = current_list;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
results.append(bridge_list);
|
|
|
|
|
return results;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QString Rept::present() const {
|
|
|
|
|
if (min_match == 0 && max_match == INT_MAX)
|
|
|
|
|
return u8"(" + this->rule_peer->present() + QString(u8")*");
|
|
|
|
|
else if (min_match == 1 && max_match == INT_MAX)
|
|
|
|
|
return u8"(" + this->rule_peer->present() + QString(u8")+");
|
|
|
|
|
else if (min_match == 0 && max_match == 1)
|
|
|
|
|
return u8"(" + this->rule_peer->present() + QString(u8")?");
|
|
|
|
|
|
|
|
|
|
return u8"(" + this->rule_peer->present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match);
|
|
|
|
|
}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
SyntaxException::SyntaxException(const QString& message) {
|
|
|
|
|
this->msg_store = message;
|
|
|
|
|
}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
QString SyntaxException::message() const {
|
|
|
|
|
return msg_store;
|
|
|
|
|
}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
ExprRule::ExprRule(const QString& rule_name, int expr_mark)
|
|
|
|
|
: name_store(rule_name), mark_store(expr_mark) { }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const IBasicRule> rule) const {
|
|
|
|
|
auto ninst = this->make_copy();
|
2024-03-17 07:58:28 +00:00
|
|
|
|
ninst->child_store = rule;
|
|
|
|
|
return ninst;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
QString ExprRule::name() const {
|
|
|
|
|
return name_store;
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-04 14:26:34 +00:00
|
|
|
|
int ExprRule::typeMark() const {
|
|
|
|
|
return this->mark_store;
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-07-12 21:52:32 +00:00
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> ExprRule::children() const {
|
|
|
|
|
return QList<std::shared_ptr<const IBasicRule>>() << this->child_store;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 09:35:35 +00:00
|
|
|
|
#include <ast_novel.h>
|
2025-02-04 14:26:34 +00:00
|
|
|
|
QString ExprRule::present() const {
|
|
|
|
|
return child_store->present();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
MatchCursor::MatchCursor() { }
|
2025-02-04 14:26:34 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
lib_syntax::MatchCursor::MatchCursor(std::shared_ptr<const MatchCursor> other_ptr)
|
|
|
|
|
: _prev_cursor(other_ptr),
|
|
|
|
|
_total_errors(other_ptr->_total_errors),
|
|
|
|
|
_exprs_errors(other_ptr->_exprs_errors),
|
|
|
|
|
_current_token(other_ptr->_current_token),
|
|
|
|
|
_remains_word(other_ptr->_remains_word) { }
|
2025-02-04 14:26:34 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
std::shared_ptr<const MatchCursor> lib_syntax::MatchCursor::previous() const {
|
|
|
|
|
return _prev_cursor;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void MatchCursor::enterExprs() {
|
|
|
|
|
auto new_expr = std::make_shared<ErrsPack>();
|
|
|
|
|
this->_exprs_errors.push_back(new_expr);
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void MatchCursor::logExprsError(const QString& msg) {
|
|
|
|
|
this->_total_errors.push_back(msg);
|
|
|
|
|
this->_exprs_errors.last()->addError(msg);
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void MatchCursor::quitExprs() {
|
|
|
|
|
this->_exprs_errors.pop_back();
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
bool MatchCursor::mustStop() const {
|
|
|
|
|
return exprsErrorCount() >= 2;
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
int MatchCursor::exprsErrorCount() const {
|
|
|
|
|
return this->_exprs_errors.last()->errorCount();
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
int MatchCursor::totalErrorCount() const {
|
|
|
|
|
return this->_total_errors.size();
|
2025-02-04 14:26:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
QList<QString> MatchCursor::totalErrors() const {
|
2025-02-04 14:26:34 +00:00
|
|
|
|
return this->_total_errors;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void MatchCursor::setCurrent(std::shared_ptr<const IActionToken> t, std::shared_ptr<const IPrimitiveWord> remains) {
|
2025-02-04 14:26:34 +00:00
|
|
|
|
this->_current_token = t;
|
|
|
|
|
this->_remains_word = remains;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
std::shared_ptr<const IActionToken> MatchCursor::currentToken() const {
|
2025-02-04 14:26:34 +00:00
|
|
|
|
return this->_current_token;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
std::shared_ptr<const IPrimitiveWord> MatchCursor::currentWords() const {
|
2025-02-04 14:26:34 +00:00
|
|
|
|
return this->_remains_word;
|
2024-06-18 17:09:45 +00:00
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
void MatchCursor::ErrsPack::addError(const QString& msg) {
|
|
|
|
|
this->_error_collection.append(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QString> MatchCursor::ErrsPack::errors() const {
|
|
|
|
|
return _error_collection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t MatchCursor::ErrsPack::errorCount() const {
|
|
|
|
|
return _error_collection.size();
|
|
|
|
|
}
|