diff --git a/libSyntax/libsyntax.cpp b/libSyntax/libsyntax.cpp index 27e701a..27d2faf 100644 --- a/libSyntax/libsyntax.cpp +++ b/libSyntax/libsyntax.cpp @@ -9,30 +9,69 @@ using namespace ast_basic; TokenMatch::TokenMatch(shared_ptr define) : define_peer(define) {} -QList> TokenMatch::children() const { return QList>(); } +QList> TokenMatch::children() const { return QList>(); } -std::tuple> TokenMatch::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { +tuple> +TokenMatch::parse(shared_ptr rt_inst, shared_ptr head, ErrOccurs& fx_type) 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); + rt_inst->appendParseError(rt_inst->currentFile(), -1, QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(rt_inst->currentFile())); + return 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]语法匹配错误,不能识别token\"%1\"(应该为:%4)") - .arg(head->content()).arg(head->row()).arg(head->column()).arg(this->define_peer->reviseWords())); - return std::make_tuple(IBasicRule::MatchResult::Part, head); - } + if (get<0>(match_result)) { + rt_inst->currentExprInst()->addToken(get<0>(match_result)); - if (std::get<1>(match_result)) { - return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared(std::get<1>(match_result), head->nextWord())); + if (get<1>(match_result)) { + return make_tuple(IBasicRule::MatchResult::Success, make_shared(get<1>(match_result), head->nextWord())); + } + else { + return make_tuple(IBasicRule::MatchResult::Success, head->nextWord()); + } } else { - return std::make_tuple(IBasicRule::MatchResult::Success, head->nextWord()); + 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]语法匹配错误,不能识别token:\"%1\"(应该为:%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]语法匹配错误,缺少token:\"%1\"") + .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]语法匹配错误,多余token:\"%1\"") + .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(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; + } } } @@ -40,23 +79,24 @@ 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) {} +Rept::Rept(shared_ptr rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {} -QList> Rept::children() const { return QList>() << rule_peer; } +QList> Rept::children() const { return QList>() << rule_peer; } -std::tuple> Rept::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { +tuple> +Rept::parse(shared_ptr rt_inst, shared_ptr head, IBasicRule::ErrOccurs& fx_type) 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)) { + auto result_gen = rule_peer->parse(rt_inst, temp_head, fx_type); + switch (get<0>(result_gen)) { case IBasicRule::MatchResult::Fail: - return std::make_tuple(IBasicRule::MatchResult::Part, temp_head); + return make_tuple(IBasicRule::MatchResult::Part, temp_head); case IBasicRule::MatchResult::Part: - return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(result_gen)); + return make_tuple(IBasicRule::MatchResult::Part, get<1>(result_gen)); default: - temp_head = std::get<1>(result_gen); + temp_head = get<1>(result_gen); break; } } @@ -66,18 +106,18 @@ std::tuple> Rept::pars if (!temp_head) break; - auto result_gen = rule_peer->parse(rt_inst, temp_head); - switch (std::get<0>(result_gen)) { + auto result_gen = rule_peer->parse(rt_inst, temp_head, fx_type); + switch (get<0>(result_gen)) { case IBasicRule::MatchResult::Fail: case IBasicRule::MatchResult::Part: - return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); + return make_tuple(IBasicRule::MatchResult::Success, temp_head); default: - temp_head = std::get<1>(result_gen); + temp_head = get<1>(result_gen); break; } } - return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); + return make_tuple(IBasicRule::MatchResult::Success, temp_head); } QString Rept::token_present() const @@ -85,29 +125,30 @@ 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) {} +Seqs::Seqs(const QList> mbrs) : mbrs_store(mbrs) {} -QList> Seqs::children() const { return mbrs_store; } +QList> Seqs::children() const { return mbrs_store; } -std::tuple> Seqs::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { +tuple> +Seqs::parse(shared_ptr rt_inst, shared_ptr head, IBasicRule::ErrOccurs& fx_type) 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)) { + auto rst_gene = r->parse(rt_inst, temp_head, fx_type); + switch (get<0>(rst_gene)) { case IBasicRule::MatchResult::Fail: - return std::make_tuple(IBasicRule::MatchResult::Part, temp_head); + return 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); + temp_head = get<1>(rst_gene); break; default: break; } } - return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); + return make_tuple(IBasicRule::MatchResult::Success, temp_head); } QString Seqs::token_present() const @@ -118,10 +159,10 @@ QString Seqs::token_present() const return QString(u8"(%1)").arg(content); } -//std::tuple, std::shared_ptr> -Any::Any(const QList> mbrs) : mbrs_store(mbrs) {} +//tuple, shared_ptr> +Any::Any(const QList> mbrs) : mbrs_store(mbrs) {} -QList> Any::children() const { return mbrs_store; } +QList> Any::children() const { return mbrs_store; } class words_span { public: @@ -135,24 +176,25 @@ public: return false; } }; -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); +tuple> +Any::parse(shared_ptr rt_inst, shared_ptr head, IBasicRule::ErrOccurs& fx_type) const { + tuple, uint64_t> temp_result = 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)) { + auto gen = fork->parse(rt_inst, head, fx_type); + switch (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); + if (!get<0>(temp_result)) + temp_result = 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); + auto span = get<1>(gen)->position() - head->position(); + if (span >= get<1>(temp_result)) + temp_result = make_tuple(fork, span); } default: break; @@ -161,8 +203,8 @@ std::tuple> Any::parse // 分析最匹配的分支 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)); + auto temp = get<0>(temp_result)->parse(rt_inst, head, fx_type); + return make_tuple(IBasicRule::MatchResult::Part, get<1>(temp)); } QString Any::token_present() const @@ -183,7 +225,7 @@ ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_na this->mark_store = expr_mark; } -std::shared_ptr ExprRule::reloadRule(std::shared_ptr rule) { +shared_ptr ExprRule::reloadRule(shared_ptr rule) { auto ninst = makeCopy(); ninst->child_store = rule; return ninst; @@ -193,49 +235,59 @@ QString ExprRule::name() const { return name_store; } int ExprRule::typeMark() const { return this->mark_store; } -QList> ExprRule::children() const { - return QList>() << this->child_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(); +tuple> +ExprRule::parse(shared_ptr rt_inst, shared_ptr head, IBasicRule::ErrOccurs& fx_type) const { + QList opts_list = { + IBasicRule::ErrOccurs::None, IBasicRule::ErrOccurs::Surplus, IBasicRule::ErrOccurs::Absence, IBasicRule::ErrOccurs::Replace }; - rt_inst->pushExprRule(this->shared_from_this()); - rt_inst->pushExprInst(elm_ast); + auto opt_current_itor = opts_list.cbegin(); + tuple> ret_tuple; + do { + auto opt_val = *opt_current_itor; + shared_ptr elm_ast = this->newEmptyInstance(); + auto text_present = this->token_present(); - auto rstg = child_store->parse(rt_inst, head); - auto tokens_decl = elm_ast->tokens(); + rt_inst->pushExprRule(this->shared_from_this()); + rt_inst->pushExprInst(elm_ast); - 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); + 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(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; } - rt_inst->popExprInst(); - rt_inst->popExprRule(); + opt_current_itor++; + } while (fx_type != IBasicRule::ErrOccurs::None && opt_current_itor != opts_list.cend()); - if (rt_inst->currentExprInst()) { - rt_inst->currentExprInst()->addChild(elm_ast); - } - else { - rt_inst->appendDocInst(elm_ast); - } - }break; - default: - break; - } - - return rstg; + return ret_tuple; } QString ExprRule::token_present() const { diff --git a/libSyntax/libsyntax.h b/libSyntax/libsyntax.h index efff551..1e6d4dd 100644 --- a/libSyntax/libsyntax.h +++ b/libSyntax/libsyntax.h @@ -81,14 +81,25 @@ namespace lib_syntax { Fail // 从第一个词起完全不匹配 }; + /** + * @brief 修正类型 + */ + enum class ErrOccurs { + None, // 无 + Replace, // 错一个词 + Absence, // 少一个词 + Surplus // 多一个词 + }; + /** * @brief 解析 * @param rt_inst 解析上下文 * @param head 列表头 + * @param fx_type 修正措施 * @return 返回结果<匹配完成新列表头,匹配长度> */ virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const = 0; + parse(std::shared_ptr rt_inst, std::shared_ptr head, ErrOccurs &fx_type) const = 0; /** * 返回匹配语法规则的词法序列表达 @@ -113,7 +124,7 @@ namespace lib_syntax { public: virtual QList> children() const override; virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + parse(std::shared_ptr rt_inst, std::shared_ptr head, ErrOccurs &fx_type) const override; virtual QString token_present() const override; }; @@ -133,7 +144,7 @@ namespace lib_syntax { public: virtual QList> children() const override; virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + parse(std::shared_ptr rt_inst, std::shared_ptr head, ErrOccurs &fx_type) const override; virtual QString token_present() const override; }; @@ -151,7 +162,7 @@ namespace lib_syntax { public: virtual QList> children() const override; virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + parse(std::shared_ptr rt_inst, std::shared_ptr head, ErrOccurs &fx_type) const override; virtual QString token_present() const override; }; @@ -170,7 +181,7 @@ namespace lib_syntax { public: virtual QList> children() const override; virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + parse(std::shared_ptr rt_inst, std::shared_ptr head, ErrOccurs &fixed_type) const override; virtual QString token_present() const override; }; @@ -194,7 +205,7 @@ namespace lib_syntax { public: virtual QList> children() const override; virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + parse(std::shared_ptr rt_inst, std::shared_ptr head, ErrOccurs &fx_type) const override; virtual QString token_present() const override; protected: