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>(); // #
|
2024-07-26 08:22:34 +00:00
|
|
|
|
auto split_mark = std::make_shared<Split>(); // &
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-07-12 22:16:11 +00:00
|
|
|
|
auto rank_key = std::make_shared<Keywords>(u8"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0xAEu); // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
auto story_key = std::make_shared<Keywords>(u8"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0xAAu); // <20><><EFBFBD><EFBFBD>
|
2024-03-17 07:58:28 +00:00
|
|
|
|
auto numbers = std::make_shared<Numbers>(); // [0-9]+
|
2024-07-12 22:16:11 +00:00
|
|
|
|
auto frag_key = std::make_shared<Keywords>(u8"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 0xABu); // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
auto volume_key = std::make_shared<Keywords>(u8"<EFBFBD>־<EFBFBD>", 0xACu); // <20>־<EFBFBD>
|
|
|
|
|
auto article_key = std::make_shared<Keywords>(u8"<EFBFBD>½<EFBFBD>", 0xADu); // <20>½<EFBFBD>
|
2024-09-22 01:57:38 +00:00
|
|
|
|
auto vtext = std::make_shared<VTextSection>(); // ^([^\\{\\}@&]+)
|
2024-07-26 08:02:48 +00:00
|
|
|
|
auto name_text = std::make_shared<NameSection>(); // ^([^\\{\\}@&]+)
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// rule-parts ===============================================================================
|
|
|
|
|
// MatchRule
|
2024-07-28 08:37:31 +00:00
|
|
|
|
#define MR(E, x) std::make_shared<const TokenMatch<E>>(x)
|
|
|
|
|
#define MER(E, xproc, t) std::make_shared<const TokenMatch<E, xproc>>(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
|
|
|
|
|
2024-07-12 09:35:35 +00:00
|
|
|
|
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MultiR(std::make_shared<const Any>(Rules{
|
|
|
|
|
MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark)
|
|
|
|
|
}))
|
2024-07-26 08:22:34 +00:00
|
|
|
|
);
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
2025-02-02 14:30:47 +00:00
|
|
|
|
void frags_nmset(std::shared_ptr<PointDefines> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2025-02-02 14:30:47 +00:00
|
|
|
|
auto fragment_decl = ElementRule<PointDefines>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
2025-02-02 14:30:47 +00:00
|
|
|
|
MR(PointDefines, leftb), MR(PointDefines, frag_key), MER(PointDefines, frags_nmset, name_text) } <<
|
2024-07-26 08:22:34 +00:00
|
|
|
|
OptR(decl_expr) <<
|
2025-02-02 14:30:47 +00:00
|
|
|
|
MR(PointDefines, rightb)
|
2024-06-18 03:54:36 +00:00
|
|
|
|
));
|
2024-05-02 11:11:03 +00:00
|
|
|
|
|
2025-02-02 14:29:03 +00:00
|
|
|
|
void frags_snm_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setStoryRefer(token->content());
|
|
|
|
|
}
|
2025-02-02 14:29:03 +00:00
|
|
|
|
void frags_fnm_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
2024-07-28 08:37:31 +00:00
|
|
|
|
inst->setFragmentRefer(token->content());
|
|
|
|
|
}
|
2025-02-02 14:29:03 +00:00
|
|
|
|
auto fragment_refer = ElementRule<PointRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
2025-02-02 14:29:03 +00:00
|
|
|
|
MR(PointRefers, leftb), MR(PointRefers, refers), MR(PointRefers, frag_key),
|
|
|
|
|
MER(PointRefers, frags_fnm_set, name_text), MR(PointRefers, split_mark), MER(PointRefers, frags_snm_set, name_text) } <<
|
2024-07-26 08:22:34 +00:00
|
|
|
|
OptR(decl_expr) <<
|
2025-02-02 14:29:03 +00:00
|
|
|
|
MR(PointRefers, rightb)
|
2024-07-12 09:35:35 +00:00
|
|
|
|
));
|
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) {
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2024-07-12 09:35:35 +00:00
|
|
|
|
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
|
|
|
|
MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, name_text) } <<
|
2024-07-26 08:22:34 +00:00
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr })) <<
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(StoryDefine, rightb)
|
2024-07-12 09:35:35 +00:00
|
|
|
|
));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
// ===================================================================
|
2024-07-28 08:37:31 +00:00
|
|
|
|
void article_nset(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2024-07-12 09:35:35 +00:00
|
|
|
|
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
|
|
|
|
MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, name_text) } <<
|
2024-07-12 09:35:35 +00:00
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) <<
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(ArticleDefine, rightb)
|
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) {
|
|
|
|
|
inst->setName(token->content());
|
|
|
|
|
}
|
2024-07-12 09:35:35 +00:00
|
|
|
|
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
|
|
|
|
MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, name_text) } <<
|
2024-07-12 09:35:35 +00:00
|
|
|
|
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
|
2024-07-28 08:37:31 +00:00
|
|
|
|
MR(VolumeDefine, rightb)
|
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) {
|
|
|
|
|
inst->setRank(token->content().toInt());
|
|
|
|
|
}
|
2024-07-12 09:35:35 +00:00
|
|
|
|
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule(
|
2024-07-28 08:37:31 +00:00
|
|
|
|
std::make_shared<const Seqs>(Rules{
|
|
|
|
|
MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, numbers) }
|
|
|
|
|
));
|
2024-07-12 09:35:35 +00:00
|
|
|
|
|
|
|
|
|
auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).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}))
|
|
|
|
|
}
|
|
|
|
|
));
|
2024-03-17 07:58:28 +00:00
|
|
|
|
|
2024-07-12 04:53:43 +00:00
|
|
|
|
std::shared_ptr<const ExprRule> NovalSyntax::getParseTree() { return document_define; }
|
2024-06-20 13:36:46 +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)
|
2024-06-18 03:54:36 +00:00
|
|
|
|
{
|
2024-07-28 08:37:31 +00:00
|
|
|
|
build_objecttree(root, children);
|
2024-06-18 04:44:18 +00:00
|
|
|
|
node_register(root, children);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
2024-07-28 08:37:31 +00:00
|
|
|
|
void NovalSyntax::build_objecttree(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
|
2024-06-18 04:44:18 +00:00
|
|
|
|
{
|
2024-06-18 03:54:36 +00:00
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 08:37:31 +00:00
|
|
|
|
build_objecttree(cinst, child_items);
|
2024-06-18 03:54:36 +00:00
|
|
|
|
}
|
2024-06-18 04:44:18 +00:00
|
|
|
|
}
|
2024-06-20 13:36:46 +00:00
|
|
|
|
void NovalSyntax::node_register(std::shared_ptr<const ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
|
2024-06-18 04:44:18 +00:00
|
|
|
|
{
|
|
|
|
|
for (auto& child : children) {
|
2024-06-20 13:36:46 +00:00
|
|
|
|
if (!child->isAnonymous()) {
|
|
|
|
|
auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child);
|
2024-07-12 09:35:35 +00:00
|
|
|
|
if (check_result)
|
2024-06-20 14:13:06 +00:00
|
|
|
|
throw new lib_syntax::SyntaxException(QString(u8"SyntaxError[0x0004]ϵͳ<CFB5>а<EFBFBD><D0B0><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㣺%1<type<70><65>%2>(%3,%4)")
|
2024-06-20 13:36:46 +00:00
|
|
|
|
.arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path()));
|
|
|
|
|
}
|
2024-06-18 03:54:36 +00:00
|
|
|
|
|
2024-06-18 04:44:18 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2024-06-20 13:36:46 +00:00
|
|
|
|
}
|