update
This commit is contained in:
parent
c4c1dc5775
commit
1744a705bd
|
@ -16,20 +16,16 @@ int main(int argc, char *argv[])
|
|||
lib_words::WordReader reader;
|
||||
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());
|
||||
auto rst = parser.parse(vwords);
|
||||
|
||||
for(auto x : rst)
|
||||
qDebug() << x->parseSyntax();
|
||||
|
||||
auto prag_root = std::make_shared< ast_basic::ExprProgram>("HelloWorld!");
|
||||
auto structx = parser.getAst(rst.first(), prag_root);
|
||||
parser.astPresent(structx);
|
||||
|
||||
|
||||
|
||||
qDebug() << "finished";
|
||||
qDebug() << "===========finished==========================";
|
||||
return a.exec();
|
||||
}
|
||||
|
|
|
@ -5,4 +5,11 @@
|
|||
}
|
||||
|
||||
{ケハハツ ケハハツテ﨤ニ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
|
||||
}
|
|
@ -13,11 +13,7 @@ QList<std::shared_ptr<const MatchCursor>> SyntaxParser::parse(std::shared_ptr<co
|
|||
auto cursor = std::make_shared<MatchCursor>(words->file());
|
||||
cursor->setCurrent(nullptr, words);
|
||||
|
||||
QList<std::shared_ptr<const MatchCursor>> out;
|
||||
auto list = this->_rule_bind->parse(cursor, out);
|
||||
|
||||
if (out.size())
|
||||
list = out;
|
||||
auto list = this->_rule_bind->parse(cursor);
|
||||
|
||||
QHash<QString, std::shared_ptr<const MatchCursor>> result_map;
|
||||
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();
|
||||
std::sort(list.begin(), list.end(),
|
||||
[&](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) {
|
||||
auto mark_a = a->currentToken()->position() * 100 - a->totalErrorCount();
|
||||
auto mark_b = b->currentToken()->position() * 100 - b->totalErrorCount();
|
||||
auto mark_a = a->token()->position() * 100 - a->totalErrorCount();
|
||||
auto mark_b = b->token()->position() * 100 - b->totalErrorCount();
|
||||
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) {
|
||||
|
||||
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) {
|
||||
token_seqs.prepend(action_token);
|
||||
action_token = action_token->prevToken();
|
||||
|
@ -54,3 +50,14 @@ std::shared_ptr<IExprInstance> ast_gen::SyntaxParser::getAst(
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -32,6 +32,8 @@ namespace ast_gen {
|
|||
std::shared_ptr<ast_basic::IExprInstance> getAst(
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -15,26 +15,28 @@ QList<std::shared_ptr<const IBasicRule>> __anyone_impl::children() const {
|
|||
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 {
|
||||
if (cursor->parseStop())
|
||||
QList<std::shared_ptr<const MatchCursor>> __anyone_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
||||
if (cursor->parseFailure() && cursor->parseComplete())
|
||||
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
||||
|
||||
auto syntax = present();
|
||||
QList<std::shared_ptr<const MatchCursor>> result_list;
|
||||
for (auto rx : this->children())
|
||||
result_list.append(rx->parse(cursor, out));
|
||||
result_list.append(rx->parse(cursor));
|
||||
|
||||
// 完全匹配分支
|
||||
decltype(result_list) 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())
|
||||
return completely_list;
|
||||
|
||||
// 经过修正的分支
|
||||
decltype(result_list) 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())
|
||||
return modify_list;
|
||||
|
||||
|
@ -55,16 +57,16 @@ QList<std::shared_ptr<const IBasicRule>> __sequence_impl::children() const {
|
|||
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 {
|
||||
if (cursor->parseStop())
|
||||
QList<std::shared_ptr<const MatchCursor>> __sequence_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
||||
if (cursor->parseFailure() && cursor->parseComplete())
|
||||
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
||||
|
||||
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
|
||||
for (auto rule : this->children()) {
|
||||
QList<std::shared_ptr<const MatchCursor>> current_result;
|
||||
for (auto vcurs : bridge_list) {
|
||||
if (!vcurs->parseStop()) {
|
||||
current_result.append(rule->parse(vcurs, out));
|
||||
if (!vcurs->parseFailure()) {
|
||||
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;
|
||||
std::copy_if(current_result.begin(), current_result.end(),
|
||||
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()) {
|
||||
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::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); });
|
||||
[&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseFailure(); });
|
||||
if (temprary_list.size()) {
|
||||
bridge_list = temprary_list;
|
||||
continue;
|
||||
|
@ -121,8 +123,8 @@ auto content_extractw = [](std::shared_ptr<const lib_token::IActionToken> token)
|
|||
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 {
|
||||
if (cursor->parseStop())
|
||||
QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
|
||||
if (cursor->parseFailure() && cursor->parseComplete())
|
||||
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
|
||||
|
||||
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;
|
||||
|
||||
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(),
|
||||
[](std::shared_ptr<const MatchCursor > ins) { return !ins->parseStop(); });
|
||||
[](std::shared_ptr<const MatchCursor > ins) { return !ins->parseFailure(); });
|
||||
if (!continue_count)
|
||||
return max_match_begin;
|
||||
|
||||
// 最小匹配次数中所有错误分支都是无用的、需要舍弃
|
||||
for (auto idx = 0; idx < max_match_begin.size(); ++idx) {
|
||||
auto current_cursor = max_match_begin.at(idx);
|
||||
if (current_cursor->parseStop())
|
||||
if (current_cursor->parseFailure())
|
||||
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)
|
||||
current_list.append(this->rule_peer->parse(ins, out));
|
||||
current_list.append(this->rule_peer->parse(ins));
|
||||
|
||||
QList<QString> contents;
|
||||
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;
|
||||
std::copy_if(current_list.begin(), current_list.end(),
|
||||
std::back_inserter(temprary_branchs), [&](std::shared_ptr<const MatchCursor> ins) {
|
||||
return cursor->totalErrorCount() == ins->totalErrorCount() &&
|
||||
ins->currentToken()->position() > cursor->currentToken()->position();
|
||||
return ins->parseComplete() || (cursor->totalErrorCount() == ins->totalErrorCount() &&
|
||||
ins->token()->position() > cursor->token()->position());
|
||||
});
|
||||
if (temprary_branchs.size()) {
|
||||
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::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()) {
|
||||
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);
|
||||
std::sort(results.begin(), results.end(),
|
||||
[](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()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if(ins->parseComplete())
|
||||
rets_completely.append(ins);
|
||||
}
|
||||
|
||||
// 提取经过修正的分支
|
||||
decltype(results) rets_modified;
|
||||
for (auto ins : results) {
|
||||
if (!ins->parseStop()) {
|
||||
if (!ins->parseFailure()) {
|
||||
if (!rets_modified.size()) {
|
||||
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_completely.append(rets_modified);
|
||||
for (auto rst : rets_modified)
|
||||
if (!rets_completely.contains(rst))
|
||||
rets_completely.append(rst);
|
||||
|
||||
if (rets_completely.size())
|
||||
return rets_completely;
|
||||
|
||||
|
@ -284,8 +291,20 @@ QString lib_syntax::MatchCursor::parseSyntax() const {
|
|||
if (!this->previous())
|
||||
return QString();
|
||||
|
||||
QString token_split = this->currentToken()->defines() ? this->currentToken()->defines()->regex() : "";
|
||||
return this->previous()->parseSyntax() + " " + token_split;
|
||||
QString token_splitx;
|
||||
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() {
|
||||
|
@ -298,19 +317,27 @@ void MatchCursor::logExprsError(const QString& msg) {
|
|||
this->_exprs_errors.last()->addError(msg);
|
||||
|
||||
// 普适性质的判定标准
|
||||
this->setStop(this->exprsErrorCount() > 1);
|
||||
this->setFailure(this->exprsErrorCount() > 1);
|
||||
}
|
||||
|
||||
void MatchCursor::quitExprs() {
|
||||
this->_exprs_errors.pop_back();
|
||||
}
|
||||
|
||||
bool lib_syntax::MatchCursor::parseStop() const {
|
||||
return this->_parse_proc_stop;
|
||||
bool lib_syntax::MatchCursor::parseFailure() const {
|
||||
return this->_parse_stop_with_errors;
|
||||
}
|
||||
|
||||
void lib_syntax::MatchCursor::setStop(bool mark) {
|
||||
this->_parse_proc_stop = mark;
|
||||
void lib_syntax::MatchCursor::setFailure(bool 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 {
|
||||
|
@ -332,11 +359,11 @@ void MatchCursor::setCurrent(std::shared_ptr<const IActionToken> t, std::shared_
|
|||
this->_remains_word = remains;
|
||||
}
|
||||
|
||||
std::shared_ptr<const IActionToken> MatchCursor::currentToken() const {
|
||||
std::shared_ptr<const IActionToken> MatchCursor::token() const {
|
||||
return this->_current_token;
|
||||
}
|
||||
|
||||
std::shared_ptr<const IPrimitiveWord> MatchCursor::currentWords() const {
|
||||
std::shared_ptr<const IPrimitiveWord> MatchCursor::words() const {
|
||||
return this->_remains_word;
|
||||
}
|
||||
|
||||
|
|
|
@ -59,22 +59,23 @@ namespace lib_syntax {
|
|||
virtual void enterExprs();
|
||||
virtual void logExprsError(const QString& msg);
|
||||
virtual void quitExprs();
|
||||
|
||||
virtual bool parseStop() const;
|
||||
virtual void setStop(bool mark=true);
|
||||
|
||||
virtual int exprsErrorCount() const;
|
||||
virtual int totalErrorCount() 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 std::shared_ptr<const lib_token::IActionToken> currentToken() const;
|
||||
virtual std::shared_ptr<const lib_words::IPrimitiveWord> currentWords() const;
|
||||
virtual std::shared_ptr<const lib_token::IActionToken> token() const;
|
||||
virtual std::shared_ptr<const lib_words::IPrimitiveWord> words() const;
|
||||
|
||||
private:
|
||||
QString _file_path;
|
||||
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<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误
|
||||
|
@ -106,7 +107,7 @@ namespace lib_syntax {
|
|||
* @param out 解析结束的分支存储集合
|
||||
* @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
|
||||
public:
|
||||
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;
|
||||
};
|
||||
|
||||
|
@ -140,7 +141,7 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
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;
|
||||
};
|
||||
|
||||
|
@ -158,7 +159,7 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
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;
|
||||
};
|
||||
|
||||
|
@ -173,7 +174,7 @@ namespace lib_syntax {
|
|||
virtual int typeMark() const;
|
||||
|
||||
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:
|
||||
QString name_store;
|
||||
|
@ -199,19 +200,27 @@ namespace lib_syntax {
|
|||
virtual QString present() const override {
|
||||
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 {
|
||||
auto w_this = current->currentWords();
|
||||
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> current) const override {
|
||||
// 如果提前结束,记录错误并返回
|
||||
if (!w_this) {
|
||||
if(current->currentToken()->defines())
|
||||
out << current;
|
||||
else
|
||||
out << current->previous();
|
||||
if (!current->words()) {
|
||||
// 只有在表达式的起始点遇到nullptr,才是正常结束。
|
||||
if (current->token()->tokenType() != lib_token::IActionToken::Type::ElementBegin) {
|
||||
auto ncurs = std::make_shared<MatchCursor>(current);
|
||||
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);
|
||||
// 解析成功
|
||||
if (std::get<0>(match_result)) {
|
||||
|
@ -247,8 +256,8 @@ namespace lib_syntax {
|
|||
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);
|
||||
if (std::get<0>(nx_result)) {
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
auto syntax = present();
|
||||
|
||||
auto t_this = cursor->currentToken();
|
||||
auto w_this = cursor->currentWords();
|
||||
auto t_this = cursor->token();
|
||||
auto w_this = cursor->words();
|
||||
// 起始Token打点
|
||||
auto split_begin = std::make_shared<lib_token::ExprBeginToken<ExprType>>(shared_from_this(), t_this);
|
||||
|
||||
|
@ -308,21 +317,21 @@ namespace lib_syntax {
|
|||
ncursor->enterExprs();
|
||||
|
||||
// 表达式语法解析
|
||||
auto nbranch = this->expr_rule_parse(ncursor, out);
|
||||
auto nbranch = this->expr_rule_parse(ncursor);
|
||||
|
||||
// 语法完全匹配的分支
|
||||
decltype(nbranch) branch_procs;
|
||||
std::copy_if(nbranch.begin(), nbranch.end(),
|
||||
std::back_inserter(branch_procs),
|
||||
[&](std::shared_ptr<const MatchCursor> ins) {
|
||||
return ins->totalErrorCount() == cursor->totalErrorCount();
|
||||
return ins->totalErrorCount() == cursor->totalErrorCount() || ins->parseComplete();
|
||||
});
|
||||
|
||||
// 语法修正后能匹配的分支
|
||||
if (!branch_procs.size()) {
|
||||
std::copy_if(nbranch.begin(), nbranch.end(),
|
||||
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())
|
||||
|
@ -331,11 +340,11 @@ namespace lib_syntax {
|
|||
|
||||
decltype(nbranch) results_fnl;
|
||||
for (auto curs : branch_procs) {
|
||||
if (curs->parseStop())
|
||||
if (curs->parseFailure())
|
||||
results_fnl.append(curs);
|
||||
else {
|
||||
auto t_end = curs->currentToken();
|
||||
auto w_end = curs->currentWords();
|
||||
auto t_end = curs->token();
|
||||
auto w_end = curs->words();
|
||||
|
||||
auto ecursor = std::make_shared<MatchCursor>(curs);
|
||||
ecursor->quitExprs();
|
||||
|
|
|
@ -84,7 +84,13 @@ namespace lib_token {
|
|||
*/
|
||||
class IActionToken : public lib_token::IToken {
|
||||
public:
|
||||
|
||||
enum class Type { ElementBegin, TokenBind, ElementEnd };
|
||||
/**
|
||||
* @brief 获取ActionToken类型.
|
||||
*
|
||||
* \return
|
||||
*/
|
||||
virtual Type tokenType() const = 0;
|
||||
/**
|
||||
* @brief 上一个Token
|
||||
* @return
|
||||
|
|
|
@ -59,19 +59,57 @@ auto content_extract = [](std::shared_ptr<const lib_token::IActionToken> token)
|
|||
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> {
|
||||
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, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
||||
auto syntax_text = this->present();
|
||||
auto current_rst = content_extract(cursor->currentToken());
|
||||
auto rst = _children_store->parse(cursor, out);
|
||||
for (auto r : rst)
|
||||
std::const_pointer_cast<MatchCursor>(r)->setStop(r->exprsErrorCount());
|
||||
return rst;
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
//// 如果提前结束,记录错误并返回
|
||||
//if (!cursor->words()) {
|
||||
// while (cursor->token()->tokenType() == lib_token::IActionToken::Type::ElementBegin)
|
||||
// cursor = cursor->previous();
|
||||
|
||||
// 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") { }
|
||||
|
||||
// 通过 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 {
|
||||
return _children_store->parse(cursor, out);
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -100,8 +138,8 @@ public:
|
|||
ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers, ReferSyntaxDef>("point_refer") { }
|
||||
|
||||
// 通过 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 {
|
||||
return _children_store->parse(cursor, out);
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -115,8 +153,8 @@ public:
|
|||
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice, SliceSyntaxDef>("slice_define") { }
|
||||
|
||||
// 通过 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 {
|
||||
return _children_store->parse(cursor, out);
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -130,14 +168,14 @@ 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, 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 current_rst = content_extract(cursor->currentToken());
|
||||
auto rst = _children_store->parse(cursor, out);
|
||||
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->currentToken()) += "\n";
|
||||
result_list += content_extract(c->token()) += "\n";
|
||||
}
|
||||
|
||||
return rst;
|
||||
|
@ -154,8 +192,8 @@ 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, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
||||
return _children_store->parse(cursor, out);
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -169,8 +207,8 @@ 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, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
||||
return _children_store->parse(cursor, out);
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -182,8 +220,8 @@ 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, QList<std::shared_ptr<const MatchCursor>>& out) const override {
|
||||
return _children_store->parse(cursor, out);
|
||||
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -195,9 +233,9 @@ 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, 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();
|
||||
return _children_store->parse(cursor, out);
|
||||
return _children_store->parse(cursor);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -65,6 +65,9 @@ namespace lib_token {
|
|||
}
|
||||
|
||||
// 通过 IActionToken 继承
|
||||
virtual Type tokenType() const {
|
||||
return Type::TokenBind;
|
||||
}
|
||||
QString file() const override {
|
||||
return _bind_content->file();
|
||||
}
|
||||
|
@ -109,6 +112,9 @@ namespace lib_token {
|
|||
}
|
||||
|
||||
// 通过 IActionToken 继承
|
||||
virtual Type tokenType() const {
|
||||
return Type::ElementBegin;
|
||||
}
|
||||
QString file() const override {
|
||||
return this->prevToken()->file();
|
||||
}
|
||||
|
@ -165,6 +171,9 @@ namespace lib_token {
|
|||
: _prev_token(prev), _self_start(start) { }
|
||||
|
||||
// 通过 IActionToken 继承
|
||||
virtual Type tokenType() const {
|
||||
return Type::ElementEnd;
|
||||
}
|
||||
QString file() const override {
|
||||
return this->prevToken()->file();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue