This commit is contained in:
codeboss 2025-02-15 01:48:14 +08:00
parent bc5ccb37e8
commit c4c1dc5775
5 changed files with 114 additions and 82 deletions

View File

@ -24,21 +24,12 @@ int main(int argc, char *argv[])
ast_gen::SyntaxParser parser(example_novel::NovalSyntax::getSyntaxTree());
auto rst = parser.parse(vwords);
for (auto one : rst) {
qDebug() << "===================================================";
QList<QString> words_list;
auto vtoken = one->currentToken();
while (vtoken) {
if(vtoken->defines())
words_list.prepend(vtoken->content());
vtoken = vtoken->prevToken();
}
auto prag_root = std::make_shared< ast_basic::ExprProgram>("HelloWorld!");
auto structx = parser.getAst(rst.first(), prag_root);
qDebug() << QString("Row:%1,Col:%2,Position:%3,ErrorCount:%4,Errors:")
.arg(one->currentToken()->row()).arg(one->currentToken()->column()).arg(one->currentToken()->position()).arg(one->exprsErrorCount())
<< one->totalErrors();
qDebug() << words_list;
}
qDebug() << "finished";
return a.exec();
}

View File

@ -13,11 +13,25 @@ QList<std::shared_ptr<const MatchCursor>> SyntaxParser::parse(std::shared_ptr<co
auto cursor = std::make_shared<MatchCursor>(words->file());
cursor->setCurrent(nullptr, words);
auto list = this->_rule_bind->parse(cursor);
QList<std::shared_ptr<const MatchCursor>> out;
auto list = this->_rule_bind->parse(cursor, out);
if (out.size())
list = out;
QHash<QString, std::shared_ptr<const MatchCursor>> result_map;
std::for_each(list.begin(), list.end(),
[&](std::shared_ptr<const MatchCursor> ins) {
result_map[ins->parseSyntax()] = ins;
});
list = result_map.values();
std::sort(list.begin(), list.end(),
[&](std::shared_ptr<const MatchCursor> a, std::shared_ptr<const MatchCursor> b) {
return a->currentToken()->position() > b->currentToken()->position();
});
auto mark_a = a->currentToken()->position() * 100 - a->totalErrorCount();
auto mark_b = b->currentToken()->position() * 100 - b->totalErrorCount();
return mark_a > mark_b;
});
return list;
}

View File

@ -15,14 +15,14 @@ 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) const {
if (cursor->mustStop())
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())
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));
result_list.append(rx->parse(cursor, out));
// 完全匹配分支
decltype(result_list) completely_list;
@ -34,7 +34,7 @@ QList<std::shared_ptr<const MatchCursor>> __anyone_impl::parse(std::shared_ptr<c
// 经过修正的分支
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->mustStop(); });
[&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); });
if (modify_list.size())
return modify_list;
@ -55,16 +55,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) const {
if (cursor->mustStop())
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())
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->mustStop()) {
current_result.append(rule->parse(vcurs));
if (!vcurs->parseStop()) {
current_result.append(rule->parse(vcurs, out));
}
}
@ -82,7 +82,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->mustStop(); });
[&](std::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); });
if (temprary_list.size()) {
bridge_list = temprary_list;
continue;
@ -111,8 +111,18 @@ QList<std::shared_ptr<const IBasicRule>> __repeat_impl::children() const {
}
#include <algorithm>
QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<const MatchCursor> cursor) const {
if (cursor->mustStop())
auto content_extractw = [](std::shared_ptr<const lib_token::IActionToken> token) {
QString content;
while (token) {
if (token->defines())
content.prepend(token->content() + " ");
token = token->prevToken();
}
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())
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
auto syntax = present();
@ -124,19 +134,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);
max_match_begin = seqs_rule->parse(cursor, out);
}
// 如果不满足最小重复匹配次数要求,则返回
int continue_count = std::count_if(max_match_begin.begin(), max_match_begin.end(),
[](std::shared_ptr<const MatchCursor > ins) { return !ins->mustStop(); });
[](std::shared_ptr<const MatchCursor > ins) { return !ins->parseStop(); });
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->mustStop())
if (current_cursor->parseStop())
max_match_begin.removeAt(idx--);
}
@ -148,7 +158,11 @@ 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));
current_list.append(this->rule_peer->parse(ins, out));
QList<QString> contents;
for (auto bx : current_list)
contents << content_extractw(bx->currentToken()) + QStringList(bx->totalErrors()).join(',');
// 提取完全匹配的分支
QList<std::shared_ptr<const MatchCursor>> temprary_branchs;
@ -165,7 +179,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->mustStop() && ins->currentToken()->position() > cursor->currentToken()->position();
return !ins->parseStop() && ins->currentToken()->position() > cursor->currentToken()->position();
});
if (temprary_branchs.size()) {
bridge_list = temprary_branchs;
@ -196,7 +210,7 @@ QList<std::shared_ptr<const MatchCursor>> __repeat_impl::parse(std::shared_ptr<c
// 提取经过修正的分支
decltype(results) rets_modified;
for (auto ins : results) {
if (!ins->mustStop()) {
if (!ins->parseStop()) {
if (!rets_modified.size()) {
rets_modified.append(ins);
}
@ -266,6 +280,14 @@ QString MatchCursor::filePath() const {
return _file_path;
}
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;
}
void MatchCursor::enterExprs() {
auto new_expr = std::make_shared<ErrsPack>();
this->_exprs_errors.push_back(new_expr);
@ -274,14 +296,21 @@ void MatchCursor::enterExprs() {
void MatchCursor::logExprsError(const QString& msg) {
this->_total_errors.push_back(msg);
this->_exprs_errors.last()->addError(msg);
// 普适性质的判定标准
this->setStop(this->exprsErrorCount() > 1);
}
void MatchCursor::quitExprs() {
this->_exprs_errors.pop_back();
}
bool MatchCursor::mustStop() const {
return exprsErrorCount() >= 2;
bool lib_syntax::MatchCursor::parseStop() const {
return this->_parse_proc_stop;
}
void lib_syntax::MatchCursor::setStop(bool mark) {
this->_parse_proc_stop = mark;
}
int MatchCursor::exprsErrorCount() const {
@ -311,10 +340,6 @@ std::shared_ptr<const IPrimitiveWord> MatchCursor::currentWords() const {
return this->_remains_word;
}
bool lib_syntax::MatchCursor::parse_stop() const {
return !currentWords();
}
lib_syntax::MatchCursor::ErrsPack::ErrsPack() { }
lib_syntax::MatchCursor::ErrsPack::ErrsPack(const ErrsPack& other)

View File

@ -54,12 +54,15 @@ namespace lib_syntax {
virtual std::shared_ptr<const MatchCursor> previous() const;
virtual QString filePath() const;
virtual QString parseSyntax() const;
virtual void enterExprs();
virtual void logExprsError(const QString& msg);
virtual void quitExprs();
virtual bool mustStop() const;
virtual bool parseStop() const;
virtual void setStop(bool mark=true);
virtual int exprsErrorCount() const;
virtual int totalErrorCount() const;
virtual QList<QString> totalErrors() const;
@ -71,13 +74,12 @@ namespace lib_syntax {
private:
QString _file_path;
std::shared_ptr<const MatchCursor> _prev_cursor = nullptr;
bool _parse_proc_stop = false;
QList<QString> _total_errors; // 所有解析错误
QList<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误
std::shared_ptr<const lib_token::IActionToken> _current_token = nullptr; // 当前Token
std::shared_ptr<const lib_token::IActionToken> _current_token = nullptr; // 当前Token
std::shared_ptr<const lib_words::IPrimitiveWord> _remains_word = nullptr; // 剩余词语
bool parse_stop() const;
};
/**
@ -101,9 +103,10 @@ namespace lib_syntax {
/**
* @brief
* @param cursor
* @param out
* @return <>
*/
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor, QList<std::shared_ptr<const MatchCursor>> &out) const = 0;
};
// 组合语法实体解析 =====================================================================================
@ -120,7 +123,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) 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 QString present() const override;
};
@ -137,7 +140,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) 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 QString present() const override;
};
@ -155,7 +158,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) 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 QString present() const override;
};
@ -170,7 +173,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) const = 0;
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;
private:
QString name_store;
@ -196,18 +199,16 @@ 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) const override {
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();
// 如果提前结束,记录错误并返回
if (!w_this) {
auto clone_ins = std::make_shared<MatchCursor>(current);
clone_ins->logExprsError(QString("Syntax[0x00001]语法匹配错误,缺失\"%1\"<file<%2>>")
.arg(this->_define_peers->reviseWords()).arg(current->filePath()));
clone_ins->logExprsError(QString("Syntax[0x00001]输入流提前结束,<%1>").arg(current->filePath()));
return QList<std::shared_ptr<const MatchCursor>>() << clone_ins;
}
if (w_this->content() == "故事介绍段落}") {
int c = w_this->column();
if(current->currentToken()->defines())
out << current;
else
out << current->previous();
return QList<std::shared_ptr<const MatchCursor>>();
}
auto t_this = current->currentToken();
@ -290,9 +291,9 @@ namespace lib_syntax {
return this->_children_store->present();
}
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) 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 {
// 提前结束,直接返回
if (cursor->mustStop())
if (cursor->parseStop())
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
auto syntax = present();
@ -307,7 +308,7 @@ namespace lib_syntax {
ncursor->enterExprs();
// 表达式语法解析
auto nbranch = this->expr_rule_parse(ncursor);
auto nbranch = this->expr_rule_parse(ncursor, out);
// 语法完全匹配的分支
decltype(nbranch) branch_procs;
@ -321,7 +322,7 @@ namespace lib_syntax {
if (!branch_procs.size()) {
std::copy_if(nbranch.begin(), nbranch.end(),
std::back_inserter(branch_procs),
[](std::shared_ptr<const MatchCursor> ins) { return !ins->mustStop(); });
[](std::shared_ptr<const MatchCursor> ins) { return !ins->parseStop(); });
}
if (!branch_procs.size())
@ -330,7 +331,7 @@ namespace lib_syntax {
decltype(nbranch) results_fnl;
for (auto curs : branch_procs) {
if (curs->mustStop())
if (curs->parseStop())
results_fnl.append(curs);
else {
auto t_end = curs->currentToken();

View File

@ -65,11 +65,12 @@ 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 {
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);
auto rst = _children_store->parse(cursor, out);
for (auto r : rst)
std::const_pointer_cast<MatchCursor>(r)->setStop(r->exprsErrorCount());
return rst;
}
};
@ -84,8 +85,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) const override {
return _children_store->parse(cursor);
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);
}
};
@ -99,8 +100,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) const override {
return _children_store->parse(cursor);
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);
}
};
@ -114,8 +115,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) const override {
return _children_store->parse(cursor);
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);
}
};
@ -129,10 +130,10 @@ 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 {
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 = this->present();
auto current_rst = content_extract(cursor->currentToken());
auto rst = _children_store->parse(cursor);
auto rst = _children_store->parse(cursor, out);
QString result_list;
for (auto c : rst) {
@ -153,8 +154,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) const override {
return _children_store->parse(cursor);
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);
}
};
@ -168,8 +169,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) const override {
return _children_store->parse(cursor);
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);
}
};
@ -181,8 +182,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) const override {
return _children_store->parse(cursor);
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);
}
};
@ -194,9 +195,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) const override {
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 text = _children_store->present();
return _children_store->parse(cursor);
return _children_store->parse(cursor, out);
}
};