332 lines
9.3 KiB
C++
332 lines
9.3 KiB
C++
|
#include "StoryUnitDocumentParser.h"
|
||
|
|
||
|
using namespace Parse;
|
||
|
using namespace Lex;
|
||
|
using namespace Syntax;
|
||
|
using namespace Syntax::Defines;
|
||
|
|
||
|
NodeStoryUnit::NodeStoryUnit(DocCore * doc, const QString & name)
|
||
|
: doc_ref(doc), name_store(name)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
QString NodeStoryUnit::name() const
|
||
|
{
|
||
|
return name_store;
|
||
|
}
|
||
|
|
||
|
DocCore * NodeStoryUnit::document() const
|
||
|
{
|
||
|
return doc_ref;
|
||
|
}
|
||
|
|
||
|
int NodeStoryUnit::typeValue() const
|
||
|
{
|
||
|
return NODE_STORYUNIT;
|
||
|
}
|
||
|
|
||
|
DesNode * NodeStoryUnit::parent() const
|
||
|
{
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
void NodeStoryUnit::appendChild(DesNode * ins)
|
||
|
{
|
||
|
children_nodes << ins;
|
||
|
}
|
||
|
|
||
|
QList<DesNode*> NodeStoryUnit::children() const
|
||
|
{
|
||
|
return children_nodes;
|
||
|
}
|
||
|
|
||
|
bool NodeStoryUnit::check(QList<QString>& reasons) const
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
QString NodeStoryUnit::toString() const
|
||
|
{
|
||
|
QString rets = "#单元 " + name_store + "{";
|
||
|
for (auto it : children_nodes)
|
||
|
rets += "\n" + it->toString();
|
||
|
rets += "\n}";
|
||
|
return rets;
|
||
|
}
|
||
|
|
||
|
|
||
|
NodeStoryFragment::NodeStoryFragment(NodeStoryUnit * unit, const QString & name)
|
||
|
: unit_ins(unit), name_store(name)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
DocCore * NodeStoryFragment::document() const
|
||
|
{
|
||
|
return unit_ins->document();
|
||
|
}
|
||
|
|
||
|
int NodeStoryFragment::typeValue() const
|
||
|
{
|
||
|
return NODE_STORYFRAGMENT;
|
||
|
}
|
||
|
|
||
|
DesNode * NodeStoryFragment::parent() const
|
||
|
{
|
||
|
return unit_ins;
|
||
|
}
|
||
|
|
||
|
void NodeStoryFragment::appendChild(DesNode * ins)
|
||
|
{
|
||
|
children_nodes << ins;
|
||
|
}
|
||
|
|
||
|
QList<DesNode*> NodeStoryFragment::children() const
|
||
|
{
|
||
|
return children_nodes;
|
||
|
}
|
||
|
|
||
|
bool NodeStoryFragment::check(QList<QString>& reasons) const
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
QString NodeStoryFragment::toString() const
|
||
|
{
|
||
|
QString rets = QString(depth(), ' ') + name() + "{";
|
||
|
for (auto cin : children_nodes)
|
||
|
rets += "\n" + QString(depth(), ' ') + cin->toString();
|
||
|
return rets + "}";
|
||
|
}
|
||
|
|
||
|
QString NodeStoryFragment::name() const
|
||
|
{
|
||
|
return name_store;
|
||
|
}
|
||
|
|
||
|
|
||
|
NodeStoryPointReference::NodeStoryPointReference(DesNode * parent, const QString & chain, const QString & point)
|
||
|
: parent_ins(parent), chain_name(chain), point_name(point) {}
|
||
|
|
||
|
DocCore * NodeStoryPointReference::document() const
|
||
|
{
|
||
|
return parent_ins->document();
|
||
|
}
|
||
|
|
||
|
int NodeStoryPointReference::typeValue() const
|
||
|
{
|
||
|
return NODE_POINTREFERENCE;
|
||
|
}
|
||
|
|
||
|
DesNode * NodeStoryPointReference::parent() const
|
||
|
{
|
||
|
return parent_ins;
|
||
|
}
|
||
|
|
||
|
void NodeStoryPointReference::appendChild(DesNode * ins) {}
|
||
|
|
||
|
QList<DesNode*> NodeStoryPointReference::children() const
|
||
|
{
|
||
|
return QList<DesNode*>();
|
||
|
}
|
||
|
|
||
|
bool NodeStoryPointReference::check(QList<QString>& reasons) const
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
QString NodeStoryPointReference::toString() const
|
||
|
{
|
||
|
return QString("{@节点 @%1 @%2}").arg(chain_name).arg(point_name);
|
||
|
}
|
||
|
|
||
|
NodeStoryFragmentComment::NodeStoryFragmentComment(DesNode * parent,
|
||
|
const QString &order, const QString & name)
|
||
|
: parent_ins(parent), order_name(order), name_store(name)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
QString NodeStoryFragmentComment::order() const
|
||
|
{
|
||
|
return order_name;
|
||
|
}
|
||
|
|
||
|
DocCore * NodeStoryFragmentComment::document() const
|
||
|
{
|
||
|
return parent_ins->document();
|
||
|
}
|
||
|
|
||
|
int NodeStoryFragmentComment::typeValue() const
|
||
|
{
|
||
|
return NODE_ORDEREDCOMMENT;
|
||
|
}
|
||
|
|
||
|
DesNode * NodeStoryFragmentComment::parent() const
|
||
|
{
|
||
|
return parent_ins;
|
||
|
}
|
||
|
|
||
|
void NodeStoryFragmentComment::appendChild(DesNode * ins)
|
||
|
{
|
||
|
children_nodes << ins;
|
||
|
}
|
||
|
|
||
|
QList<DesNode*> NodeStoryFragmentComment::children() const
|
||
|
{
|
||
|
return children_nodes;
|
||
|
}
|
||
|
|
||
|
bool NodeStoryFragmentComment::check(QList<QString>& reasons) const
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
QString NodeStoryFragmentComment::toString() const
|
||
|
{
|
||
|
auto xrets = QString(depth(), ' ') + "#注解 " + name() + "{";
|
||
|
for (auto it : children_nodes)
|
||
|
xrets += "\n" + QString(depth(), ' ') + it->toString();
|
||
|
xrets += "\n" + QString(depth(), ' ') + "}";
|
||
|
|
||
|
return xrets;
|
||
|
}
|
||
|
|
||
|
QString NodeStoryFragmentComment::name() const
|
||
|
{
|
||
|
return name_store;
|
||
|
}
|
||
|
|
||
|
NodeStoryUnitParser::NodeStoryUnitParser(ProjectCore * core)
|
||
|
: XSyntaxBase("故事单元"), pjt_core(core)
|
||
|
{
|
||
|
auto rule = addRule("进入单元解析", 0, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
auto nmidx = cnt - 1, defidx = cnt - 2;;
|
||
|
|
||
|
auto node = new NodeStoryUnit(this->docRef(), seqs[nmidx].Text);
|
||
|
this->refocusNode(node);
|
||
|
|
||
|
auto word0 = new Words(node, docRef(), seqs[defidx].Text, seqs[defidx].StartRow, seqs[defidx].StartCol);
|
||
|
auto word1 = new Words(node, docRef(), seqs[nmidx].Text, seqs[nmidx].StartRow, seqs[nmidx].StartCol);
|
||
|
docRef()->append(word0);
|
||
|
docRef()->append(word1);
|
||
|
|
||
|
return ParseResult::SelfManipulate;
|
||
|
});
|
||
|
rule->addExpression("基础单元解析", { Elm("{单元定义}"), Elm("描述文本", true) });
|
||
|
rule->addExpression("拓展单元解析", { Elm("{换行符}"),Elm("{换行符}"), Exp("基础单元解析", true) });
|
||
|
|
||
|
rule = addRule("单元成分解析", 1, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
auto node = currNode();
|
||
|
|
||
|
auto word = new Words(node, docRef(), seqs[cnt - 1].Text, seqs[cnt - 1].StartRow, seqs[cnt - 1].StartCol);
|
||
|
docRef()->append(word);
|
||
|
|
||
|
return ParseResult::EnterNext;
|
||
|
});
|
||
|
rule->addExpression("基础单元成分解析", { Elm("{左界限}", true) });
|
||
|
rule->addExpression("拓展单元成分解析", { Elm("{换行符}"), Elm("{换行符}"), Exp("基础单元成分解析", true) });
|
||
|
|
||
|
rule = addRule("跳出单元成分解析", 2, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
auto node = currNode();
|
||
|
|
||
|
auto word = new Words(node, docRef(), seqs[cnt - 1].Text, seqs[cnt - 1].StartRow, seqs[cnt - 1].StartCol);
|
||
|
docRef()->append(word);
|
||
|
|
||
|
return ParseResult::Completed;
|
||
|
});
|
||
|
rule->addExpression("基础跳出单元解析", { Elm("{右界限}", true) });
|
||
|
rule->addExpression("拓展跳出单元解析", { Elm("{换行符}"),Elm("{换行符}"),Exp("基础跳出单元解析", true) });
|
||
|
}
|
||
|
|
||
|
ProjectCore * NodeStoryUnitParser::project() const
|
||
|
{
|
||
|
return pjt_core;
|
||
|
}
|
||
|
|
||
|
StoryUnitDocumentParser::StoryUnitDocumentParser(ProjectCore * ins)
|
||
|
{
|
||
|
appendTokensDefine({
|
||
|
{"{单元定义}", "#单元"},
|
||
|
{"{情节定义}", "#情节"},
|
||
|
{"{注解定义}", "#注解"},
|
||
|
{"{描述文本}", "[^#@\\{\\}\\n]+"},
|
||
|
{"{左界限}", "\\{"},
|
||
|
{"{右界限}", "\\}"},
|
||
|
{"{换行符}", "\\n"},
|
||
|
{"{节点引用}", "\\{@节点"},
|
||
|
}, "{无法识别}");
|
||
|
|
||
|
auto chain_parser = new NodeStoryUnitParser(ins);
|
||
|
appendParser(chain_parser);
|
||
|
}
|
||
|
|
||
|
NodeStoryFragmentParser::NodeStoryFragmentParser(NodeStoryUnitParser * pparser)
|
||
|
: XSyntaxBase("故事情节"), parent_parser(pparser)
|
||
|
{
|
||
|
auto rule = addRule("进入情节解析", 0, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
auto nmidx = cnt - 1, defidx = cnt - 2;;
|
||
|
|
||
|
auto parent_parser = nodeStoryUnitParser();
|
||
|
auto node = new NodeStoryFragment(static_cast<NodeStoryUnit*>(parent_parser->currNode()), seqs[nmidx].Text);
|
||
|
this->refocusNode(node);
|
||
|
|
||
|
auto word0 = new Words(node, docRef(), seqs[defidx].Text, seqs[defidx].StartRow, seqs[defidx].StartCol);
|
||
|
auto word1 = new Words(node, docRef(), seqs[nmidx].Text, seqs[nmidx].StartRow, seqs[nmidx].StartCol);
|
||
|
docRef()->append(word0);
|
||
|
docRef()->append(word1);
|
||
|
|
||
|
return ParseResult::SelfManipulate;
|
||
|
});
|
||
|
|
||
|
rule->addExpression("基础情节解析", { Elm("{情节定义}"), Elm("描述文本", true) });
|
||
|
rule->addExpression("拓展情节解析", { Elm("{换行符}"),Elm("{换行符}"), Exp("基础情节解析", true) });
|
||
|
|
||
|
rule = addRule("情节成分解析", 1, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
auto node = currNode();
|
||
|
|
||
|
auto word = new Words(node, docRef(), seqs[cnt - 1].Text, seqs[cnt - 1].StartRow, seqs[cnt - 1].StartCol);
|
||
|
docRef()->append(word);
|
||
|
|
||
|
return ParseResult::EnterNext;
|
||
|
});
|
||
|
rule->addExpression("基础情节成分解析", { Elm("{左界限}", true) });
|
||
|
rule->addExpression("拓展情节成分解析", { Elm("{换行符}"), Elm("{换行符}"), Exp("基础情节成分解析", true) });
|
||
|
|
||
|
rule = addRule("跳出情节成分解析", 2, [this](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
auto node = currNode();
|
||
|
|
||
|
auto word = new Words(node, docRef(), seqs[cnt - 1].Text, seqs[cnt - 1].StartRow, seqs[cnt - 1].StartCol);
|
||
|
docRef()->append(word);
|
||
|
|
||
|
return ParseResult::Completed;
|
||
|
});
|
||
|
rule->addExpression("基础跳出情节解析", { Elm("{右界限}", true) });
|
||
|
rule->addExpression("拓展跳出情节解析", { Elm("{换行符}"),Elm("{换行符}"),Exp("基础跳出情节解析") });
|
||
|
}
|
||
|
|
||
|
NodeStoryUnitParser * NodeStoryFragmentParser::nodeStoryUnitParser() const
|
||
|
{
|
||
|
return parent_parser;
|
||
|
}
|
||
|
|
||
|
DocCore * NodeStoryFragmentParser::docRef() const
|
||
|
{
|
||
|
return parent_parser->docRef();
|
||
|
}
|
||
|
|
||
|
NodeStoryUnitDesGroupParser::NodeStoryUnitDesGroupParser(XSyntaxBase * parent)
|
||
|
: XSyntaxBase("描述块"), parent_parser(parent)
|
||
|
{
|
||
|
auto rule = addRule("进入描述块解析", 0, [](const QList<LexResult> &seqs, int cnt)->ParseResult {
|
||
|
return ParseResult::EnterNext;
|
||
|
});
|
||
|
rule->addExpression("::基础引用定义", { Elm("{节点引用}"),Elm("{描述文本}1"), Elm("{描述文本}2"), Elm("{右界限}", true) });
|
||
|
rule->addExpression("::基础文本块定义", {Elm("{描述文本}"), Elm("{描述文本}", true) });
|
||
|
rule->addExpression("::文本块混合定义x", { Exp("::基础引用定义"), Exp("::基础引用定义", true), Exp("::基础文本块定义", true), Exp("::基础引用定义")});
|
||
|
rule->addExpression("::拓展文本块x", { Elm("{换行符}"),Elm("{换行符}"), Exp("::基础引用定义")});
|
||
|
rule->addExpression("::文本块混合定义y", {Exp("::文本块混合定义"), Exp("::文本块混合定义x", true)} );
|
||
|
rule->addExpression("::文本块混合定义y", {Exp("::文本块混合定义"), Exp("::文本块混合定义x", true)} );
|
||
|
|
||
|
|
||
|
rule->resetFinal(MatchStatus::OnlyForm);
|
||
|
}
|