WsParser_VS/libSyntax/libsyntax.cpp

279 lines
8.0 KiB
C++
Raw Normal View History

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>
#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-04 14:26:34 +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-04 14:26:34 +00:00
QList<std::shared_ptr<const IBasicRule>> Rept::children() const {
return QList<std::shared_ptr<const IBasicRule>>() << rule_peer;
}
2024-03-17 07:58:28 +00:00
2024-07-12 21:52:32 +00:00
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> Rept::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
2024-06-18 14:59:41 +00:00
auto temp_head = head;
2024-03-17 07:58:28 +00:00
// min-match
for (auto idx = 0; idx < min_match; ++idx) {
2024-06-18 14:59:41 +00:00
auto result_gen = rule_peer->parse(rt_inst, temp_head);
switch (std::get<0>(result_gen)) {
2024-07-12 21:52:32 +00:00
case IBasicRule::MatchResult::Fail:
return std::make_tuple(IBasicRule::MatchResult::Part, temp_head);
case IBasicRule::MatchResult::Part:
return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(result_gen));
default:
temp_head = std::get<1>(result_gen);
break;
}
2024-03-17 07:58:28 +00:00
}
// max-match
for (auto idx = min_match; idx < max_match; ++idx) {
if (!temp_head)
break;
2024-03-17 07:58:28 +00:00
auto result_gen = rule_peer->parse(rt_inst, temp_head);
switch (std::get<0>(result_gen)) {
2024-07-12 21:52:32 +00:00
case IBasicRule::MatchResult::Fail:
case IBasicRule::MatchResult::Part:
return std::make_tuple(IBasicRule::MatchResult::Success, temp_head);
default:
2024-06-18 14:59:41 +00:00
temp_head = std::get<1>(result_gen);
break;
2024-03-17 07:58:28 +00:00
}
}
2024-07-12 21:52:32 +00:00
return std::make_tuple(IBasicRule::MatchResult::Success, temp_head);
2024-03-17 07:58:28 +00:00
}
2025-02-04 14:26:34 +00:00
QString Rept::present() const {
if (min_match == 0 && max_match == INT_MAX)
return u8"(" + this->rule_peer->present() + QString(u8")*");
2024-07-28 06:46:47 +00:00
else if (min_match == 1 && max_match == INT_MAX)
2025-02-04 14:26:34 +00:00
return u8"(" + this->rule_peer->present() + QString(u8")+");
2024-07-28 06:46:47 +00:00
else if (min_match == 0 && max_match == 1)
2025-02-04 14:26:34 +00:00
return u8"(" + this->rule_peer->present() + QString(u8")?");
2024-07-25 03:54:40 +00:00
2025-02-04 14:26:34 +00:00
return u8"(" + this->rule_peer->present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match);
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
2024-07-12 21:52:32 +00:00
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> Seqs::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
2024-06-18 14:59:41 +00:00
auto temp_head = head;
2024-03-17 07:58:28 +00:00
for (auto& r : mbrs_store) {
2024-06-18 14:59:41 +00:00
auto rst_gene = r->parse(rt_inst, temp_head);
switch (std::get<0>(rst_gene)) {
2024-07-12 21:52:32 +00:00
case IBasicRule::MatchResult::Fail:
return std::make_tuple(IBasicRule::MatchResult::Part, temp_head);
case IBasicRule::MatchResult::Part:
return rst_gene;
2024-07-12 21:52:32 +00:00
case IBasicRule::MatchResult::Success:
temp_head = std::get<1>(rst_gene);
break;
default:
break;
}
2024-03-17 07:58:28 +00:00
}
2024-07-12 21:52:32 +00:00
return std::make_tuple(IBasicRule::MatchResult::Success, temp_head);
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
}
2024-07-12 21:52:32 +00:00
//std::tuple<IBasicRule::MatchResult, uint, std::shared_ptr<const IBasicRule>, std::shared_ptr<const Token>>
2025-02-04 14:26:34 +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-04 14:26:34 +00:00
QList<std::shared_ptr<const IBasicRule>> Any::children() const {
return mbrs_store;
}
2024-03-17 07:58:28 +00:00
2024-07-12 21:52:32 +00:00
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
std::tuple<std::shared_ptr<const IBasicRule>, uint64_t> temp_result = std::make_tuple(nullptr, 0);
2025-02-04 14:26:34 +00:00
auto rule_present = this->present();
2024-06-18 17:09:45 +00:00
for (auto& fork : mbrs_store) {
auto gen = fork->parse(rt_inst, head);
switch (std::get<0>(gen)) {
2024-06-18 17:09:45 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-07-12 21:52:32 +00:00
case IBasicRule::MatchResult::Success:
return gen;
2025-02-04 14:26:34 +00:00
case IBasicRule::MatchResult::Fail:
{
if (!std::get<0>(temp_result))
temp_result = std::make_tuple(fork, 0);
}break;
case IBasicRule::MatchResult::Part:
{
auto span = std::get<1>(gen)->position() - head->position();
if (span >= std::get<1>(temp_result))
temp_result = std::make_tuple(fork, span);
}
default:
break;
2024-06-18 17:09:45 +00:00
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>ķ<EFBFBD>֧
2024-07-12 09:35:35 +00:00
rt_inst->clearErrors(rt_inst->currentFile(), head->position());
auto temp = std::get<0>(temp_result)->parse(rt_inst, head);
2024-07-12 21:52:32 +00:00
return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(temp));
2024-03-17 07:58:28 +00:00
}
2025-02-04 14:26:34 +00:00
QString Any::present() const {
2024-03-17 07:58:28 +00:00
QString members_content;
for (auto& it : children()) {
2025-02-04 14:26:34 +00:00
members_content += it->present() + u8"|";
2024-03-17 07:58:28 +00:00
}
2024-07-25 03:54:40 +00:00
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
SyntaxException::SyntaxException(const QString& message) {
this->msg_store = message;
}
2025-02-04 14:26:34 +00:00
QString SyntaxException::message() const {
return msg_store;
}
ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
2024-03-17 07:58:28 +00:00
this->mark_store = expr_mark;
}
2024-07-12 21:52:32 +00:00
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const IBasicRule> rule) {
auto ninst = makeCopy();
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>
2024-07-12 21:52:32 +00:00
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>>
ExprRule::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
std::shared_ptr<IExprInst> elm_ast = this->newEmptyInstance();
2025-02-04 14:26:34 +00:00
auto text_present = this->present();
2024-06-18 14:59:41 +00:00
2024-07-12 21:52:32 +00:00
rt_inst->pushExprRule(this->shared_from_this());
rt_inst->pushExprInst(elm_ast);
2024-03-17 07:58:28 +00:00
auto rstg = child_store->parse(rt_inst, head);
2024-07-12 09:35:35 +00:00
auto tokens_decl = elm_ast->tokens();
2024-03-17 07:58:28 +00:00
switch (std::get<0>(rstg)) {
2024-07-12 21:52:32 +00:00
case IBasicRule::MatchResult::Fail:
case IBasicRule::MatchResult::Part:
rt_inst->popExprInst();
rt_inst->popExprRule();
break;
2025-02-04 14:26:34 +00:00
case IBasicRule::MatchResult::Success:
{
if (!std::dynamic_pointer_cast<example_novel::Document>(elm_ast)) {
auto start_pos = tokens_decl.first()->position();
rt_inst->clearErrors(rt_inst->currentFile(), start_pos);
}
rt_inst->popExprInst();
rt_inst->popExprRule();
if (rt_inst->currentExprInst()) {
rt_inst->currentExprInst()->addChild(elm_ast);
}
else {
rt_inst->appendDocInst(elm_ast);
}
}break;
default:
break;
2024-06-18 17:09:45 +00:00
}
2024-03-17 07:58:28 +00:00
return rstg;
2024-03-17 07:58:28 +00:00
}
2024-06-18 17:09:45 +00:00
2025-02-04 14:26:34 +00:00
QString ExprRule::present() const {
return child_store->present();
}
lib_syntax::MatchCursor::MatchCursor() { }
lib_syntax::MatchCursor::MatchCursor(const MatchCursor& other)
: _expr_through(other._expr_through),
_total_errors(other._total_errors),
_exprs_errors(other._exprs_errors),
_current_token(other._current_token),
_remains_word(other._remains_word) { }
void lib_syntax::MatchCursor::enterExprs(std::shared_ptr<IExprInst> ins) {
this->_expr_through.push_back(ins);
}
std::shared_ptr<ast_basic::IExprInst> lib_syntax::MatchCursor::currentExprs() const {
return this->_expr_through.size()?this->_expr_through.last():nullptr;
}
void lib_syntax::MatchCursor::logExprsError(const QString& msg) {
this->_exprs_errors.push_back(msg);
}
void lib_syntax::MatchCursor::quitExprs() {
this->_expr_through.pop_back();
this->_total_errors.append(this->_exprs_errors);
this->_exprs_errors.clear();
}
bool lib_syntax::MatchCursor::mustStop() const {
return this->_exprs_errors.size() >= 2;
}
int lib_syntax::MatchCursor::exprsErrorCount() const {
return this->_exprs_errors.size();
}
int lib_syntax::MatchCursor::totalErrorCount() const {
return this->_total_errors.size() + this->_exprs_errors.size();
}
QList<QString> lib_syntax::MatchCursor::totalErrors() const {
return this->_total_errors;
}
void lib_syntax::MatchCursor::setCurrent(std::shared_ptr<lib_token::IToken> t, std::shared_ptr<lib_words::IPrimitiveWord> remains) {
this->_current_token = t;
this->_remains_word = remains;
}
std::shared_ptr<lib_token::IToken> lib_syntax::MatchCursor::currentToken() const {
return this->_current_token;
}
std::shared_ptr<lib_words::IPrimitiveWord> lib_syntax::MatchCursor::currentWords() const {
return this->_remains_word;
2024-06-18 17:09:45 +00:00
}