2024-03-17 07:58:28 +00:00
|
|
|
|
#include "libsyntax.h"
|
|
|
|
|
#include "ast_basic.h"
|
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;
|
|
|
|
|
using namespace ast_basic;
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
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-19 12:48:36 +00:00
|
|
|
|
tuple<IBasicRule::MatchResult, shared_ptr<const IWordBase>>
|
|
|
|
|
TokenMatch::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, ErrOccurs& fx_type) const
|
|
|
|
|
{
|
2024-07-12 04:53:43 +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()));
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Fail, head);
|
2024-06-27 04:55:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
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-19 12:48:36 +00:00
|
|
|
|
if (get<1>(match_result)) {
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, make_shared<WordImpl>(get<1>(match_result), head->nextWord()));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, head->nextWord());
|
|
|
|
|
}
|
2024-06-20 13:36:46 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2024-07-19 12:48:36 +00:00
|
|
|
|
switch (fx_type) {
|
|
|
|
|
case lib_syntax::IBasicRule::ErrOccurs::None:
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Part, head);
|
|
|
|
|
case lib_syntax::IBasicRule::ErrOccurs::Replace:
|
|
|
|
|
fx_type = IBasicRule::ErrOccurs::None;
|
|
|
|
|
rt_inst->appendParseError(rt_inst->currentFile(), head->position(),
|
|
|
|
|
QString(u8"Syntax[0x00001]<5D>ƥ<EFB7A8><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACB2><EFBFBD>ʶ<EFBFBD><CAB6>token<65><6E>\"%1\"<row:%2,col:%3>(Ӧ<><D3A6>Ϊ<EFBFBD><CEAA>%4)")
|
|
|
|
|
.arg(head->content()).arg(head->row()).arg(head->column()).arg(this->define_peer->reviseWords()));
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, head->nextWord());
|
|
|
|
|
case lib_syntax::IBasicRule::ErrOccurs::Absence:
|
|
|
|
|
fx_type = IBasicRule::ErrOccurs::None;
|
|
|
|
|
rt_inst->appendParseError(rt_inst->currentFile(), head->position(),
|
|
|
|
|
QString(u8"Syntax[0x00001]<5D>ƥ<EFB7A8><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1>token<65><6E>\"%1\"<row: % 2, col : % 3>")
|
|
|
|
|
.arg(this->define_peer->reviseWords()).arg(head->row()).arg(head->column()));
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, head);
|
|
|
|
|
case lib_syntax::IBasicRule::ErrOccurs::Surplus:
|
|
|
|
|
fx_type = IBasicRule::ErrOccurs::None;
|
|
|
|
|
{
|
|
|
|
|
auto temp_head = head->nextWord();
|
|
|
|
|
auto match_result = define_peer->analysis(temp_head);
|
|
|
|
|
if (get<0>(match_result)) {
|
|
|
|
|
rt_inst->appendParseError(rt_inst->currentFile(), head->position(),
|
|
|
|
|
QString(u8"Syntax[0x00001]<5D>ƥ<EFB7A8><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACB6><EFBFBD>token<65><6E>\"%1\"<row: % 2, col : % 3>")
|
|
|
|
|
.arg(head->content()).arg(head->row()).arg(head->column()));
|
|
|
|
|
|
|
|
|
|
rt_inst->currentExprInst()->addToken(get<0>(match_result));
|
|
|
|
|
if (get<1>(match_result)) {
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, make_shared<WordImpl>(get<1>(match_result), temp_head->nextWord()));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, temp_head->nextWord());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Part, head);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-06-20 13:36:46 +00:00
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
QString TokenMatch::token_present() const {
|
2024-07-12 22:16:11 +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-19 12:48:36 +00:00
|
|
|
|
tuple<IBasicRule::MatchResult, shared_ptr<const IWordBase>>
|
|
|
|
|
Rept::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, IBasicRule::ErrOccurs& fx_type) const {
|
2024-06-18 14:59:41 +00:00
|
|
|
|
auto temp_head = head;
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
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);
|
|
|
|
|
switch (get<0>(result_gen)) {
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Fail:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Part, temp_head);
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Part:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Part, get<1>(result_gen));
|
2024-07-12 04:53:43 +00:00
|
|
|
|
default:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
temp_head = get<1>(result_gen);
|
2024-07-12 04:53:43 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// max-match
|
|
|
|
|
for (auto idx = min_match; idx < max_match; ++idx) {
|
2024-07-12 04:53:43 +00:00
|
|
|
|
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);
|
|
|
|
|
switch (get<0>(result_gen)) {
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Fail:
|
|
|
|
|
case IBasicRule::MatchResult::Part:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, temp_head);
|
2024-07-12 04:53:43 +00:00
|
|
|
|
default:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
temp_head = get<1>(result_gen);
|
2024-07-12 04:53:43 +00:00
|
|
|
|
break;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 12:48:36 +00:00
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, temp_head);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Rept::token_present() const
|
|
|
|
|
{
|
|
|
|
|
return u8"(" + this->rule_peer->token_present() + QString(u8"{%1, %2}").arg(min_match).arg(max_match) + u8")";
|
|
|
|
|
}
|
|
|
|
|
|
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-19 12:48:36 +00:00
|
|
|
|
tuple<IBasicRule::MatchResult, shared_ptr<const IWordBase>>
|
|
|
|
|
Seqs::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, IBasicRule::ErrOccurs& fx_type) 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-07-19 12:48:36 +00:00
|
|
|
|
auto rst_gene = r->parse(rt_inst, temp_head, fx_type);
|
|
|
|
|
switch (get<0>(rst_gene)) {
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Fail:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Part, temp_head);
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Part:
|
2024-07-12 04:53:43 +00:00
|
|
|
|
return rst_gene;
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Success:
|
2024-07-19 12:48:36 +00:00
|
|
|
|
temp_head = get<1>(rst_gene);
|
2024-07-12 04:53:43 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 12:48:36 +00:00
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Success, temp_head);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Seqs::token_present() const
|
|
|
|
|
{
|
|
|
|
|
QString content;
|
|
|
|
|
for (auto& it : children())
|
|
|
|
|
content += it->token_present();
|
|
|
|
|
return QString(u8"(%1)").arg(content);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 12:48:36 +00:00
|
|
|
|
//tuple<IBasicRule::MatchResult, uint, shared_ptr<const IBasicRule>, shared_ptr<const Token>>
|
|
|
|
|
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-06-20 13:36:46 +00:00
|
|
|
|
class words_span {
|
|
|
|
|
public:
|
|
|
|
|
int row_span, column_span;
|
2024-07-12 04:53:43 +00:00
|
|
|
|
words_span(int rspan, int cspan) :row_span(rspan), column_span(cspan) {}
|
2024-06-20 13:36:46 +00:00
|
|
|
|
bool operator>(const words_span& other) {
|
2024-07-12 04:53:43 +00:00
|
|
|
|
if (row_span > other.row_span)
|
2024-06-20 13:36:46 +00:00
|
|
|
|
return true;
|
2024-07-12 04:53:43 +00:00
|
|
|
|
if (row_span == other.row_span)
|
2024-06-20 13:36:46 +00:00
|
|
|
|
return column_span > other.column_span;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-19 12:48:36 +00:00
|
|
|
|
tuple<IBasicRule::MatchResult, shared_ptr<const IWordBase>>
|
|
|
|
|
Any::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, IBasicRule::ErrOccurs& fx_type) const {
|
|
|
|
|
tuple<shared_ptr<const IBasicRule>, uint64_t> temp_result = make_tuple(nullptr, 0);
|
2024-07-12 04:53:43 +00:00
|
|
|
|
auto rule_present = this->token_present();
|
2024-06-18 17:09:45 +00:00
|
|
|
|
|
|
|
|
|
for (auto& fork : mbrs_store) {
|
2024-07-19 12:48:36 +00:00
|
|
|
|
auto gen = fork->parse(rt_inst, head, fx_type);
|
|
|
|
|
switch (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:
|
2024-07-12 04:53:43 +00:00
|
|
|
|
return gen;
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Fail: {
|
2024-07-19 12:48:36 +00:00
|
|
|
|
if (!get<0>(temp_result))
|
|
|
|
|
temp_result = make_tuple(fork, 0);
|
2024-07-12 04:53:43 +00:00
|
|
|
|
}break;
|
2024-07-12 21:52:32 +00:00
|
|
|
|
case IBasicRule::MatchResult::Part: {
|
2024-07-19 12:48:36 +00:00
|
|
|
|
auto span = get<1>(gen)->position() - head->position();
|
|
|
|
|
if (span >= get<1>(temp_result))
|
|
|
|
|
temp_result = make_tuple(fork, span);
|
2024-06-18 17:09:45 +00:00
|
|
|
|
}
|
2024-07-12 04:53:43 +00:00
|
|
|
|
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());
|
2024-07-19 12:48:36 +00:00
|
|
|
|
auto temp = get<0>(temp_result)->parse(rt_inst, head, fx_type);
|
|
|
|
|
return make_tuple(IBasicRule::MatchResult::Part, get<1>(temp));
|
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"|";
|
|
|
|
|
}
|
|
|
|
|
return u8"(" + members_content.mid(0, members_content.size() - 1) + u8")";
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
|
|
|
|
SyntaxException::SyntaxException(const QString& message) { this->msg_store = message; }
|
|
|
|
|
|
|
|
|
|
QString SyntaxException::message() const { return msg_store; }
|
|
|
|
|
|
2024-07-12 04:53:43 +00:00
|
|
|
|
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) {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
auto ninst = makeCopy();
|
2024-03-17 07:58:28 +00:00
|
|
|
|
ninst->child_store = rule;
|
|
|
|
|
return ninst;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 04:53:43 +00:00
|
|
|
|
QString ExprRule::name() const { return name_store; }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-07-12 04:53:43 +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-19 12:48:36 +00:00
|
|
|
|
tuple<IBasicRule::MatchResult, shared_ptr<const IWordBase>>
|
|
|
|
|
ExprRule::parse(shared_ptr<IContext> rt_inst, shared_ptr<const IWordBase> head, IBasicRule::ErrOccurs& fx_type) const {
|
|
|
|
|
QList<IBasicRule::ErrOccurs> opts_list = {
|
|
|
|
|
IBasicRule::ErrOccurs::None, IBasicRule::ErrOccurs::Surplus, IBasicRule::ErrOccurs::Absence, IBasicRule::ErrOccurs::Replace };
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2024-07-19 12:48:36 +00:00
|
|
|
|
auto opt_current_itor = opts_list.cbegin();
|
|
|
|
|
tuple<IBasicRule::MatchResult, shared_ptr<const IWordBase>> ret_tuple;
|
|
|
|
|
do {
|
|
|
|
|
auto opt_val = *opt_current_itor;
|
|
|
|
|
shared_ptr<IExprInst> elm_ast = this->newEmptyInstance();
|
|
|
|
|
auto text_present = this->token_present();
|
2024-07-12 04:53:43 +00:00
|
|
|
|
|
2024-07-19 12:48:36 +00:00
|
|
|
|
rt_inst->pushExprRule(this->shared_from_this());
|
|
|
|
|
rt_inst->pushExprInst(elm_ast);
|
|
|
|
|
|
|
|
|
|
ret_tuple = child_store->parse(rt_inst, head, opt_val);
|
|
|
|
|
auto tokens_decl = elm_ast->tokens();
|
|
|
|
|
|
|
|
|
|
switch (get<0>(ret_tuple)) {
|
|
|
|
|
case IBasicRule::MatchResult::Fail:
|
|
|
|
|
case IBasicRule::MatchResult::Part:
|
|
|
|
|
rt_inst->popExprInst();
|
|
|
|
|
rt_inst->popExprRule();
|
|
|
|
|
break;
|
|
|
|
|
case IBasicRule::MatchResult::Success: {
|
|
|
|
|
if (!dynamic_pointer_cast<example_novel::Document>(elm_ast) && opt_val == IBasicRule::ErrOccurs::None) {
|
|
|
|
|
auto start_pos = head->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-07-12 04:53:43 +00:00
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-07-19 12:48:36 +00:00
|
|
|
|
opt_current_itor++;
|
|
|
|
|
} while (fx_type != IBasicRule::ErrOccurs::None && opt_current_itor != opts_list.cend());
|
|
|
|
|
|
|
|
|
|
return ret_tuple;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
2024-06-18 17:09:45 +00:00
|
|
|
|
|
2024-07-12 04:53:43 +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
|
|
|
|
}
|