Compare commits

..

No commits in common. "8e019fb3d86f1bef0f803821f9ea728ca7fb058d" and "eeafd5fef83179e44f7fb2da5528d73aed200de4" have entirely different histories.

10 changed files with 161 additions and 175 deletions

View File

@ -5,42 +5,42 @@ using namespace ast_basic;
using namespace lib_token; using namespace lib_token;
using namespace lib_syntax; using namespace lib_syntax;
ExprElement::ExprElement(std::shared_ptr<const ExprRule> bind) : _expr_rule(bind) {} ExpressionElement::ExpressionElement(std::shared_ptr<const ExprRule> bind) : _expr_rule(bind) {}
std::shared_ptr<const ExprRule> ExprElement::definedRule() const { std::shared_ptr<const ExprRule> ExpressionElement::definedRule() const {
return _expr_rule; return _expr_rule;
} }
QString ExprElement::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 ExprElement::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 IExprInst>> ExprElement::children() const { QList<std::shared_ptr<const IExprInst>> ExpressionElement::children() const {
return this->children_store; return this->children_store;
} }
void ExprElement::addChild(std::shared_ptr<const IExprInst> inst) { void ExpressionElement::addChild(std::shared_ptr<const IExprInst> inst) {
this->children_store.append(inst); this->children_store.append(inst);
} }
QList<std::shared_ptr<const IToken>> ExprElement::tokens() const { QList<std::shared_ptr<const IToken>> ExpressionElement::tokens() const {
return this->tokens_bind; return this->tokens_bind;
} }
ExprsContext::ExprsContext() {} ExpressionContext::ExpressionContext() {}
void ExprsContext::setCurrentFile(const QString& path) { this->current_file_path = path; } void ExpressionContext::setCurrentFile(const QString& path) { this->current_file_path = path; }
QString ExprsContext::currentFile() const { return this->current_file_path; } QString ExpressionContext::currentFile() const { return this->current_file_path; }
std::shared_ptr<IExprInst> ExprsContext::currentExprInst() const std::shared_ptr<IExprInst> ExpressionContext::currentExprInst() const
{ {
if (expression_stack.size()) if (expression_stack.size())
return expression_stack.last(); return expression_stack.last();
@ -48,39 +48,39 @@ std::shared_ptr<IExprInst> ExprsContext::currentExprInst() const
return nullptr; return nullptr;
} }
void ExprsContext::pushExprInst(std::shared_ptr<IExprInst> current_inst) void ExpressionContext::pushExprInst(std::shared_ptr<IExprInst> 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<IExprInst> ExprsContext::popExprInst() std::shared_ptr<IExprInst> ExpressionContext::popExprInst()
{ {
auto lastx = expression_stack.takeLast(); auto lastx = expression_stack.takeLast();
return lastx; return lastx;
} }
std::shared_ptr<const IBasicRule> ExprsContext::currentExprRule() const { std::shared_ptr<const IBasicRule> ExpressionContext::currentExprRule() const {
if (rule_stack.size()) if (rule_stack.size())
return rule_stack.last(); return rule_stack.last();
return nullptr; return nullptr;
} }
void ExprsContext::pushExprRule(std::shared_ptr<const IBasicRule> inst) { void ExpressionContext::pushExprRule(std::shared_ptr<const IBasicRule> 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 IBasicRule> ExprsContext::popExprRule() std::shared_ptr<const IBasicRule> ExpressionContext::popExprRule()
{ {
return rule_stack.takeLast(); return rule_stack.takeLast();
} }
void ExprsContext::appendParseErrors(const QString& file_path, int start, const QString& e) { void ExpressionContext::appendParseErrors(const QString& file_path, int start, const QString& e) {
this->errors_storage.append(std::make_tuple(file_path, start, e)); this->errors_storage.append(std::make_tuple(file_path, start, e));
} }
QStringList ExprsContext::errors() const { QStringList ExpressionContext::errors() const {
QStringList values; QStringList values;
for (auto& tp : this->errors_storage) for (auto& tp : this->errors_storage)
values.append(QString(u8"文件:%1\n\t%2").arg(std::get<0>(tp)).arg(std::get<2>(tp))); values.append(QString(u8"文件:%1\n\t%2").arg(std::get<0>(tp)).arg(std::get<2>(tp)));
@ -88,7 +88,7 @@ QStringList ExprsContext::errors() const {
return values; return values;
} }
void ExprsContext::clearErrors(const QString &file_path, int start) { void ExpressionContext::clearErrors(const QString &file_path, int start) {
for (int idx = 0; idx < this->errors_storage.size(); ++idx) { for (int idx = 0; idx < this->errors_storage.size(); ++idx) {
auto &tp = errors_storage[idx]; auto &tp = errors_storage[idx];
if(std::get<0>(tp) == file_path && std::get<1>(tp) >= start) if(std::get<0>(tp) == file_path && std::get<1>(tp) >= start)
@ -96,15 +96,15 @@ void ExprsContext::clearErrors(const QString &file_path, int start) {
} }
} }
QList<std::shared_ptr<const IBasicRule>> ExprsContext::currentExprRuleStack() const { QList<std::shared_ptr<const IBasicRule>> ExpressionContext::currentExprRuleStack() const {
return rule_stack; return rule_stack;
} }
void ExprsContext::appendDocInst(std::shared_ptr<ast_basic::IExprInst> inst) { void ExpressionContext::appendDocInst(std::shared_ptr<ast_basic::IExprInst> inst) {
this->document_store.append(inst); this->document_store.append(inst);
} }
QList<std::shared_ptr<const ast_basic::IExprInst>> ExprsContext::getDocInsts() const { QList<std::shared_ptr<const ast_basic::IExprInst>> ExpressionContext::getDocInsts() const {
return this->document_store; return this->document_store;
} }

View File

@ -57,14 +57,14 @@ namespace ast_basic {
/** /**
* @brief * @brief
*/ */
class LIBSYNTAX_EXPORT ExprElement : public ast_basic::IExprInst, public std::enable_shared_from_this<ExprElement> { class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::IExprInst, public std::enable_shared_from_this<ExpressionElement> {
private: private:
std::shared_ptr<const lib_syntax::ExprRule> _expr_rule; std::shared_ptr<const lib_syntax::ExprRule> _expr_rule;
QList<std::shared_ptr<const IExprInst>> children_store; QList<std::shared_ptr<const IExprInst>> children_store;
QList<std::shared_ptr<const lib_token::IToken>> tokens_bind; QList<std::shared_ptr<const lib_token::IToken>> tokens_bind;
public: public:
ExprElement(std::shared_ptr<const lib_syntax::ExprRule> bind); ExpressionElement(std::shared_ptr<const lib_syntax::ExprRule> bind);
// 通过 Expression 继承 // 通过 Expression 继承
std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override; std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override;
@ -77,7 +77,7 @@ namespace ast_basic {
void addChild(std::shared_ptr<const IExprInst> inst) override; void addChild(std::shared_ptr<const IExprInst> inst) override;
}; };
class LIBSYNTAX_EXPORT ExprsContext : public lib_syntax::IContext, public std::enable_shared_from_this<ExprsContext> { class LIBSYNTAX_EXPORT ExpressionContext : public lib_syntax::IContext, public std::enable_shared_from_this<ExpressionContext> {
private: private:
QList<std::shared_ptr<const lib_syntax::IBasicRule>> rule_stack; QList<std::shared_ptr<const lib_syntax::IBasicRule>> rule_stack;
QList<std::shared_ptr<IExprInst>> expression_stack; QList<std::shared_ptr<IExprInst>> expression_stack;
@ -86,7 +86,7 @@ namespace ast_basic {
QList<std::tuple<QString, int, QString>> errors_storage; QList<std::tuple<QString, int, QString>> errors_storage;
public: public:
ExprsContext(); ExpressionContext();
// 通过 IContext 继承 // 通过 IContext 继承
virtual void setCurrentFile(const QString& path); virtual void setCurrentFile(const QString& path);

View File

@ -44,6 +44,10 @@ std::shared_ptr<const ast_basic::IExprInst> ast_gen::GlobalElement::bindExpressi
return bind_exprs; return bind_exprs;
} }
void ast_gen::GlobalElement::cacheLoad()
{
}
void GlobalElement::addChild(std::shared_ptr<ast_gen::SyntaxElement> citem) { void GlobalElement::addChild(std::shared_ptr<ast_gen::SyntaxElement> citem) {
auto convx = std::dynamic_pointer_cast<ast_basic::IExprInst>(citem); auto convx = std::dynamic_pointer_cast<ast_basic::IExprInst>(citem);
bind_exprs->addChild(convx); bind_exprs->addChild(convx);

View File

@ -65,6 +65,7 @@ namespace ast_gen
* @return * @return
*/ */
virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const = 0; virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const = 0;
virtual void cacheLoad() = 0;
}; };
/** /**
@ -101,12 +102,12 @@ namespace ast_gen
/** /**
* @brief * @brief
*/ */
class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement, public ast_basic::ExprsContext { class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement, public ast_basic::ExpressionContext {
private: private:
QString names_store; QString names_store;
QHash<QString, std::shared_ptr<const SyntaxElement>> node_cache; QHash<QString, std::shared_ptr<const SyntaxElement>> node_cache;
std::shared_ptr<ast_basic::IExprInst> bind_exprs = std::make_shared<ast_basic::ExprElement>(nullptr); std::shared_ptr<ast_basic::IExprInst> bind_exprs = std::make_shared<ast_basic::ExpressionElement>(nullptr);
public: public:
static GlobalElement* UniquePtr; static GlobalElement* UniquePtr;
@ -135,5 +136,6 @@ namespace ast_gen
// 通过 SyntaxElement 继承 // 通过 SyntaxElement 继承
virtual std::shared_ptr<const ast_basic::IExprInst> bindExpression() const override; virtual std::shared_ptr<const ast_basic::IExprInst> bindExpression() const override;
virtual void cacheLoad() override;
}; };
} }

View File

@ -9,11 +9,7 @@ TextSection::TextSection(std::shared_ptr<const ExprRule> rule_bind)
QString TextSection::content() const QString TextSection::content() const
{ {
QString text; return context_store;
for (auto& t : selfTokens()) {
text += t->token()->content() + " ";
}
return text;
} }
int TextSection::typeMark() const { return (int)NovelNode::TextSection; } int TextSection::typeMark() const { return (int)NovelNode::TextSection; }
@ -25,21 +21,27 @@ bool TextSection::isAnonymous() const
QString TextSection::signature() const { return u8"::section"; } QString TextSection::signature() const { return u8"::section"; }
void TextSection::cacheLoad()
{
QString text;
for (auto& t : selfTokens()) {
text += t->token()->content() + " ";
}
context_store = text;
}
FragmentRefers::FragmentRefers(std::shared_ptr<const ExprRule> rule_bind) FragmentRefers::FragmentRefers(std::shared_ptr<const ExprRule> rule_bind)
: AbstractImpl(rule_bind) {} : AbstractImpl(rule_bind) {}
QString FragmentRefers::storyRefer() const { return story_refs; } QString FragmentRefers::storyRefer() const { return story_refs; }
void example_novel::FragmentRefers::setStoryRefer(const QString& refer) {
this->story_refs = refer;
}
QString FragmentRefers::fragmentRefer() const { return fragment_ref; } QString FragmentRefers::fragmentRefer() const { return fragment_ref; }
void example_novel::FragmentRefers::setFragmentRefer(const QString& refer) { void FragmentRefers::cacheLoad()
this->fragment_ref = refer; {
this->story_refs = selfTokens()[5]->token()->content();
this->fragment_ref = selfTokens()[3]->token()->content();
} }
QString FragmentRefers::referSignature() const { QString FragmentRefers::referSignature() const {
return storyRefer() + u8"&" + fragmentRefer(); return storyRefer() + u8"&" + fragmentRefer();
} }
@ -62,11 +64,10 @@ FragmentDefine::FragmentDefine(std::shared_ptr<const ExprRule> rule_bind)
QString FragmentDefine::name() const { return name_store; } QString FragmentDefine::name() const { return name_store; }
void example_novel::FragmentDefine::setName(const QString& nm) void FragmentDefine::cacheLoad()
{ {
this->name_store = nm; name_store = selfTokens()[2]->token()->content();
} }
int FragmentDefine::typeMark() const { return (int)NovelNode::FragmentDefine; } int FragmentDefine::typeMark() const { return (int)NovelNode::FragmentDefine; }
bool FragmentDefine::isAnonymous() const bool FragmentDefine::isAnonymous() const
@ -82,17 +83,17 @@ StoryDefine::StoryDefine(std::shared_ptr<const ExprRule> rule_bind)
QString StoryDefine::name() const { return name_store; } QString StoryDefine::name() const { return name_store; }
void example_novel::StoryDefine::setName(const QString& nm) void example_novel::StoryDefine::setSort(int value){
{
this->name_store = nm;
}
void example_novel::StoryDefine::setSort(int value) {
sort_index = value; sort_index = value;
} }
int StoryDefine::sort() const { return sort_index; } int StoryDefine::sort() const { return sort_index; }
void StoryDefine::cacheLoad()
{
name_store = selfTokens()[2]->token()->content();
}
int StoryDefine::typeMark() const { return (int)NovelNode::StoryDefine; } int StoryDefine::typeMark() const { return (int)NovelNode::StoryDefine; }
bool StoryDefine::isAnonymous() const bool StoryDefine::isAnonymous() const
@ -115,13 +116,15 @@ bool Document::isAnonymous() const
QString Document::signature() const { return QString(u8"::document<%1>").arg(path()); } QString Document::signature() const { return QString(u8"::document<%1>").arg(path()); }
AbstractImpl::AbstractImpl(std::shared_ptr<const ExprRule> rule_bind) void Document::cacheLoad()
: ExprElement(rule_bind) { {
parent_store.reset();
} }
AbstractImpl::AbstractImpl(std::shared_ptr<const ExprRule> rule_bind)
: ExpressionElement(rule_bind) { parent_store.reset(); }
QList<std::shared_ptr<const ast_gen::TokenAccess> > AbstractImpl::selfTokens() const { QList<std::shared_ptr<const ast_gen::TokenAccess> > AbstractImpl::selfTokens() const {
auto tokensx = ast_basic::ExprElement::tokens(); auto tokensx = ast_basic::ExpressionElement::tokens();
QList<std::shared_ptr<const ast_gen::TokenAccess>> values; QList<std::shared_ptr<const ast_gen::TokenAccess>> values;
for (auto xit : tokensx) { for (auto xit : tokensx) {
values.append(std::make_shared<ast_gen::TokenAccess>(std::dynamic_pointer_cast<const ast_gen::SyntaxElement>(shared_from_this()), xit)); values.append(std::make_shared<ast_gen::TokenAccess>(std::dynamic_pointer_cast<const ast_gen::SyntaxElement>(shared_from_this()), xit));
@ -148,7 +151,7 @@ std::shared_ptr<const ast_basic::IExprInst> AbstractImpl::bindExpression() const
QString AbstractImpl::path() const QString AbstractImpl::path() const
{ {
return ast_basic::ExprElement::filePath(); return ast_basic::ExpressionElement::filePath();
} }
VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind) VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind)
@ -156,9 +159,9 @@ VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind)
QString VolumeDefine::name() const { return name_store; } QString VolumeDefine::name() const { return name_store; }
void example_novel::VolumeDefine::setName(const QString& nm) void VolumeDefine::cacheLoad()
{ {
this->name_store = nm; name_store = selfTokens()[2]->token()->content();
} }
int VolumeDefine::typeMark() const { return (int)NovelNode::VolumeDefine; } int VolumeDefine::typeMark() const { return (int)NovelNode::VolumeDefine; }
@ -175,9 +178,9 @@ ArticleDefine::ArticleDefine(std::shared_ptr<const ExprRule> rule_bind)
QString ArticleDefine::name() const { return name_store; } QString ArticleDefine::name() const { return name_store; }
void example_novel::ArticleDefine::setName(const QString& nm) void ArticleDefine::cacheLoad()
{ {
this->name_store = nm; name_store = selfTokens()[2]->token()->content();
} }
int ArticleDefine::typeMark() const { return (int)NovelNode::ArticleDefine; } int ArticleDefine::typeMark() const { return (int)NovelNode::ArticleDefine; }
@ -190,7 +193,7 @@ bool ArticleDefine::isAnonymous() const
QString ArticleDefine::signature() const { return parent()->signature() + u8"&" + name(); } QString ArticleDefine::signature() const { return parent()->signature() + u8"&" + name(); }
RankDeclare::RankDeclare(std::shared_ptr<const ExprRule> rule) RankDeclare::RankDeclare(std::shared_ptr<const ExprRule> rule)
: AbstractImpl(rule) : AbstractImpl(rule)
{ {
} }
@ -199,11 +202,6 @@ int example_novel::RankDeclare::rankNumber() const
return page_rank; return page_rank;
} }
void example_novel::RankDeclare::setRank(int nums)
{
this->page_rank = nums;
}
int RankDeclare::typeMark() const int RankDeclare::typeMark() const
{ {
return (int)NovelNode::RankDeclaration; return (int)NovelNode::RankDeclaration;
@ -218,3 +216,8 @@ QString RankDeclare::signature() const
{ {
return u8"::rank"; return u8"::rank";
} }
void RankDeclare::cacheLoad()
{
page_rank = selfTokens()[2]->token()->content().toInt();
}

View File

@ -16,7 +16,7 @@ namespace example_novel
RankDeclaration = 8, RankDeclaration = 8,
}; };
class LIBSYNTAX_EXPORT AbstractImpl : public ast_basic::ExprElement, public ast_gen::SyntaxElement { class LIBSYNTAX_EXPORT AbstractImpl : public ast_basic::ExpressionElement, public ast_gen::SyntaxElement {
private: private:
std::weak_ptr<const SyntaxElement> parent_store; std::weak_ptr<const SyntaxElement> parent_store;
@ -43,6 +43,10 @@ namespace example_novel
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
private:
QString context_store;
}; };
class LIBSYNTAX_EXPORT FragmentRefers : public AbstractImpl { class LIBSYNTAX_EXPORT FragmentRefers : public AbstractImpl {
@ -50,11 +54,7 @@ namespace example_novel
FragmentRefers(std::shared_ptr<const lib_syntax::ExprRule> rule_bind); FragmentRefers(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString storyRefer() const; QString storyRefer() const;
void setStoryRefer(const QString &refer);
QString fragmentRefer() const; QString fragmentRefer() const;
void setFragmentRefer(const QString &refer);
QString referSignature() const; QString referSignature() const;
// SyntaxElement interface // SyntaxElement interface
@ -62,6 +62,7 @@ namespace example_novel
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
private: private:
QString story_refs, fragment_ref; QString story_refs, fragment_ref;
@ -72,13 +73,13 @@ namespace example_novel
FragmentDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind); FragmentDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const; QString name() const;
void setName(const QString &nm);
// SyntaxElement interface // SyntaxElement interface
public: public:
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
private: private:
QString name_store; QString name_store;
@ -89,13 +90,13 @@ namespace example_novel
ArticleDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind); ArticleDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const; QString name() const;
void setName(const QString &nm);
// SyntaxElement interface // SyntaxElement interface
public: public:
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
private: private:
QString name_store; QString name_store;
@ -106,13 +107,13 @@ namespace example_novel
VolumeDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind); VolumeDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const; QString name() const;
void setName(const QString &nm);
// SyntaxElement interface // SyntaxElement interface
public: public:
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
private: private:
QString name_store; QString name_store;
@ -123,8 +124,6 @@ namespace example_novel
StoryDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind); StoryDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const; QString name() const;
void setName(const QString &nm);
void setSort(int value); void setSort(int value);
int sort() const; int sort() const;
@ -133,6 +132,7 @@ namespace example_novel
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
private: private:
QString name_store; QString name_store;
@ -148,6 +148,7 @@ namespace example_novel
virtual int typeMark() const override; virtual int typeMark() const override;
virtual bool isAnonymous() const override; virtual bool isAnonymous() const override;
virtual QString signature() const override; virtual QString signature() const override;
virtual void cacheLoad() override;
}; };
class LIBSYNTAX_EXPORT RankDeclare : public AbstractImpl { class LIBSYNTAX_EXPORT RankDeclare : public AbstractImpl {
@ -157,12 +158,12 @@ namespace example_novel
RankDeclare(std::shared_ptr<const lib_syntax::ExprRule> rule); RankDeclare(std::shared_ptr<const lib_syntax::ExprRule> rule);
int rankNumber() const; int rankNumber() const;
void setRank(int nums);
// ͨ¹ý AbstractImpl ¼Ì³Ð // ͨ¹ý AbstractImpl ¼Ì³Ð
int typeMark() const override; int typeMark() const override;
bool isAnonymous() const override; bool isAnonymous() const override;
QString signature() const override; QString signature() const override;
void cacheLoad() override;
}; };
} }

View File

@ -7,6 +7,40 @@ using namespace std;
using namespace lib_token; using namespace lib_token;
using namespace ast_basic; using namespace ast_basic;
TokenMatch::TokenMatch(shared_ptr<const ITokenDefine> define) : define_peer(define) {}
QList<std::shared_ptr<const IBasicRule>> TokenMatch::children() const { return QList<std::shared_ptr<const IBasicRule>>(); }
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) 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);
}
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]语法匹配错误,无法识别\"%1\"<row:%2,col:%3>(应该为:%4)\n\t目标语法:%5。\n")
.arg(head->content()).arg(head->row()).arg(head->column()).arg(this->define_peer->reviseWords())
.arg(rt_inst->currentExprRule()->token_present()));
return std::make_tuple(IBasicRule::MatchResult::Part, head);
}
if (std::get<1>(match_result)) {
return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
}
else {
return std::make_tuple(IBasicRule::MatchResult::Success, head->nextWord());
}
}
QString TokenMatch::token_present() const {
return QString(u8"%1").arg(this->define_peer->reviseWords());
}
Rept::Rept(std::shared_ptr<const IBasicRule> rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {} Rept::Rept(std::shared_ptr<const IBasicRule> rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {}
QList<std::shared_ptr<const IBasicRule>> Rept::children() const { return QList<std::shared_ptr<const IBasicRule>>() << rule_peer; } QList<std::shared_ptr<const IBasicRule>> Rept::children() const { return QList<std::shared_ptr<const IBasicRule>>() << rule_peer; }
@ -51,10 +85,8 @@ QString Rept::token_present() const
{ {
if(min_match == 0 && max_match == INT_MAX) if(min_match == 0 && max_match == INT_MAX)
return u8"(" + this->rule_peer->token_present() + QString(u8")*"); return u8"(" + this->rule_peer->token_present() + QString(u8")*");
else if (min_match == 1 && max_match == INT_MAX) else if(min_match == 1 && max_match == INT_MAX)
return u8"(" + this->rule_peer->token_present() + QString(u8")+"); return u8"(" + this->rule_peer->token_present() + QString(u8")+");
else if (min_match == 0 && max_match == 1)
return u8"(" + this->rule_peer->token_present() + QString(u8")?");
return u8"(" + this->rule_peer->token_present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match); return u8"(" + this->rule_peer->token_present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match);
} }

View File

@ -7,7 +7,7 @@
#include <functional> #include <functional>
namespace ast_basic { namespace ast_basic {
class IExprInst; class IExprInst;
class ExprElement; class ExpressionElement;
} }
namespace lib_syntax { namespace lib_syntax {
@ -35,12 +35,12 @@ namespace lib_syntax {
public: public:
virtual ~IContext() = default; virtual ~IContext() = default;
virtual void setCurrentFile(const QString& path) = 0; virtual void setCurrentFile(const QString &path) = 0;
virtual QString currentFile() const = 0; virtual QString currentFile() const = 0;
virtual void appendParseErrors(const QString& file_path, int start, const QString& error_msg) = 0; virtual void appendParseErrors(const QString & file_path, int start, const QString &error_msg) = 0;
virtual QStringList errors() const = 0; virtual QStringList errors() const = 0;
virtual void clearErrors(const QString& file_path, int start) = 0; virtual void clearErrors(const QString &file_path, int start) = 0;
virtual void appendDocInst(std::shared_ptr<ast_basic::IExprInst> inst) = 0; virtual void appendDocInst(std::shared_ptr<ast_basic::IExprInst> inst) = 0;
virtual QList<std::shared_ptr<const ast_basic::IExprInst>> getDocInsts() const = 0; virtual QList<std::shared_ptr<const ast_basic::IExprInst>> getDocInsts() const = 0;
@ -88,7 +88,7 @@ namespace lib_syntax {
* @return <,> * @return <,>
*/ */
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0; parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0;
/** /**
* *
@ -98,55 +98,23 @@ namespace lib_syntax {
virtual QString token_present() const = 0; virtual QString token_present() const = 0;
}; };
template<typename elem>
using token_proc = void(*)(std::shared_ptr<elem> expr_inst, std::shared_ptr<const lib_token::IToken> token);
/** /**
* @brief token匹配 * @brief token匹配
*/ */
template<typename elem, token_proc<elem> xproc = nullptr> class LIBSYNTAX_EXPORT TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch> {
class TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch<elem, xproc>> {
private: private:
std::shared_ptr<const lib_token::ITokenDefine> define_peer; std::shared_ptr<const lib_token::ITokenDefine> define_peer;
public: public:
TokenMatch(std::shared_ptr<const lib_token::ITokenDefine> define) : define_peer(define) {} TokenMatch(std::shared_ptr<const lib_token::ITokenDefine> define);
// IBasicRule interface // IBasicRule interface
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override { return QList<std::shared_ptr<const IBasicRule>>(); } virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override { virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
if (!head) { parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
rt_inst->appendParseErrors(rt_inst->currentFile(), -1, QString(u8"Syntax[0x0000]token流%1提前终止").arg(rt_inst->currentFile())); virtual QString token_present() const override;
return std::make_tuple(IBasicRule::MatchResult::Fail, head);
}
auto match_result = define_peer->analysis(head);
if (std::get<0>(match_result)) {
auto current_inst = rt_inst->currentExprInst();
current_inst->addToken(std::get<0>(match_result));
if (xproc) {
xproc(std::dynamic_pointer_cast<elem>(current_inst), std::get<0>(match_result));
}
if (std::get<1>(match_result)) {
return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
}
else {
return std::make_tuple(IBasicRule::MatchResult::Success, head->nextWord());
}
}
else {
rt_inst->appendParseErrors(rt_inst->currentFile(), head->position(),
QString(u8"Syntax[0x00001]语法匹配错误,无法识别\"%1\"<row:%2,col:%3>(应该为:%4)\n\t目标语法:%5。\n")
.arg(head->content()).arg(head->row()).arg(head->column()).arg(this->define_peer->reviseWords())
.arg(rt_inst->currentExprRule()->token_present()));
return std::make_tuple(IBasicRule::MatchResult::Part, head);
}
}
virtual QString token_present() const override {
return QString(u8"%1").arg(this->define_peer->reviseWords());
}
}; };
/** /**
@ -165,7 +133,7 @@ namespace lib_syntax {
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override; parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
virtual QString token_present() const override; virtual QString token_present() const override;
}; };
@ -183,7 +151,7 @@ namespace lib_syntax {
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override; parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
virtual QString token_present() const override; virtual QString token_present() const override;
}; };
@ -202,7 +170,7 @@ namespace lib_syntax {
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override; parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
virtual QString token_present() const override; virtual QString token_present() const override;
}; };
@ -226,7 +194,7 @@ namespace lib_syntax {
public: public:
virtual QList<std::shared_ptr<const lib_syntax::IBasicRule>> children() const override; virtual QList<std::shared_ptr<const lib_syntax::IBasicRule>> children() const override;
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override; parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
virtual QString token_present() const override; virtual QString token_present() const override;
protected: protected:
@ -244,7 +212,7 @@ namespace lib_syntax {
class ElementRule : public ExprRule { class ElementRule : public ExprRule {
public: public:
ElementRule(const QString& rule_name, int expr_mark) ElementRule(const QString& rule_name, int expr_mark)
:ExprRule(rule_name, expr_mark) {} :ExprRule(rule_name, expr_mark){}
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const { virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const {
return std::dynamic_pointer_cast<ast_basic::IExprInst>( return std::dynamic_pointer_cast<ast_basic::IExprInst>(

View File

@ -27,8 +27,7 @@ auto name_text = std::make_shared<NameSection>(); // ^
// rule-parts =============================================================================== // rule-parts ===============================================================================
// MatchRule // MatchRule
#define MR(E, x) std::make_shared<const TokenMatch<E>>(x) #define MR(x) std::make_shared<const TokenMatch>(x)
#define MER(E, xproc, t) std::make_shared<const TokenMatch<E, xproc>>(t)
// Buffer // Buffer
#define Rules QList<std::shared_ptr<const IBasicRule>> #define Rules QList<std::shared_ptr<const IBasicRule>>
// Option // Option
@ -42,72 +41,48 @@ auto name_text = std::make_shared<NameSection>(); // ^
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule( auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule(
MultiR(std::make_shared<const Any>(Rules{ MultiR(std::make_shared<const Any>(Rules{ MR(numbers), MR(vtext), MR(refers), MR(split_mark)}))
MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark)
}))
); );
void frags_nmset(std::shared_ptr<FragmentDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setName(token->content());
}
auto fragment_decl = ElementRule<FragmentDefine>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule( auto fragment_decl = ElementRule<FragmentDefine>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule(
std::make_shared<const Seqs>(Rules{ std::make_shared<const Seqs>(Rules{
MR(FragmentDefine, leftb), MR(FragmentDefine, frag_key), MER(FragmentDefine, frags_nmset, name_text) } << MR(leftb), MR(frag_key), MR(name_text) } <<
OptR(decl_expr) << OptR(decl_expr) <<
MR(FragmentDefine, rightb) MR(rightb)
)); ));
void frags_snm_set(std::shared_ptr<FragmentRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setStoryRefer(token->content());
}
void frags_fnm_set(std::shared_ptr<FragmentRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setFragmentRefer(token->content());
}
auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule( auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule(
std::make_shared<const Seqs>(Rules{ std::make_shared<const Seqs>(Rules{
MR(FragmentRefers, leftb), MR(FragmentRefers, refers), MR(FragmentRefers, frag_key), MR(leftb), MR(refers), MR(frag_key), MR(name_text), MR(split_mark), MR(name_text) } <<
MER(FragmentRefers, frags_fnm_set, name_text), MR(FragmentRefers, split_mark), MER(FragmentRefers, frags_snm_set, name_text) } <<
OptR(decl_expr) << OptR(decl_expr) <<
MR(FragmentRefers, rightb) MR(rightb)
)); ));
void story_nmset(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setName(token->content());
}
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule( auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(
std::make_shared<const Seqs>(Rules{ std::make_shared<const Seqs>(Rules{
MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, name_text) } << MR(leftb), MR(story_key), MR(name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr })) << OptMulR(std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr })) <<
MR(StoryDefine, rightb) MR(rightb)
)); ));
// =================================================================== // ===================================================================
void article_nset(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setName(token->content());
}
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule( auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule(
std::make_shared<const Seqs>(Rules{ std::make_shared<const Seqs>(Rules{
MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, name_text) } << MR(leftb), MR(article_key), MR(name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) << OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) <<
MR(ArticleDefine, rightb) MR(rightb)
)); ));
void volume_nset(std::shared_ptr<VolumeDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setName(token->content());
}
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule( auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule(
std::make_shared<const Seqs>(Rules{ std::make_shared<const Seqs>(Rules{
MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, name_text) } << MR(leftb), MR(volume_key), MR(name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) << OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
MR(VolumeDefine, rightb) MR(rightb)
)); ));
void rank_set(std::shared_ptr<RankDeclare> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->setRank(token->content().toInt());
}
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule( auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule(
std::make_shared<const Seqs>(Rules{ std::make_shared<const Seqs>(Rules{
MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, numbers) } MR(declare), MR(rank_key), MR(numbers) }
)); ));
auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).reloadRule( auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).reloadRule(
std::make_shared<const Seqs>( std::make_shared<const Seqs>(
@ -120,12 +95,13 @@ auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC
std::shared_ptr<const ExprRule> NovalSyntax::getParseTree() { return document_define; } std::shared_ptr<const ExprRule> NovalSyntax::getParseTree() { return document_define; }
std::shared_ptr<const ast_gen::SyntaxElement> NovalSyntax::tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children) std::shared_ptr<const ast_gen::SyntaxElement> NovalSyntax::tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
{ {
build_objecttree(root, children); cache_load(root, children);
node_register(root, children); node_register(root, children);
return root; return root;
} }
void NovalSyntax::build_objecttree(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children) void NovalSyntax::cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
{ {
root->cacheLoad();
for (auto& cinst : children) { for (auto& cinst : children) {
cinst->setParent(root); cinst->setParent(root);
@ -135,7 +111,7 @@ void NovalSyntax::build_objecttree(std::shared_ptr<ast_gen::SyntaxElement> root,
child_items.append(std::const_pointer_cast<ast_gen::SyntaxElement>(const_it)); child_items.append(std::const_pointer_cast<ast_gen::SyntaxElement>(const_it));
} }
build_objecttree(cinst, child_items); cache_load(cinst, child_items);
} }
} }
void NovalSyntax::node_register(std::shared_ptr<const ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children) void NovalSyntax::node_register(std::shared_ptr<const ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)

View File

@ -33,7 +33,7 @@ namespace example_novel {
static std::shared_ptr<const ast_gen::SyntaxElement> tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs); static std::shared_ptr<const ast_gen::SyntaxElement> tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs);
private: private:
static void build_objecttree(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs); static void cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs);
static void node_register(std::shared_ptr<const ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs); static void node_register(std::shared_ptr<const ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs);
}; };