WsParser_VS/libSyntax/ast_novel.h

254 lines
6.6 KiB
C
Raw Normal View History

2024-03-17 07:58:28 +00:00
#pragma once
#include "ast_gen.h"
2025-02-02 14:06:49 +00:00
namespace example_novel {
enum class NovelNode {
GlobalElement = 0,
TextSection = 1,
2025-02-02 14:26:27 +00:00
PointRefers = 2,
PointDefines = 3,
StoryDefine = 4,
Document = 5,
ArticleDefine = 6,
VolumeDefine = 7,
RankDeclaration = 8,
2025-02-02 14:29:03 +00:00
FragmentSlice = 9,
};
2024-07-28 06:41:54 +00:00
class LIBSYNTAX_EXPORT AbstractImpl : public ast_basic::ExprElement, public ast_gen::SyntaxElement {
private:
2024-06-18 11:55:36 +00:00
std::weak_ptr<const SyntaxElement> parent_store;
public:
explicit AbstractImpl(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
// ͨ<><CDA8> SyntaxElement <20>̳<EFBFBD>
2024-07-12 21:52:32 +00:00
virtual std::shared_ptr<const ast_basic::IExprInst> bindExpression() const override;
QString path() const override;
virtual std::shared_ptr<const ast_gen::SyntaxElement> parent() const override;
virtual void setParent(std::shared_ptr<const ast_gen::SyntaxElement> inst) override;
virtual QList<std::shared_ptr<const ast_gen::TokenAccess>> selfTokens() const override;
};
class LIBSYNTAX_EXPORT TextSection : public AbstractImpl {
public:
TextSection(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString content() const;
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
};
2025-02-04 14:26:34 +00:00
class LIBSYNTAX_EXPORT FragmentSlice : public AbstractImpl {
public:
// ͨ<><CDA8> AbstractImpl <20>̳<EFBFBD>
int typeMark() const override;
bool isAnonymous() const override;
QString signature() const override;
};
2025-02-02 14:29:03 +00:00
class LIBSYNTAX_EXPORT PointRefers : public AbstractImpl {
2025-02-04 14:26:34 +00:00
private:
QString story_refs, slice_ref, point_ref;
public:
2025-02-02 14:29:03 +00:00
PointRefers(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString storyRefer() const;
2025-02-02 14:06:49 +00:00
void setStoryRefer(const QString& refer);
2024-07-28 08:37:31 +00:00
2025-02-04 14:26:34 +00:00
QString sliceRefer() const;
void setSliceRefer(const QString& refer);
QString pointRefer() const;
void setPointRefer(const QString& refer);
2024-07-28 08:37:31 +00:00
QString referSignature() const;
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
};
2025-02-02 14:30:47 +00:00
class LIBSYNTAX_EXPORT PointDefines : public AbstractImpl {
2025-02-04 14:26:34 +00:00
private:
QString name_store;
public:
2025-02-02 14:30:47 +00:00
PointDefines(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const;
2025-02-02 14:06:49 +00:00
void setName(const QString& nm);
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
};
class LIBSYNTAX_EXPORT ArticleDefine : public AbstractImpl {
public:
ArticleDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const;
2025-02-02 14:06:49 +00:00
void setName(const QString& nm);
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
private:
QString name_store;
};
class LIBSYNTAX_EXPORT VolumeDefine : public AbstractImpl {
public:
VolumeDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const;
2025-02-02 14:06:49 +00:00
void setName(const QString& nm);
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
2024-07-13 05:17:56 +00:00
private:
QString name_store;
};
class LIBSYNTAX_EXPORT StoryDefine : public AbstractImpl {
public:
StoryDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
QString name() const;
2025-02-02 14:06:49 +00:00
void setName(const QString& nm);
2024-07-28 08:37:31 +00:00
void setSort(int value);
int sort() const;
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
private:
QString name_store;
int sort_index;
};
class LIBSYNTAX_EXPORT Document : public AbstractImpl {
public:
Document(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
// SyntaxElement interface
public:
virtual int typeMark() const override;
virtual bool isAnonymous() const override;
virtual QString signature() const override;
};
class LIBSYNTAX_EXPORT RankDeclare : public AbstractImpl {
private:
int page_rank = 0;
public:
RankDeclare(std::shared_ptr<const lib_syntax::ExprRule> rule);
int rankNumber() const;
2024-07-28 08:37:31 +00:00
void setRank(int nums);
// ͨ<><CDA8> AbstractImpl <20>̳<EFBFBD>
int typeMark() const override;
bool isAnonymous() const override;
QString signature() const override;
};
2024-03-17 07:58:28 +00:00
}
2024-07-13 05:17:56 +00:00
namespace lib_syntax {
template<> class ElementRule<example_novel::TextSection> : public ExprRule {
public:
ElementRule<example_novel::TextSection>(const QString& rule_name, int expr_mark)
2025-02-02 14:06:49 +00:00
:ExprRule(rule_name, expr_mark) { }
2024-07-13 05:17:56 +00:00
// ͨ<><CDA8> ExprRule <20>̳<EFBFBD>
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const {
return std::dynamic_pointer_cast<ast_basic::IExprInst>(
std::make_shared<example_novel::TextSection>(this->shared_from_this()));
}
virtual std::shared_ptr<ExprRule> makeCopy() const {
return std::make_shared<ElementRule<example_novel::TextSection>>(name(), typeMark());
}
2025-02-02 12:54:32 +00:00
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const override {
2025-02-02 14:06:49 +00:00
2024-07-13 05:17:56 +00:00
std::shared_ptr<ast_basic::IExprInst> elm_ast = this->newEmptyInstance();
auto text_present = this->token_present();
rt_inst->pushExprRule(this->shared_from_this());
rt_inst->pushExprInst(elm_ast);
auto rstg = child_store->parse(rt_inst, head);
auto tokens_decl = elm_ast->tokens();
switch (std::get<0>(rstg)) {
case IBasicRule::MatchResult::Fail:
case IBasicRule::MatchResult::Part:
rt_inst->popExprInst();
rt_inst->popExprRule();
break;
2025-02-02 14:06:49 +00:00
case IBasicRule::MatchResult::Success:
{
if (!std::dynamic_pointer_cast<example_novel::Document>(elm_ast)) {
auto start_pos = tokens_decl.first()->position();
rt_inst->clearErrors(rt_inst->currentFile(), start_pos);
}
2024-07-13 05:17:56 +00:00
2025-02-02 14:06:49 +00:00
rt_inst->popExprInst();
rt_inst->popExprRule();
while (tokens_decl.size()) {
auto text_paragraph = this->newEmptyInstance();
int row_n = tokens_decl.first()->row();
for (int idx = 0; idx < tokens_decl.size(); ++idx) {
auto target_token = tokens_decl.at(idx);
if (target_token->row() == row_n) {
text_paragraph->addToken(target_token);
tokens_decl.removeAt(idx--);
}
2024-07-13 05:17:56 +00:00
}
2025-02-02 14:06:49 +00:00
if (rt_inst->currentExprInst()) {
rt_inst->currentExprInst()->addChild(text_paragraph);
}
else {
rt_inst->appendDocInst(text_paragraph);
}
2024-07-13 05:17:56 +00:00
}
2025-02-02 14:06:49 +00:00
}break;
2024-07-13 05:17:56 +00:00
default:
break;
}
return rstg;
}
};
}