diff --git a/libSyntax/ast_basic.cpp b/libSyntax/ast_basic.cpp index ed1469e..3e061a9 100644 --- a/libSyntax/ast_basic.cpp +++ b/libSyntax/ast_basic.cpp @@ -5,106 +5,31 @@ using namespace ast_basic; using namespace lib_token; using namespace lib_syntax; -ExprElement::ExprElement(std::shared_ptr bind) : _expr_rule(bind) {} +ExprInstance::ExprInstance(std::shared_ptr bind) : _expr_rule(bind) {} -std::shared_ptr ExprElement::definedRule() const { +std::shared_ptr ExprInstance::definedRule() const { return _expr_rule; } -QString ExprElement::filePath() const { +QString ExprInstance::filePath() const { if (!tokens_bind.size()) throw new SyntaxException(u8"InternalError[0x0002]一个空的非法无效节点"); return tokens_bind.first()->file(); } -void ExprElement::addToken(std::shared_ptr token_inst) { +void ExprInstance::addToken(std::shared_ptr token_inst) { this->tokens_bind.append(token_inst); } -QList> ExprElement::children() const { +QList> ExprInstance::children() const { return this->children_store; } -void ExprElement::addChild(std::shared_ptr inst) { +void ExprInstance::addChild(std::shared_ptr inst) { this->children_store.append(inst); } -QList> ExprElement::tokens() const { +QList> ExprInstance::tokens() const { return this->tokens_bind; } - -ExprsContext::ExprsContext() {} - -void ExprsContext::setCurrentFile(const QString& path) { this->current_file_path = path; } - -QString ExprsContext::currentFile() const { return this->current_file_path; } - -std::shared_ptr ExprsContext::currentExprInst() const -{ - if (expression_stack.size()) - return expression_stack.last(); - - return nullptr; -} - -void ExprsContext::pushExprInst(std::shared_ptr current_inst) -{ - if (!expression_stack.size() || expression_stack.last() != current_inst) - expression_stack.append(current_inst); -} - -std::shared_ptr ExprsContext::popExprInst() -{ - auto lastx = expression_stack.takeLast(); - return lastx; -} - -std::shared_ptr ExprsContext::currentExprRule() const { - if (rule_stack.size()) - return rule_stack.last(); - return nullptr; -} - -void ExprsContext::pushExprRule(std::shared_ptr inst) { - if (!rule_stack.size() || rule_stack.last() != inst) - rule_stack.append(inst); -} - -std::shared_ptr ExprsContext::popExprRule() -{ - return rule_stack.takeLast(); -} - -void ExprsContext::appendParseErrors(const QString& file_path, int start, const QString& e) { - this->errors_storage.append(std::make_tuple(file_path, start, e)); -} - -QStringList ExprsContext::errors() const { - QStringList values; - for (auto& tp : this->errors_storage) - values.append(QString(u8"文件:%1\n\t%2").arg(std::get<0>(tp)).arg(std::get<2>(tp))); - - return values; -} - -void ExprsContext::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) == file_path && std::get<1>(tp) >= start) - errors_storage.removeAt(idx--); - } -} - -QList> ExprsContext::currentExprRuleStack() const { - return rule_stack; -} - - -void ExprsContext::appendDocInst(std::shared_ptr inst) { - this->document_store.append(inst); -} - -QList> ExprsContext::getDocInsts() const { - return this->document_store; -} \ No newline at end of file diff --git a/libSyntax/ast_basic.h b/libSyntax/ast_basic.h index 200e4c9..f2cf30c 100644 --- a/libSyntax/ast_basic.h +++ b/libSyntax/ast_basic.h @@ -57,14 +57,14 @@ namespace ast_basic { /** * @brief 表达式节点 */ - class LIBSYNTAX_EXPORT ExprElement : public ast_basic::IExprInst, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT ExprInstance : public ast_basic::IExprInst, public std::enable_shared_from_this { private: std::shared_ptr _expr_rule; QList> children_store; QList> tokens_bind; public: - ExprElement(std::shared_ptr bind); + ExprInstance(std::shared_ptr bind); // 通过 Expression 继承 std::shared_ptr definedRule() const override; @@ -76,36 +76,4 @@ namespace ast_basic { QList> children() const override; void addChild(std::shared_ptr inst) override; }; - - class LIBSYNTAX_EXPORT ExprsContext : public lib_syntax::IContext, public std::enable_shared_from_this { - private: - QList> rule_stack; - QList> expression_stack; - QList> document_store; - QString current_file_path; - QList> errors_storage; - - public: - ExprsContext(); - - // 通过 IContext 继承 - virtual void setCurrentFile(const QString& path); - virtual QString currentFile() const; - - std::shared_ptr currentExprInst() const override; - void pushExprInst(std::shared_ptr current_inst) override; - std::shared_ptr popExprInst() override; - - std::shared_ptr currentExprRule() const override; - void pushExprRule(std::shared_ptr inst) override; - std::shared_ptr popExprRule() override; - virtual QList> currentExprRuleStack() const; - - virtual void appendDocInst(std::shared_ptr inst) override; - virtual QList> getDocInsts() const override; - - void appendParseErrors(const QString& file_path, int start, const QString& error_msg) override; - QStringList errors() const override; - void clearErrors(const QString& file_path, int start) override; - }; } \ No newline at end of file diff --git a/libSyntax/ast_novel.cpp b/libSyntax/ast_novel.cpp index 473e595..1965d78 100644 --- a/libSyntax/ast_novel.cpp +++ b/libSyntax/ast_novel.cpp @@ -34,14 +34,14 @@ void example_novel::PointRefers::setStoryRefer(const QString& refer) { this->story_refs = refer; } -QString PointRefers::fragmentRefer() const { return fragment_ref; } +QString PointRefers::pointRefer() const { return point_ref; } -void example_novel::PointRefers::setFragmentRefer(const QString& refer) { - this->fragment_ref = refer; +void example_novel::PointRefers::setPointRefer(const QString& refer) { + this->point_ref = refer; } QString PointRefers::referSignature() const { - return storyRefer() + u8"&" + fragmentRefer(); + return storyRefer() + u8"&" + sliceRefer() + u8"&" + pointRefer(); } int PointRefers::typeMark() const { return (int)NovelNode::PointRefers; } @@ -218,3 +218,15 @@ QString RankDeclare::signature() const { return u8"::rank"; } + +int example_novel::FragmentSlice::typeMark() const { + return 0; +} + +bool example_novel::FragmentSlice::isAnonymous() const { + return false; +} + +QString example_novel::FragmentSlice::signature() const { + return QString(); +} diff --git a/libSyntax/ast_novel.h b/libSyntax/ast_novel.h index 4545ff3..ca0714e 100644 --- a/libSyntax/ast_novel.h +++ b/libSyntax/ast_novel.h @@ -45,15 +45,29 @@ namespace example_novel { virtual QString signature() const override; }; + class LIBSYNTAX_EXPORT FragmentSlice : public AbstractImpl { + public: + // 通过 AbstractImpl 继承 + int typeMark() const override; + bool isAnonymous() const override; + QString signature() const override; + }; + class LIBSYNTAX_EXPORT PointRefers : public AbstractImpl { + private: + QString story_refs, slice_ref, point_ref; + public: PointRefers(std::shared_ptr rule_bind); QString storyRefer() const; void setStoryRefer(const QString& refer); - QString fragmentRefer() const; - void setFragmentRefer(const QString& refer); + QString sliceRefer() const; + void setSliceRefer(const QString& refer); + + QString pointRefer() const; + void setPointRefer(const QString& refer); QString referSignature() const; @@ -62,12 +76,12 @@ namespace example_novel { virtual int typeMark() const override; virtual bool isAnonymous() const override; virtual QString signature() const override; - - private: - QString story_refs, fragment_ref; }; class LIBSYNTAX_EXPORT PointDefines : public AbstractImpl { + private: + QString name_store; + public: PointDefines(std::shared_ptr rule_bind); @@ -79,9 +93,6 @@ namespace example_novel { virtual int typeMark() const override; virtual bool isAnonymous() const override; virtual QString signature() const override; - - private: - QString name_store; }; class LIBSYNTAX_EXPORT ArticleDefine : public AbstractImpl { diff --git a/libSyntax/libsyntax.cpp b/libSyntax/libsyntax.cpp index d0bf1fa..19e1f11 100644 --- a/libSyntax/libsyntax.cpp +++ b/libSyntax/libsyntax.cpp @@ -9,9 +9,11 @@ using namespace lib_token; using namespace lib_words; using namespace ast_basic; -Rept::Rept(std::shared_ptr rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {} +Rept::Rept(std::shared_ptr rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) { } -QList> Rept::children() const { return QList>() << rule_peer; } +QList> Rept::children() const { + return QList>() << rule_peer; +} std::tuple> Rept::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { auto temp_head = head; @@ -49,21 +51,22 @@ std::tuple> Rept::pars return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); } -QString Rept::token_present() const -{ - if(min_match == 0 && max_match == INT_MAX) - return u8"(" + this->rule_peer->token_present() + QString(u8")*"); +QString Rept::present() const { + if (min_match == 0 && max_match == INT_MAX) + return u8"(" + this->rule_peer->present() + QString(u8")*"); else if (min_match == 1 && max_match == INT_MAX) - return u8"(" + this->rule_peer->token_present() + QString(u8")+"); + return u8"(" + this->rule_peer->present() + QString(u8")+"); else if (min_match == 0 && max_match == 1) - return u8"(" + this->rule_peer->token_present() + QString(u8")?"); + return u8"(" + this->rule_peer->present() + QString(u8")?"); - return u8"(" + this->rule_peer->token_present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match); + return u8"(" + this->rule_peer->present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match); } -Seqs::Seqs(const QList> mbrs) : mbrs_store(mbrs) {} +Seqs::Seqs(const QList> mbrs) : mbrs_store(mbrs) { } -QList> Seqs::children() const { return mbrs_store; } +QList> Seqs::children() const { + return mbrs_store; +} std::tuple> Seqs::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { auto temp_head = head; @@ -86,22 +89,23 @@ std::tuple> Seqs::pars return std::make_tuple(IBasicRule::MatchResult::Success, temp_head); } -QString Seqs::token_present() const -{ +QString Seqs::present() const { QString content; for (auto& it : children()) - content += it->token_present() + " "; + content += it->present() + " "; return content.mid(0, content.size() - 1); } //std::tuple, std::shared_ptr> -Any::Any(const QList> mbrs) : mbrs_store(mbrs) {} +Any::Any(const QList> mbrs) : mbrs_store(mbrs) { } -QList> Any::children() const { return mbrs_store; } +QList> Any::children() const { + return mbrs_store; +} std::tuple> Any::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { std::tuple, uint64_t> temp_result = std::make_tuple(nullptr, 0); - auto rule_present = this->token_present(); + auto rule_present = this->present(); for (auto& fork : mbrs_store) { auto gen = fork->parse(rt_inst, head); @@ -109,15 +113,17 @@ std::tuple> Any::parse // 遇到成功的直接返回解析结果 case IBasicRule::MatchResult::Success: return gen; - case IBasicRule::MatchResult::Fail: { - if (!std::get<0>(temp_result)) - temp_result = std::make_tuple(fork, 0); - }break; - case IBasicRule::MatchResult::Part: { - auto span = std::get<1>(gen)->position() - head->position(); - if (span >= std::get<1>(temp_result)) - temp_result = std::make_tuple(fork, span); - } + case IBasicRule::MatchResult::Fail: + { + if (!std::get<0>(temp_result)) + temp_result = std::make_tuple(fork, 0); + }break; + case IBasicRule::MatchResult::Part: + { + auto span = std::get<1>(gen)->position() - head->position(); + if (span >= std::get<1>(temp_result)) + temp_result = std::make_tuple(fork, span); + } default: break; } @@ -129,19 +135,22 @@ std::tuple> Any::parse return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(temp)); } -QString Any::token_present() const -{ +QString Any::present() const { QString members_content; for (auto& it : children()) { - members_content += it->token_present() + u8"|"; + members_content += it->present() + u8"|"; } return members_content.mid(0, members_content.size() - 1); } -SyntaxException::SyntaxException(const QString& message) { this->msg_store = message; } +SyntaxException::SyntaxException(const QString& message) { + this->msg_store = message; +} -QString SyntaxException::message() const { return msg_store; } +QString SyntaxException::message() const { + return msg_store; +} ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_name) { this->mark_store = expr_mark; @@ -153,9 +162,13 @@ std::shared_ptr ExprRule::reloadRule(std::shared_ptrmark_store; } +int ExprRule::typeMark() const { + return this->mark_store; +} QList> ExprRule::children() const { return QList>() << this->child_store; @@ -165,7 +178,7 @@ QList> ExprRule::children() const { std::tuple> ExprRule::parse(std::shared_ptr rt_inst, std::shared_ptr head) const { std::shared_ptr elm_ast = this->newEmptyInstance(); - auto text_present = this->token_present(); + auto text_present = this->present(); rt_inst->pushExprRule(this->shared_from_this()); rt_inst->pushExprInst(elm_ast); @@ -179,22 +192,23 @@ ExprRule::parse(std::shared_ptr rt_inst, std::shared_ptrpopExprInst(); rt_inst->popExprRule(); break; - case IBasicRule::MatchResult::Success: { - if (!std::dynamic_pointer_cast(elm_ast)) { - auto start_pos = tokens_decl.first()->position(); - rt_inst->clearErrors(rt_inst->currentFile(), start_pos); - } + case IBasicRule::MatchResult::Success: + { + if (!std::dynamic_pointer_cast(elm_ast)) { + auto start_pos = tokens_decl.first()->position(); + rt_inst->clearErrors(rt_inst->currentFile(), start_pos); + } - rt_inst->popExprInst(); - rt_inst->popExprRule(); + rt_inst->popExprInst(); + rt_inst->popExprRule(); - if (rt_inst->currentExprInst()) { - rt_inst->currentExprInst()->addChild(elm_ast); - } - else { - rt_inst->appendDocInst(elm_ast); - } - }break; + if (rt_inst->currentExprInst()) { + rt_inst->currentExprInst()->addChild(elm_ast); + } + else { + rt_inst->appendDocInst(elm_ast); + } + }break; default: break; } @@ -202,6 +216,63 @@ ExprRule::parse(std::shared_ptr rt_inst, std::shared_ptrtoken_present(); +QString ExprRule::present() const { + return child_store->present(); +} + +lib_syntax::MatchCursor::MatchCursor() { } + +lib_syntax::MatchCursor::MatchCursor(const MatchCursor& other) + : _expr_through(other._expr_through), + _total_errors(other._total_errors), + _exprs_errors(other._exprs_errors), + _current_token(other._current_token), + _remains_word(other._remains_word) { } + +void lib_syntax::MatchCursor::enterExprs(std::shared_ptr ins) { + this->_expr_through.push_back(ins); +} + +std::shared_ptr lib_syntax::MatchCursor::currentExprs() const { + return this->_expr_through.size()?this->_expr_through.last():nullptr; +} + +void lib_syntax::MatchCursor::logExprsError(const QString& msg) { + this->_exprs_errors.push_back(msg); +} + +void lib_syntax::MatchCursor::quitExprs() { + this->_expr_through.pop_back(); + + this->_total_errors.append(this->_exprs_errors); + this->_exprs_errors.clear(); +} + +bool lib_syntax::MatchCursor::mustStop() const { + return this->_exprs_errors.size() >= 2; +} + +int lib_syntax::MatchCursor::exprsErrorCount() const { + return this->_exprs_errors.size(); +} + +int lib_syntax::MatchCursor::totalErrorCount() const { + return this->_total_errors.size() + this->_exprs_errors.size(); +} + +QList lib_syntax::MatchCursor::totalErrors() const { + return this->_total_errors; +} + +void lib_syntax::MatchCursor::setCurrent(std::shared_ptr t, std::shared_ptr remains) { + this->_current_token = t; + this->_remains_word = remains; +} + +std::shared_ptr lib_syntax::MatchCursor::currentToken() const { + return this->_current_token; +} + +std::shared_ptr lib_syntax::MatchCursor::currentWords() const { + return this->_remains_word; } diff --git a/libSyntax/libsyntax.h b/libSyntax/libsyntax.h index e91a065..a9ee6f7 100644 --- a/libSyntax/libsyntax.h +++ b/libSyntax/libsyntax.h @@ -12,8 +12,6 @@ namespace ast_basic { } namespace lib_syntax { - class IBasicRule; - /** * @brief 语法异常 */ @@ -32,33 +30,32 @@ namespace lib_syntax { /** * @brief 解析上下文接口 */ - class IContext { + class MatchCursor { + private: + QList> _expr_through; //表达式解析路径 + QList _total_errors; // 所有解析错误 + QList _exprs_errors; // 当前表达式解析错误 + std::shared_ptr _current_token = nullptr; // 当前Token + std::shared_ptr _remains_word = nullptr; // 剩余词语 + public: - virtual ~IContext() = default; + MatchCursor(); + MatchCursor(const MatchCursor& other); + virtual ~MatchCursor() = default; - virtual void setCurrentFile(const QString& path) = 0; - virtual QString currentFile() const = 0; + virtual void enterExprs(std::shared_ptr ins); + virtual std::shared_ptr currentExprs() const; + virtual void logExprsError(const QString& msg); + virtual void quitExprs(); - virtual void appendParseErrors(const QString& file_path, int start, const QString& error_msg) = 0; - virtual QStringList errors() const = 0; - virtual void clearErrors(const QString& file_path, int start) = 0; + virtual bool mustStop() const; + virtual int exprsErrorCount() const; + virtual int totalErrorCount() const; + virtual QList totalErrors() const; - virtual void appendDocInst(std::shared_ptr inst) = 0; - virtual QList> getDocInsts() const = 0; - - /** - * \brief 当前表达式元素. - * - * \return 返回当前表达式 - */ - virtual std::shared_ptr currentExprInst() const = 0; - virtual void pushExprInst(std::shared_ptr current_inst) = 0; - virtual std::shared_ptr popExprInst() = 0; - - virtual std::shared_ptr currentExprRule() const = 0; - virtual void pushExprRule(std::shared_ptr inst) = 0; - virtual std::shared_ptr popExprRule() = 0; - virtual QList> currentExprRuleStack() const = 0; + virtual void setCurrent(std::shared_ptr t, std::shared_ptr remains); + virtual std::shared_ptr currentToken() const; + virtual std::shared_ptr currentWords() const; }; /** @@ -66,21 +63,12 @@ namespace lib_syntax { */ class IBasicRule { public: - virtual ~IBasicRule() = default; /** - * @brief 子规则 - * @return + * 返回匹配语法规则的词法序列表达 + * + * \return 词法表达序列 */ - virtual QList> children() const = 0; - - /** - * @brief 匹配结果 - */ - enum class MatchResult { - Success, // 符合匹配条件 - Part, // 部分匹配 - Fail // 从第一个词起完全不匹配 - }; + virtual QString present() const = 0; /** * @brief 解析 @@ -88,15 +76,19 @@ namespace lib_syntax { * @param head 列表头 * @return 返回结果<匹配完成新列表头,匹配长度> */ - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const = 0; + virtual QList> parse(std::shared_ptr cursor) const = 0; + }; + /** + * @brief 组合语法匹配规则接口 + */ + class ICompositRule : public IBasicRule { + public: /** - * 返回匹配语法规则的词法序列表达 - * - * \return 词法表达序列 + * @brief 子规则 + * @return */ - virtual QString token_present() const = 0; + virtual QList> children() const = 0; }; template @@ -108,45 +100,70 @@ namespace lib_syntax { template xproc = nullptr> class TokenMatch : public IBasicRule, public std::enable_shared_from_this> { private: - std::shared_ptr define_peer; + std::shared_ptr _define_peers; public: - TokenMatch(std::shared_ptr define) : define_peer(define) {} + TokenMatch(std::shared_ptr define) : _define_peers(define) { } // IBasicRule interface public: - virtual QList> children() const override { return QList>(); } - virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override { - if (!head) { - rt_inst->appendParseErrors(rt_inst->currentFile(), -1, QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(rt_inst->currentFile())); - return std::make_tuple(IBasicRule::MatchResult::Fail, head); - } + virtual QList> parse(std::shared_ptr current) const override { + auto w_this = current->currentWords(); - auto match_result = define_peer->analysis(head); + auto match_result = _define_peers->analysis(w_this); if (std::get<0>(match_result)) { - auto current_inst = rt_inst->currentExprInst(); + auto current_inst = current->currentExprs(); current_inst->addToken(std::get<0>(match_result)); if (xproc) { xproc(std::dynamic_pointer_cast(current_inst), std::get<0>(match_result)); } + auto remains = w_this->nextWord(); if (std::get<1>(match_result)) { - return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared(std::get<1>(match_result), head->nextWord())); - } - else { - return std::make_tuple(IBasicRule::MatchResult::Success, head->nextWord()); + remains = std::make_shared(std::get<1>(match_result), remains); } + + auto clone_ins = std::make_shared(current); + auto chain = std::make_shared(std::get<0>(match_result), current->currentToken()); + clone_ins->setCurrent(std::get<0>(chain, remains)); + return QList>() << clone_ins; } else { + QList> retvals; + // 少一个 + auto short_one = std::make_shared(current); + short_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,缺失\"%1\"") + .arg(this->_define_peers->reviseWords()) + .arg(w_this->row()).arg(w_this->column()).arg(w_this->file())); + retvals << short_one; + + // 错一个 + auto error_one = std::make_shared(current); + error_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,请修正\"%1\"") + .arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file())); + + auto tkins = std::make_shared( + w_this->row(), w_this->column(), w_this->position(), + w_this->content(), w_this->file(), this->_define_peers); + auto tkchain = std::make_shared(tkins, error_one->currentToken()); + error_one->setCurrent(tkchain, w_this->nextWord()); + retvals << error_one; + + // 多一个 + + + + + rt_inst->appendParseErrors(rt_inst->currentFile(), head->position(), QString(u8"Syntax[0x00001]语法匹配错误,无法识别\"%1\"(应该为:%4)\n\t目标语法:%5。\n") - .arg(head->content()).arg(head->row()).arg(head->column()).arg(this->define_peer->reviseWords()) - .arg(rt_inst->currentExprRule()->token_present())); + .arg(head->content()).arg(head->row()).arg(head->column()).arg(this->_define_peers->reviseWords()) + .arg(rt_inst->currentExprRule()->present())); return std::make_tuple(IBasicRule::MatchResult::Part, head); } } - virtual QString token_present() const override { - return QString(u8"%1").arg(this->define_peer->reviseWords()); + virtual QString present() const override { + return QString(u8"%1").arg(this->_define_peers->reviseWords()); } }; @@ -167,7 +184,7 @@ namespace lib_syntax { virtual QList> children() const override; virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; - virtual QString token_present() const override; + virtual QString present() const override; }; /** @@ -185,7 +202,7 @@ namespace lib_syntax { virtual QList> children() const override; virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; - virtual QString token_present() const override; + virtual QString present() const override; }; /** @@ -204,7 +221,7 @@ namespace lib_syntax { virtual QList> children() const override; virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; - virtual QString token_present() const override; + virtual QString present() const override; }; // 组合语法实体解析 =================================================================================================== @@ -228,7 +245,7 @@ namespace lib_syntax { virtual QList> children() const override; virtual std::tuple> parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; - virtual QString token_present() const override; + virtual QString present() const override; protected: std::shared_ptr child_store; @@ -245,7 +262,7 @@ namespace lib_syntax { class ElementRule : public ExprRule { public: ElementRule(const QString& rule_name, int expr_mark) - :ExprRule(rule_name, expr_mark) {} + :ExprRule(rule_name, expr_mark) { } virtual std::shared_ptr newEmptyInstance() const { return std::dynamic_pointer_cast( diff --git a/libSyntax/libtokens.cpp b/libSyntax/libtokens.cpp index cde63a1..a15291b 100644 --- a/libSyntax/libtokens.cpp +++ b/libSyntax/libtokens.cpp @@ -7,3 +7,34 @@ TokenException::TokenException(const QString& message) : msg_store(message) { } QString TokenException::message() const { return msg_store; } + +lib_token::TokenChain::TokenChain(std::shared_ptr content, std::shared_ptr prev) + : _token_content(content), _token_previous(prev) { } + +QString lib_token::TokenChain::file() const { + return _token_content->file(); +} + +uint64_t lib_token::TokenChain::position() const { + return _token_content->position(); +} + +QString lib_token::TokenChain::content() const { + return _token_content->content(); +} + +int lib_token::TokenChain::row() const { + return _token_content->row(); +} + +int lib_token::TokenChain::column() const { + return _token_content->column(); +} + +std::shared_ptr lib_token::TokenChain::defines() const { + return _token_content->defines(); +} + +std::shared_ptr lib_token::TokenChain::previousToken() const { + return _token_previous; +} diff --git a/libSyntax/libtokens.h b/libSyntax/libtokens.h index 5287712..2764e35 100644 --- a/libSyntax/libtokens.h +++ b/libSyntax/libtokens.h @@ -17,17 +17,11 @@ namespace lib_token { virtual QString message() const; }; - class IToken; /** - * @brief token解析机制定义 + * @brief token类型定义 */ class ITokenDefine { public: - /** - * @brief 解析机制名称 - * @return - */ - virtual QString reviseWords() const = 0; /** * @brief 整数类型标识. * @@ -39,17 +33,8 @@ namespace lib_token { * @return */ virtual QString regex() const = 0; - - /** - * @brief 解析词语处理过程 - * @param content 词语 - * @return tuple 解析结果 - */ - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const = 0; }; - /** * @brief token解析结果 */ @@ -59,6 +44,83 @@ namespace lib_token { * @brief token解析机制关联 * @return */ - virtual std::shared_ptr define() const = 0; + virtual std::shared_ptr defines() const = 0; + /** + * @brief 上一个Token + * @return + */ + virtual std::shared_ptr previousToken() const = 0; + }; + + /** + * @brief token解析机制定义 + */ + class ITokenProcess : public ITokenDefine { + public: + /** + * @brief 解析机制名称 + * @return + */ + virtual QString reviseWords() const = 0; + /** + * @brief 解析词语处理过程 + * @param content 词语 + * @return tuple 解析结果 + */ + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const = 0; + }; + + /** + * @brief 词法解析成果 + */ + class TokenContent : public IToken { + private: + int row_n, col_n, doc_offset; + QString text_n, path_p; + std::shared_ptr type_def; + + public: + /** + * @brief 词法解析内容 + * @param r 行 + * @param c 列 + * @param pos 位置 + * @param t 内容 + * @param p path + * @param paramType 类型定义 + */ + TokenContent(int r, int c, uint64_t pos, const QString& t, const QString& p, std::shared_ptr paramType); + + // WordBase interface + public: + virtual QString file() const override; + virtual uint64_t position() const override; + virtual QString content() const override; + virtual int row() const override; + virtual int column() const override; + + // Token interface + public: + virtual std::shared_ptr defines() const override; + virtual std::shared_ptr previousToken() const override; + }; + + class TokenChain : public IToken { + private: + std::shared_ptr _token_previous; + std::shared_ptr _token_content; + + public: + TokenChain(std::shared_ptr content, std::shared_ptr prev); + + // 通过 IToken 继承 + QString file() const override; + uint64_t position() const override; + QString content() const override; + int row() const override; + int column() const override; + std::shared_ptr defines() const override; + std::shared_ptr previousToken() const override; }; } \ No newline at end of file diff --git a/libSyntax/tokens_novel.cpp b/libSyntax/tokens_novel.cpp index 1cfda99..5316695 100644 --- a/libSyntax/tokens_novel.cpp +++ b/libSyntax/tokens_novel.cpp @@ -11,7 +11,7 @@ TokenContent::TokenContent(int r, int c, uint64_t pos, const QString& t, const Q QString TokenContent::file() const { return path_p; } -uint64_t lib_token::TokenContent::position() const { +uint64_t TokenContent::position() const { return doc_offset; } @@ -21,12 +21,12 @@ int TokenContent::row() const { return row_n; } int TokenContent::column() const { return col_n; } -std::shared_ptr lib_token::TokenContent::nextWord() const +std::shared_ptr TokenContent::previousToken() const { return nullptr; } -std::shared_ptr TokenContent::define() const { return this->type_def; } +std::shared_ptr TokenContent::defines() const { return this->type_def; } QString LeftBracket::reviseWords() const { return u8"'{'"; } @@ -37,8 +37,8 @@ int LeftBracket::typeMark() const QString LeftBracket::regex() const { return u8"{"; } -std::tuple, std::shared_ptr> -LeftBracket::analysis(std::shared_ptr content) const { +std::tuple, std::shared_ptr> +LeftBracket::analysis(std::shared_ptr content) const { auto text = content->content(); if (!text.startsWith(regex())) return std::make_tuple(nullptr, content); @@ -84,8 +84,8 @@ int Keywords::typeMark() const QString Keywords::regex() const { return means_store; } -std::tuple, std::shared_ptr> -Keywords::analysis(std::shared_ptr content) const { +std::tuple, std::shared_ptr> +Keywords::analysis(std::shared_ptr content) const { if (content->content() != regex()) { return std::make_tuple(nullptr, content); } @@ -104,8 +104,8 @@ int Numbers::typeMark() const QString Numbers::regex() const { return u8"^([0-9]+)$"; } -std::tuple, std::shared_ptr> -Numbers::analysis(std::shared_ptr content) const { +std::tuple, std::shared_ptr> +Numbers::analysis(std::shared_ptr content) const { auto text = content->content(); QRegExp regx(regex()); if (regx.indexIn(text) == -1) @@ -125,8 +125,8 @@ int VTextSection::typeMark() const QString VTextSection::regex() const { return u8"^([^\\{\\}@&]+)"; } -std::tuple, std::shared_ptr> -VTextSection::analysis(std::shared_ptr content) const { +std::tuple, std::shared_ptr> +VTextSection::analysis(std::shared_ptr content) const { auto text = content->content(); QRegExp regx(regex()); if (regx.indexIn(text) == -1) { @@ -164,7 +164,8 @@ int NameSection::typeMark() const QString NameSection::regex() const { return u8"^([^\\{\\}@&]+)"; } -std::tuple, std::shared_ptr > NameSection::analysis(std::shared_ptr content) const +std::tuple, std::shared_ptr > +NameSection::analysis(std::shared_ptr content) const { auto text = content->content(); QRegExp regx(regex()); @@ -199,7 +200,8 @@ QString DeclareSymbo::regex() const return u8"#"; } -std::tuple, std::shared_ptr> DeclareSymbo::analysis(std::shared_ptr content) const +std::tuple, std::shared_ptr> +DeclareSymbo::analysis(std::shared_ptr content) const { auto text = content->content(); if (content->column() != 1 && !text.startsWith(regex())) { diff --git a/libSyntax/tokens_novel.h b/libSyntax/tokens_novel.h index 931cb36..bfe0cb2 100644 --- a/libSyntax/tokens_novel.h +++ b/libSyntax/tokens_novel.h @@ -5,43 +5,16 @@ #include #include -namespace lib_token { - /** - * @brief 词法解析成果 - */ - class TokenContent : public IToken { - private: - int row_n, col_n, doc_offset; - QString text_n, path_p; - std::shared_ptr type_def; - - public: - TokenContent(int r, int c, uint64_t pos, const QString& t, const QString& p, std::shared_ptr paramType); - - // WordBase interface - public: - virtual QString file() const override; - virtual uint64_t position() const override; - virtual QString content() const override; - virtual int row() const override; - virtual int column() const override; - virtual std::shared_ptr nextWord() const override; - - // Token interface - public: - virtual std::shared_ptr define() const override; - }; -} namespace example_novel { - class LIBSYNTAX_EXPORT LeftBracket : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT LeftBracket : public lib_token::ITokenProcess, public std::enable_shared_from_this { // TokenDefine interface public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const override; + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const override; }; class LIBSYNTAX_EXPORT RightBracket : public LeftBracket { @@ -65,7 +38,7 @@ namespace example_novel { virtual QString regex() const override; }; - class LIBSYNTAX_EXPORT Keywords : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT Keywords : public lib_token::ITokenProcess, public std::enable_shared_from_this { private: QString means_store; int type_code; @@ -78,49 +51,49 @@ namespace example_novel { virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const override; + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const override; }; - class LIBSYNTAX_EXPORT Numbers : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT Numbers : public lib_token::ITokenProcess, public std::enable_shared_from_this { // TokenDefine interface public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const override; + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const override; }; - class LIBSYNTAX_EXPORT NameSection : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT NameSection : public lib_token::ITokenProcess, public std::enable_shared_from_this { // TokenDefine interface public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const override; + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const override; }; - class LIBSYNTAX_EXPORT VTextSection : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT VTextSection : public lib_token::ITokenProcess, public std::enable_shared_from_this { // TokenDefine interface public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const override; + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const override; }; - class LIBSYNTAX_EXPORT DeclareSymbo : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT DeclareSymbo : public lib_token::ITokenProcess, public std::enable_shared_from_this { public: // 通过 TokenDefine 继承 virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; - virtual std::tuple, std::shared_ptr> - analysis(std::shared_ptr content) const override; + virtual std::tuple, std::shared_ptr> + analysis(std::shared_ptr content) const override; }; } // namespace example_novel diff --git a/libWords/libwords.h b/libWords/libwords.h index 734c0d6..feadea0 100644 --- a/libWords/libwords.h +++ b/libWords/libwords.h @@ -36,6 +36,10 @@ namespace lib_words { * @return */ virtual int column() const = 0; + }; + + class IPrimitiveWord : public IWordBase { + public: /** * @brief 下一个单词. * @@ -47,7 +51,7 @@ namespace lib_words { /** * @brief 未解析原始词语 */ - class LIBWORDS_EXPORT WordContent : public IWordBase { + class LIBWORDS_EXPORT WordContent : public IPrimitiveWord { private: int row_n, col_n; uint64_t doc_offset; @@ -66,7 +70,7 @@ namespace lib_words { virtual std::shared_ptr nextWord() const override; }; - class LIBWORDS_EXPORT WordImpl : public IWordBase { + class LIBWORDS_EXPORT WordImpl : public IPrimitiveWord { private: std::shared_ptr content_ptr; std::shared_ptr next_ptr;