WsParser_VS/libSyntax/libsyntax.cpp

313 lines
8.7 KiB
C++
Raw Normal View History

2024-03-17 07:58:28 +00:00
#include "libsyntax.h"
#include "ast_basic.h"
#include <QDebug>
2024-03-17 07:58:28 +00:00
using namespace lib_syntax;
using namespace std;
using namespace lib_token;
using namespace ast_basic;
TokenMatch::TokenMatch(shared_ptr<const ITokenDefine> define) : define_peer(define) {}
2024-03-17 07:58:28 +00:00
2024-07-19 12:48:36 +00:00
QList<shared_ptr<const IBasicRule>> TokenMatch::children() const { return QList<shared_ptr<const IBasicRule>>(); }
2024-03-17 07:58:28 +00:00
2024-07-21 02:40:43 +00:00
ParseResult TokenMatch::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, ErrDeals& fx_type) const
2024-07-19 12:48:36 +00:00
{
if (!head) {
2024-07-19 12:48:36 +00:00
rt_inst->appendParseError(rt_inst->currentFile(), -1, QString(u8"Syntax[0x0000]token<65><6E><EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ֹ").arg(rt_inst->currentFile()));
2024-07-21 02:40:43 +00:00
return ParseResult();
}
2024-07-21 02:40:43 +00:00
ParseResult result_ret;
auto match_result = define_peer->analysis(head);
2024-07-19 12:48:36 +00:00
if (get<0>(match_result)) {
rt_inst->currentExprInst()->addToken(get<0>(match_result));
2024-06-18 14:59:41 +00:00
2024-07-21 02:40:43 +00:00
decltype(result_ret.result_repair) v_result;
2024-07-19 12:48:36 +00:00
if (get<1>(match_result)) {
2024-07-21 02:40:43 +00:00
v_result = make_tuple(MatchResult::Success, make_shared<WordImpl>(get<1>(match_result), head->nextWord()));
2024-07-19 12:48:36 +00:00
}
else {
2024-07-21 02:40:43 +00:00
v_result = make_tuple(MatchResult::Success, head->nextWord());
2024-07-19 12:48:36 +00:00
}
2024-07-21 02:40:43 +00:00
if (fx_type == ErrDeals::None)
result_ret.result_straight = v_result;
else
result_ret.result_repair = v_result;
}
else {
2024-07-19 12:48:36 +00:00
switch (fx_type) {
2024-07-21 02:40:43 +00:00
case lib_syntax::ErrDeals::Replace:
fx_type = ErrDeals::None;
result_ret.result_repair = make_tuple(MatchResult::Success, head->nextWord());
break;
case lib_syntax::ErrDeals::Absence:
fx_type = ErrDeals::None;
result_ret.result_repair = make_tuple(MatchResult::Success, head);
break;
case lib_syntax::ErrDeals::Surplus:
fx_type = ErrDeals::None;
2024-07-19 12:48:36 +00:00
{
auto temp_head = head->nextWord();
auto match_result = define_peer->analysis(temp_head);
if (get<0>(match_result)) {
rt_inst->currentExprInst()->addToken(get<0>(match_result));
2024-07-21 02:40:43 +00:00
if (get<1>(match_result))
result_ret.result_repair = make_tuple(MatchResult::Success, make_shared<WordImpl>(get<1>(match_result), temp_head->nextWord()));
else
result_ret.result_repair = make_tuple(MatchResult::Success, temp_head->nextWord());
2024-07-19 12:48:36 +00:00
}
else {
2024-07-21 02:40:43 +00:00
result_ret.result_repair = make_tuple(MatchResult::Part, head);
2024-07-19 12:48:36 +00:00
}
}
break;
2024-07-21 02:40:43 +00:00
2024-07-19 12:48:36 +00:00
default:
2024-07-21 02:40:43 +00:00
result_ret.result_straight = make_tuple(MatchResult::Part, head);
2024-07-19 12:48:36 +00:00
break;
}
}
2024-07-21 02:40:43 +00:00
return result_ret;
2024-03-17 07:58:28 +00:00
}
QString TokenMatch::token_present() const {
2024-07-21 02:40:43 +00:00
return QString(u8"%1 ").arg(this->define_peer->reviseWords());
2024-03-17 07:58:28 +00:00
}
2024-07-19 12:48:36 +00:00
Rept::Rept(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
2024-07-19 12:48:36 +00:00
QList<shared_ptr<const IBasicRule>> Rept::children() const { return QList<shared_ptr<const IBasicRule>>() << rule_peer; }
2024-03-17 07:58:28 +00:00
2024-07-21 02:40:43 +00:00
ParseResult Rept::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, ErrDeals& fx_type) const {
ParseResult result_final;
2024-07-21 02:40:43 +00:00
bool straight_mark = fx_type == ErrDeals::None;
auto temp_head = head;
2024-03-17 07:58:28 +00:00
// min-match
for (auto idx = 0; idx < min_match; ++idx) {
2024-07-19 12:48:36 +00:00
auto result_gen = rule_peer->parse(rt_inst, temp_head, fx_type);
2024-07-21 02:40:43 +00:00
auto v_result = result_gen.result_straight;
if (std::get<0>(v_result) == MatchResult::None)
v_result = result_gen.result_repair;
switch (std::get<0>(v_result)) {
case MatchResult::Part:
if (straight_mark)
result_final.result_straight = make_tuple(MatchResult::Part, std::get<1>(v_result));
else
result_final.result_repair = make_tuple(MatchResult::Part, std::get<1>(v_result));
return result_final;
default:
2024-07-21 02:40:43 +00:00
temp_head = std::get<1>(v_result);
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
2024-07-19 12:48:36 +00:00
auto result_gen = rule_peer->parse(rt_inst, temp_head, fx_type);
2024-07-21 02:40:43 +00:00
auto v_result = result_gen.result_straight;
if (std::get<0>(v_result) == MatchResult::None)
v_result = result_gen.result_repair;
switch (get<0>(v_result)) {
case MatchResult::Part:
if(straight_mark)
result_final.result_straight = make_tuple(MatchResult::Success, temp_head);
else
result_final.result_repair = make_tuple(MatchResult::Success, temp_head);
return result_final;
default:
2024-07-21 02:40:43 +00:00
temp_head = get<1>(v_result);
break;
2024-03-17 07:58:28 +00:00
}
}
2024-07-21 02:40:43 +00:00
if(straight_mark)
result_final.result_straight = make_tuple(MatchResult::Success, temp_head);
else
result_final.result_repair = make_tuple(MatchResult::Success, temp_head);
return result_final;
2024-03-17 07:58:28 +00:00
}
QString Rept::token_present() const
{
2024-07-21 02:40:43 +00:00
return u8"(" + this->rule_peer->token_present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match);
2024-03-17 07:58:28 +00:00
}
2024-07-19 12:48:36 +00:00
Seqs::Seqs(const QList<shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) {}
2024-03-17 07:58:28 +00:00
2024-07-19 12:48:36 +00:00
QList<shared_ptr<const IBasicRule>> Seqs::children() const { return mbrs_store; }
2024-03-17 07:58:28 +00:00
2024-07-21 02:40:43 +00:00
ParseResult Seqs::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, ErrDeals& fx_type) const {
ParseResult result_final;
2024-06-18 14:59:41 +00:00
2024-07-21 02:40:43 +00:00
auto temp_head = head;
bool straight_mark = fx_type == ErrDeals::None;
2024-03-17 07:58:28 +00:00
for (auto& r : mbrs_store) {
2024-07-21 02:40:43 +00:00
auto rule_present = r->token_present();
2024-07-19 12:48:36 +00:00
auto rst_gene = r->parse(rt_inst, temp_head, fx_type);
2024-07-21 02:40:43 +00:00
auto v_result = rst_gene.result_straight;
if(std::get<0>(v_result) == MatchResult::None)
v_result = rst_gene.result_repair;
switch (get<0>(v_result)) {
case MatchResult::Part:
if(straight_mark)
result_final.result_straight = v_result;
else
result_final.result_repair = v_result;
return result_final;
case MatchResult::Success:
temp_head = get<1>(v_result);
break;
default:
break;
}
2024-03-17 07:58:28 +00:00
}
2024-07-21 02:40:43 +00:00
if (straight_mark)
result_final.result_straight = make_tuple(MatchResult::Success, temp_head);
else
result_final.result_repair = make_tuple(MatchResult::Success, temp_head);
return result_final;
2024-03-17 07:58:28 +00:00
}
QString Seqs::token_present() const
{
QString content;
for (auto& it : children())
content += it->token_present();
2024-07-21 02:40:43 +00:00
return content;
2024-03-17 07:58:28 +00:00
}
2024-07-21 02:40:43 +00:00
//tuple<MatchResult, uint, shared_ptr<const IBasicRule>, shared_ptr<const Token>>
2024-07-19 12:48:36 +00:00
Any::Any(const QList<shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) {}
2024-03-17 07:58:28 +00:00
2024-07-19 12:48:36 +00:00
QList<shared_ptr<const IBasicRule>> Any::children() const { return mbrs_store; }
2024-03-17 07:58:28 +00:00
2024-07-21 02:40:43 +00:00
ParseResult Any::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, ErrDeals& fx_type) const {
auto rule_present = this->token_present();
2024-06-18 17:09:45 +00:00
2024-07-21 02:40:43 +00:00
ParseResult result_final;
bool straight_mark = fx_type == ErrDeals::None;
switch (fx_type) {
case ErrDeals::None: {
for (auto& fork : mbrs_store) {
auto gen = fork->parse(rt_inst, head, fx_type);
switch (get<0>(gen.result_straight)) {
// <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MatchResult::Success:
return gen;
case MatchResult::Part: {
auto span = get<1>(gen)->position() - head->position();
if (span >= get<1>(temp_result))
temp_result = make_tuple(fork, span);
}
default:
break;
}
2024-06-18 17:09:45 +00:00
}
2024-07-21 02:40:43 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>ķ<EFBFBD>֧
rt_inst->clearErrors(rt_inst->currentFile(), head->position());
auto temp = get<0>(temp_result)->parse(rt_inst, head, fx_type);
return make_tuple(MatchResult::Part, get<1>(temp));
}
default:
break;
2024-06-18 17:09:45 +00:00
}
2024-03-17 07:58:28 +00:00
}
QString Any::token_present() const
{
QString members_content;
for (auto& it : children()) {
members_content += it->token_present() + u8"|";
}
2024-07-21 02:40:43 +00:00
return members_content.mid(0, members_content.size() - 1);
2024-03-17 07:58:28 +00:00
}
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) {
2024-03-17 07:58:28 +00:00
this->mark_store = expr_mark;
}
2024-07-19 12:48:36 +00:00
shared_ptr<const ExprRule> ExprRule::reloadRule(shared_ptr<const IBasicRule> rule) {
auto ninst = makeCopy();
2024-03-17 07:58:28 +00:00
ninst->child_store = rule;
return ninst;
}
QString ExprRule::name() const { return name_store; }
2024-03-17 07:58:28 +00:00
int ExprRule::typeMark() const { return this->mark_store; }
2024-03-17 07:58:28 +00:00
2024-07-19 12:48:36 +00:00
QList<shared_ptr<const IBasicRule>> ExprRule::children() const {
return QList<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-21 02:40:43 +00:00
tuple<MatchResult, shared_ptr<const IWordBase>>
ExprRule::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, ErrDeals& fx_type) const {
auto text_present = this->token_present();
shared_ptr<IExprInst> elm_ast = this->newEmptyInstance();
rt_inst->pushExprRule(this->shared_from_this());
rt_inst->pushExprInst(elm_ast);
auto ret_tuple = child_store->parse(rt_inst, head, fx_type);
switch (get<0>(ret_tuple)) {
case MatchResult::Success:
if (!dynamic_pointer_cast<example_novel::Document>(elm_ast) && fx_type == ErrDeals::None) {
rt_inst->clearErrors(rt_inst->currentFile(), head->position());
}
2024-07-19 12:48:36 +00:00
2024-07-21 02:40:43 +00:00
rt_inst->popExprInst();
rt_inst->popExprRule();
2024-07-19 12:48:36 +00:00
2024-07-21 02:40:43 +00:00
if (rt_inst->currentExprInst()) {
rt_inst->currentExprInst()->addChild(elm_ast);
}
2024-07-21 02:40:43 +00:00
else {
rt_inst->appendDocInst(elm_ast);
}
break;
2024-03-17 07:58:28 +00:00
2024-07-21 02:40:43 +00:00
case MatchResult::Part:
case MatchResult::Fail:
rt_inst->popExprInst();
rt_inst->popExprRule();
break;
default:
break;
}
2024-07-19 12:48:36 +00:00
return ret_tuple;
2024-03-17 07:58:28 +00:00
}
2024-06-18 17:09:45 +00:00
QString ExprRule::token_present() const {
2024-07-12 09:35:35 +00:00
return child_store->token_present();
2024-06-18 17:09:45 +00:00
}