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;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
using namespace std;
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
|
|
// rule-parts ===============================================================================
|
2025-02-12 04:37:49 +00:00
|
|
|
|
#include "syntax_templets.h"
|
|
|
|
|
using namespace lib_composit;
|
2024-05-02 11:11:03 +00:00
|
|
|
|
|
2025-02-12 06:41:24 +00:00
|
|
|
|
void point_nm_set(std::shared_ptr<PointDefines> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-12 04:37:49 +00:00
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
|
|
|
|
void ref_story_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setStoryRefer(token->content());
|
|
|
|
|
}
|
|
|
|
|
void ref_slice_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setSliceRefer(token->content());
|
|
|
|
|
}
|
|
|
|
|
void ref_point_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setPointRefer(token->content());
|
|
|
|
|
}
|
|
|
|
|
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-12 06:41:24 +00:00
|
|
|
|
void story_nm_set(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-12 04:37:49 +00:00
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-12 06:41:24 +00:00
|
|
|
|
void article_nm_set(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-12 04:37:49 +00:00
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-12 06:41:24 +00:00
|
|
|
|
void volume_nm_set(std::shared_ptr<VolumeDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2025-02-12 04:37:49 +00:00
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
|
|
|
|
void rank_set(std::shared_ptr<RankDeclare> inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->addToken(token);
|
|
|
|
|
inst->setRank(token->content().toInt());
|
|
|
|
|
}
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
auto content_extract = [](std::shared_ptr<const lib_token::IActionToken> token) {
|
|
|
|
|
QString content;
|
|
|
|
|
while (token) {
|
|
|
|
|
if (token->defines())
|
|
|
|
|
content.prepend(token->content() + " ");
|
|
|
|
|
token = token->prevToken();
|
|
|
|
|
}
|
|
|
|
|
return content;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using TextDeclsSyntaxDef = Any<Match<Numbers>, Match<NormalText>, Match<ReferMk>, Match<SplitMk>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class DeclSyntax : public ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
DeclSyntax() : ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef>("decl_section") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
2025-02-14 01:26:42 +00:00
|
|
|
|
auto syntax_text = this->present();
|
|
|
|
|
auto current_rst = content_extract(cursor->currentToken());
|
2025-02-14 17:48:14 +00:00
|
|
|
|
auto rst = _children_store->parse(cursor, out);
|
|
|
|
|
for (auto r : rst)
|
|
|
|
|
std::const_pointer_cast<MatchCursor>(r)->setStop(r->exprsErrorCount());
|
2025-02-14 01:26:42 +00:00
|
|
|
|
return rst;
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 05:44:35 +00:00
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using PointSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<PointWord>, Action<PointDefines, NameText, point_nm_set>,
|
|
|
|
|
OptMulti<DeclSyntax>,
|
|
|
|
|
Match<RBracket>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class PointSyntax : public ElementRule<PointDefines, (int) NovelNode::PointDefines, PointSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
PointSyntax() : ElementRule<PointDefines, (int) NovelNode::PointDefines, PointSyntaxDef>("point_define") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 05:44:35 +00:00
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using ReferSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<ReferMk>, Match<PointWord>, Action<PointRefers, NameText, ref_story_set>, Match<ReferMk>, Action<PointRefers, NameText, ref_slice_set>, Match<ReferMk>, Action<PointRefers, NameText, ref_point_set>,
|
|
|
|
|
OptMulti<DeclSyntax>,
|
|
|
|
|
Match<RBracket>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class ReferSyntax : public ElementRule<PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef>("point_refer") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 05:44:35 +00:00
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using SliceSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<SliceWord>, Action<FragmentSlice, NameText, slice_nm_set>,
|
|
|
|
|
lib_composit::OptMulti<Any<PointSyntax, ReferSyntax, DeclSyntax>>,
|
|
|
|
|
Match<RBracket>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class SliceSyntax : public ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef>("slice_define") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 05:44:35 +00:00
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using StorySyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<StoryWord>, Action<StoryDefine, NameText, story_nm_set>,
|
|
|
|
|
lib_composit::OptMulti<Any<SliceSyntax, DeclSyntax>>,
|
|
|
|
|
Match<RBracket>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class StorySyntax : public ElementRule<StoryDefine, (int) NovelNode::StoryDefine, StorySyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
StorySyntax() : ElementRule<StoryDefine, (int) NovelNode::StoryDefine, StorySyntaxDef>("story_define") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
2025-02-14 07:33:23 +00:00
|
|
|
|
auto syntax = this->present();
|
|
|
|
|
auto current_rst = content_extract(cursor->currentToken());
|
2025-02-14 17:48:14 +00:00
|
|
|
|
auto rst = _children_store->parse(cursor, out);
|
2025-02-14 07:33:23 +00:00
|
|
|
|
|
|
|
|
|
QString result_list;
|
|
|
|
|
for (auto c : rst) {
|
|
|
|
|
result_list += content_extract(c->currentToken()) += "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rst;
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using ArticleSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<ArticleWord>, Action<ArticleDefine, NameText, article_nm_set>,
|
|
|
|
|
lib_composit::OptMulti<Any<ReferSyntax, DeclSyntax>>,
|
|
|
|
|
Match<RBracket>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class ArticleSyntax : public ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine, ArticleSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
ArticleSyntax() : ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine, ArticleSyntaxDef>("article_define") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 05:44:35 +00:00
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using VolumeSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<VolumeWord>, Action<VolumeDefine, NameText, volume_nm_set>,
|
|
|
|
|
lib_composit::OptMulti<Any<ArticleSyntax, DeclSyntax>>,
|
|
|
|
|
Match<RBracket>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class VolumeSyntax : public ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine, VolumeSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
VolumeSyntax() : ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine, VolumeSyntaxDef>("volume_define") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using RankSyntaxDef = lib_composit::Seqs<Match<DeclareSymbo>, Match<RankWord>, Action<RankDeclare, Numbers, rank_set>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class RankSyntax : public ElementRule<RankDeclare, (int) NovelNode::RankDeclaration, RankSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
RankSyntax() : ElementRule<RankDeclare, (int) NovelNode::RankDeclaration, RankSyntaxDef>("rank_define") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-14 01:26:42 +00:00
|
|
|
|
using DocSyntaxDef = lib_composit::Seqs<Opt<RankSyntax>, lib_composit::OptMulti<Any<StorySyntax, VolumeSyntax>>>;
|
2025-02-12 05:44:35 +00:00
|
|
|
|
class DocumentSyntax : public ElementRule<Document, (int) NovelNode::Document, DocSyntaxDef> {
|
2025-02-11 14:32:10 +00:00
|
|
|
|
public:
|
2025-02-12 05:44:35 +00:00
|
|
|
|
DocumentSyntax() : ElementRule<Document, (int) NovelNode::Document, DocSyntaxDef>("decls-doc") { }
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
|
|
|
|
// 通过 ElementRule 继承
|
2025-02-14 17:48:14 +00:00
|
|
|
|
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
2025-02-12 13:50:45 +00:00
|
|
|
|
auto text = _children_store->present();
|
2025-02-14 17:48:14 +00:00
|
|
|
|
return _children_store->parse(cursor, out);
|
2025-02-11 14:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
2025-02-12 07:40:35 +00:00
|
|
|
|
std::shared_ptr<const lib_syntax::ExprRule> example_novel::NovalSyntax::getSyntaxTree() {
|
|
|
|
|
return std::make_shared<DocumentSyntax>();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 14:32:10 +00:00
|
|
|
|
|
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);
|
|
|
|
|
// }
|
2025-02-11 15:36:30 +00:00
|
|
|
|
//}
|