221 lines
9.2 KiB
C++
221 lines
9.2 KiB
C++
#include "syntax_novel.h"
|
||
#include <tokens_novel.h>
|
||
#include "ast_novel.h"
|
||
|
||
using namespace lib_syntax;
|
||
using namespace example_novel;
|
||
using namespace lib_token;
|
||
using namespace ast_basic;
|
||
using namespace std;
|
||
|
||
// rule-parts ===============================================================================
|
||
#include "syntax_templets.h"
|
||
using namespace lib_composit;
|
||
|
||
void point_nmset(std::shared_ptr<PointDefines> inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
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());
|
||
}
|
||
void story_nmset(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
inst->addToken(token);
|
||
inst->setName(token->content());
|
||
}
|
||
void article_nset(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
inst->addToken(token);
|
||
inst->setName(token->content());
|
||
}
|
||
void volume_nset(std::shared_ptr<VolumeDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
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());
|
||
}
|
||
|
||
using TextDeclsSyntaxDef = lib_composit::Multi<AnyR<TokenRn<TextSection, Numbers>, TokenRn<TextSection, NormalText>, TokenRn<TextSection, ReferMk>, TokenRn<TextSection, SplitMk>>>;
|
||
class DeclSyntax : public ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef> {
|
||
public:
|
||
DeclSyntax() : ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef>("decl_section") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using PointSyntaxDef = lib_composit::SeqsR<TokenRn<PointDefines, LBracket>, TokenRn<PointDefines, PointWord>, TokenR<PointDefines, NameSection, point_nmset>,
|
||
Opt<DeclSyntax>,
|
||
TokenRn<PointDefines, RBracket>>;
|
||
class PointSyntax : public ElementRule<PointDefines, (int) NovelNode::PointDefines, PointSyntaxDef> {
|
||
public:
|
||
PointSyntax() : ElementRule<PointDefines, (int) NovelNode::PointDefines, PointSyntaxDef>("point_define") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using ReferSyntaxDef = lib_composit::SeqsR<TokenRn<PointRefers, LBracket>, TokenRn<PointRefers, ReferMk>, TokenRn<PointRefers, PointWord>, TokenR<PointRefers, NameSection, ref_story_set>, TokenRn<PointRefers, ReferMk>, TokenR<PointRefers, NameSection, ref_slice_set>, TokenRn<PointRefers, ReferMk>, TokenR<PointRefers, NameSection, ref_point_set>,
|
||
Opt<DeclSyntax>,
|
||
TokenRn<PointDefines, RBracket>>;
|
||
class ReferSyntax : public ElementRule<PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef> {
|
||
public:
|
||
ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef>("point_refer") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using SliceSyntaxDef = lib_composit::SeqsR<TokenRn<FragmentSlice, LBracket>, TokenRn<FragmentSlice, SliceWord>, TokenR<FragmentSlice, NameSection, slice_nm_set>,
|
||
lib_composit::OptMulti<AnyR<PointSyntax, ReferSyntax, DeclSyntax>>,
|
||
TokenRn<FragmentSlice, RBracket>>;
|
||
class SliceSyntax : public ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef> {
|
||
public:
|
||
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef>("slice_define") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using StorySyntaxDef = lib_composit::SeqsR<TokenRn<StoryDefine, LBracket>, TokenRn<StoryDefine, StoryWord>, TokenR<StoryDefine, NameSection, story_nmset>,
|
||
lib_composit::OptMulti<AnyR<SliceSyntax, DeclSyntax>>,
|
||
TokenRn<StoryDefine, RBracket>>;
|
||
class StorySyntax : public ElementRule<StoryDefine, (int) NovelNode::StoryDefine, StorySyntaxDef> {
|
||
public:
|
||
StorySyntax() : ElementRule<StoryDefine, (int) NovelNode::StoryDefine, StorySyntaxDef>("story_define") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using ArticleSyntaxDef = lib_composit::SeqsR<TokenRn<ArticleDefine, LBracket>, TokenRn<ArticleDefine, ArticleWord>, TokenR<ArticleDefine, NameSection, article_nset>,
|
||
lib_composit::OptMulti<AnyR<ReferSyntax, DeclSyntax>>,
|
||
TokenRn<ArticleDefine, RBracket>>;
|
||
class ArticleSyntax : public ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine, ArticleSyntaxDef> {
|
||
public:
|
||
ArticleSyntax() : ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine, ArticleSyntaxDef>("article_define") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using VolumeSyntaxDef = lib_composit::SeqsR<TokenRn<VolumeDefine, LBracket>, TokenRn<VolumeDefine, VolumeWord>, TokenR<VolumeDefine, NameSection, volume_nset>,
|
||
lib_composit::OptMulti<AnyR<ArticleSyntax, DeclSyntax>>,
|
||
TokenRn<VolumeDefine, RBracket>>;
|
||
class VolumeSyntax : public ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine, VolumeSyntaxDef> {
|
||
public:
|
||
VolumeSyntax() : ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine, VolumeSyntaxDef>("volume_define") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using RankSyntaxDef = lib_composit::SeqsR<TokenRn<RankDeclare, DeclareSymbo>, TokenRn<RankDeclare, RankWord>, TokenR<RankDeclare, Numbers, rank_set>>;
|
||
class RankSyntax : public ElementRule<RankDeclare, (int) NovelNode::RankDeclaration, RankSyntaxDef> {
|
||
public:
|
||
RankSyntax() : ElementRule<RankDeclare, (int) NovelNode::RankDeclaration, RankSyntaxDef>("rank_define") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using DocSyntaxDef = lib_composit::SeqsR<Opt<RankSyntax>, lib_composit::OptMulti<AnyR<StorySyntax, VolumeSyntax>>>;
|
||
class DocumentSyntax : public ElementRule<Document, (int) NovelNode::Document, DocSyntaxDef> {
|
||
public:
|
||
DocumentSyntax() : ElementRule<Document, (int) NovelNode::Document, DocSyntaxDef>("decls-doc") { }
|
||
|
||
// 通过 ElementRule 继承
|
||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
|
||
|
||
//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)
|
||
// throw new SyntaxException(QString("SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type:%2>(%3,%4)")
|
||
// .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);
|
||
// }
|
||
//}
|