diff --git a/WsNovelParser/novelparser.cpp b/WsNovelParser/novelparser.cpp index 466dd53..d7f2be4 100644 --- a/WsNovelParser/novelparser.cpp +++ b/WsNovelParser/novelparser.cpp @@ -30,7 +30,7 @@ QString NovelParser::version() const } std::shared_ptr NovelParser::parse(const QFileInfoList source_list) const { - QList> forst_root; + QList> forst_root; auto word_reader = std::make_shared(); auto context = std::make_shared(u8"小说"); @@ -38,9 +38,10 @@ std::shared_ptr NovelParser::parse(const QFileInfo for (auto& file : source_list) { context->setCurrentFile(file.canonicalFilePath()); auto words = word_reader->wordsFrom(file.canonicalFilePath()); - auto exprs_result = this->syntax_defines->parse(context, words); - forst_root.append(std::get<0>(exprs_result)); + this->syntax_defines->parse(context, words); } + + forst_root = context->getDocs(); auto current_stamp = QTime::currentTime(); qDebug().noquote() << QString(u8"%词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp)); diff --git a/WsNovelParser/novelparser.h b/WsNovelParser/novelparser.h index 202e809..d56da51 100644 --- a/WsNovelParser/novelparser.h +++ b/WsNovelParser/novelparser.h @@ -12,7 +12,7 @@ namespace example_novel { class NovelParser { private: - std::shared_ptr syntax_defines; + std::shared_ptr syntax_defines; QList> checker_list; std::shared_ptr analyzer_ref; diff --git a/libParse/libparse.cpp b/libParse/libparse.cpp index 16a26ce..89a83a0 100644 --- a/libParse/libparse.cpp +++ b/libParse/libparse.cpp @@ -48,4 +48,5 @@ bool VisitorControl::visitWith(std::shared_ptr syntax_elm, default: break; } + return false; } diff --git a/libSyntax/ast_basic.cpp b/libSyntax/ast_basic.cpp index 17fee14..c40b209 100644 --- a/libSyntax/ast_basic.cpp +++ b/libSyntax/ast_basic.cpp @@ -5,9 +5,9 @@ using namespace ast_basic; using namespace lib_token; using namespace lib_syntax; -ExpressionElement::ExpressionElement(std::shared_ptr bind) : _expr_rule(bind) {} +ExpressionElement::ExpressionElement(std::shared_ptr bind) : _expr_rule(bind) {} -std::shared_ptr ExpressionElement::definedRule() const { +std::shared_ptr ExpressionElement::definedRule() const { return _expr_rule; } @@ -26,11 +26,11 @@ void ExpressionElement::addToken(std::shared_ptr token_inst) { this->tokens_bind.append(token_inst); } -QList> ExpressionElement::children() const { +QList> ExpressionElement::children() const { return this->children_store; } -void ExpressionElement::addChild(std::shared_ptr inst) { +void ExpressionElement::addChild(std::shared_ptr inst) { this->children_store.append(inst); } @@ -44,7 +44,7 @@ void ExpressionContext::setCurrentFile(const QString& path) { this->current_file QString ExpressionContext::currentFile() const { return this->current_file_path; } -std::shared_ptr ExpressionContext::currentInst() const +std::shared_ptr ExpressionContext::currentInst() const { if (expression_stack.size()) return expression_stack.last(); @@ -52,13 +52,13 @@ std::shared_ptr ExpressionContext::currentInst() const return nullptr; } -void ExpressionContext::pushInst(std::shared_ptr current_inst) +void ExpressionContext::pushInst(std::shared_ptr current_inst) { if (!expression_stack.size() || expression_stack.last() != current_inst) expression_stack.append(current_inst); } -std::shared_ptr ExpressionContext::popInst() +std::shared_ptr ExpressionContext::popInst() { auto lastx = expression_stack.takeLast(); return lastx; @@ -102,4 +102,13 @@ void ExpressionContext::clearErrors(int start) { QList> ExpressionContext::currentExpressionRuleStack() const { return rule_stack; +} + + +void ExpressionContext::appendDoc(std::shared_ptr inst) { + this->document_store.append(inst); +} + +QList> ExpressionContext::getDocs() const { + return this->document_store; } \ No newline at end of file diff --git a/libSyntax/ast_basic.h b/libSyntax/ast_basic.h index f1fd8ac..12a3a9a 100644 --- a/libSyntax/ast_basic.h +++ b/libSyntax/ast_basic.h @@ -10,15 +10,15 @@ namespace ast_basic { /** * @brief 抽象语法树集合节点/表达式节点 */ - class Expression { + class ExprInst { public: - virtual ~Expression() = default; + virtual ~ExprInst() = default; /** * @brief 获取表达式的解析规则 * @return */ - virtual std::shared_ptr definedRule() const = 0; + virtual std::shared_ptr definedRule() const = 0; //===================================================== /** * 获取语法节点的源码文件路径. @@ -49,13 +49,13 @@ namespace ast_basic { * @brief 子表达式集合 * @return */ - virtual QList> children() const = 0; + virtual QList> children() const = 0; /** * @brief 添加子表达式. * * \param inst 子表达式实例 */ - virtual void addChild(std::shared_ptr inst) = 0; + virtual void addChild(std::shared_ptr inst) = 0; }; @@ -63,31 +63,32 @@ namespace ast_basic { /** * @brief 表达式节点 */ - class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::Expression, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::ExprInst, public std::enable_shared_from_this { private: - std::shared_ptr _expr_rule; - QList> children_store; + std::shared_ptr _expr_rule; + QList> children_store; QList> tokens_bind; public: - ExpressionElement(std::shared_ptr bind); + ExpressionElement(std::shared_ptr bind); // 通过 Expression 继承 - std::shared_ptr definedRule() const override; + std::shared_ptr definedRule() const override; QString filePath() const override; QList> tokens() const override; void tokensReset(const QList>& list) override; void addToken(std::shared_ptr token_inst) override; - QList> children() const override; - void addChild(std::shared_ptr inst) override; + QList> children() const override; + void addChild(std::shared_ptr inst) override; }; class LIBSYNTAX_EXPORT ExpressionContext : public lib_syntax::ParseContext, public std::enable_shared_from_this { private: QList> rule_stack; - QList> expression_stack; + QList> expression_stack; + QList> document_store; QString current_file_path; QList> errors_storage; @@ -98,15 +99,18 @@ namespace ast_basic { virtual QString currentFile() const; // 通过 ParseContext 继承 - std::shared_ptr currentInst() const override; - void pushInst(std::shared_ptr current_inst) override; - std::shared_ptr popInst() override; + std::shared_ptr currentInst() const override; + void pushInst(std::shared_ptr current_inst) override; + std::shared_ptr popInst() override; std::shared_ptr currentExpressionRule() const override; void pushExpressionRule(std::shared_ptr inst) override; std::shared_ptr popExpressionRule() override; virtual QList> currentExpressionRuleStack() const; + virtual void appendDoc(std::shared_ptr inst) override; + virtual QList> getDocs() const override; + void appendParseErrors(int start, const QString& error_msg) override; QStringList errors() const override; void clearErrors(int start) override; diff --git a/libSyntax/ast_gen.cpp b/libSyntax/ast_gen.cpp index 87bd96e..3284993 100644 --- a/libSyntax/ast_gen.cpp +++ b/libSyntax/ast_gen.cpp @@ -39,7 +39,7 @@ void ast_gen::GlobalElement::setParent(std::shared_ptr inst QList> GlobalElement::selfTokens() const { return QList>(); } -std::shared_ptr ast_gen::GlobalElement::bindExpression() const +std::shared_ptr ast_gen::GlobalElement::bindExpression() const { return bind_exprs; } @@ -49,7 +49,7 @@ void ast_gen::GlobalElement::cacheLoad() } void GlobalElement::addChild(std::shared_ptr citem) { - auto convx = std::dynamic_pointer_cast(citem); + auto convx = std::dynamic_pointer_cast(citem); bind_exprs->addChild(convx); } diff --git a/libSyntax/ast_gen.h b/libSyntax/ast_gen.h index b0c280b..71c54bb 100644 --- a/libSyntax/ast_gen.h +++ b/libSyntax/ast_gen.h @@ -21,7 +21,7 @@ namespace ast_gen * * \return 表达式实例 */ - virtual std::shared_ptr bindExpression() const = 0; + virtual std::shared_ptr bindExpression() const = 0; /** * @brief 类型标记 @@ -107,7 +107,7 @@ namespace ast_gen QString names_store; QHash> node_cache; - std::shared_ptr bind_exprs = std::make_shared(nullptr); + std::shared_ptr bind_exprs = std::make_shared(nullptr); public: static GlobalElement* UniquePtr; @@ -135,7 +135,7 @@ namespace ast_gen virtual QList> selfTokens() const override; // 通过 SyntaxElement 继承 - virtual std::shared_ptr bindExpression() const override; + virtual std::shared_ptr bindExpression() const override; virtual void cacheLoad() override; }; } \ No newline at end of file diff --git a/libSyntax/ast_novel.cpp b/libSyntax/ast_novel.cpp index 6a0fbb0..1dc2662 100644 --- a/libSyntax/ast_novel.cpp +++ b/libSyntax/ast_novel.cpp @@ -4,7 +4,7 @@ using namespace example_novel; using namespace lib_syntax; -TextSection::TextSection(std::shared_ptr rule_bind) +TextSection::TextSection(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) {} QString TextSection::content() const @@ -30,7 +30,7 @@ void TextSection::cacheLoad() context_store = text; } -FragmentRefers::FragmentRefers(std::shared_ptr rule_bind) +FragmentRefers::FragmentRefers(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) {} QString FragmentRefers::storyRefer() const { return story_refs; } @@ -59,7 +59,7 @@ QString FragmentRefers::signature() const { } -FragmentDefine::FragmentDefine(std::shared_ptr rule_bind) +FragmentDefine::FragmentDefine(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) {} QString FragmentDefine::name() const { return name_store; } @@ -78,7 +78,7 @@ bool FragmentDefine::isAnonymous() const QString FragmentDefine::signature() const { return parent()->signature() + u8"&" + name(); } -StoryDefine::StoryDefine(std::shared_ptr rule_bind) +StoryDefine::StoryDefine(std::shared_ptr rule_bind) : AbstractImpl(rule_bind), sort_index(0) {} QString StoryDefine::name() const { return name_store; } @@ -104,7 +104,7 @@ bool StoryDefine::isAnonymous() const QString StoryDefine::signature() const { return name(); } #include "syntax_novel.h" -Document::Document(std::shared_ptr rule_bind) +Document::Document(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) {} int Document::typeMark() const { return (int)NovelNode::Document; } @@ -120,7 +120,7 @@ void Document::cacheLoad() { } -AbstractImpl::AbstractImpl(std::shared_ptr rule_bind) +AbstractImpl::AbstractImpl(std::shared_ptr rule_bind) : ExpressionElement(rule_bind) { parent_store.reset(); } QList > AbstractImpl::selfTokens() const { @@ -145,7 +145,7 @@ void AbstractImpl::setParent(std::shared_ptr inst) // 通过 SyntaxElement 继承 -std::shared_ptr AbstractImpl::bindExpression() const { +std::shared_ptr AbstractImpl::bindExpression() const { return shared_from_this(); } @@ -154,7 +154,7 @@ QString AbstractImpl::path() const return ast_basic::ExpressionElement::filePath(); } -VolumeDefine::VolumeDefine(std::shared_ptr rule_bind) +VolumeDefine::VolumeDefine(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) {} QString VolumeDefine::name() const { return name_store; } @@ -173,7 +173,7 @@ bool VolumeDefine::isAnonymous() const QString VolumeDefine::signature() const { return name(); } -ArticleDefine::ArticleDefine(std::shared_ptr rule_bind) +ArticleDefine::ArticleDefine(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) {} QString ArticleDefine::name() const { return name_store; } @@ -192,7 +192,7 @@ bool ArticleDefine::isAnonymous() const QString ArticleDefine::signature() const { return parent()->signature() + u8"&" + name(); } -RankDeclare::RankDeclare(std::shared_ptr rule) +RankDeclare::RankDeclare(std::shared_ptr rule) : AbstractImpl(rule) { } diff --git a/libSyntax/ast_novel.h b/libSyntax/ast_novel.h index 47c10ea..45e3b79 100644 --- a/libSyntax/ast_novel.h +++ b/libSyntax/ast_novel.h @@ -21,10 +21,10 @@ namespace example_novel std::weak_ptr parent_store; public: - explicit AbstractImpl(std::shared_ptr rule_bind); + explicit AbstractImpl(std::shared_ptr rule_bind); // 通过 SyntaxElement 继承 - virtual std::shared_ptr bindExpression() const override; + virtual std::shared_ptr bindExpression() const override; QString path() const override; virtual std::shared_ptr parent() const override; virtual void setParent(std::shared_ptr inst) override; @@ -34,7 +34,7 @@ namespace example_novel class LIBSYNTAX_EXPORT TextSection : public AbstractImpl { public: - TextSection(std::shared_ptr rule_bind); + TextSection(std::shared_ptr rule_bind); QString content() const; @@ -51,7 +51,7 @@ namespace example_novel class LIBSYNTAX_EXPORT FragmentRefers : public AbstractImpl { public: - FragmentRefers(std::shared_ptr rule_bind); + FragmentRefers(std::shared_ptr rule_bind); QString storyRefer() const; QString fragmentRefer() const; @@ -70,7 +70,7 @@ namespace example_novel class LIBSYNTAX_EXPORT FragmentDefine : public AbstractImpl { public: - FragmentDefine(std::shared_ptr rule_bind); + FragmentDefine(std::shared_ptr rule_bind); QString name() const; @@ -87,7 +87,7 @@ namespace example_novel class LIBSYNTAX_EXPORT ArticleDefine : public AbstractImpl { public: - ArticleDefine(std::shared_ptr rule_bind); + ArticleDefine(std::shared_ptr rule_bind); QString name() const; @@ -104,7 +104,7 @@ namespace example_novel class LIBSYNTAX_EXPORT VolumeDefine : public AbstractImpl { public: - VolumeDefine(std::shared_ptr rule_bind); + VolumeDefine(std::shared_ptr rule_bind); QString name() const; @@ -121,7 +121,7 @@ namespace example_novel class LIBSYNTAX_EXPORT StoryDefine : public AbstractImpl { public: - StoryDefine(std::shared_ptr rule_bind); + StoryDefine(std::shared_ptr rule_bind); QString name() const; void setSort(int value); @@ -141,7 +141,7 @@ namespace example_novel class LIBSYNTAX_EXPORT Document : public AbstractImpl { public: - Document(std::shared_ptr rule_bind); + Document(std::shared_ptr rule_bind); // SyntaxElement interface public: @@ -155,7 +155,7 @@ namespace example_novel private: int page_rank = 0; public: - RankDeclare(std::shared_ptr rule); + RankDeclare(std::shared_ptr rule); int rankNumber() const; diff --git a/libSyntax/libsyntax.cpp b/libSyntax/libsyntax.cpp index 5767ca8..cc4c0a9 100644 --- a/libSyntax/libsyntax.cpp +++ b/libSyntax/libsyntax.cpp @@ -11,12 +11,10 @@ TokenMatch::TokenMatch(shared_ptr define) : define_peer(defi QList> TokenMatch::children() const { return QList>(); } -std::tuple, std::shared_ptr> TokenMatch::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { - if (!head) - throw new InputTerminal(rt_inst->currentFile()); - - if (head->content() == u8"初遇江枫的") { - qDebug() << u8"初遇江枫的"; +std::tuple> TokenMatch::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { + if (!head) { + rt_inst->appendParseErrors(-1, QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(rt_inst->currentFile())); + return std::make_tuple(BaseRule::MatchResult::Fail, head); } auto match_result = define_peer->analysis(head); @@ -24,14 +22,17 @@ std::tuple, std::shared_ptr> rt_inst->currentInst()->addToken(std::get<0>(match_result)); } else { - throw new MismatchException(head); + rt_inst->appendParseErrors(head->position(), + QString(u8"Syntax[0x00001]语法匹配错误,不能识别token:%1<%2,%3>(%4)") + .arg(head->content()).arg(head->row()).arg(head->column()).arg(head->file())); + return std::make_tuple(BaseRule::MatchResult::Part, head); } if (std::get<1>(match_result)) { - return std::make_tuple(nullptr, std::make_shared(std::get<1>(match_result), head->nextWord())); + return std::make_tuple(BaseRule::MatchResult::Success, std::make_shared(std::get<1>(match_result), head->nextWord())); } else { - return std::make_tuple(nullptr, head->nextWord()); + return std::make_tuple(BaseRule::MatchResult::Success, head->nextWord()); } } @@ -43,34 +44,40 @@ Rept::Rept(std::shared_ptr rule, int min, int max) : rule_peer(r QList> Rept::children() const { return QList>() << rule_peer; } -std::tuple, std::shared_ptr> Rept::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { +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); - if (std::get<0>(result_gen)) - rt_inst->currentInst()->addChild(std::get<0>(result_gen)); - - temp_head = std::get<1>(result_gen); + switch (std::get<0>(result_gen)) { + case BaseRule::MatchResult::Fail: + return std::make_tuple(BaseRule::MatchResult::Part, temp_head); + case BaseRule::MatchResult::Part: + return std::make_tuple(BaseRule::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) { - 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)); + if (!temp_head) + break; + auto result_gen = rule_peer->parse(rt_inst, temp_head); + switch (std::get<0>(result_gen)) { + case BaseRule::MatchResult::Fail: + case BaseRule::MatchResult::Part: + return std::make_tuple(BaseRule::MatchResult::Success, temp_head); + default: temp_head = std::get<1>(result_gen); - } - catch (SyntaxException* ex) { - delete ex; - return std::make_tuple(nullptr, temp_head); + break; } } - return std::make_tuple(nullptr, temp_head); + return std::make_tuple(BaseRule::MatchResult::Success, temp_head); } QString Rept::token_present() const @@ -82,18 +89,25 @@ Seqs::Seqs(const QList> mbrs) : mbrs_store(mbrs) QList> Seqs::children() const { return mbrs_store; } -std::tuple, std::shared_ptr> Seqs::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { +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); - temp_head = std::get<1>(rst_gene); - - if (std::get<0>(rst_gene)) - rt_inst->currentInst()->addChild(std::get<0>(rst_gene)); + switch (std::get<0>(rst_gene)) { + case BaseRule::MatchResult::Fail: + return std::make_tuple(BaseRule::MatchResult::Part, temp_head); + case BaseRule::MatchResult::Part: + return rst_gene; + case BaseRule::MatchResult::Success: + temp_head = std::get<1>(rst_gene); + break; + default: + break; + } } - return std::make_tuple(nullptr, temp_head); + return std::make_tuple(BaseRule::MatchResult::Success, temp_head); } QString Seqs::token_present() const @@ -112,44 +126,43 @@ QList> Any::children() const { return mbrs_store class words_span { public: int row_span, column_span; - words_span(int rspan, int cspan):row_span(rspan), column_span(cspan){} + words_span(int rspan, int cspan) :row_span(rspan), column_span(cspan) {} bool operator>(const words_span& other) { - if(row_span > other.row_span) + if (row_span > other.row_span) return true; - if(row_span == other.row_span) + if (row_span == other.row_span) return column_span > other.column_span; return false; } }; -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)->words_span { - return words_span(anchor->row() - head->row(), anchor->column() - head->column()); - }; +std::tuple> Any::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { + std::tuple, int> temp_result = std::make_tuple(nullptr, -1); + auto rule_present = this->token_present(); - std::tuple, words_span> temp_result = std::make_tuple(mbrs_store.first(), words_span(0,0)); for (auto& fork : mbrs_store) { - try { - auto gen = fork->parse(rt_inst, head); - + auto gen = fork->parse(rt_inst, head); + switch (std::get<0>(gen)) { // 遇到成功的直接返回解析结果 - if (std::get<0>(gen)) - rt_inst->currentInst()->addChild(std::get<0>(gen)); - return std::make_tuple(nullptr, std::get<1>(gen)); + case BaseRule::MatchResult::Success: + return gen; + case BaseRule::MatchResult::Fail: { + if (!std::get<0>(temp_result)) + temp_result = std::make_tuple(fork, 0); + }break; + case BaseRule::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); } - // 语法错误的会进行比较 - catch (MismatchException* ex) { - auto current_span = measure_span(ex->targetWord(), head); - - if (current_span > std::get<1>(temp_result)) - temp_result = std::make_tuple(fork, current_span); - - delete ex; + default: + break; } } // 分析最匹配的分支 - return std::get<0>(temp_result)->parse(rt_inst, head); + rt_inst->clearErrors(head->position()); + auto temp = std::get<0>(temp_result)->parse(rt_inst, head); + return std::make_tuple(BaseRule::MatchResult::Part, std::get<1>(temp)); } QString Any::token_present() const @@ -166,60 +179,63 @@ SyntaxException::SyntaxException(const QString& message) { this->msg_store = mes QString SyntaxException::message() const { return msg_store; } -ExpressionRule::ExpressionRule(const QString& rule_name, int expr_mark) : name_store(rule_name) { +ExprRule::ExprRule(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) { +std::shared_ptr ExprRule::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; } +QString ExprRule::name() const { return name_store; } -int ExpressionRule::typeMark() const { return this->mark_store; } +int ExprRule::typeMark() const { return this->mark_store; } -QList> ExpressionRule::children() const { +QList> ExprRule::children() const { return QList>() << this->child_store; } -std::tuple, std::shared_ptr> ExpressionRule::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { - std::shared_ptr elm_ast = this->newEmptyInstance(); +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->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); + auto rstg = child_store->parse(rt_inst, head); + auto tokens_decl = this->filter_proc(elm_ast->tokens()); + elm_ast->tokensReset(tokens_decl); + switch (std::get<0>(rstg)) { + case BaseRule::MatchResult::Fail: + case BaseRule::MatchResult::Part: rt_inst->popInst(); rt_inst->popExpressionRule(); - return std::make_tuple(elm_ast, std::get<1>(rstg)); - } - catch (...) { + break; + case BaseRule::MatchResult::Success: { + rt_inst->clearErrors(tokens_decl.first()->position()); rt_inst->popInst(); rt_inst->popExpressionRule(); - throw; + + if (rt_inst->currentInst()) { + rt_inst->currentInst()->addChild(elm_ast); + } + else { + rt_inst->appendDoc(elm_ast); + } + }break; + default: + break; } + + return rstg; } -QString ExpressionRule::token_present() const { +QString ExprRule::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>(%4)") - .arg(inst->content()).arg(inst->row()).arg(inst->column()).arg(inst->file())), target(inst) {} - -std::shared_ptrMismatchException::targetWord() const { - return this->target; -} - -InputTerminal::InputTerminal(const QString& file_path) - :SyntaxException(QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(file_path)) {} diff --git a/libSyntax/libsyntax.h b/libSyntax/libsyntax.h index 4fc94fa..600528b 100644 --- a/libSyntax/libsyntax.h +++ b/libSyntax/libsyntax.h @@ -6,7 +6,7 @@ #include #include namespace ast_basic { - class Expression; + class ExprInst; class ExpressionElement; } @@ -42,14 +42,17 @@ namespace lib_syntax { virtual QStringList errors() const = 0; virtual void clearErrors(int start) = 0; + virtual void appendDoc(std::shared_ptr inst) = 0; + virtual QList> getDocs() const = 0; + /** * \brief 当前表达式元素. * * \return 返回当前表达式 */ - virtual std::shared_ptr currentInst() const = 0; - virtual void pushInst(std::shared_ptr current_inst) = 0; - virtual std::shared_ptr popInst() = 0; + virtual std::shared_ptr currentInst() const = 0; + virtual void pushInst(std::shared_ptr current_inst) = 0; + virtual std::shared_ptr popInst() = 0; virtual std::shared_ptr currentExpressionRule() const = 0; virtual void pushExpressionRule(std::shared_ptr inst) = 0; @@ -84,7 +87,7 @@ namespace lib_syntax { * @param head 列表头 * @return 返回结果<匹配完成新列表头,匹配长度> */ - virtual std::tuple, std::shared_ptr> + virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const = 0; /** @@ -109,7 +112,7 @@ namespace lib_syntax { // BaseRule interface public: virtual QList> children() const override; - virtual std::tuple, std::shared_ptr> + virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -129,7 +132,7 @@ namespace lib_syntax { // BaseRule interface public: virtual QList> children() const override; - virtual std::tuple, std::shared_ptr> + virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -147,7 +150,7 @@ namespace lib_syntax { // BaseRule interface public: virtual QList> children() const override; - virtual std::tuple, std::shared_ptr> + virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -166,7 +169,7 @@ namespace lib_syntax { // BaseRule interface public: virtual QList> children() const override; - virtual std::tuple, std::shared_ptr> + virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -175,22 +178,22 @@ namespace lib_syntax { /** * @brief 对应语法表达式解析规则 */ - class LIBSYNTAX_EXPORT ExpressionRule : public lib_syntax::BaseRule, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT ExprRule : public lib_syntax::BaseRule, public std::enable_shared_from_this { public: typedef QList> TokenSeqs; - ExpressionRule(const QString& rule_name, int expr_mark); + ExprRule(const QString& rule_name, int expr_mark); - virtual std::shared_ptr reloadRule(std::function filter, std::shared_ptr rule); + virtual std::shared_ptr reloadRule(std::function filter, std::shared_ptr rule); virtual QString name() const; virtual int typeMark() const; - virtual std::shared_ptr newEmptyInstance() const = 0; - virtual std::shared_ptr makeCopy() const = 0; + virtual std::shared_ptr newEmptyInstance() const = 0; + virtual std::shared_ptr makeCopy() const = 0; // BaseRule interface public: virtual QList> children() const override; - virtual std::tuple, std::shared_ptr> + virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; @@ -205,35 +208,18 @@ namespace lib_syntax { * 语法元素解析规则. */ template - class ElementRule : public ExpressionRule { + class ElementRule : public ExprRule { public: ElementRule(const QString& rule_name, int expr_mark) - :ExpressionRule(rule_name, expr_mark){} + :ExprRule(rule_name, expr_mark){} - virtual std::shared_ptr newEmptyInstance() const { - return std::dynamic_pointer_cast( + virtual std::shared_ptr newEmptyInstance() const { + return std::dynamic_pointer_cast( std::make_shared(this->shared_from_this())); } - virtual std::shared_ptr makeCopy() const { + virtual std::shared_ptr makeCopy() const { return std::make_shared>(name(), typeMark()); } }; - - - class MismatchException : public SyntaxException { - private: - std::shared_ptr target; - - public: - MismatchException(std::shared_ptr inst); - virtual ~MismatchException() = default; - - virtual std::shared_ptr targetWord() const; - }; - - class InputTerminal : public SyntaxException { - public: - InputTerminal(const QString &file_path); - }; } // namespace lib_syntax \ No newline at end of file diff --git a/libSyntax/syntax_novel.cpp b/libSyntax/syntax_novel.cpp index 14f97f8..e8940a4 100644 --- a/libSyntax/syntax_novel.cpp +++ b/libSyntax/syntax_novel.cpp @@ -52,8 +52,8 @@ QList> LinesMerge(std::shared_ptrExpressionRule::TokenSeqs { - ExpressionRule::TokenSeqs result; +auto remove_nl = [](const ExprRule::TokenSeqs& p)->ExprRule::TokenSeqs { + ExprRule::TokenSeqs result; for (auto& n : p) { if (n->define()->typeMark() == newl->typeMark()) continue; @@ -109,7 +109,7 @@ auto document_define = ElementRule(u8"decls-doc", (int)NovelExprs::DOC } )); -std::shared_ptr NovalSyntax::getParseTree() { return document_define; } +std::shared_ptr NovalSyntax::getParseTree() { return document_define; } std::shared_ptr NovalSyntax::tidy(std::shared_ptr root, QList> children) { cache_load(root, children); diff --git a/libSyntax/syntax_novel.h b/libSyntax/syntax_novel.h index f46271c..6c4b776 100644 --- a/libSyntax/syntax_novel.h +++ b/libSyntax/syntax_novel.h @@ -28,7 +28,7 @@ namespace example_novel { * @brief 获取novel语法解析树 * @return */ - static std::shared_ptr getParseTree(); + static std::shared_ptr getParseTree(); static std::shared_ptr tidy(std::shared_ptr root, QList> docs);