2024-03-17 07:58:28 +00:00
|
|
|
|
#include "syntax_novel.h"
|
|
|
|
|
#include <tokens_novel.h>
|
2024-06-18 03:54:36 +00:00
|
|
|
|
#include "ast_novel.h"
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
|
|
using namespace lib_syntax;
|
|
|
|
|
using namespace example_novel;
|
|
|
|
|
using namespace lib_token;
|
|
|
|
|
using namespace ast_basic;
|
|
|
|
|
|
|
|
|
|
// token-avaliable ==========================================================================
|
|
|
|
|
auto leftb = std::make_shared<LeftBracket>(); // {
|
|
|
|
|
auto rightb = std::make_shared<RightBracket>(); // }
|
|
|
|
|
auto refers = std::make_shared<ReferMark>(); // @
|
2024-06-20 13:36:46 +00:00
|
|
|
|
auto declare = std::make_shared<DeclareSymbo>(); // #
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto split_mark = std::make_shared<Split>(); // &
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
auto rank_key = std::make_shared<Keywords>("排序", 0xAEu); // 排序
|
|
|
|
|
auto story_key = std::make_shared<Keywords>("故事", 0xAAu); // 故事
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto numbers = std::make_shared<Numbers>(); // [0-9]+
|
2025-02-11 14:32:10 +00:00
|
|
|
|
auto slice_key = std::make_shared<Keywords>("剧情", 0xAFu); // 剧情
|
|
|
|
|
auto point_key = std::make_shared<Keywords>("节点", 0xABu); // 节点
|
|
|
|
|
auto volume_key = std::make_shared<Keywords>("分卷", 0xACu); // 分卷
|
|
|
|
|
auto article_key = std::make_shared<Keywords>("章节", 0xADu); // 章节
|
2024-09-22 01:57:38 +00:00
|
|
|
|
auto vtext = std::make_shared<VTextSection>(); // ^([^\\{\\}@&]+)
|
2025-02-07 15:26:20 +00:00
|
|
|
|
auto name_text = std::make_shared<NameSection>(); // ^([^\\{\\}@&]+)
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// rule-parts ===============================================================================
|
2025-02-07 15:26:20 +00:00
|
|
|
|
template<typename ExprT>
|
|
|
|
|
void apntk(std::shared_ptr<ExprT> expr, std::shared_ptr<const lib_token::IToken> t) {
|
|
|
|
|
expr->addToken(t);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// MakeRule
|
|
|
|
|
#define MK(type) std::make_shared<const type>()
|
2024-03-17 07:58:28 +00:00
|
|
|
|
// MatchRule
|
2025-02-07 15:26:20 +00:00
|
|
|
|
#define MER(E, XProc, t) std::make_shared<const TokenMatch<E, XProc>>(t)
|
|
|
|
|
#define MR(E, t) MER(E, apntk<E>, t)
|
2024-03-17 07:58:28 +00:00
|
|
|
|
// Buffer
|
2024-07-12 21:52:32 +00:00
|
|
|
|
#define Rules QList<std::shared_ptr<const IBasicRule>>
|
2024-03-17 07:58:28 +00:00
|
|
|
|
// Option
|
|
|
|
|
#define OptMulT(token) std::make_shared<const Rept>(MR(token), 0, INT_MAX)
|
|
|
|
|
#define OptMulR(rule) std::make_shared<const Rept>(rule, 0, INT_MAX)
|
|
|
|
|
// multi+
|
|
|
|
|
#define MultiR(rule) std::make_shared<const Rept>(rule, 1, INT_MAX)
|
2024-07-26 08:22:34 +00:00
|
|
|
|
// opt?
|
|
|
|
|
#define OptR(rule) std::make_shared<const Rept>(rule, 0, 1)
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-05-02 11:11:03 +00:00
|
|
|
|
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class DeclExpr : public ElementRule<TextSection, (int) NovelNode::TextSection> {
|
|
|
|
|
public:
|
|
|
|
|
DeclExpr() : ElementRule<TextSection, (int) NovelNode::TextSection>(
|
|
|
|
|
"decl_section",
|
|
|
|
|
MultiR(std::make_shared<const Any>(Rules{
|
|
|
|
|
MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark)
|
|
|
|
|
}))) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void point_nmset(std::shared_ptr<PointDefines> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class PointSyntax : public ElementRule<PointDefines, (int) NovelNode::PointDefines> {
|
|
|
|
|
public:
|
|
|
|
|
PointSyntax() : ElementRule<PointDefines, (int) NovelNode::PointDefines>(
|
|
|
|
|
"point_define", std::make_shared<const Seqs>(Rules{
|
|
|
|
|
MR(PointDefines, leftb), MR(PointDefines, point_key), MER(PointDefines, point_nmset, name_text) } <<
|
|
|
|
|
OptR(MK(DeclExpr)) <<
|
|
|
|
|
MR(PointDefines, rightb)
|
|
|
|
|
)) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-05-02 11:11:03 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void point_ref_story_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setStoryRefer(token->content());
|
|
|
|
|
}
|
2025-02-07 15:26:20 +00:00
|
|
|
|
void point_ref_slice_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setSliceRefer(token->content());
|
|
|
|
|
}
|
|
|
|
|
void point_ref_point_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setPointRefer(token->content());
|
2024-07-28 08:37:31 +00:00
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class ReferSyntax : public ElementRule<PointRefers, (int) NovelNode::PointRefers> {
|
|
|
|
|
public:
|
|
|
|
|
ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers>(
|
|
|
|
|
"point_refer", std::make_shared<const Seqs>(Rules{
|
2025-02-07 15:26:20 +00:00
|
|
|
|
MR(PointRefers, leftb), MR(PointRefers, refers), MR(PointRefers, point_key),
|
|
|
|
|
MER(PointRefers, point_ref_story_set, name_text),
|
|
|
|
|
MR(PointRefers, split_mark),
|
|
|
|
|
MER(PointRefers, point_ref_slice_set, name_text),
|
|
|
|
|
MR(PointRefers, split_mark),
|
|
|
|
|
MER(PointRefers, point_ref_point_set, name_text) } <<
|
2025-02-11 14:32:10 +00:00
|
|
|
|
OptR(MK(DeclExpr)) <<
|
|
|
|
|
MR(PointRefers, rightb))) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
|
|
|
|
|
void slice_nm_set(std::shared_ptr<FragmentSlice> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class SliceSyntax : public ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice> {
|
|
|
|
|
public:
|
|
|
|
|
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice>(
|
|
|
|
|
"slice_define", std::make_shared<const Seqs>(Rules{
|
|
|
|
|
MR(FragmentSlice, leftb), MR(FragmentSlice, slice_key), MER(FragmentSlice, slice_nm_set, name_text) } <<
|
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ MK(PointSyntax), MK(ReferSyntax), MK(DeclExpr) })) <<
|
|
|
|
|
MR(FragmentSlice, rightb))) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-05-02 11:11:03 +00:00
|
|
|
|
|
2024-07-28 08:37:31 +00:00
|
|
|
|
void story_nmset(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
inst->addToken(token);
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class StorySyntax : public ElementRule<StoryDefine, (int) NovelNode::StoryDefine> {
|
|
|
|
|
public:
|
|
|
|
|
StorySyntax() : ElementRule<StoryDefine, (int) NovelNode::StoryDefine>(
|
|
|
|
|
"story_define",
|
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, name_text) } <<
|
2025-02-11 14:32:10 +00:00
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ MK(SliceSyntax), MK(DeclExpr) })) <<
|
|
|
|
|
MR(StoryDefine, rightb)
|
|
|
|
|
)) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-28 08:37:31 +00:00
|
|
|
|
void article_nset(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
inst->addToken(token);
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class ArticleSyntax : public ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine> {
|
|
|
|
|
public:
|
|
|
|
|
ArticleSyntax() : ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine>(
|
|
|
|
|
"article_define", std::make_shared<const Seqs>(Rules{
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, name_text) } <<
|
2025-02-11 14:32:10 +00:00
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ MK(ReferSyntax), MK(DeclExpr) })) <<
|
|
|
|
|
MR(ArticleDefine, rightb)
|
|
|
|
|
)) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2024-07-28 08:37:31 +00:00
|
|
|
|
void volume_nset(std::shared_ptr<VolumeDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
inst->addToken(token);
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class VolumeSyntax : public ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine> {
|
|
|
|
|
public:
|
|
|
|
|
VolumeSyntax() : ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine>(
|
|
|
|
|
"volume_define", std::make_shared<const Seqs>(Rules{
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, name_text) } <<
|
2025-02-11 14:32:10 +00:00
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ MK(DeclExpr), MK(ArticleSyntax) })) <<
|
|
|
|
|
MR(VolumeDefine, rightb)
|
|
|
|
|
)) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2024-07-28 08:37:31 +00:00
|
|
|
|
void rank_set(std::shared_ptr<RankDeclare> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-07 15:26:20 +00:00
|
|
|
|
inst->addToken(token);
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setRank(token->content().toInt());
|
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class RankSyntax : public ElementRule<RankDeclare, (int) NovelNode::RankDeclaration> {
|
|
|
|
|
public:
|
|
|
|
|
RankSyntax() : ElementRule<RankDeclare, (int) NovelNode::RankDeclaration>(
|
|
|
|
|
"rank_define", std::make_shared<const Seqs>(Rules{
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, numbers) }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
)) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DocumentSyntax : public ElementRule<Document, (int) NovelNode::Document> {
|
|
|
|
|
public:
|
|
|
|
|
DocumentSyntax() : ElementRule<Document, (int) NovelNode::Document>(
|
|
|
|
|
"decls-doc", std::make_shared<const Seqs>(Rules{
|
|
|
|
|
OptR(MK(RankSyntax)), MultiR(std::make_shared<const Any>(Rules{ MK(StorySyntax), MK(VolumeSyntax) }))
|
|
|
|
|
})) { }
|
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return _children_store->parse(cursor);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
class MM : public lib_syntax::IBasicRule {
|
|
|
|
|
// 通过 IBasicRule 继承
|
|
|
|
|
QList<std::shared_ptr<const IBasicRule>> children() const override {
|
|
|
|
|
return QList<std::shared_ptr<const IBasicRule>>();
|
2024-06-18 04:44:18 +00:00
|
|
|
|
}
|
2025-02-11 14:32:10 +00:00
|
|
|
|
QString present() const override {
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
|
|
|
|
return QList<std::shared_ptr<const MatchCursor>>();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#include "syntax_templets.h"
|
|
|
|
|
std::shared_ptr<const ExprRule> NovalSyntax::getSyntaxTree() {
|
|
|
|
|
lib_composit::TypeList<MM, MM> vv;
|
|
|
|
|
|
|
|
|
|
return std::make_shared<DocumentSyntax>();
|
|
|
|
|
}
|
2025-02-08 01:06:39 +00:00
|
|
|
|
|
2025-02-07 15:26:20 +00:00
|
|
|
|
//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);
|
|
|
|
|
// node_register(root, children);
|
|
|
|
|
// return root;
|
|
|
|
|
//}
|
|
|
|
|
//void NovalSyntax::build_objecttree(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
|
|
|
|
|
//{
|
|
|
|
|
// for (auto& cinst : children) {
|
|
|
|
|
// cinst->setParent(root);
|
|
|
|
|
//
|
|
|
|
|
// QList<std::shared_ptr<ast_gen::SyntaxElement>> child_items;
|
|
|
|
|
// for (auto& it : cinst->bindExpression()->children()) {
|
|
|
|
|
// auto const_it = std::dynamic_pointer_cast<const ast_gen::SyntaxElement>(it);
|
|
|
|
|
// child_items.append(std::const_pointer_cast<ast_gen::SyntaxElement>(const_it));
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
//{
|
|
|
|
|
// for (auto& child : children) {
|
|
|
|
|
// if (!child->isAnonymous()) {
|
|
|
|
|
// auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child);
|
|
|
|
|
// if (check_result)
|
2025-02-11 14:32:10 +00:00
|
|
|
|
// throw new SyntaxException(QString("SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type:%2>(%3,%4)")
|
2025-02-07 15:26:20 +00:00
|
|
|
|
// .arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path()));
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// QList<std::shared_ptr<ast_gen::SyntaxElement>> next_child_items;
|
|
|
|
|
// for (auto& it : child->bindExpression()->children()) {
|
|
|
|
|
// auto const_it = std::dynamic_pointer_cast<const ast_gen::SyntaxElement>(it);
|
|
|
|
|
// next_child_items.append(std::const_pointer_cast<ast_gen::SyntaxElement>(const_it));
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// node_register(child, next_child_items);
|
|
|
|
|
// }
|
|
|
|
|
//}
|