From e1fe5e1104353e32706d53dd32cabdb28c79c2ce Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Thu, 20 Jun 2024 22:13:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BStoryDefine=E8=8A=82=E7=82=B9?= =?UTF-8?q?CacheLoad=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WsNovelParser/novelparser.cpp | 16 ++++++--- WsNovelParser/novelparser.h | 5 +++ libParse/parse_novel.cpp | 65 ++++++++++++++++++++--------------- libParse/parse_novel.h | 5 ++- libSyntax/ast_basic.cpp | 34 +++++++++--------- libSyntax/ast_novel.cpp | 3 +- libSyntax/syntax_novel.cpp | 4 +-- 7 files changed, 77 insertions(+), 55 deletions(-) diff --git a/WsNovelParser/novelparser.cpp b/WsNovelParser/novelparser.cpp index d252b68..a1f2fdf 100644 --- a/WsNovelParser/novelparser.cpp +++ b/WsNovelParser/novelparser.cpp @@ -7,15 +7,21 @@ #include using namespace example_novel; +using namespace lib_parse; NovelParser::NovelParser() { - this->syntax_defines = example_novel::NovalSyntax::getParseTree(); - checker_list << std::make_shared(); - checker_list << std::make_shared(); - checker_list << std::make_shared(); + this->syntax_defines = NovalSyntax::getParseTree(); + checker_list << std::make_shared(); + checker_list << std::make_shared(); + checker_list << std::make_shared(); - analyzer_ref = std::make_shared(checker_list); + analyzer_ref = std::make_shared(checker_list); +} + +QList> NovelParser::fragmentsSorted() const +{ + return std::dynamic_pointer_cast(checker_list[1])->fragmentsSequence(); } QString NovelParser::version() const diff --git a/WsNovelParser/novelparser.h b/WsNovelParser/novelparser.h index f2aba35..202e809 100644 --- a/WsNovelParser/novelparser.h +++ b/WsNovelParser/novelparser.h @@ -5,6 +5,10 @@ #include #include +namespace example_novel { + class FragmentGraphHelper; +} + class NovelParser { private: @@ -14,6 +18,7 @@ private: public: NovelParser(); + QList> fragmentsSorted() const; virtual QString version() const; std::shared_ptr parse(const QFileInfoList souurce_list) const; diff --git a/libParse/parse_novel.cpp b/libParse/parse_novel.cpp index 42e1a42..812ab43 100644 --- a/libParse/parse_novel.cpp +++ b/libParse/parse_novel.cpp @@ -16,7 +16,7 @@ void FragmentExistsCheck::exists_check(std::shared_ptr root if (target->element()->typeMark() == (int)NovelNode::FragmentRefer) { auto refer = std::dynamic_pointer_cast(target->element()); auto signature = refer->storyRefer() + u8"&" + refer->fragmentRefer(); - if(!root->getNamedNodeBy((int)NovelNode::FragmentDefine, signature)) + if (!root->getNamedNodeBy((int)NovelNode::FragmentDefine, signature)) throw new SyntaxException(QString(u8"CheckError[0x0005]系统中不包含指定签名的节点:%1{%3:(%4)}") .arg(signature).arg((int)NovelNode::FragmentDefine).arg(refer->signature()).arg(refer->filePath())); } @@ -58,6 +58,10 @@ void FragmentGraphCheck::setElement(std::shared_ptr inst) elements_store[inst->nodePeer()->signature()] = inst; } +QList> FragmentGraphCheck::fragmentsSequence() const { + return fragments_sort_list; +} + std::shared_ptr FragmentGraphCheck::getElement(const QString& signature) const { return elements_store[signature]; @@ -92,6 +96,8 @@ bool FragmentGraphCheck::nodeDismantle(std::shared_ptr inst void FragmentGraphCheck::validCheck(std::shared_ptr root) const { + const_cast(this)->fragments_sort_list.clear(); + std::function>(std::shared_ptr)> story_peak = [&](std::shared_ptr root)->QList> { QList> return_temp; @@ -200,6 +206,9 @@ void FragmentGraphCheck::validCheck(std::shared_ptr root) c throw new CheckException(error_msg); } } + + for (auto& inst : values) + const_cast(this)->fragments_sort_list.append(inst); } QString FragmentGraphCheck::name() const { @@ -232,32 +241,32 @@ QList> StoryOrderCheck::valid_docs_peak(std auto type_code = pnode->element()->typeMark(); switch ((NovelNode)type_code) { - case NovelNode::GlobalElement:{ - auto children = pnode->children(); - for(auto &cinst : children){ - values.append(valid_docs_peak(cinst)); - } - }break; + case NovelNode::GlobalElement: { + auto children = pnode->children(); + for (auto& cinst : children) { + values.append(valid_docs_peak(cinst)); + } + }break; - case NovelNode::Document: { - auto storys_collection = pnode->children(); - - bool story_exists = false; - for (auto& syntax_elm : storys_collection) { - if (syntax_elm->element()->typeMark() == (int)NovelNode::StoryDefine) { - story_exists = true; - break; - } - } + case NovelNode::Document: { + auto storys_collection = pnode->children(); - if (story_exists) { - auto first_elm = storys_collection.at(0); - values.append(pnode); - - if(first_elm->element()->typeMark() != (int)NovelNode::RankDeclaration) - throw new CheckException(QString(u8"CheckError[0x0007]具有故事节点的文档必须在第一行指定排序(%1)").arg(pnode->element()->path())); + bool story_exists = false; + for (auto& syntax_elm : storys_collection) { + if (syntax_elm->element()->typeMark() == (int)NovelNode::StoryDefine) { + story_exists = true; + break; } - }break; + } + + if (story_exists) { + auto first_elm = storys_collection.at(0); + values.append(pnode); + + if (first_elm->element()->typeMark() != (int)NovelNode::RankDeclaration) + throw new CheckException(QString(u8"CheckError[0x0007]具有故事节点的文档必须在第一行指定排序(%1)").arg(pnode->element()->path())); + } + }break; default: break; @@ -276,7 +285,7 @@ void StoryOrderCheck::validCheck(std::shared_ptr root) cons const_cast(this)->sort_index = 1; auto story_docs = valid_docs_peak(root); - std::sort(story_docs.begin(), story_docs.end(), [](std::shared_ptr adoc, std::shared_ptr bdoc){ + std::sort(story_docs.begin(), story_docs.end(), [](std::shared_ptr adoc, std::shared_ptr bdoc) { auto elm_xa = std::dynamic_pointer_cast(adoc->children().first()->element()); auto elm_xb = std::dynamic_pointer_cast(bdoc->children().first()->element()); return elm_xa->rankNumber() < elm_xb->rankNumber(); @@ -292,9 +301,9 @@ void StoryOrderCheck::validCheck(std::shared_ptr root) cons } // 故事节点排序 - auto story_node_sort = [](std::shared_ptr doc_node, int start_index) -> int{ + auto story_node_sort = [](std::shared_ptr doc_node, int start_index) -> int { auto childs = doc_node->children(); - for (auto &inst : childs) { + for (auto& inst : childs) { if (inst->element()->typeMark() == (int)NovelNode::StoryDefine) { auto cast_inst = std::dynamic_pointer_cast(inst->element()); std::const_pointer_cast(cast_inst)->setSort(start_index++); @@ -304,6 +313,6 @@ void StoryOrderCheck::validCheck(std::shared_ptr root) cons }; int ranks_number = 1; - for(auto &story : story_docs) + for (auto& story : story_docs) ranks_number = story_node_sort(story, ranks_number); } diff --git a/libParse/parse_novel.h b/libParse/parse_novel.h index 17de0c9..901bcf2 100644 --- a/libParse/parse_novel.h +++ b/libParse/parse_novel.h @@ -19,9 +19,10 @@ namespace example_novel { class LIBPARSE_EXPORT FragmentGraphCheck : public std::enable_shared_from_this, public lib_parse::CheckProvider { private: QHash> elements_store; + QList> fragments_sort_list; QList> refers_cycle_check( - std::shared_ptr item, QList> prevs = QList>()) const; + std::shared_ptr item, QList> prevs = QList>()) const; public: void setElement(std::shared_ptr inst); @@ -30,6 +31,8 @@ namespace example_novel { QList> getHangoutNodes(); bool nodeDismantle(std::shared_ptr inst); + QList> fragmentsSequence() const; + // CheckProvider interface public: virtual void validCheck(std::shared_ptr root) const override; diff --git a/libSyntax/ast_basic.cpp b/libSyntax/ast_basic.cpp index f695f70..3e8d51e 100644 --- a/libSyntax/ast_basic.cpp +++ b/libSyntax/ast_basic.cpp @@ -5,32 +5,32 @@ 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 ast_basic::ExpressionElement::definedRule() const { +std::shared_ptr ExpressionElement::definedRule() const { return _expr_rule; } -QString ast_basic::ExpressionElement::filePath() const { - if(tokens_bind.size()) +QString ExpressionElement::filePath() const { + if(!tokens_bind.size()) throw new SyntaxException(u8"InternalError[0x0002]一个空的非法无效节点"); return tokens_bind.first()->file(); } -void ast_basic::ExpressionElement::tokensReset(const QList>& list) { +void ExpressionElement::tokensReset(const QList>& list) { this->tokens_bind = list; } -void ast_basic::ExpressionElement::addToken(std::shared_ptr token_inst) { +void ExpressionElement::addToken(std::shared_ptr token_inst) { this->tokens_bind.append(token_inst); } -QList> ast_basic::ExpressionElement::children() const { +QList> ExpressionElement::children() const { return this->children_store; } -void ast_basic::ExpressionElement::addChild(std::shared_ptr inst) { +void ExpressionElement::addChild(std::shared_ptr inst) { this->children_store.append(inst); } @@ -38,13 +38,13 @@ QList> ExpressionElement::tokens() const { return this->tokens_bind; } -ast_basic::ExpressionContext::ExpressionContext() {} +ExpressionContext::ExpressionContext() {} -void ast_basic::ExpressionContext::setCurrentFile(const QString& path) { this->current_file_path = path; } +void ExpressionContext::setCurrentFile(const QString& path) { this->current_file_path = path; } -QString ast_basic::ExpressionContext::currentFile() const { return this->current_file_path; } +QString ExpressionContext::currentFile() const { return this->current_file_path; } -std::shared_ptr ast_basic::ExpressionContext::currentInst() const +std::shared_ptr ExpressionContext::currentInst() const { if(expression_stack.size()) return expression_stack.last(); @@ -52,30 +52,30 @@ std::shared_ptr ast_basic::ExpressionContext::currentInst return nullptr; } -void ast_basic::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 ast_basic::ExpressionContext::popInst() +std::shared_ptr ExpressionContext::popInst() { auto lastx = expression_stack.takeLast(); return lastx; } -std::shared_ptr ast_basic::ExpressionContext::currentExpressionRule() const { +std::shared_ptr ExpressionContext::currentExpressionRule() const { if(rule_stack.size()) return rule_stack.last(); return nullptr; } -void ast_basic::ExpressionContext::pushExpressionRule(std::shared_ptr inst) { +void ExpressionContext::pushExpressionRule(std::shared_ptr inst) { if(!rule_stack.size() || rule_stack.last() != inst) rule_stack.append(inst); } -std::shared_ptr ast_basic::ExpressionContext::popExpressionRule() +std::shared_ptr ExpressionContext::popExpressionRule() { return rule_stack.takeLast(); } diff --git a/libSyntax/ast_novel.cpp b/libSyntax/ast_novel.cpp index 25a9e78..6a0fbb0 100644 --- a/libSyntax/ast_novel.cpp +++ b/libSyntax/ast_novel.cpp @@ -91,8 +91,7 @@ int StoryDefine::sort() const { return sort_index; } void StoryDefine::cacheLoad() { - name_store = selfTokens()[3]->token()->content(); - sort_index = selfTokens()[2]->token()->content().toInt(); + name_store = selfTokens()[2]->token()->content(); } int StoryDefine::typeMark() const { return (int)NovelNode::StoryDefine; } diff --git a/libSyntax/syntax_novel.cpp b/libSyntax/syntax_novel.cpp index 2ea1395..14f97f8 100644 --- a/libSyntax/syntax_novel.cpp +++ b/libSyntax/syntax_novel.cpp @@ -81,7 +81,7 @@ auto fragment_refer = ElementRule(u8"fragment_refer", (int)Novel auto fragment_comp = std::make_shared(Rules{ fragment_decl, fragment_refer, decl_expr }); auto story_define = ElementRule(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(remove_nl, std::make_shared( - LinesMerge(Rules{ MR(leftb), MR(story_key), MR(numbers), MR(name_text) }) << + LinesMerge(Rules{ MR(leftb), MR(story_key), MR(name_text) }) << OptMulR(fragment_comp) << LinesMerge(MR(rightb)) )); @@ -137,7 +137,7 @@ void NovalSyntax::node_register(std::shared_ptr ro if (!child->isAnonymous()) { auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child); if(check_result) - throw new lib_syntax::SyntaxException(QString(u8"Parse[0x0004]系统中包含同类型重名命名节点:%1(%3,%4)") + throw new lib_syntax::SyntaxException(QString(u8"SyntaxError[0x0004]系统中包含同类型重名命名节点:%1(%3,%4)") .arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path())); }