313 lines
9.3 KiB
C++
313 lines
9.3 KiB
C++
#include "libsyntax.h"
|
|
#include "ast_basic.h"
|
|
#include <tuple>
|
|
#include <QDebug>
|
|
|
|
using namespace lib_syntax;
|
|
using namespace std;
|
|
using namespace lib_token;
|
|
using namespace lib_words;
|
|
using namespace ast_basic;
|
|
|
|
__anyone_impl::__anyone_impl(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) { }
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> __anyone_impl::children() const {
|
|
return mbrs_store;
|
|
}
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> __anyone_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
|
if (cursor->mustStop())
|
|
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> result_list;
|
|
for (auto rx : this->children())
|
|
result_list.append(rx->parse(cursor));
|
|
|
|
// 完全匹配分支
|
|
decltype(result_list) completely_list;
|
|
std::copy_if(result_list.begin(), result_list.end(), std::back_inserter(completely_list),
|
|
[&](std::shared_ptr<const MatchCursor> ins) { return cursor->totalErrorCount() == ins->totalErrorCount(); });
|
|
if (completely_list.size())
|
|
return completely_list;
|
|
|
|
// 经过修正的分支
|
|
decltype(result_list) modify_list;
|
|
std::copy_if(result_list.begin(), result_list.end(), std::back_inserter(modify_list),
|
|
[&](std::shared_ptr<const MatchCursor> ins) { return !ins->mustStop(); });
|
|
if (modify_list.size())
|
|
return modify_list;
|
|
|
|
return result_list;
|
|
}
|
|
|
|
QString __anyone_impl::present() const {
|
|
QString members_content;
|
|
for (auto& it : children()) {
|
|
members_content += it->present() + "|";
|
|
}
|
|
return members_content.mid(0, members_content.size() - 1);
|
|
}
|
|
|
|
__sequence_impl::__sequence_impl(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) { }
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> __sequence_impl::children() const {
|
|
return mbrs_store;
|
|
}
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> __sequence_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
|
if (cursor->mustStop())
|
|
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
|
|
|
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()) {
|
|
current_result.append(rule->parse(vcurs));
|
|
}
|
|
else {
|
|
results.push_back(vcurs);
|
|
}
|
|
});
|
|
|
|
bridge_list = current_result;
|
|
}
|
|
|
|
// 完全匹配分支集合
|
|
decltype(bridge_list) completely_list;
|
|
std::copy_if(bridge_list.begin(), bridge_list.end(), std::back_inserter(completely_list),
|
|
[&](std::shared_ptr<const MatchCursor> ins) { return ins->totalErrorCount() == cursor->totalErrorCount(); });
|
|
if (completely_list.size())
|
|
return completely_list;
|
|
|
|
// 经过修正的分支
|
|
decltype(bridge_list) modify_list;
|
|
std::copy_if(bridge_list.begin(), bridge_list.end(), std::back_inserter(modify_list),
|
|
[](std::shared_ptr<const MatchCursor> ins) { return !ins->mustStop(); });
|
|
if (modify_list.size())
|
|
return modify_list;
|
|
|
|
results.append(bridge_list);
|
|
return results;
|
|
}
|
|
|
|
QString __sequence_impl::present() const {
|
|
QString content;
|
|
for (auto& it : children())
|
|
content += it->present() + " ";
|
|
return content.mid(0, content.size() - 1);
|
|
}
|
|
|
|
__repeat_impl::__repeat_impl(std::shared_ptr<const IBasicRule> rule, int min, int max)
|
|
: rule_peer(rule), min_match(min), max_match(max) { }
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> __repeat_impl::children() const {
|
|
return QList<std::shared_ptr<const IBasicRule>>() << rule_peer;
|
|
}
|
|
|
|
#include <algorithm>
|
|
QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
|
if (cursor->mustStop())
|
|
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> max_match_begin = { cursor };
|
|
if (min_match) {
|
|
QList<std::shared_ptr<const IBasicRule>> temp_rules;
|
|
for (auto idx = 0; idx < min_match; ++idx)
|
|
temp_rules << this->rule_peer;
|
|
|
|
auto seqs_rule = std::make_shared<__sequence_impl>(temp_rules);
|
|
max_match_begin = seqs_rule->parse(cursor);
|
|
}
|
|
|
|
// 如果不满足最小重复匹配次数要求,则返回
|
|
int continue_count = std::count_if(max_match_begin.begin(), max_match_begin.end(),
|
|
[](std::shared_ptr<const MatchCursor > ins) { return !ins->mustStop(); });
|
|
if (!continue_count)
|
|
return max_match_begin;
|
|
|
|
// 最小匹配次数中所有错误分支都是无用的、需要舍弃
|
|
for (auto idx = 0; idx < max_match_begin.size(); ++idx) {
|
|
auto current_cursor = max_match_begin.at(idx);
|
|
if (current_cursor->mustStop())
|
|
max_match_begin.removeAt(idx--);
|
|
}
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> results = max_match_begin;
|
|
decltype(results) bridge_list = max_match_begin;
|
|
// 尝试重复匹配最大次数
|
|
for (auto idx = min_match; idx < max_match && bridge_list.size(); ++idx) {
|
|
QList<std::shared_ptr<const MatchCursor>> current_list;
|
|
|
|
// 匹配迭代一次
|
|
std::for_each(bridge_list.begin(), bridge_list.end(),
|
|
[&](std::shared_ptr<const MatchCursor> ins) {
|
|
current_list.append(this->rule_peer->parse(ins));
|
|
});
|
|
|
|
for (auto vdx = 0; vdx < current_list.size(); ++vdx) {
|
|
auto rst_branch = current_list.at(vdx);
|
|
if (rst_branch->mustStop()) {
|
|
results.append(rst_branch);
|
|
current_list.removeAt(vdx--);
|
|
}
|
|
}
|
|
|
|
bridge_list = current_list;
|
|
}
|
|
results.append(bridge_list);
|
|
|
|
|
|
std::sort(results.begin(), results.end(),
|
|
[](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) {
|
|
return a->currentToken()->position() > b->currentToken()->position();
|
|
});
|
|
|
|
// 提取完全匹配的分支
|
|
decltype(results) rets_completely;
|
|
std::for_each(results.begin(), results.end(),
|
|
[&](std::shared_ptr<const MatchCursor> ins) {
|
|
if (ins->totalErrorCount() == cursor->totalErrorCount()) {
|
|
if (!rets_completely.size()) {
|
|
rets_completely.append(ins);
|
|
}
|
|
else if (rets_completely.last()->currentToken()->position() == ins->currentToken()->position()) {
|
|
rets_completely.append(ins);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 提取经过修正的分支
|
|
decltype(results) rets_modified;
|
|
std::for_each(results.begin(), results.end(),
|
|
[&](std::shared_ptr<const MatchCursor> ins) {
|
|
if (!ins->mustStop()) {
|
|
if (!rets_modified.size()) {
|
|
rets_modified.append(ins);
|
|
}
|
|
else if (rets_modified.last()->currentToken()->position() == ins->currentToken()->position()) {
|
|
rets_modified.append(ins);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 允许持续的集合
|
|
rets_completely.append(rets_modified);
|
|
if (rets_completely.size())
|
|
return rets_completely;
|
|
|
|
return results;
|
|
}
|
|
|
|
QString __repeat_impl::present() const {
|
|
if (min_match == 0 && max_match == INT_MAX)
|
|
return "(" + this->rule_peer->present() + QString(")*");
|
|
else if (min_match == 1 && max_match == INT_MAX)
|
|
return "(" + this->rule_peer->present() + QString(")+");
|
|
else if (min_match == 0 && max_match == 1)
|
|
return "(" + this->rule_peer->present() + QString(")?");
|
|
|
|
return "(" + this->rule_peer->present() + QString("){%1, %2}").arg(min_match).arg(max_match);
|
|
}
|
|
|
|
SyntaxException::SyntaxException(const QString& message) {
|
|
this->msg_store = message;
|
|
}
|
|
|
|
QString SyntaxException::message() const {
|
|
return msg_store;
|
|
}
|
|
|
|
ExprRule::ExprRule(const QString& rule_name, int expr_mark)
|
|
: name_store(rule_name), mark_store(expr_mark) { }
|
|
|
|
QString ExprRule::name() const {
|
|
return name_store;
|
|
}
|
|
|
|
int ExprRule::typeMark() const {
|
|
return this->mark_store;
|
|
}
|
|
|
|
#include <ast_novel.h>
|
|
MatchCursor::MatchCursor(const QString& path) :_file_path(path) { }
|
|
|
|
MatchCursor::MatchCursor(std::shared_ptr<const MatchCursor> other_ptr)
|
|
: _prev_cursor(other_ptr),
|
|
_file_path(other_ptr->_file_path),
|
|
_total_errors(other_ptr->_total_errors),
|
|
_exprs_errors(other_ptr->_exprs_errors),
|
|
_current_token(other_ptr->_current_token),
|
|
_remains_word(other_ptr->_remains_word) { }
|
|
|
|
std::shared_ptr<const MatchCursor> MatchCursor::previous() const {
|
|
return _prev_cursor;
|
|
}
|
|
|
|
QString MatchCursor::filePath() const {
|
|
return _file_path;
|
|
}
|
|
|
|
void MatchCursor::enterExprs() {
|
|
auto new_expr = std::make_shared<ErrsPack>();
|
|
this->_exprs_errors.push_back(new_expr);
|
|
}
|
|
|
|
void MatchCursor::logExprsError(const QString& msg) {
|
|
this->_total_errors.push_back(msg);
|
|
this->_exprs_errors.last()->addError(msg);
|
|
}
|
|
|
|
void MatchCursor::quitExprs() {
|
|
this->_exprs_errors.pop_back();
|
|
}
|
|
|
|
bool MatchCursor::mustStop() const {
|
|
return exprsErrorCount() >= 2;
|
|
}
|
|
|
|
int MatchCursor::exprsErrorCount() const {
|
|
if (this->_exprs_errors.size())
|
|
return this->_exprs_errors.last()->errorCount();
|
|
return 0;
|
|
}
|
|
|
|
int MatchCursor::totalErrorCount() const {
|
|
return this->_total_errors.size();
|
|
}
|
|
|
|
QList<QString> MatchCursor::totalErrors() const {
|
|
return this->_total_errors;
|
|
}
|
|
|
|
void MatchCursor::setCurrent(std::shared_ptr<const IActionToken> t, std::shared_ptr<const IPrimitiveWord> remains) {
|
|
this->_current_token = t;
|
|
this->_remains_word = remains;
|
|
}
|
|
|
|
std::shared_ptr<const IActionToken> MatchCursor::currentToken() const {
|
|
return this->_current_token;
|
|
}
|
|
|
|
std::shared_ptr<const IPrimitiveWord> MatchCursor::currentWords() const {
|
|
return this->_remains_word;
|
|
}
|
|
|
|
bool lib_syntax::MatchCursor::parse_stop() const {
|
|
return !currentWords();
|
|
}
|
|
|
|
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();
|
|
}
|