改进StoryDefine节点CacheLoad机制

This commit is contained in:
codeboss 2024-06-20 22:13:06 +08:00
parent 4e99a6f120
commit e1fe5e1104
7 changed files with 77 additions and 55 deletions

View File

@ -7,15 +7,21 @@
#include <QDebug> #include <QDebug>
using namespace example_novel; using namespace example_novel;
using namespace lib_parse;
NovelParser::NovelParser() NovelParser::NovelParser()
{ {
this->syntax_defines = example_novel::NovalSyntax::getParseTree(); this->syntax_defines = NovalSyntax::getParseTree();
checker_list << std::make_shared<example_novel::FragmentExistsCheck>(); checker_list << std::make_shared<FragmentExistsCheck>();
checker_list << std::make_shared<example_novel::StoryOrderCheck>(); checker_list << std::make_shared<StoryOrderCheck>();
checker_list << std::make_shared<example_novel::FragmentGraphCheck>(); checker_list << std::make_shared<FragmentGraphCheck>();
analyzer_ref = std::make_shared<lib_parse::Analyzer>(checker_list); analyzer_ref = std::make_shared<Analyzer>(checker_list);
}
QList<std::shared_ptr<const FragmentGraphHelper>> NovelParser::fragmentsSorted() const
{
return std::dynamic_pointer_cast<const FragmentGraphCheck>(checker_list[1])->fragmentsSequence();
} }
QString NovelParser::version() const QString NovelParser::version() const

View File

@ -5,6 +5,10 @@
#include <libsyntax.h> #include <libsyntax.h>
#include <libparse.h> #include <libparse.h>
namespace example_novel {
class FragmentGraphHelper;
}
class NovelParser class NovelParser
{ {
private: private:
@ -14,6 +18,7 @@ private:
public: public:
NovelParser(); NovelParser();
QList<std::shared_ptr<const example_novel::FragmentGraphHelper>> fragmentsSorted() const;
virtual QString version() const; virtual QString version() const;
std::shared_ptr<const ast_gen::ElementAccess> parse(const QFileInfoList souurce_list) const; std::shared_ptr<const ast_gen::ElementAccess> parse(const QFileInfoList souurce_list) const;

View File

@ -58,6 +58,10 @@ void FragmentGraphCheck::setElement(std::shared_ptr<FragmentGraphHelper> inst)
elements_store[inst->nodePeer()->signature()] = inst; elements_store[inst->nodePeer()->signature()] = inst;
} }
QList<std::shared_ptr<const FragmentGraphHelper>> FragmentGraphCheck::fragmentsSequence() const {
return fragments_sort_list;
}
std::shared_ptr<FragmentGraphHelper> FragmentGraphCheck::getElement(const QString& signature) const std::shared_ptr<FragmentGraphHelper> FragmentGraphCheck::getElement(const QString& signature) const
{ {
return elements_store[signature]; return elements_store[signature];
@ -92,6 +96,8 @@ bool FragmentGraphCheck::nodeDismantle(std::shared_ptr<FragmentGraphHelper> inst
void FragmentGraphCheck::validCheck(std::shared_ptr<const ElementAccess> root) const void FragmentGraphCheck::validCheck(std::shared_ptr<const ElementAccess> root) const
{ {
const_cast<FragmentGraphCheck*>(this)->fragments_sort_list.clear();
std::function<QList<std::shared_ptr<const ElementAccess>>(std::shared_ptr<const ElementAccess>)> story_peak std::function<QList<std::shared_ptr<const ElementAccess>>(std::shared_ptr<const ElementAccess>)> story_peak
= [&](std::shared_ptr<const ElementAccess> root)->QList<std::shared_ptr<const ElementAccess>> { = [&](std::shared_ptr<const ElementAccess> root)->QList<std::shared_ptr<const ElementAccess>> {
QList<std::shared_ptr<const ElementAccess>> return_temp; QList<std::shared_ptr<const ElementAccess>> return_temp;
@ -200,6 +206,9 @@ void FragmentGraphCheck::validCheck(std::shared_ptr<const ElementAccess> root) c
throw new CheckException(error_msg); throw new CheckException(error_msg);
} }
} }
for (auto& inst : values)
const_cast<FragmentGraphCheck*>(this)->fragments_sort_list.append(inst);
} }
QString FragmentGraphCheck::name() const { QString FragmentGraphCheck::name() const {

View File

@ -19,6 +19,7 @@ namespace example_novel {
class LIBPARSE_EXPORT FragmentGraphCheck : public std::enable_shared_from_this<FragmentGraphCheck>, public lib_parse::CheckProvider { class LIBPARSE_EXPORT FragmentGraphCheck : public std::enable_shared_from_this<FragmentGraphCheck>, public lib_parse::CheckProvider {
private: private:
QHash<QString, std::shared_ptr<FragmentGraphHelper>> elements_store; QHash<QString, std::shared_ptr<FragmentGraphHelper>> elements_store;
QList<std::shared_ptr<const FragmentGraphHelper>> fragments_sort_list;
QList<std::shared_ptr<FragmentGraphHelper>> refers_cycle_check( QList<std::shared_ptr<FragmentGraphHelper>> refers_cycle_check(
std::shared_ptr<FragmentGraphHelper> item, QList<std::shared_ptr<FragmentGraphHelper>> prevs = QList<std::shared_ptr<FragmentGraphHelper>>()) const; std::shared_ptr<FragmentGraphHelper> item, QList<std::shared_ptr<FragmentGraphHelper>> prevs = QList<std::shared_ptr<FragmentGraphHelper>>()) const;
@ -30,6 +31,8 @@ namespace example_novel {
QList<std::shared_ptr<FragmentGraphHelper>> getHangoutNodes(); QList<std::shared_ptr<FragmentGraphHelper>> getHangoutNodes();
bool nodeDismantle(std::shared_ptr<FragmentGraphHelper> inst); bool nodeDismantle(std::shared_ptr<FragmentGraphHelper> inst);
QList<std::shared_ptr<const FragmentGraphHelper>> fragmentsSequence() const;
// CheckProvider interface // CheckProvider interface
public: public:
virtual void validCheck(std::shared_ptr<const ast_gen::ElementAccess> root) const override; virtual void validCheck(std::shared_ptr<const ast_gen::ElementAccess> root) const override;

View File

@ -5,32 +5,32 @@ using namespace ast_basic;
using namespace lib_token; using namespace lib_token;
using namespace lib_syntax; using namespace lib_syntax;
ExpressionElement::ExpressionElement(std::shared_ptr<const lib_syntax::ExpressionRule> bind) : _expr_rule(bind) {} ExpressionElement::ExpressionElement(std::shared_ptr<const ExpressionRule> bind) : _expr_rule(bind) {}
std::shared_ptr<const lib_syntax::ExpressionRule> ast_basic::ExpressionElement::definedRule() const { std::shared_ptr<const ExpressionRule> ExpressionElement::definedRule() const {
return _expr_rule; return _expr_rule;
} }
QString ast_basic::ExpressionElement::filePath() const { QString ExpressionElement::filePath() const {
if(tokens_bind.size()) if(!tokens_bind.size())
throw new SyntaxException(u8"InternalError[0x0002]Ò»¸ö¿ÕµÄ·Ç·¨ÎÞЧ½Úµã"); throw new SyntaxException(u8"InternalError[0x0002]Ò»¸ö¿ÕµÄ·Ç·¨ÎÞЧ½Úµã");
return tokens_bind.first()->file(); return tokens_bind.first()->file();
} }
void ast_basic::ExpressionElement::tokensReset(const QList<std::shared_ptr<const IToken>>& list) { void ExpressionElement::tokensReset(const QList<std::shared_ptr<const IToken>>& list) {
this->tokens_bind = list; this->tokens_bind = list;
} }
void ast_basic::ExpressionElement::addToken(std::shared_ptr<const IToken> token_inst) { void ExpressionElement::addToken(std::shared_ptr<const IToken> token_inst) {
this->tokens_bind.append(token_inst); this->tokens_bind.append(token_inst);
} }
QList<std::shared_ptr<const Expression>> ast_basic::ExpressionElement::children() const { QList<std::shared_ptr<const Expression>> ExpressionElement::children() const {
return this->children_store; return this->children_store;
} }
void ast_basic::ExpressionElement::addChild(std::shared_ptr<const Expression> inst) { void ExpressionElement::addChild(std::shared_ptr<const Expression> inst) {
this->children_store.append(inst); this->children_store.append(inst);
} }
@ -38,13 +38,13 @@ QList<std::shared_ptr<const IToken>> ExpressionElement::tokens() const {
return this->tokens_bind; 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::Expression> ast_basic::ExpressionContext::currentInst() const std::shared_ptr<Expression> ExpressionContext::currentInst() const
{ {
if(expression_stack.size()) if(expression_stack.size())
return expression_stack.last(); return expression_stack.last();
@ -52,30 +52,30 @@ std::shared_ptr<ast_basic::Expression> ast_basic::ExpressionContext::currentInst
return nullptr; return nullptr;
} }
void ast_basic::ExpressionContext::pushInst(std::shared_ptr<ast_basic::Expression> current_inst) void ExpressionContext::pushInst(std::shared_ptr<Expression> current_inst)
{ {
if(!expression_stack.size() || expression_stack.last() != current_inst) if(!expression_stack.size() || expression_stack.last() != current_inst)
expression_stack.append(current_inst); expression_stack.append(current_inst);
} }
std::shared_ptr<ast_basic::Expression> ast_basic::ExpressionContext::popInst() std::shared_ptr<Expression> ExpressionContext::popInst()
{ {
auto lastx = expression_stack.takeLast(); auto lastx = expression_stack.takeLast();
return lastx; return lastx;
} }
std::shared_ptr<const lib_syntax::BaseRule> ast_basic::ExpressionContext::currentExpressionRule() const { std::shared_ptr<const BaseRule> ExpressionContext::currentExpressionRule() const {
if(rule_stack.size()) if(rule_stack.size())
return rule_stack.last(); return rule_stack.last();
return nullptr; return nullptr;
} }
void ast_basic::ExpressionContext::pushExpressionRule(std::shared_ptr<const lib_syntax::BaseRule> inst) { void ExpressionContext::pushExpressionRule(std::shared_ptr<const BaseRule> inst) {
if(!rule_stack.size() || rule_stack.last() != inst) if(!rule_stack.size() || rule_stack.last() != inst)
rule_stack.append(inst); rule_stack.append(inst);
} }
std::shared_ptr<const lib_syntax::BaseRule> ast_basic::ExpressionContext::popExpressionRule() std::shared_ptr<const BaseRule> ExpressionContext::popExpressionRule()
{ {
return rule_stack.takeLast(); return rule_stack.takeLast();
} }

View File

@ -91,8 +91,7 @@ int StoryDefine::sort() const { return sort_index; }
void StoryDefine::cacheLoad() void StoryDefine::cacheLoad()
{ {
name_store = selfTokens()[3]->token()->content(); name_store = selfTokens()[2]->token()->content();
sort_index = selfTokens()[2]->token()->content().toInt();
} }
int StoryDefine::typeMark() const { return (int)NovelNode::StoryDefine; } int StoryDefine::typeMark() const { return (int)NovelNode::StoryDefine; }

View File

@ -81,7 +81,7 @@ auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)Novel
auto fragment_comp = std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr }); auto fragment_comp = std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr });
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>( auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>(
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) << OptMulR(fragment_comp) <<
LinesMerge(MR(rightb)) LinesMerge(MR(rightb))
)); ));
@ -137,7 +137,7 @@ void NovalSyntax::node_register(std::shared_ptr<const ast_gen::SyntaxElement> ro
if (!child->isAnonymous()) { if (!child->isAnonymous()) {
auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child); auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child);
if(check_result) if(check_result)
throw new lib_syntax::SyntaxException(QString(u8"Parse[0x0004]系统中包含同类型重名命名节点:%1<type%2>(%3,%4)") throw new lib_syntax::SyntaxException(QString(u8"SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type%2>(%3,%4)")
.arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path())); .arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path()));
} }