最新改进,增加了错误识别
This commit is contained in:
parent
cbc2b2ccb6
commit
7a07e0b266
|
|
@ -70,6 +70,14 @@ int main(int argc, char* argv[]) {
|
|||
try {
|
||||
auto parser = std::make_shared<NovelParser>();
|
||||
access_ptr = parser->parse(files);
|
||||
|
||||
auto errors_list = std::dynamic_pointer_cast<const ast_gen::GlobalElement>(access_ptr->element())->errors();
|
||||
if (errors_list.size()) {
|
||||
for (auto& err : errors_list) {
|
||||
qDebug().noquote() << err;
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
catch (lib_syntax::SyntaxException* e) {
|
||||
qDebug().noquote() << e->message();
|
||||
|
|
|
|||
|
|
@ -80,22 +80,22 @@ std::shared_ptr<const BaseRule> ExpressionContext::popExpressionRule()
|
|||
return rule_stack.takeLast();
|
||||
}
|
||||
|
||||
void ExpressionContext::appendParseErrors(int start, const QString& e) {
|
||||
this->errors_storage.append(std::make_tuple(start, e));
|
||||
void ExpressionContext::appendParseErrors(const QString& file_path, int start, const QString& e) {
|
||||
this->errors_storage.append(std::make_tuple(file_path, start, e));
|
||||
}
|
||||
|
||||
QStringList ExpressionContext::errors() const {
|
||||
QStringList values;
|
||||
for (auto& tp : this->errors_storage)
|
||||
values.append(std::get<1>(tp));
|
||||
values.append(QString(u8"%2\n\tÎļþ£¨%1£©").arg(std::get<0>(tp)).arg(std::get<2>(tp)));
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
void ExpressionContext::clearErrors(int start) {
|
||||
void ExpressionContext::clearErrors(const QString &file_path, int start) {
|
||||
for (int idx = 0; idx < this->errors_storage.size(); ++idx) {
|
||||
auto &tp = errors_storage[idx];
|
||||
if(std::get<0>(tp) >= start)
|
||||
if(std::get<0>(tp) == file_path && std::get<1>(tp) >= start)
|
||||
errors_storage.removeAt(idx--);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ namespace ast_basic {
|
|||
QList<std::shared_ptr<ExprInst>> expression_stack;
|
||||
QList<std::shared_ptr<const ast_basic::ExprInst>> document_store;
|
||||
QString current_file_path;
|
||||
QList<std::tuple<int, QString>> errors_storage;
|
||||
QList<std::tuple<QString, int, QString>> errors_storage;
|
||||
|
||||
public:
|
||||
ExpressionContext();
|
||||
|
|
@ -111,8 +111,8 @@ namespace ast_basic {
|
|||
virtual void appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) override;
|
||||
virtual QList<std::shared_ptr<const ast_basic::ExprInst>> getDocs() const override;
|
||||
|
||||
void appendParseErrors(int start, const QString& error_msg) override;
|
||||
void appendParseErrors(const QString& file_path, int start, const QString& error_msg) override;
|
||||
QStringList errors() const override;
|
||||
void clearErrors(int start) override;
|
||||
void clearErrors(const QString& file_path, int start) override;
|
||||
};
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ QList<std::shared_ptr<const BaseRule>> TokenMatch::children() const { return QLi
|
|||
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
if (!head) {
|
||||
rt_inst->appendParseErrors(-1, QString(u8"Syntax[0x0000]tokenÁ÷£¨%1£©ÌáǰÖÕÖ¹").arg(rt_inst->currentFile()));
|
||||
rt_inst->appendParseErrors(rt_inst->currentFile(), - 1, QString(u8"Syntax[0x0000]tokenÁ÷£¨%1£©ÌáǰÖÕÖ¹").arg(rt_inst->currentFile()));
|
||||
return std::make_tuple(BaseRule::MatchResult::Fail, head);
|
||||
}
|
||||
|
||||
|
|
@ -22,9 +22,9 @@ std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> TokenMatch::
|
|||
rt_inst->currentInst()->addToken(std::get<0>(match_result));
|
||||
}
|
||||
else {
|
||||
rt_inst->appendParseErrors(head->position(),
|
||||
rt_inst->appendParseErrors(rt_inst->currentFile(), head->position(),
|
||||
QString(u8"Syntax[0x00001]语法匹配错误,不能识别token:%1<%2,%3>(%4)")
|
||||
.arg(head->content()).arg(head->row()).arg(head->column()).arg(head->file()));
|
||||
.arg(head->content()).arg(head->row()).arg(head->column()).arg(head->file()));
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, head);
|
||||
}
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ public:
|
|||
}
|
||||
};
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<std::shared_ptr<const BaseRule>, int> temp_result = std::make_tuple(nullptr, -1);
|
||||
std::tuple<std::shared_ptr<const BaseRule>, uint64_t> temp_result = std::make_tuple(nullptr, 0);
|
||||
auto rule_present = this->token_present();
|
||||
|
||||
for (auto& fork : mbrs_store) {
|
||||
|
|
@ -151,7 +151,7 @@ std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(s
|
|||
}break;
|
||||
case BaseRule::MatchResult::Part: {
|
||||
auto span = std::get<1>(gen)->position() - head->position();
|
||||
if (span > std::get<1>(temp_result))
|
||||
if (span >= std::get<1>(temp_result))
|
||||
temp_result = std::make_tuple(fork, span);
|
||||
}
|
||||
default:
|
||||
|
|
@ -160,7 +160,7 @@ std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(s
|
|||
}
|
||||
|
||||
// 分析最匹配的分支
|
||||
rt_inst->clearErrors(head->position());
|
||||
rt_inst->clearErrors(rt_inst->currentFile(), head->position());
|
||||
auto temp = std::get<0>(temp_result)->parse(rt_inst, head);
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, std::get<1>(temp));
|
||||
}
|
||||
|
|
@ -180,14 +180,12 @@ SyntaxException::SyntaxException(const QString& message) { this->msg_store = mes
|
|||
QString SyntaxException::message() const { return msg_store; }
|
||||
|
||||
ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
|
||||
this->filter_proc = [](const TokenSeqs& seqs) { return seqs; };
|
||||
this->mark_store = expr_mark;
|
||||
}
|
||||
|
||||
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule) {
|
||||
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const BaseRule> rule) {
|
||||
auto ninst = makeCopy();
|
||||
ninst->child_store = rule;
|
||||
ninst->filter_proc = filter;
|
||||
return ninst;
|
||||
}
|
||||
|
||||
|
|
@ -199,6 +197,7 @@ QList<std::shared_ptr<const BaseRule>> ExprRule::children() const {
|
|||
return QList<std::shared_ptr<const BaseRule>>() << this->child_store;
|
||||
}
|
||||
|
||||
#include <ast_novel.h>
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>>
|
||||
ExprRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::shared_ptr<ExprInst> elm_ast = this->newEmptyInstance();
|
||||
|
|
@ -208,8 +207,7 @@ ExprRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWo
|
|||
rt_inst->pushInst(elm_ast);
|
||||
|
||||
auto rstg = child_store->parse(rt_inst, head);
|
||||
auto tokens_decl = this->filter_proc(elm_ast->tokens());
|
||||
elm_ast->tokensReset(tokens_decl);
|
||||
auto tokens_decl = elm_ast->tokens();
|
||||
|
||||
switch (std::get<0>(rstg)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
|
|
@ -218,7 +216,11 @@ ExprRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWo
|
|||
rt_inst->popExpressionRule();
|
||||
break;
|
||||
case BaseRule::MatchResult::Success: {
|
||||
rt_inst->clearErrors(tokens_decl.first()->position());
|
||||
if (!std::dynamic_pointer_cast<example_novel::Document>(elm_ast)) {
|
||||
auto start_pos = tokens_decl.first()->position();
|
||||
rt_inst->clearErrors(rt_inst->currentFile(), start_pos);
|
||||
}
|
||||
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
|
||||
|
|
@ -237,5 +239,5 @@ ExprRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWo
|
|||
}
|
||||
|
||||
QString ExprRule::token_present() const {
|
||||
return QString(u8"(%1)").arg(child_store->token_present());
|
||||
return child_store->token_present();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,9 +38,9 @@ namespace lib_syntax {
|
|||
virtual void setCurrentFile(const QString &path) = 0;
|
||||
virtual QString currentFile() const = 0;
|
||||
|
||||
virtual void appendParseErrors(int start, const QString &error_msg) = 0;
|
||||
virtual void appendParseErrors(const QString & file_path, int start, const QString &error_msg) = 0;
|
||||
virtual QStringList errors() const = 0;
|
||||
virtual void clearErrors(int start) = 0;
|
||||
virtual void clearErrors(const QString &file_path, int start) = 0;
|
||||
|
||||
virtual void appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) = 0;
|
||||
virtual QList<std::shared_ptr<const ast_basic::ExprInst>> getDocs() const = 0;
|
||||
|
|
@ -183,7 +183,7 @@ namespace lib_syntax {
|
|||
typedef QList<std::shared_ptr<const lib_token::IToken>> TokenSeqs;
|
||||
ExprRule(const QString& rule_name, int expr_mark);
|
||||
|
||||
virtual std::shared_ptr<const ExprRule> reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule);
|
||||
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const BaseRule> rule);
|
||||
virtual QString name() const;
|
||||
virtual int typeMark() const;
|
||||
|
||||
|
|
@ -198,7 +198,6 @@ namespace lib_syntax {
|
|||
virtual QString token_present() const override;
|
||||
|
||||
private:
|
||||
std::function<TokenSeqs(const TokenSeqs&)> filter_proc;
|
||||
std::shared_ptr<const lib_syntax::BaseRule> child_store;
|
||||
QString name_store;
|
||||
int mark_store;
|
||||
|
|
|
|||
|
|
@ -23,9 +23,6 @@ auto split_mark = std::make_shared<Split>(); //
|
|||
auto vtext = std::make_shared<VTextSection>(); // ^([^\\{\\}\\n@&]+)
|
||||
auto name_text = std::make_shared<NameSection>(); // ^([^:\\{\\}\\n@&][^\\{\\}\\n@&]*)
|
||||
|
||||
auto newl = std::make_shared<NewLine>();
|
||||
|
||||
|
||||
|
||||
|
||||
// rule-parts ===============================================================================
|
||||
|
|
@ -40,74 +37,59 @@ auto newl = std::make_shared<NewLine>();
|
|||
#define MultiR(rule) std::make_shared<const Rept>(rule, 1, INT_MAX)
|
||||
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> LinesMerge(const QList<std::shared_ptr<const BaseRule>>& mbrs) {
|
||||
QList<std::shared_ptr<const BaseRule>> values_ret;
|
||||
for (auto& item : mbrs) {
|
||||
values_ret << item << OptMulT(newl);
|
||||
}
|
||||
return values_ret;
|
||||
}
|
||||
QList<std::shared_ptr<const BaseRule>> LinesMerge(std::shared_ptr<const BaseRule> item) {
|
||||
return QList<std::shared_ptr<const BaseRule>>() << item << OptMulT(newl);
|
||||
}
|
||||
|
||||
// remove-return
|
||||
auto remove_nl = [](const ExprRule::TokenSeqs& p)->ExprRule::TokenSeqs {
|
||||
ExprRule::TokenSeqs result;
|
||||
for (auto& n : p) {
|
||||
if (n->define()->typeMark() == newl->typeMark())
|
||||
continue;
|
||||
result.append(n);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
auto decl_comp = std::make_shared<const Any>(Rules{ MR(numbers), MR(vtext), MR(name_text), MR(split_mark) });
|
||||
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule(remove_nl,
|
||||
std::make_shared<const Seqs>(LinesMerge(MultiR(decl_comp))
|
||||
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule(
|
||||
MultiR(decl_comp));
|
||||
|
||||
auto fragment_decl = ElementRule<FragmentDefine>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(frag_key), MR(name_text) } <<
|
||||
OptMulR(decl_expr) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto fragment_decl = ElementRule<FragmentDefine>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(frag_key), MR(name_text) }) <<
|
||||
OptMulR(decl_expr) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
|
||||
auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(refers), MR(frag_key), MR(name_text), MR(split_mark), MR(name_text) }) <<
|
||||
OptMulR(decl_expr) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(refers), MR(frag_key), MR(name_text), MR(split_mark), MR(name_text) } <<
|
||||
OptMulR(decl_expr) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto fragment_comp = std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr });
|
||||
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(story_key), MR(name_text) }) <<
|
||||
OptMulR(fragment_comp) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(story_key), MR(name_text) } <<
|
||||
OptMulR(fragment_comp) <<
|
||||
MR(rightb)
|
||||
));
|
||||
// ===================================================================
|
||||
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(article_key), MR(name_text) }) <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(article_key), MR(name_text) } <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(volume_key), MR(name_text) }) <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(volume_key), MR(name_text) } <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
Rules{ MR(declare), MR(rank_key), MR(numbers), OptMulT(newl)}
|
||||
));
|
||||
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(declare), MR(rank_key), MR(numbers) }
|
||||
));
|
||||
|
||||
auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
Rules{
|
||||
std::make_shared<const Rept>(rank_define, 0, 1),
|
||||
MultiR(std::make_shared<const Any>(Rules{story_define, volume_decl}))
|
||||
}
|
||||
));
|
||||
auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{
|
||||
std::make_shared<const Rept>(rank_define, 0, 1),
|
||||
MultiR(std::make_shared<const Any>(Rules{story_define, volume_decl}))
|
||||
}
|
||||
));
|
||||
|
||||
std::shared_ptr<const ExprRule> NovalSyntax::getParseTree() { return document_define; }
|
||||
std::shared_ptr<const ast_gen::SyntaxElement> NovalSyntax::tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
|
||||
|
|
@ -136,7 +118,7 @@ void NovalSyntax::node_register(std::shared_ptr<const ast_gen::SyntaxElement> ro
|
|||
for (auto& child : children) {
|
||||
if (!child->isAnonymous()) {
|
||||
auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child);
|
||||
if(check_result)
|
||||
if (check_result)
|
||||
throw new lib_syntax::SyntaxException(QString(u8"SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type:%2>(%3,%4)")
|
||||
.arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ QList<std::shared_ptr<const IWordBase>> WordReader::extract_from(const QString&
|
|||
while (!tin.atEnd()) {
|
||||
uint64_t relative_offset = line_number;
|
||||
relative_offset = relative_offset << 32;
|
||||
auto line = tin.readLine() + "\n";
|
||||
auto line = tin.readLine();
|
||||
ret_list.append(this->parse_line(relative_offset, line_number++, line, path));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -93,15 +93,6 @@ Keywords::analysis(std::shared_ptr<const IWordBase> content) const {
|
|||
return std::make_tuple(token_inst, nullptr);
|
||||
}
|
||||
|
||||
QString NewLine::typeName() const { return u8"new-line"; }
|
||||
|
||||
int NewLine::typeMark() const
|
||||
{
|
||||
return 0x04000000;
|
||||
}
|
||||
|
||||
QString NewLine::regex() const { return u8"\n"; }
|
||||
|
||||
QString Numbers::typeName() const { return u8"numbers"; }
|
||||
|
||||
int Numbers::typeMark() const
|
||||
|
|
|
|||
|
|
@ -60,13 +60,6 @@ namespace example_novel {
|
|||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT NewLine : public LeftBracket {
|
||||
public:
|
||||
virtual QString typeName() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT Split : public LeftBracket {
|
||||
public:
|
||||
virtual QString typeName() const override;
|
||||
|
|
|
|||
Loading…
Reference in New Issue