#include "libsyntax.h" #include "ast_basic.h" #include using namespace lib_syntax; using namespace std; using namespace lib_token; using namespace ast_basic; TokenMatch::TokenMatch(shared_ptr define) : define_peer(define) {} QList> TokenMatch::children() const { return QList>(); } std::tuple> TokenMatch::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { if (!head) { rt_inst->appendParseErrors(rt_inst->currentFile(), - 1, QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(rt_inst->currentFile())); return std::make_tuple(IBasicRule::MatchResult::Fail, head); } auto match_result = define_peer->analysis(head); if (std::get<0>(match_result)) { rt_inst->currentExprInst()->addToken(std::get<0>(match_result)); } else { rt_inst->appendParseErrors(rt_inst->currentFile(), head->position(), QString(u8"Syntax[0x00001]语法匹配错误,无法识别\"%1\"(应该为:%4)\n\t目标语法:%5。\n") .arg(head->content()).arg(head->row()).arg(head->column()).arg(this->define_peer->reviseWords()) .arg(rt_inst->currentExprRule()->token_present())); return std::make_tuple(IBasicRule::MatchResult::Part, head); } if (std::get<1>(match_result)) { return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared(std::get<1>(match_result), head->nextWord())); } else { return std::make_tuple(IBasicRule::MatchResult::Success, head->nextWord()); } } QString TokenMatch::token_present() const { return QString(u8"%1").arg(this->define_peer->reviseWords()); } Rept::Rept(std::shared_ptr rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {} QList> Rept::children() const { return QList>() << rule_peer; } std::tuple> Rept::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { auto temp_head = head; // min-match for (auto idx = 0; idx < min_match; ++idx) { auto result_gen = rule_peer->parse(rt_inst, temp_head); switch (std::get<0>(result_gen)) { 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; } } // max-match for (auto idx = min_match; idx < max_match; ++idx) { if (!temp_head) break; auto result_gen = rule_peer->parse(rt_inst, temp_head); switch (std::get<0>(result_gen)) { case IBasicRule::MatchResult::Fail: case IBasicRule::MatchResult::Part: return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); default: temp_head = std::get<1>(result_gen); break; } } return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); } QString Rept::token_present() const { if(min_match == 0 && max_match == INT_MAX) return u8"(" + this->rule_peer->token_present() + QString(u8")*"); else if(min_match == 1 && max_match == INT_MAX) return u8"(" + this->rule_peer->token_present() + QString(u8")+"); return u8"(" + this->rule_peer->token_present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match); } Seqs::Seqs(const QList> mbrs) : mbrs_store(mbrs) {} QList> Seqs::children() const { return mbrs_store; } std::tuple> Seqs::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { auto temp_head = head; for (auto& r : mbrs_store) { auto rst_gene = r->parse(rt_inst, temp_head); switch (std::get<0>(rst_gene)) { case IBasicRule::MatchResult::Fail: return std::make_tuple(IBasicRule::MatchResult::Part, temp_head); case IBasicRule::MatchResult::Part: return rst_gene; case IBasicRule::MatchResult::Success: temp_head = std::get<1>(rst_gene); break; default: break; } } return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); } QString Seqs::token_present() const { QString content; for (auto& it : children()) content += it->token_present() + " "; return content.mid(0, content.size() - 1); } //std::tuple, std::shared_ptr> Any::Any(const QList> mbrs) : mbrs_store(mbrs) {} QList> Any::children() const { return mbrs_store; } std::tuple> Any::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { std::tuple, uint64_t> temp_result = std::make_tuple(nullptr, 0); auto rule_present = this->token_present(); for (auto& fork : mbrs_store) { auto gen = fork->parse(rt_inst, head); switch (std::get<0>(gen)) { // 遇到成功的直接返回解析结果 case IBasicRule::MatchResult::Success: return gen; 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; } } // 分析最匹配的分支 rt_inst->clearErrors(rt_inst->currentFile(), head->position()); auto temp = std::get<0>(temp_result)->parse(rt_inst, head); return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(temp)); } QString Any::token_present() const { QString members_content; for (auto& it : children()) { members_content += it->token_present() + u8"|"; } return members_content.mid(0, members_content.size() - 1); } 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) { this->mark_store = expr_mark; } std::shared_ptr ExprRule::reloadRule(std::shared_ptr rule) { auto ninst = makeCopy(); ninst->child_store = rule; return ninst; } QString ExprRule::name() const { return name_store; } int ExprRule::typeMark() const { return this->mark_store; } QList> ExprRule::children() const { return QList>() << this->child_store; } #include std::tuple> ExprRule::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { std::shared_ptr elm_ast = this->newEmptyInstance(); auto text_present = this->token_present(); rt_inst->pushExprRule(this->shared_from_this()); rt_inst->pushExprInst(elm_ast); auto rstg = child_store->parse(rt_inst, head); auto tokens_decl = elm_ast->tokens(); switch (std::get<0>(rstg)) { case IBasicRule::MatchResult::Fail: case IBasicRule::MatchResult::Part: rt_inst->popExprInst(); rt_inst->popExprRule(); break; case IBasicRule::MatchResult::Success: { if (!std::dynamic_pointer_cast(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; } return rstg; } QString ExprRule::token_present() const { return child_store->token_present(); }