Compare commits

..

4 Commits

Author SHA1 Message Date
codeboss 8e019fb3d8 语法解析固定化完成 2024-07-28 16:37:31 +08:00
codeboss 74e065581c update - syntax:msg 2024-07-28 14:46:47 +08:00
codeboss 8b7ebd11ac update 2024-07-28 14:41:54 +08:00
codeboss c2c6f0a083 改进context实现名称 2024-07-28 14:39:18 +08:00
10 changed files with 175 additions and 161 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;
ExpressionElement::ExpressionElement(std::shared_ptr<const ExprRule> bind) : _expr_rule(bind) {} ExprElement::ExprElement(std::shared_ptr<const ExprRule> bind) : _expr_rule(bind) {}
std::shared_ptr<const ExprRule> ExpressionElement::definedRule() const { std::shared_ptr<const ExprRule> ExprElement::definedRule() const {
return _expr_rule; return _expr_rule;
} }
QString ExpressionElement::filePath() const { QString ExprElement::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 ExpressionElement::addToken(std::shared_ptr<const IToken> token_inst) { void ExprElement::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>> ExpressionElement::children() const { QList<std::shared_ptr<const IExprInst>> ExprElement::children() const {
return this->children_store; return this->children_store;
} }
void ExpressionElement::addChild(std::shared_ptr<const IExprInst> inst) { void ExprElement::addChild(std::shared_ptr<const IExprInst> inst) {
this->children_store.append(inst); this->children_store.append(inst);
} }
QList<std::shared_ptr<const IToken>> ExpressionElement::tokens() const { QList<std::shared_ptr<const IToken>> ExprElement::tokens() const {
return this->tokens_bind; return this->tokens_bind;
} }
ExpressionContext::ExpressionContext() {} ExprsContext::ExprsContext() {}
void ExpressionContext::setCurrentFile(const QString& path) { this->current_file_path = path; } void ExprsContext::setCurrentFile(const QString& path) { this->current_file_path = path; }
QString ExpressionContext::currentFile() const { return this->current_file_path; } QString ExprsContext::currentFile() const { return this->current_file_path; }
std::shared_ptr<IExprInst> ExpressionContext::currentExprInst() const std::shared_ptr<IExprInst> ExprsContext::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> ExpressionContext::currentExprInst() const
return nullptr; return nullptr;
} }
void ExpressionContext::pushExprInst(std::shared_ptr<IExprInst> current_inst) void ExprsContext::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> ExpressionContext::popExprInst() std::shared_ptr<IExprInst> ExprsContext::popExprInst()
{ {
auto lastx = expression_stack.takeLast(); auto lastx = expression_stack.takeLast();
return lastx; return lastx;
} }
std::shared_ptr<const IBasicRule> ExpressionContext::currentExprRule() const { std::shared_ptr<const IBasicRule> ExprsContext::currentExprRule() const {
if (rule_stack.size()) if (rule_stack.size())
return rule_stack.last(); return rule_stack.last();
return nullptr; return nullptr;
} }
void ExpressionContext::pushExprRule(std::shared_ptr<const IBasicRule> inst) { void ExprsContext::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> ExpressionContext::popExprRule() std::shared_ptr<const IBasicRule> ExprsContext::popExprRule()
{ {
return rule_stack.takeLast(); return rule_stack.takeLast();
} }
void ExpressionContext::appendParseErrors(const QString& file_path, int start, const QString& e) { void ExprsContext::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 ExpressionContext::errors() const { QStringList ExprsContext::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 ExpressionContext::errors() const {
return values; return values;
} }
void ExpressionContext::clearErrors(const QString &file_path, int start) { void ExprsContext::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 ExpressionContext::clearErrors(const QString &file_path, int start) {
} }
} }
QList<std::shared_ptr<const IBasicRule>> ExpressionContext::currentExprRuleStack() const { QList<std::shared_ptr<const IBasicRule>> ExprsContext::currentExprRuleStack() const {
return rule_stack; return rule_stack;
} }
void ExpressionContext::appendDocInst(std::shared_ptr<ast_basic::IExprInst> inst) { void ExprsContext::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>> ExpressionContext::getDocInsts() const { QList<std::shared_ptr<const ast_basic::IExprInst>> ExprsContext::getDocInsts() const {
return this->document_store; return this->document_store;
} }

View File

@ -57,14 +57,14 @@ namespace ast_basic {
/** /**
* @brief * @brief
*/ */
class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::IExprInst, public std::enable_shared_from_this<ExpressionElement> { class LIBSYNTAX_EXPORT ExprElement : public ast_basic::IExprInst, public std::enable_shared_from_this<ExprElement> {
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:
ExpressionElement(std::shared_ptr<const lib_syntax::ExprRule> bind); ExprElement(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 ExpressionContext : public lib_syntax::IContext, public std::enable_shared_from_this<ExpressionContext> { class LIBSYNTAX_EXPORT ExprsContext : public lib_syntax::IContext, public std::enable_shared_from_this<ExprsContext> {
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:
ExpressionContext(); ExprsContext();
// 通过 IContext 继承 // 通过 IContext 继承
virtual void setCurrentFile(const QString& path); virtual void setCurrentFile(const QString& path);

View File

@ -44,10 +44,6 @@ 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,7 +65,6 @@ 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;
}; };
/** /**
@ -102,12 +101,12 @@ namespace ast_gen
/** /**
* @brief * @brief
*/ */
class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement, public ast_basic::ExpressionContext { class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement, public ast_basic::ExprsContext {
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::ExpressionElement>(nullptr); std::shared_ptr<ast_basic::IExprInst> bind_exprs = std::make_shared<ast_basic::ExprElement>(nullptr);
public: public:
static GlobalElement* UniquePtr; static GlobalElement* UniquePtr;
@ -136,6 +135,5 @@ 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,7 +9,11 @@ TextSection::TextSection(std::shared_ptr<const ExprRule> rule_bind)
QString TextSection::content() const QString TextSection::content() const
{ {
return context_store; QString text;
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; }
@ -21,27 +25,21 @@ 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 FragmentRefers::cacheLoad() void example_novel::FragmentRefers::setFragmentRefer(const QString& refer) {
{ 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();
} }
@ -64,10 +62,11 @@ FragmentDefine::FragmentDefine(std::shared_ptr<const ExprRule> rule_bind)
QString FragmentDefine::name() const { return name_store; } QString FragmentDefine::name() const { return name_store; }
void FragmentDefine::cacheLoad() void example_novel::FragmentDefine::setName(const QString& nm)
{ {
name_store = selfTokens()[2]->token()->content(); this->name_store = nm;
} }
int FragmentDefine::typeMark() const { return (int)NovelNode::FragmentDefine; } int FragmentDefine::typeMark() const { return (int)NovelNode::FragmentDefine; }
bool FragmentDefine::isAnonymous() const bool FragmentDefine::isAnonymous() const
@ -83,17 +82,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::setSort(int value){ void example_novel::StoryDefine::setName(const QString& nm)
{
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
@ -116,15 +115,13 @@ 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()); }
void Document::cacheLoad() AbstractImpl::AbstractImpl(std::shared_ptr<const ExprRule> rule_bind)
{ : 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::ExpressionElement::tokens(); auto tokensx = ast_basic::ExprElement::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));
@ -151,7 +148,7 @@ std::shared_ptr<const ast_basic::IExprInst> AbstractImpl::bindExpression() const
QString AbstractImpl::path() const QString AbstractImpl::path() const
{ {
return ast_basic::ExpressionElement::filePath(); return ast_basic::ExprElement::filePath();
} }
VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind) VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind)
@ -159,9 +156,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 VolumeDefine::cacheLoad() void example_novel::VolumeDefine::setName(const QString& nm)
{ {
name_store = selfTokens()[2]->token()->content(); this->name_store = nm;
} }
int VolumeDefine::typeMark() const { return (int)NovelNode::VolumeDefine; } int VolumeDefine::typeMark() const { return (int)NovelNode::VolumeDefine; }
@ -178,9 +175,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 ArticleDefine::cacheLoad() void example_novel::ArticleDefine::setName(const QString& nm)
{ {
name_store = selfTokens()[2]->token()->content(); this->name_store = nm;
} }
int ArticleDefine::typeMark() const { return (int)NovelNode::ArticleDefine; } int ArticleDefine::typeMark() const { return (int)NovelNode::ArticleDefine; }
@ -193,7 +190,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)
{ {
} }
@ -202,6 +199,11 @@ 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;
@ -216,8 +218,3 @@ 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::ExpressionElement, public ast_gen::SyntaxElement { class LIBSYNTAX_EXPORT AbstractImpl : public ast_basic::ExprElement, public ast_gen::SyntaxElement {
private: private:
std::weak_ptr<const SyntaxElement> parent_store; std::weak_ptr<const SyntaxElement> parent_store;
@ -43,10 +43,6 @@ 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 {
@ -54,7 +50,11 @@ 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,7 +62,6 @@ 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;
@ -73,13 +72,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;
@ -90,13 +89,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;
@ -107,13 +106,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;
@ -124,6 +123,8 @@ 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;
@ -132,7 +133,6 @@ 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,7 +148,6 @@ 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 {
@ -158,12 +157,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,40 +7,6 @@ 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; }
@ -85,8 +51,10 @@ 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 ExpressionElement; class ExprElement;
} }
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,23 +98,55 @@ 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匹配
*/ */
class LIBSYNTAX_EXPORT TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch> { template<typename elem, token_proc<elem> xproc = nullptr>
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); TokenMatch(std::shared_ptr<const lib_token::ITokenDefine> define) : define_peer(define) {}
// IBasicRule interface // IBasicRule interface
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override { return QList<std::shared_ptr<const IBasicRule>>(); }
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; if (!head) {
virtual QString token_present() const override; 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)) {
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());
}
}; };
/** /**
@ -133,7 +165,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;
}; };
@ -151,7 +183,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;
}; };
@ -170,7 +202,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;
}; };
@ -194,7 +226,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:
@ -212,7 +244,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,7 +27,8 @@ auto name_text = std::make_shared<NameSection>(); // ^
// rule-parts =============================================================================== // rule-parts ===============================================================================
// MatchRule // MatchRule
#define MR(x) std::make_shared<const TokenMatch>(x) #define MR(E, x) std::make_shared<const TokenMatch<E>>(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
@ -41,48 +42,72 @@ 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{ MR(numbers), MR(vtext), MR(refers), MR(split_mark)})) MultiR(std::make_shared<const Any>(Rules{
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(leftb), MR(frag_key), MR(name_text) } << MR(FragmentDefine, leftb), MR(FragmentDefine, frag_key), MER(FragmentDefine, frags_nmset, name_text) } <<
OptR(decl_expr) << OptR(decl_expr) <<
MR(rightb) MR(FragmentDefine, 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(leftb), MR(refers), MR(frag_key), MR(name_text), MR(split_mark), MR(name_text) } << MR(FragmentRefers, leftb), MR(FragmentRefers, refers), MR(FragmentRefers, frag_key),
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(rightb) MR(FragmentRefers, 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(leftb), MR(story_key), MR(name_text) } << MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, 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(rightb) MR(StoryDefine, 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(leftb), MR(article_key), MR(name_text) } << MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, 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(rightb) MR(ArticleDefine, 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(leftb), MR(volume_key), MR(name_text) } << MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, 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(rightb) MR(VolumeDefine, 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(declare), MR(rank_key), MR(numbers) } MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, 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>(
@ -95,13 +120,12 @@ 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)
{ {
cache_load(root, children); build_objecttree(root, children);
node_register(root, children); node_register(root, children);
return root; return root;
} }
void NovalSyntax::cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children) void NovalSyntax::build_objecttree(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);
@ -111,7 +135,7 @@ void NovalSyntax::cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList
child_items.append(std::const_pointer_cast<ast_gen::SyntaxElement>(const_it)); child_items.append(std::const_pointer_cast<ast_gen::SyntaxElement>(const_it));
} }
cache_load(cinst, child_items); build_objecttree(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 cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs); static void build_objecttree(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);
}; };