284 lines
11 KiB
C++
284 lines
11 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 ref_story_set(std::shared_ptr<FragmentRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
inst->addToken(token);
|
||
inst->setStoryRefer(token->content());
|
||
}
|
||
void ref_slice_set(std::shared_ptr<FragmentRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
inst->addToken(token);
|
||
inst->setSliceRefer(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_nm_set(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
inst->addToken(token);
|
||
inst->setName(token->content());
|
||
}
|
||
void article_nm_set(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
|
||
inst->addToken(token);
|
||
inst->setName(token->content());
|
||
}
|
||
void volume_nm_set(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());
|
||
}
|
||
|
||
//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>>;
|
||
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 {
|
||
//// 如果提前结束,记录错误并返回
|
||
//if (!cursor->words()) {
|
||
// // 只有在表达式的起始点遇到nullptr,才是正常结束。
|
||
// if (cursor->token()->tokenType() != lib_token::IActionToken::Type::ElementBegin) {
|
||
// auto ncurs = std::make_shared<MatchCursor>(cursor);
|
||
// ncurs->logExprsError(QString("Syntax[0x00001]输入错误,程序提前结束:%1。").arg(cursor->filePath()));
|
||
// ncurs->setFailure();
|
||
// return QList<std::shared_ptr<const MatchCursor>>() << ncurs;
|
||
// }
|
||
|
||
// while (cursor->token()->tokenType() == lib_token::IActionToken::Type::ElementBegin)
|
||
// cursor = cursor->previous();
|
||
|
||
// auto ncurs = std::make_shared<MatchCursor>(cursor);
|
||
// ncurs->setComplete();
|
||
// return QList<std::shared_ptr<const MatchCursor>>() << ncurs;
|
||
//}
|
||
|
||
//auto content = content_extract(cursor->token());
|
||
|
||
//// 绑定文字行
|
||
//auto bind_row = cursor->words()->row();
|
||
//QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
|
||
//decltype(bridge_list) final_result;
|
||
//for (; bridge_list.size();) {
|
||
// // 一次匹配
|
||
// decltype(bridge_list) current_result;
|
||
// for (auto branch : bridge_list) {
|
||
// if (branch->words()->row() == bind_row) {
|
||
// auto mrst = _children_store->parse(branch);
|
||
|
||
// // 本次解析无法通过,全错
|
||
// if (!std::count_if(mrst.begin(), mrst.end(),
|
||
// [&](std::shared_ptr<const MatchCursor> v) {
|
||
// return !v->exprsErrorCount();// 没错
|
||
// })) {
|
||
// final_result.append(branch);
|
||
// continue;
|
||
// }
|
||
|
||
// //
|
||
// for (auto m : mrst) {
|
||
// if (m->exprsErrorCount()) {
|
||
// std::const_pointer_cast<MatchCursor>(m)->setFailure(true);
|
||
// final_result.append(m);
|
||
// }
|
||
// else {
|
||
// current_result.append(m);
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// bridge_list = current_result;
|
||
//}
|
||
|
||
//for (auto x : bridge_list)
|
||
// if (!final_result.contains(x))
|
||
// final_result.append(x);
|
||
//return final_result;
|
||
|
||
auto results = _children_store->parse(cursor);
|
||
std::for_each(results.begin(), results.end(),
|
||
[](std::shared_ptr<const MatchCursor> ins) {
|
||
std::const_pointer_cast<MatchCursor>(ins)->setFailure(ins->exprsErrorCount());
|
||
});
|
||
return results;
|
||
}
|
||
};
|
||
|
||
|
||
using ReferSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<ReferMk>, Match<SliceWord>, Action<FragmentRefers, NameText, ref_story_set>, Match<SplitMk>, Action<FragmentRefers, NameText, ref_slice_set>,
|
||
OptMulti<DeclSyntax>,
|
||
Match<RBracket>>;
|
||
class ReferSyntax : public ElementRule<FragmentRefers, (int) NovelNode::FragmentRefers, ReferSyntaxDef> {
|
||
public:
|
||
ReferSyntax() : ElementRule<FragmentRefers, (int) NovelNode::FragmentRefers, ReferSyntaxDef>("fragment_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::Seqs<Match<LBracket>, Match<SliceWord>, Action<FragmentSlice, NameText, slice_nm_set>,
|
||
lib_composit::OptMulti<Any<ReferSyntax, DeclSyntax>>,
|
||
Match<RBracket>>;
|
||
class SliceSyntax : public ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef> {
|
||
public:
|
||
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef>("fragment_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::Seqs<Match<LBracket>, Match<StoryWord>, Action<StoryDefine, NameText, story_nm_set>,
|
||
lib_composit::OptMulti<Any<SliceSyntax, DeclSyntax>>,
|
||
Match<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 {
|
||
auto syntax = this->present();
|
||
//auto current_rst = content_extract(cursor->token());
|
||
auto rst = _children_store->parse(cursor);
|
||
|
||
/* QString result_list;
|
||
for (auto c : rst) {
|
||
result_list += content_extract(c->token()) += "\n";
|
||
}*/
|
||
|
||
return rst;
|
||
}
|
||
};
|
||
|
||
|
||
|
||
using ArticleSyntaxDef = lib_composit::Seqs<Match<LBracket>, Match<ArticleWord>, Action<ArticleDefine, NameText, article_nm_set>,
|
||
lib_composit::OptMulti<Any<ReferSyntax, DeclSyntax>>,
|
||
Match<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::Seqs<Match<LBracket>, Match<VolumeWord>, Action<VolumeDefine, NameText, volume_nm_set>,
|
||
lib_composit::OptMulti<Any<ArticleSyntax, DeclSyntax>>,
|
||
Match<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::Seqs<Match<DeclareSymbo>, Match<RankWord>, Action<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::Seqs<Opt<RankSyntax>, lib_composit::OptMulti<Any<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 {
|
||
auto text = _children_store->present();
|
||
return _children_store->parse(cursor);
|
||
}
|
||
};
|
||
|
||
|
||
std::shared_ptr<const lib_syntax::ExprRule> example_novel::NovalSyntax::getSyntaxTree() {
|
||
return std::make_shared<DocumentSyntax>();
|
||
}
|
||
|
||
|
||
|
||
//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);
|
||
// }
|
||
//}
|