This commit is contained in:
codeboss 2025-02-15 12:57:53 +08:00
parent c4c1dc5775
commit 1744a705bd
9 changed files with 214 additions and 113 deletions

View File

@ -16,20 +16,16 @@ int main(int argc, char *argv[])
lib_words::WordReader reader; lib_words::WordReader reader;
auto vwords = reader.wordsFrom(tt, "D:\\Projects\\Cpp\\WsNovelParser\\CoreTest\\syntax_example.txt"); auto vwords = reader.wordsFrom(tt, "D:\\Projects\\Cpp\\WsNovelParser\\CoreTest\\syntax_example.txt");
auto words = vwords;
while (words) {
qDebug() << words->content();
words = words->nextWord();
}
ast_gen::SyntaxParser parser(example_novel::NovalSyntax::getSyntaxTree()); ast_gen::SyntaxParser parser(example_novel::NovalSyntax::getSyntaxTree());
auto rst = parser.parse(vwords); auto rst = parser.parse(vwords);
for(auto x : rst)
qDebug() << x->parseSyntax();
auto prag_root = std::make_shared< ast_basic::ExprProgram>("HelloWorld!"); auto prag_root = std::make_shared< ast_basic::ExprProgram>("HelloWorld!");
auto structx = parser.getAst(rst.first(), prag_root); auto structx = parser.getAst(rst.first(), prag_root);
parser.astPresent(structx);
qDebug() << "===========finished==========================";
qDebug() << "finished";
return a.exec(); return a.exec();
} }

View File

@ -5,4 +5,11 @@
} }
{ケハハツ ケハハツテ﨤ニ2 {ケハハツ ケハハツテ﨤ニ2
¹<><C2B9><EFBFBD>½י<C2BD><D799><EFBFBD><C2B6>ה} ¹<><C2B9><EFBFBD>½י<C2BD><D799><EFBFBD><C2B6>ה}
<><C2B9><EFBFBD> ¹<><C2B9><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>3
¹<><C2B9><EFBFBD>½י<C2BD><D799><EFBFBD><C2B6>ה aldkfjl flwief}
<><C2B9><EFBFBD> ¹<><C2B9><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>4
¹<><C2B9><EFBFBD>½י<C2BD><D799><EFBFBD><C2B6>ה aldkfjl flwief
}

View File

@ -13,11 +13,7 @@ QList<std::shared_ptr<const MatchCursor>> SyntaxParser::parse(std::shared_ptr<co
auto cursor = std::make_shared<MatchCursor>(words->file()); auto cursor = std::make_shared<MatchCursor>(words->file());
cursor->setCurrent(nullptr, words); cursor->setCurrent(nullptr, words);
QList<std::shared_ptr<const MatchCursor>> out; auto list = this->_rule_bind->parse(cursor);
auto list = this->_rule_bind->parse(cursor, out);
if (out.size())
list = out;
QHash<QString, std::shared_ptr<const MatchCursor>> result_map; QHash<QString, std::shared_ptr<const MatchCursor>> result_map;
std::for_each(list.begin(), list.end(), std::for_each(list.begin(), list.end(),
@ -28,8 +24,8 @@ QList<std::shared_ptr<const MatchCursor>> SyntaxParser::parse(std::shared_ptr<co
list = result_map.values(); list = result_map.values();
std::sort(list.begin(), list.end(), std::sort(list.begin(), list.end(),
[&](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) { [&](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) {
auto mark_a = a->currentToken()->position() * 100 - a->totalErrorCount(); auto mark_a = a->token()->position() * 100 - a->totalErrorCount();
auto mark_b = b->currentToken()->position() * 100 - b->totalErrorCount(); auto mark_b = b->token()->position() * 100 - b->totalErrorCount();
return mark_a > mark_b; return mark_a > mark_b;
}); });
@ -41,7 +37,7 @@ std::shared_ptr<IExprInstance> ast_gen::SyntaxParser::getAst(
std::shared_ptr<const lib_syntax::MatchCursor> cursor, std::shared_ptr<IExprInstance> root) { std::shared_ptr<const lib_syntax::MatchCursor> cursor, std::shared_ptr<IExprInstance> root) {
QList<std::shared_ptr<const IActionToken>> token_seqs; QList<std::shared_ptr<const IActionToken>> token_seqs;
std::shared_ptr<const IActionToken> action_token = cursor->currentToken(); std::shared_ptr<const IActionToken> action_token = cursor->token();
while (action_token) { while (action_token) {
token_seqs.prepend(action_token); token_seqs.prepend(action_token);
action_token = action_token->prevToken(); action_token = action_token->prevToken();
@ -54,3 +50,14 @@ std::shared_ptr<IExprInstance> ast_gen::SyntaxParser::getAst(
return expr_inst; return expr_inst;
} }
void ast_gen::SyntaxParser::astPresent(std::shared_ptr<const ast_basic::IExprInstance> node, int depth) {
auto msg = QString(depth * 4, ' ');
if (node->definedRule())
qDebug() << msg + node->definedRule()->name();
else
qDebug() << msg + "Program";
for (auto it : node->children())
astPresent(it, depth + 1);
}

View File

@ -32,6 +32,8 @@ namespace ast_gen {
std::shared_ptr<ast_basic::IExprInstance> getAst( std::shared_ptr<ast_basic::IExprInstance> getAst(
std::shared_ptr<const lib_syntax::MatchCursor> cursor, std::shared_ptr<ast_basic::IExprInstance> root); std::shared_ptr<const lib_syntax::MatchCursor> cursor, std::shared_ptr<ast_basic::IExprInstance> root);
void astPresent(std::shared_ptr<const ast_basic::IExprInstance> node, int depth = 0);
}; };

View File

@ -15,26 +15,28 @@ QList<std::shared_ptr<const IBasicRule>> __anyone_impl::children() const {
return mbrs_store; return mbrs_store;
} }
QList<std::shared_ptr<const MatchCursor>> __anyone_impl::parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const { QList<std::shared_ptr<const MatchCursor>> __anyone_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
if (cursor->parseStop()) if (cursor->parseFailure() && cursor->parseComplete())
return QList<std::shared_ptr<const MatchCursor>>() << cursor; return QList<std::shared_ptr<const MatchCursor>>() << cursor;
auto syntax = present(); auto syntax = present();
QList<std::shared_ptr<const MatchCursor>> result_list; QList<std::shared_ptr<const MatchCursor>> result_list;
for (auto rx : this->children()) for (auto rx : this->children())
result_list.append(rx->parse(cursor, out)); result_list.append(rx->parse(cursor));
// 完全匹配分支 // 完全匹配分支
decltype(result_list) completely_list; decltype(result_list) completely_list;
std::copy_if(result_list.begin(), result_list.end(), std::back_inserter(completely_list), std::copy_if(result_list.begin(), result_list.end(), std::back_inserter(completely_list),
[&](std::shared_ptr<const MatchCursor> ins) { return cursor->totalErrorCount() == ins->totalErrorCount(); }); [&](std::shared_ptr<const MatchCursor> ins) {
return cursor->totalErrorCount() == ins->totalErrorCount() || ins->parseComplete();
});
if (completely_list.size()) if (completely_list.size())
return completely_list; return completely_list;
// 经过修正的分支 // 经过修正的分支
decltype(result_list) modify_list; decltype(result_list) modify_list;
std::copy_if(result_list.begin(), result_list.end(), std::back_inserter(modify_list), std::copy_if(result_list.begin(), result_list.end(), std::back_inserter(modify_list),
[&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); }); [&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseFailure(); });
if (modify_list.size()) if (modify_list.size())
return modify_list; return modify_list;
@ -55,16 +57,16 @@ QList<std::shared_ptr<const IBasicRule>> __sequence_impl::children() const {
return mbrs_store; return mbrs_store;
} }
QList<std::shared_ptr<const MatchCursor>> __sequence_impl::parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const { QList<std::shared_ptr<const MatchCursor>> __sequence_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
if (cursor->parseStop()) if (cursor->parseFailure() && cursor->parseComplete())
return QList<std::shared_ptr<const MatchCursor>>() << cursor; return QList<std::shared_ptr<const MatchCursor>>() << cursor;
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor }; QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
for (auto rule : this->children()) { for (auto rule : this->children()) {
QList<std::shared_ptr<const MatchCursor>> current_result; QList<std::shared_ptr<const MatchCursor>> current_result;
for (auto vcurs : bridge_list) { for (auto vcurs : bridge_list) {
if (!vcurs->parseStop()) { if (!vcurs->parseFailure()) {
current_result.append(rule->parse(vcurs, out)); current_result.append(rule->parse(vcurs));
} }
} }
@ -72,7 +74,7 @@ QList<std::shared_ptr<const MatchCursor>> __sequence_impl::parse(std::shared_ptr
decltype(current_result) temprary_list; decltype(current_result) temprary_list;
std::copy_if(current_result.begin(), current_result.end(), std::copy_if(current_result.begin(), current_result.end(),
std::back_inserter(temprary_list), [&](std::shared_ptr<const MatchCursor> ins) { std::back_inserter(temprary_list), [&](std::shared_ptr<const MatchCursor> ins) {
return cursor->totalErrorCount() == ins->totalErrorCount(); return cursor->totalErrorCount() == ins->totalErrorCount() || ins->parseComplete();
}); });
if (temprary_list.size()) { if (temprary_list.size()) {
bridge_list = temprary_list; bridge_list = temprary_list;
@ -82,7 +84,7 @@ QList<std::shared_ptr<const MatchCursor>> __sequence_impl::parse(std::shared_ptr
// 经过修复的分支 // 经过修复的分支
std::copy_if(current_result.begin(), current_result.end(), std::back_inserter(temprary_list), std::copy_if(current_result.begin(), current_result.end(), std::back_inserter(temprary_list),
[&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); }); [&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseFailure(); });
if (temprary_list.size()) { if (temprary_list.size()) {
bridge_list = temprary_list; bridge_list = temprary_list;
continue; continue;
@ -121,8 +123,8 @@ auto content_extractw = [](std::shared_ptr<const lib_token::IActionToken> token)
return content; return content;
}; };
QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const { QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
if (cursor->parseStop()) if (cursor->parseFailure() && cursor->parseComplete())
return QList<std::shared_ptr<const MatchCursor>>() << cursor; return QList<std::shared_ptr<const MatchCursor>>() << cursor;
auto syntax = present(); auto syntax = present();
@ -134,19 +136,19 @@ QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<c
temp_rules << this->rule_peer; temp_rules << this->rule_peer;
auto seqs_rule = std::make_shared<__sequence_impl>(temp_rules); auto seqs_rule = std::make_shared<__sequence_impl>(temp_rules);
max_match_begin = seqs_rule->parse(cursor, out); max_match_begin = seqs_rule->parse(cursor);
} }
// 如果不满足最小重复匹配次数要求,则返回 // 如果不满足最小重复匹配次数要求,则返回
int continue_count = std::count_if(max_match_begin.begin(), max_match_begin.end(), int continue_count = std::count_if(max_match_begin.begin(), max_match_begin.end(),
[](std::shared_ptr<const MatchCursor > ins) { return !ins->parseStop(); }); [](std::shared_ptr<const MatchCursor > ins) { return !ins->parseFailure(); });
if (!continue_count) if (!continue_count)
return max_match_begin; return max_match_begin;
// 最小匹配次数中所有错误分支都是无用的、需要舍弃 // 最小匹配次数中所有错误分支都是无用的、需要舍弃
for (auto idx = 0; idx < max_match_begin.size(); ++idx) { for (auto idx = 0; idx < max_match_begin.size(); ++idx) {
auto current_cursor = max_match_begin.at(idx); auto current_cursor = max_match_begin.at(idx);
if (current_cursor->parseStop()) if (current_cursor->parseFailure())
max_match_begin.removeAt(idx--); max_match_begin.removeAt(idx--);
} }
@ -158,18 +160,18 @@ QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<c
// 匹配迭代一次 // 匹配迭代一次
for (auto ins : bridge_list) for (auto ins : bridge_list)
current_list.append(this->rule_peer->parse(ins, out)); current_list.append(this->rule_peer->parse(ins));
QList<QString> contents; QList<QString> contents;
for (auto bx : current_list) for (auto bx : current_list)
contents << content_extractw(bx->currentToken()) + QStringList(bx->totalErrors()).join(','); contents << content_extractw(bx->token()) + QStringList(bx->totalErrors()).join(',');
// 提取完全匹配的分支 // 提取完全匹配的分支
QList<std::shared_ptr<const MatchCursor>> temprary_branchs; QList<std::shared_ptr<const MatchCursor>> temprary_branchs;
std::copy_if(current_list.begin(), current_list.end(), std::copy_if(current_list.begin(), current_list.end(),
std::back_inserter(temprary_branchs), [&](std::shared_ptr<const MatchCursor> ins) { std::back_inserter(temprary_branchs), [&](std::shared_ptr<const MatchCursor> ins) {
return cursor->totalErrorCount() == ins->totalErrorCount() && return ins->parseComplete() || (cursor->totalErrorCount() == ins->totalErrorCount() &&
ins->currentToken()->position() > cursor->currentToken()->position(); ins->token()->position() > cursor->token()->position());
}); });
if (temprary_branchs.size()) { if (temprary_branchs.size()) {
bridge_list = temprary_branchs; bridge_list = temprary_branchs;
@ -179,7 +181,7 @@ QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<c
// 提取语法修正分支 // 提取语法修正分支
std::copy_if(current_list.begin(), current_list.end(), std::copy_if(current_list.begin(), current_list.end(),
std::back_inserter(temprary_branchs), [&](std::shared_ptr<const MatchCursor> ins) { std::back_inserter(temprary_branchs), [&](std::shared_ptr<const MatchCursor> ins) {
return !ins->parseStop() && ins->currentToken()->position() > cursor->currentToken()->position(); return !ins->parseFailure() && ins->token()->position() > cursor->token()->position();
}); });
if (temprary_branchs.size()) { if (temprary_branchs.size()) {
bridge_list = temprary_branchs; bridge_list = temprary_branchs;
@ -191,7 +193,7 @@ QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<c
results.append(bridge_list); results.append(bridge_list);
std::sort(results.begin(), results.end(), std::sort(results.begin(), results.end(),
[](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) { [](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) {
return a->currentToken()->position() > b->currentToken()->position(); return a->token()->position() > b->token()->position();
}); });
// 提取完全匹配的分支 // 提取完全匹配的分支
@ -201,27 +203,32 @@ QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<c
if (!rets_completely.size()) { if (!rets_completely.size()) {
rets_completely.append(ins); rets_completely.append(ins);
} }
else if (rets_completely.last()->currentToken()->position() == ins->currentToken()->position()) { else if (rets_completely.last()->token()->position() == ins->token()->position()) {
rets_completely.append(ins); rets_completely.append(ins);
} }
} }
else if(ins->parseComplete())
rets_completely.append(ins);
} }
// 提取经过修正的分支 // 提取经过修正的分支
decltype(results) rets_modified; decltype(results) rets_modified;
for (auto ins : results) { for (auto ins : results) {
if (!ins->parseStop()) { if (!ins->parseFailure()) {
if (!rets_modified.size()) { if (!rets_modified.size()) {
rets_modified.append(ins); rets_modified.append(ins);
} }
else if (rets_modified.last()->currentToken()->position() == ins->currentToken()->position()) { else if (rets_modified.last()->token()->position() == ins->token()->position()) {
rets_modified.append(ins); rets_modified.append(ins);
} }
} }
} }
// 允许持续的集合 // 允许持续的集合
rets_completely.append(rets_modified); for (auto rst : rets_modified)
if (!rets_completely.contains(rst))
rets_completely.append(rst);
if (rets_completely.size()) if (rets_completely.size())
return rets_completely; return rets_completely;
@ -284,8 +291,20 @@ QString lib_syntax::MatchCursor::parseSyntax() const {
if (!this->previous()) if (!this->previous())
return QString(); return QString();
QString token_split = this->currentToken()->defines() ? this->currentToken()->defines()->regex() : ""; QString token_splitx;
return this->previous()->parseSyntax() + " " + token_split; switch (this->token()->tokenType()) {
case lib_token::IActionToken::Type::ElementBegin:
token_splitx = "<B>";
break;
case lib_token::IActionToken::Type::TokenBind:
token_splitx = this->token()->defines()->regex();
break;
case lib_token::IActionToken::Type::ElementEnd:
token_splitx = "<E>";
break;
}
return this->previous()->parseSyntax() + " " + token_splitx;
} }
void MatchCursor::enterExprs() { void MatchCursor::enterExprs() {
@ -298,19 +317,27 @@ void MatchCursor::logExprsError(const QString& msg) {
this->_exprs_errors.last()->addError(msg); this->_exprs_errors.last()->addError(msg);
// 普适性质的判定标准 // 普适性质的判定标准
this->setStop(this->exprsErrorCount() > 1); this->setFailure(this->exprsErrorCount() > 1);
} }
void MatchCursor::quitExprs() { void MatchCursor::quitExprs() {
this->_exprs_errors.pop_back(); this->_exprs_errors.pop_back();
} }
bool lib_syntax::MatchCursor::parseStop() const { bool lib_syntax::MatchCursor::parseFailure() const {
return this->_parse_proc_stop; return this->_parse_stop_with_errors;
} }
void lib_syntax::MatchCursor::setStop(bool mark) { void lib_syntax::MatchCursor::setFailure(bool mark) {
this->_parse_proc_stop = mark; this->_parse_stop_with_errors = mark;
}
bool lib_syntax::MatchCursor::parseComplete() const {
return this->_parse_complete;
}
void lib_syntax::MatchCursor::setComplete(bool mark) {
this->_parse_complete = mark;
} }
int MatchCursor::exprsErrorCount() const { int MatchCursor::exprsErrorCount() const {
@ -332,11 +359,11 @@ void MatchCursor::setCurrent(std::shared_ptr<const IActionToken> t, std::shared_
this->_remains_word = remains; this->_remains_word = remains;
} }
std::shared_ptr<const IActionToken> MatchCursor::currentToken() const { std::shared_ptr<const IActionToken> MatchCursor::token() const {
return this->_current_token; return this->_current_token;
} }
std::shared_ptr<const IPrimitiveWord> MatchCursor::currentWords() const { std::shared_ptr<const IPrimitiveWord> MatchCursor::words() const {
return this->_remains_word; return this->_remains_word;
} }

View File

@ -59,22 +59,23 @@ namespace lib_syntax {
virtual void enterExprs(); virtual void enterExprs();
virtual void logExprsError(const QString& msg); virtual void logExprsError(const QString& msg);
virtual void quitExprs(); virtual void quitExprs();
virtual bool parseStop() const;
virtual void setStop(bool mark=true);
virtual int exprsErrorCount() const; virtual int exprsErrorCount() const;
virtual int totalErrorCount() const; virtual int totalErrorCount() const;
virtual QList<QString> totalErrors() const; virtual QList<QString> totalErrors() const;
virtual bool parseFailure() const;
virtual void setFailure(bool mark = true);
virtual bool parseComplete() const;
virtual void setComplete(bool mark = true);
virtual void setCurrent(std::shared_ptr<const lib_token::IActionToken> t, std::shared_ptr<const lib_words::IPrimitiveWord> remains); virtual void setCurrent(std::shared_ptr<const lib_token::IActionToken> t, std::shared_ptr<const lib_words::IPrimitiveWord> remains);
virtual std::shared_ptr<const lib_token::IActionToken> currentToken() const; virtual std::shared_ptr<const lib_token::IActionToken> token() const;
virtual std::shared_ptr<const lib_words::IPrimitiveWord> currentWords() const; virtual std::shared_ptr<const lib_words::IPrimitiveWord> words() const;
private: private:
QString _file_path; QString _file_path;
std::shared_ptr<const MatchCursor> _prev_cursor = nullptr; std::shared_ptr<const MatchCursor> _prev_cursor = nullptr;
bool _parse_proc_stop = false; bool _parse_stop_with_errors = false, _parse_complete = false;
QList<QString> _total_errors; // 所有解析错误 QList<QString> _total_errors; // 所有解析错误
QList<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误 QList<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误
@ -106,7 +107,7 @@ namespace lib_syntax {
* @param out * @param out
* @return <> * @return <>
*/ */
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>> &out) const = 0; virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
}; };
// 组合语法实体解析 ===================================================================================== // 组合语法实体解析 =====================================================================================
@ -123,7 +124,7 @@ namespace lib_syntax {
// IBasicRule interface // IBasicRule interface
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override; virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
virtual QString present() const override; virtual QString present() const override;
}; };
@ -140,7 +141,7 @@ namespace lib_syntax {
// IBasicRule interface // IBasicRule interface
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override; virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
virtual QString present() const override; virtual QString present() const override;
}; };
@ -158,7 +159,7 @@ namespace lib_syntax {
// IBasicRule interface // IBasicRule interface
public: public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override; virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override; virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
virtual QString present() const override; virtual QString present() const override;
}; };
@ -173,7 +174,7 @@ namespace lib_syntax {
virtual int typeMark() const; virtual int typeMark() const;
protected: protected:
virtual QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const = 0; virtual QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
private: private:
QString name_store; QString name_store;
@ -199,19 +200,27 @@ namespace lib_syntax {
virtual QString present() const override { virtual QString present() const override {
return QString("%1").arg(this->_define_peers->reviseWords()); return QString("%1").arg(this->_define_peers->reviseWords());
} }
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> current, QList<std::shared_ptr<const MatchCursor>>& out) const override { virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> current) const override {
auto w_this = current->currentWords();
// 如果提前结束,记录错误并返回 // 如果提前结束,记录错误并返回
if (!w_this) { if (!current->words()) {
if(current->currentToken()->defines()) // 只有在表达式的起始点遇到nullptr才是正常结束。
out << current; if (current->token()->tokenType() != lib_token::IActionToken::Type::ElementBegin) {
else auto ncurs = std::make_shared<MatchCursor>(current);
out << current->previous(); ncurs->logExprsError(QString("Syntax[0x00001]输入错误,程序提前结束:%1。").arg(current->filePath()));
ncurs->setFailure();
return QList<std::shared_ptr<const MatchCursor>>() << ncurs;
}
return QList<std::shared_ptr<const MatchCursor>>(); while (current->token()->tokenType() == lib_token::IActionToken::Type::ElementBegin)
current = current->previous();
auto ncurs = std::make_shared<MatchCursor>(current);
ncurs->setComplete();
return QList<std::shared_ptr<const MatchCursor>>() << ncurs;
} }
auto t_this = current->currentToken(); auto w_this = current->words();
auto t_this = current->token();
auto match_result = _define_peers->analysis(w_this); auto match_result = _define_peers->analysis(w_this);
// 解析成功 // 解析成功
if (std::get<0>(match_result)) { if (std::get<0>(match_result)) {
@ -247,8 +256,8 @@ namespace lib_syntax {
retvals << error_one; retvals << error_one;
// 多一个 // 多一个
if (w_this->nextWord()) { auto nx_word = w_this->nextWord();
auto nx_word = w_this->nextWord(); if (nx_word) {
auto nx_result = this->_define_peers->analysis(nx_word); auto nx_result = this->_define_peers->analysis(nx_word);
if (std::get<0>(nx_result)) { if (std::get<0>(nx_result)) {
auto chain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(std::get<0>(nx_result), t_this); auto chain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(std::get<0>(nx_result), t_this);
@ -291,15 +300,15 @@ namespace lib_syntax {
return this->_children_store->present(); return this->_children_store->present();
} }
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override {
// 提前结束,直接返回 // 提前结束,直接返回
if (cursor->parseStop()) if (cursor->parseFailure() && cursor->parseComplete())
return QList<std::shared_ptr<const MatchCursor>>() << cursor; return QList<std::shared_ptr<const MatchCursor>>() << cursor;
auto syntax = present(); auto syntax = present();
auto t_this = cursor->currentToken(); auto t_this = cursor->token();
auto w_this = cursor->currentWords(); auto w_this = cursor->words();
// 起始Token打点 // 起始Token打点
auto split_begin = std::make_shared<lib_token::ExprBeginToken<ExprType>>(shared_from_this(), t_this); auto split_begin = std::make_shared<lib_token::ExprBeginToken<ExprType>>(shared_from_this(), t_this);
@ -308,21 +317,21 @@ namespace lib_syntax {
ncursor->enterExprs(); ncursor->enterExprs();
// 表达式语法解析 // 表达式语法解析
auto nbranch = this->expr_rule_parse(ncursor, out); auto nbranch = this->expr_rule_parse(ncursor);
// 语法完全匹配的分支 // 语法完全匹配的分支
decltype(nbranch) branch_procs; decltype(nbranch) branch_procs;
std::copy_if(nbranch.begin(), nbranch.end(), std::copy_if(nbranch.begin(), nbranch.end(),
std::back_inserter(branch_procs), std::back_inserter(branch_procs),
[&](std::shared_ptr<const MatchCursor> ins) { [&](std::shared_ptr<const MatchCursor> ins) {
return ins->totalErrorCount() == cursor->totalErrorCount(); return ins->totalErrorCount() == cursor->totalErrorCount() || ins->parseComplete();
}); });
// 语法修正后能匹配的分支 // 语法修正后能匹配的分支
if (!branch_procs.size()) { if (!branch_procs.size()) {
std::copy_if(nbranch.begin(), nbranch.end(), std::copy_if(nbranch.begin(), nbranch.end(),
std::back_inserter(branch_procs), std::back_inserter(branch_procs),
[](std::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); }); [](std::shared_ptr<const MatchCursor> ins) { return !ins->parseFailure(); });
} }
if (!branch_procs.size()) if (!branch_procs.size())
@ -331,11 +340,11 @@ namespace lib_syntax {
decltype(nbranch) results_fnl; decltype(nbranch) results_fnl;
for (auto curs : branch_procs) { for (auto curs : branch_procs) {
if (curs->parseStop()) if (curs->parseFailure())
results_fnl.append(curs); results_fnl.append(curs);
else { else {
auto t_end = curs->currentToken(); auto t_end = curs->token();
auto w_end = curs->currentWords(); auto w_end = curs->words();
auto ecursor = std::make_shared<MatchCursor>(curs); auto ecursor = std::make_shared<MatchCursor>(curs);
ecursor->quitExprs(); ecursor->quitExprs();

View File

@ -84,7 +84,13 @@ namespace lib_token {
*/ */
class IActionToken : public lib_token::IToken { class IActionToken : public lib_token::IToken {
public: public:
enum class Type { ElementBegin, TokenBind, ElementEnd };
/**
* @brief ActionToken类型.
*
* \return
*/
virtual Type tokenType() const = 0;
/** /**
* @brief Token * @brief Token
* @return * @return

View File

@ -59,19 +59,57 @@ auto content_extract = [](std::shared_ptr<const lib_token::IActionToken> token)
return content; return content;
}; };
using TextDeclsSyntaxDef = Any<Match<Numbers>, Match<NormalText>, Match<ReferMk>, Match<SplitMk>>; using TextDeclsSyntaxDef = Any<Match<Numbers>, Match<NormalText>>;
class DeclSyntax : public ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef> { class DeclSyntax : public ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef> {
public: public:
DeclSyntax() : ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef>("decl_section") { } DeclSyntax() : ElementRule<TextSection, (int) NovelNode::TextSection, TextDeclsSyntaxDef>("decl_section") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
auto syntax_text = this->present(); //// 如果提前结束,记录错误并返回
auto current_rst = content_extract(cursor->currentToken()); //if (!cursor->words()) {
auto rst = _children_store->parse(cursor, out); // while (cursor->token()->tokenType() == lib_token::IActionToken::Type::ElementBegin)
for (auto r : rst) // cursor = cursor->previous();
std::const_pointer_cast<MatchCursor>(r)->setStop(r->exprsErrorCount());
return rst; // out << cursor;
// return QList<std::shared_ptr<const MatchCursor>>();
//}
//// 绑定文字行
//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, out);
// 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;
} }
}; };
@ -85,8 +123,8 @@ public:
PointSyntax() : ElementRule<PointDefines, (int) NovelNode::PointDefines, PointSyntaxDef>("point_define") { } PointSyntax() : ElementRule<PointDefines, (int) NovelNode::PointDefines, PointSyntaxDef>("point_define") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };
@ -100,8 +138,8 @@ public:
ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef>("point_refer") { } ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef>("point_refer") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };
@ -115,8 +153,8 @@ public:
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef>("slice_define") { } SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef>("slice_define") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };
@ -130,14 +168,14 @@ public:
StorySyntax() : ElementRule<StoryDefine, (int) NovelNode::StoryDefine, StorySyntaxDef>("story_define") { } StorySyntax() : ElementRule<StoryDefine, (int) NovelNode::StoryDefine, StorySyntaxDef>("story_define") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
auto syntax = this->present(); auto syntax = this->present();
auto current_rst = content_extract(cursor->currentToken()); auto current_rst = content_extract(cursor->token());
auto rst = _children_store->parse(cursor, out); auto rst = _children_store->parse(cursor);
QString result_list; QString result_list;
for (auto c : rst) { for (auto c : rst) {
result_list += content_extract(c->currentToken()) += "\n"; result_list += content_extract(c->token()) += "\n";
} }
return rst; return rst;
@ -154,8 +192,8 @@ public:
ArticleSyntax() : ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine, ArticleSyntaxDef>("article_define") { } ArticleSyntax() : ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine, ArticleSyntaxDef>("article_define") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };
@ -169,8 +207,8 @@ public:
VolumeSyntax() : ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine, VolumeSyntaxDef>("volume_define") { } VolumeSyntax() : ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine, VolumeSyntaxDef>("volume_define") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };
@ -182,8 +220,8 @@ public:
RankSyntax() : ElementRule<RankDeclare, (int) NovelNode::RankDeclaration, RankSyntaxDef>("rank_define") { } RankSyntax() : ElementRule<RankDeclare, (int) NovelNode::RankDeclaration, RankSyntaxDef>("rank_define") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };
@ -195,9 +233,9 @@ public:
DocumentSyntax() : ElementRule<Document, (int) NovelNode::Document, DocSyntaxDef>("decls-doc") { } DocumentSyntax() : ElementRule<Document, (int) NovelNode::Document, DocSyntaxDef>("decls-doc") { }
// 通过 ElementRule 继承 // 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>>& out) const override { QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
auto text = _children_store->present(); auto text = _children_store->present();
return _children_store->parse(cursor, out); return _children_store->parse(cursor);
} }
}; };

View File

@ -65,6 +65,9 @@ namespace lib_token {
} }
// 通过 IActionToken 继承 // 通过 IActionToken 继承
virtual Type tokenType() const {
return Type::TokenBind;
}
QString file() const override { QString file() const override {
return _bind_content->file(); return _bind_content->file();
} }
@ -109,6 +112,9 @@ namespace lib_token {
} }
// 通过 IActionToken 继承 // 通过 IActionToken 继承
virtual Type tokenType() const {
return Type::ElementBegin;
}
QString file() const override { QString file() const override {
return this->prevToken()->file(); return this->prevToken()->file();
} }
@ -165,6 +171,9 @@ namespace lib_token {
: _prev_token(prev), _self_start(start) { } : _prev_token(prev), _self_start(start) { }
// 通过 IActionToken 继承 // 通过 IActionToken 继承
virtual Type tokenType() const {
return Type::ElementEnd;
}
QString file() const override { QString file() const override {
return this->prevToken()->file(); return this->prevToken()->file();
} }