#include "libsyntax.h" #include "ast_basic.h" 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::match(std::shared_ptr remains_head) const { QString token_seqs = this->token_present(); if (remains_head && remains_head->define()->name() == define_peer->name()) return std::make_tuple(MatchResult::Success, 1, remains_head->nextToken()); return std::make_tuple(MatchResult::Fail, 0, nullptr); // auto mis_match = define_peer->name(); // auto real_match = stream.first()->define()->name(); } // std::tuple, std::shared_ptr> std::tuple, std::shared_ptr> TokenMatch::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { if (!head) throw std::make_shared(); if (head->define()->name() == define_peer->name()) { rt_inst->currentInst()->addToken(head); return std::make_tuple(nullptr, head->nextToken()); } throw std::make_shared(head); } QString TokenMatch::token_present() const { return QString(u8"<%1>").arg(this->define_peer->name()); } 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::match(std::shared_ptr list_head) const { auto token_offset = 0; QString token_seqs = this->token_present(); auto temp_head = list_head; // min-match for (auto idx = 0; idx < min_match; ++idx) { auto result = rule_peer->match(temp_head); token_offset += std::get<1>(result); temp_head = std::get<2>(result); if (std::get<0>(result) != MatchResult::Success) { return std::make_tuple(token_offset ? MatchResult::Part : MatchResult::Fail, token_offset, temp_head); } } // max-match for (auto idx = min_match; idx < max_match; ++idx) { auto result = rule_peer->match(temp_head); switch (std::get<0>(result)) { case MatchResult::Fail: case MatchResult::Part: return std::make_tuple(MatchResult::Success, token_offset, temp_head); default: temp_head = std::get<2>(result); token_offset += std::get<1>(result); break; } } return std::make_tuple(MatchResult::Success, token_offset, temp_head); } std::tuple, std::shared_ptr> 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); if (std::get<0>(result_gen)) rt_inst->currentInst()->addChild(std::get<0>(result_gen)); temp_head = std::get<1>(result_gen); } // max-match for (auto idx = min_match; idx < max_match; ++idx) { 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)); temp_head = std::get<1>(result_gen); } catch (std::shared_ptr ex) { return std::make_tuple(nullptr, temp_head); } catch (std::shared_ptr ex) { return std::make_tuple(nullptr, temp_head); } } return std::make_tuple(nullptr, temp_head); } 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> mbrs) : mbrs_store(mbrs) {} QList> Seqs::children() const { return mbrs_store; } std::tuple> Seqs::match(std::shared_ptr list_head) const { auto token_offset = 0; QString token_seqs = this->token_present(); auto temp_head = list_head; for (auto& r : mbrs_store) { auto v_token_seqs = r->token_present(); auto result = r->match(list_head); token_offset += std::get<1>(result); temp_head = std::get<2>(result); switch (std::get<0>(result)) { case MatchResult::Fail: case MatchResult::Part: return std::make_tuple(token_offset ? MatchResult::Part : MatchResult::Fail, token_offset, temp_head); default: break; } } return std::make_tuple(MatchResult::Success, token_offset, temp_head); } std::tuple, std::shared_ptr> 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); temp_head = std::get<1>(rst_gene); if (std::get<0>(rst_gene)) rt_inst->currentInst()->addChild(std::get<0>(rst_gene)); } return std::make_tuple(nullptr, temp_head); } QString Seqs::token_present() const { QString content; for (auto& it : children()) content += it->token_present(); return QString(u8"(%1)").arg(content); } std::tuple, std::shared_ptr> Any::rule_select(std::shared_ptr head) const { QString token_seqs = this->token_present(); std::tuple, std::shared_ptr> temp = std::make_tuple(MatchResult::Fail, 0, nullptr, nullptr); for (auto& r : mbrs_store) { auto mbr_seqs = r->token_present(); auto result = r->match(head); if (std::get<0>(result) == MatchResult::Success) return std::make_tuple(std::get<0>(result), std::get<1>(result), r, std::get<2>(result)); else if (std::get<0>(result) == MatchResult::Part) { if (std::get<0>(temp) == MatchResult::Fail || std::get<1>(result) > std::get<1>(temp)) temp = std::make_tuple(MatchResult::Part, std::get<1>(result), r, std::get<2>(result)); else temp = std::make_tuple(MatchResult::Part, std::get<1>(temp), std::get<2>(temp), std::get<3>(temp)); } else if (std::get<0>(temp) == MatchResult::Fail) { if (!std::get<2>(temp) || std::get<1>(result) > std::get<1>(temp)) temp = std::make_tuple(MatchResult::Fail, std::get<1>(result), r, std::get<2>(result)); else temp = std::make_tuple(MatchResult::Fail, std::get<1>(temp), std::get<2>(temp), std::get<3>(temp)); } } return temp; } Any::Any(const QList> mbrs) : mbrs_store(mbrs) {} QList> Any::children() const { return mbrs_store; } std::tuple> Any::match(std::shared_ptr list_head) const { auto item = rule_select(list_head); return std::make_tuple(std::get<0>(item), std::get<1>(item), std::get<3>(item)); } std::tuple, std::shared_ptr> Any::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { std::function, std::shared_ptr)> measure_span = [&](std::shared_ptr anchor, std::shared_ptr head)->int { if (anchor == head) return 1; return measure_span(anchor, head->nextToken()) + 1; }; std::tuple, int> temp_result = std::make_tuple(mbrs_store.first(), 0); for (auto& fork : mbrs_store) { try { auto gen = fork->parse(rt_inst, head); // 遇到成功的直接返回解析结果 if (std::get<0>(gen)) rt_inst->currentInst()->addChild(std::get<0>(gen)); return std::make_tuple(nullptr, std::get<1>(gen)); } // 语法错误的会进行比较 catch (std::shared_ptr ex) { auto current_span = measure_span(ex->targetToken(), head); if (current_span > std::get<1>(temp_result)) temp_result = std::make_tuple(fork, current_span); } } // 分析最匹配的分支 return std::get<0>(temp_result)->parse(rt_inst, head); } 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")"; } SyntaxException::SyntaxException(const QString& message) { this->msg_store = message; } QString SyntaxException::message() const { return msg_store; } ExpressionRule::ExpressionRule(const QString& rule_name, int expr_mark) : name_store(rule_name) { this->filter_proc = [](const TokenSeqs& seqs) { return seqs; }; this->mark_store = expr_mark; } std::shared_ptr ExpressionRule::reloadRule(std::function filter, std::shared_ptr rule) { auto ninst = makeCopy(); ninst->child_store = rule; ninst->filter_proc = filter; return ninst; } QString ExpressionRule::name() const { return name_store; } int ExpressionRule::typeMark() const { return this->mark_store; } QList> ExpressionRule::children() const { return QList>() << this->child_store; } std::tuple> ExpressionRule::match(std::shared_ptr list_head) const { return child_store->match(list_head); } std::tuple, std::shared_ptr> ExpressionRule::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { std::shared_ptr elm_ast = this->newEmptyInstance(); rt_inst->pushExpressionRule(this->shared_from_this()); rt_inst->pushInst(elm_ast); try { auto rstg = child_store->parse(rt_inst, head); auto tokens_decl = this->filter_proc(elm_ast->tokens()); elm_ast->tokensReset(tokens_decl); rt_inst->popInst(); rt_inst->popExpressionRule(); return std::make_tuple(elm_ast, std::get<1>(rstg)); } catch (...) { rt_inst->popInst(); rt_inst->popExpressionRule(); throw; } } QString ExpressionRule::token_present() const { return QString(u8"(%1)").arg(child_store->token_present()); } MismatchException::MismatchException(std::shared_ptr inst) :SyntaxException( QString(u8"Syntax[0x00001]语法匹配错误,不能识别token:%1<%2,%3>").arg(inst->content()).arg(inst->row()).arg(inst->column())), target(inst) {} std::shared_ptrMismatchException::targetToken() const { return this->target; } InputTerminal::InputTerminal() :SyntaxException(u8"Syntax[0x0000]token流提前终止") {}