2024-03-17 07:58:28 +00:00
|
|
|
|
#include "libsyntax.h"
|
|
|
|
|
#include "ast_basic.h"
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
QList<std::shared_ptr<const BaseRule>> TokenMatch::children() const { return QList<std::shared_ptr<const BaseRule>>(); }
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
2024-06-18 14:59:41 +00:00
|
|
|
|
if (!head)
|
2024-06-20 13:36:46 +00:00
|
|
|
|
throw new InputTerminal(rt_inst->currentFile());
|
2024-06-18 14:59:41 +00:00
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
auto match_result = define_peer->analysis(head);
|
|
|
|
|
if (std::get<0>(match_result)) {
|
|
|
|
|
rt_inst->currentInst()->addToken(std::get<0>(match_result));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw new MismatchException(head);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
2024-06-18 14:59:41 +00:00
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
if (std::get<1>(match_result)) {
|
|
|
|
|
return std::make_tuple(nullptr, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return std::make_tuple(nullptr, head->nextWord());
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
QString TokenMatch::token_present() const {
|
2024-06-19 04:05:43 +00:00
|
|
|
|
return QString(u8"<%1>").arg(this->define_peer->typeName());
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rept::Rept(std::shared_ptr<const BaseRule> rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {}
|
|
|
|
|
|
|
|
|
|
QList<std::shared_ptr<const BaseRule>> Rept::children() const { return QList<std::shared_ptr<const BaseRule>>() << rule_peer; }
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> Rept::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) 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-06-18 14:59:41 +00:00
|
|
|
|
auto result_gen = rule_peer->parse(rt_inst, temp_head);
|
|
|
|
|
if (std::get<0>(result_gen))
|
|
|
|
|
rt_inst->currentInst()->addChild(std::get<0>(result_gen));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
temp_head = std::get<1>(result_gen);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// max-match
|
|
|
|
|
for (auto idx = min_match; idx < max_match; ++idx) {
|
2024-06-18 14:59:41 +00:00
|
|
|
|
try {
|
|
|
|
|
auto result_gen = rule_peer->parse(rt_inst, temp_head);
|
|
|
|
|
if (std::get<0>(result_gen))
|
|
|
|
|
rt_inst->currentInst()->addChild(std::get<0>(result_gen));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
temp_head = std::get<1>(result_gen);
|
|
|
|
|
}
|
2024-06-20 13:36:46 +00:00
|
|
|
|
catch (SyntaxException* ex) {
|
|
|
|
|
delete ex;
|
2024-06-18 14:59:41 +00:00
|
|
|
|
return std::make_tuple(nullptr, temp_head);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
return std::make_tuple(nullptr, 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")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Seqs::Seqs(const QList<std::shared_ptr<const BaseRule>> mbrs) : mbrs_store(mbrs) {}
|
|
|
|
|
|
|
|
|
|
QList<std::shared_ptr<const BaseRule>> Seqs::children() const { return mbrs_store; }
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> Seqs::parse(std::shared_ptr<ParseContext> 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);
|
|
|
|
|
temp_head = std::get<1>(rst_gene);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
if (std::get<0>(rst_gene))
|
|
|
|
|
rt_inst->currentInst()->addChild(std::get<0>(rst_gene));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
return std::make_tuple(nullptr, 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-06-20 13:36:46 +00:00
|
|
|
|
//std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const Token>>
|
2024-03-17 07:58:28 +00:00
|
|
|
|
Any::Any(const QList<std::shared_ptr<const BaseRule>> mbrs) : mbrs_store(mbrs) {}
|
|
|
|
|
|
|
|
|
|
QList<std::shared_ptr<const BaseRule>> Any::children() const { return mbrs_store; }
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
class words_span {
|
|
|
|
|
public:
|
|
|
|
|
int row_span, column_span;
|
|
|
|
|
words_span(int rspan, int cspan):row_span(rspan), column_span(cspan){}
|
|
|
|
|
bool operator>(const words_span& other) {
|
|
|
|
|
if(row_span > other.row_span)
|
|
|
|
|
return true;
|
|
|
|
|
if(row_span == other.row_span)
|
|
|
|
|
return column_span > other.column_span;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> Any::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
|
|
|
|
std::function<words_span(std::shared_ptr<const IWordBase>, std::shared_ptr<const IWordBase>)> measure_span =
|
|
|
|
|
[&](std::shared_ptr<const IWordBase> anchor, std::shared_ptr<const IWordBase> head)->words_span {
|
|
|
|
|
return words_span(anchor->row() - head->row(), anchor->column() - head->column());
|
2024-06-18 17:09:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::tuple<std::shared_ptr<const BaseRule>, words_span> temp_result = std::make_tuple(mbrs_store.first(), words_span(0,0));
|
2024-06-18 17:09:45 +00:00
|
|
|
|
for (auto& fork : mbrs_store) {
|
|
|
|
|
try {
|
|
|
|
|
auto gen = fork->parse(rt_inst, head);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (std::get<0>(gen))
|
|
|
|
|
rt_inst->currentInst()->addChild(std::get<0>(gen));
|
|
|
|
|
return std::make_tuple(nullptr, std::get<1>(gen));
|
|
|
|
|
}
|
|
|
|
|
// <20><EFBFBD><EFB7A8><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>бȽ<D0B1>
|
2024-06-20 13:36:46 +00:00
|
|
|
|
catch (MismatchException* ex) {
|
|
|
|
|
auto current_span = measure_span(ex->targetWord(), head);
|
2024-06-18 17:09:45 +00:00
|
|
|
|
|
|
|
|
|
if (current_span > std::get<1>(temp_result))
|
|
|
|
|
temp_result = std::make_tuple(fork, current_span);
|
2024-06-20 13:36:46 +00:00
|
|
|
|
|
|
|
|
|
delete ex;
|
2024-06-18 17:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>ķ<EFBFBD>֧
|
|
|
|
|
return std::get<0>(temp_result)->parse(rt_inst, head);
|
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-06-18 14:59:41 +00:00
|
|
|
|
ExpressionRule::ExpressionRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
|
2024-03-17 07:58:28 +00:00
|
|
|
|
this->filter_proc = [](const TokenSeqs& seqs) { return seqs; };
|
|
|
|
|
this->mark_store = expr_mark;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
std::shared_ptr<const ExpressionRule> ExpressionRule::reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule) {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
auto ninst = makeCopy();
|
2024-03-17 07:58:28 +00:00
|
|
|
|
ninst->child_store = rule;
|
|
|
|
|
ninst->filter_proc = filter;
|
|
|
|
|
return ninst;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
QString ExpressionRule::name() const { return name_store; }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
int ExpressionRule::typeMark() const { return this->mark_store; }
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
QList<std::shared_ptr<const BaseRule>> ExpressionRule::children() const {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
return QList<std::shared_ptr<const BaseRule>>() << this->child_store;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> ExpressionRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
2024-06-18 17:09:45 +00:00
|
|
|
|
std::shared_ptr<Expression> elm_ast = this->newEmptyInstance();
|
2024-06-18 14:59:41 +00:00
|
|
|
|
|
2024-06-18 03:54:36 +00:00
|
|
|
|
rt_inst->pushExpressionRule(this->shared_from_this());
|
|
|
|
|
rt_inst->pushInst(elm_ast);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 17:09:45 +00:00
|
|
|
|
try {
|
|
|
|
|
auto rstg = child_store->parse(rt_inst, head);
|
|
|
|
|
|
|
|
|
|
auto tokens_decl = this->filter_proc(elm_ast->tokens());
|
|
|
|
|
elm_ast->tokensReset(tokens_decl);
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 17:09:45 +00:00
|
|
|
|
rt_inst->popInst();
|
|
|
|
|
rt_inst->popExpressionRule();
|
|
|
|
|
return std::make_tuple(elm_ast, std::get<1>(rstg));
|
|
|
|
|
}
|
|
|
|
|
catch (...) {
|
|
|
|
|
rt_inst->popInst();
|
|
|
|
|
rt_inst->popExpressionRule();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-06-18 14:59:41 +00:00
|
|
|
|
QString ExpressionRule::token_present() const {
|
2024-06-18 03:54:36 +00:00
|
|
|
|
return QString(u8"(%1)").arg(child_store->token_present());
|
2024-03-17 07:58:28 +00:00
|
|
|
|
}
|
2024-06-18 17:09:45 +00:00
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
MismatchException::MismatchException(std::shared_ptr<const lib_token::IWordBase> inst) :SyntaxException(
|
|
|
|
|
QString(u8"Syntax[0x00001]<5D>ƥ<EFB7A8><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACB2><EFBFBD>ʶ<EFBFBD><CAB6>token<65><6E>%1<%2,%3>(%4)")
|
|
|
|
|
.arg(inst->content()).arg(inst->row()).arg(inst->column()).arg(inst->file())), target(inst) {}
|
2024-06-18 17:09:45 +00:00
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
std::shared_ptr<const IWordBase>MismatchException::targetWord() const {
|
2024-06-18 17:09:45 +00:00
|
|
|
|
return this->target;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 13:36:46 +00:00
|
|
|
|
InputTerminal::InputTerminal(const QString& file_path)
|
|
|
|
|
:SyntaxException(QString(u8"Syntax[0x0000]token<65><6E><EFBFBD><EFBFBD>%1<><31><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ֹ").arg(file_path)) {}
|