WsParser_VS/libSyntax/syntax_novel.cpp

191 lines
8.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
// token-avaliable ==========================================================================
auto leftb = std::make_shared<LeftBracket>(); // {
auto rightb = std::make_shared<RightBracket>(); // }
auto refers = std::make_shared<ReferMark>(); // @
auto declare = std::make_shared<DeclareSymbo>(); // #
auto split_mark = std::make_shared<Split>(); // &
auto rank_key = std::make_shared<Keywords>(u8"排序", 0xAEu); // 排序
auto story_key = std::make_shared<Keywords>(u8"故事", 0xAAu); // 故事
auto numbers = std::make_shared<Numbers>(); // [0-9]+
auto slice_key = std::make_shared<Keywords>(u8"剧情", 0xAFu); // 剧情
auto point_key = std::make_shared<Keywords>(u8"节点", 0xABu); // 节点
auto volume_key = std::make_shared<Keywords>(u8"分卷", 0xACu); // 分卷
auto article_key = std::make_shared<Keywords>(u8"章节", 0xADu); // 章节
auto vtext = std::make_shared<VTextSection>(); // ^([^\\{\\}@&]+)
auto name_text = std::make_shared<NameSection>(); // ^([^\\{\\}@&]+)
// rule-parts ===============================================================================
template<typename ExprT>
void apntk(std::shared_ptr<ExprT> expr, std::shared_ptr<const lib_token::IToken> t) {
expr->addToken(t);
}
// MatchRule
#define MER(E, XProc, t) std::make_shared<const TokenMatch<E, XProc>>(t)
#define MR(E, t) MER(E, apntk<E>, t)
// Buffer
#define Rules QList<std::shared_ptr<const IBasicRule>>
// 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)
// opt?
#define OptR(rule) std::make_shared<const Rept>(rule, 0, 1)
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int) NovelNode::TextSection).reloadRule(
MultiR(std::make_shared<const Any>(Rules{
MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark)
}))
);
void point_nmset(std::shared_ptr<PointDefines> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto point_decl = ElementRule<PointDefines>(u8"point_define", (int) NovelNode::PointDefines).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(PointDefines, leftb), MR(PointDefines, point_key), MER(PointDefines, point_nmset, name_text) } <<
OptR(decl_expr) <<
MR(PointDefines, rightb)
));
void point_ref_story_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setStoryRefer(token->content());
}
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());
}
auto point_refer = ElementRule<PointRefers>(u8"point_refer", (int) NovelNode::PointRefers).reloadRule(
std::make_shared<const Seqs>(Rules{
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) } <<
OptR(decl_expr) <<
MR(PointRefers, rightb)
));
void slice_nm_set(std::shared_ptr<FragmentSlice> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto slice_define = ElementRule<FragmentSlice>(u8"slice_define", (int) NovelNode::FragmentSlice).reloadRule(
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{ point_decl, point_refer, decl_expr })) <<
MR(FragmentSlice, rightb)
));
void story_nmset(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int) NovelNode::StoryDefine).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ slice_define, decl_expr })) <<
MR(StoryDefine, rightb)
));
// ===================================================================
void article_nset(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int) NovelNode::ArticleDefine).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ point_refer, decl_expr })) <<
MR(ArticleDefine, rightb)
));
void volume_nset(std::shared_ptr<VolumeDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int) NovelNode::VolumeDefine).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
MR(VolumeDefine, rightb)
));
void rank_set(std::shared_ptr<RankDeclare> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setRank(token->content().toInt());
}
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int) NovelNode::RankDeclaration).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, numbers) }
));
auto document_define = ElementRule<Document>(u8"decls-doc", (int) NovelNode::Document).reloadRule(
std::make_shared<const Seqs>(
Rules{
std::make_shared<const Rept>(rank_define, 0, 1),
MultiR(std::make_shared<const Any>(Rules{story_define, volume_decl}))
}
));
std::shared_ptr<const ExprRule> NovalSyntax::getSyntaxTree() { 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)
//{
// 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(u8"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);
// }
//}