From 735a9fb210869761551d160895230dfc1f713aba Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Tue, 11 Feb 2025 22:32:10 +0800 Subject: [PATCH] encoding update --- libSyntax/ast_basic.cpp | 2 +- libSyntax/ast_basic.h | 34 ++--- libSyntax/ast_gen.h | 16 +- libSyntax/ast_novel.cpp | 30 ++-- libSyntax/ast_novel.h | 84 +++++------ libSyntax/libSyntax.vcxproj | 5 +- libSyntax/libSyntax.vcxproj.filters | 3 + libSyntax/libsyntax.cpp | 134 ++--------------- libSyntax/libsyntax.h | 149 ++++++++----------- libSyntax/libtokens.h | 36 ++--- libSyntax/syntax_novel.cpp | 218 +++++++++++++++++++++------- libSyntax/syntax_novel.h | 2 +- libSyntax/syntax_templets.h | 51 +++++++ libSyntax/tokens_impl.h | 48 +++--- libSyntax/tokens_novel.cpp | 34 ++--- libSyntax/tokens_novel.h | 2 +- libToken/libtoken.cpp | 7 - libToken/libtoken.h | 11 -- libWords/libWords.vcxproj | 6 + libWords/libwords.cpp | 2 +- libWords/libwords.h | 26 ++-- 21 files changed, 458 insertions(+), 442 deletions(-) create mode 100644 libSyntax/syntax_templets.h delete mode 100644 libToken/libtoken.cpp delete mode 100644 libToken/libtoken.h diff --git a/libSyntax/ast_basic.cpp b/libSyntax/ast_basic.cpp index 10fd45f..75c0db6 100644 --- a/libSyntax/ast_basic.cpp +++ b/libSyntax/ast_basic.cpp @@ -14,7 +14,7 @@ std::shared_ptr ExprInstance::definedRule() const { QString ExprInstance::filePath() const { if (!tokens_bind.size()) - throw new SyntaxException(u8"InternalError[0x0002]一个空的非法无效节点"); + throw new SyntaxException("InternalError[0x0002]涓涓┖鐨勯潪娉曟棤鏁堣妭鐐"); return tokens_bind.first()->file(); } diff --git a/libSyntax/ast_basic.h b/libSyntax/ast_basic.h index b5d1ef8..cce8015 100644 --- a/libSyntax/ast_basic.h +++ b/libSyntax/ast_basic.h @@ -9,55 +9,55 @@ namespace lib_syntax { class ExprRule; } -// 抽象语法树结构 ==================================================================== +// 鎶借薄璇硶鏍戠粨鏋 ==================================================================== namespace ast_basic { /** - * @brief 抽象语法树集合节点/表达式节点 + * @brief 鎶借薄璇硶鏍戦泦鍚堣妭鐐/琛ㄨ揪寮忚妭鐐 */ class IExprInstance { public: /** - * @brief 获取表达式的解析规则 + * @brief 鑾峰彇琛ㄨ揪寮忕殑瑙f瀽瑙勫垯 * @return */ virtual std::shared_ptr definedRule() const = 0; //===================================================== /** - * 获取语法节点的源码文件路径. + * 鑾峰彇璇硶鑺傜偣鐨勬簮鐮佹枃浠惰矾寰. * - * \return 文件路径 + * \return 鏂囦欢璺緞 */ virtual QString filePath() const = 0; /** - * 获取语法节点关联token序列. + * 鑾峰彇璇硶鑺傜偣鍏宠仈token搴忓垪. * - * \return token序列 + * \return token搴忓垪 */ virtual QList> tokens() const = 0; /** - * 解析过程中,向表达式内部添加token实例. + * 瑙f瀽杩囩▼涓紝鍚戣〃杈惧紡鍐呴儴娣诲姞token瀹炰緥. * - * \param token_inst 实例 + * \param token_inst 瀹炰緥 */ virtual void addToken(std::shared_ptr token_inst) = 0; //===================================================== /** - * @brief 子表达式集合 + * @brief 瀛愯〃杈惧紡闆嗗悎 * @return */ virtual QList> children() const = 0; /** - * @brief 添加子表达式. + * @brief 娣诲姞瀛愯〃杈惧紡. * - * \param inst 子表达式实例 + * \param inst 瀛愯〃杈惧紡瀹炰緥 */ virtual void addChild(std::shared_ptr inst) = 0; }; - // 基础语法树构成 ========================================================================================== + // 鍩虹璇硶鏍戞瀯鎴 ========================================================================================== /** - * @brief 表达式节点 + * @brief 琛ㄨ揪寮忚妭鐐 */ class LIBSYNTAX_EXPORT ExprInstance : public IExprInstance, public std::enable_shared_from_this { private: @@ -68,7 +68,7 @@ namespace ast_basic { public: ExprInstance(std::shared_ptr bind); - // 通过 Expression 继承 + // 閫氳繃 Expression 缁ф壙 std::shared_ptr definedRule() const override; QString filePath() const override; @@ -80,7 +80,7 @@ namespace ast_basic { }; /** - * @brief 表达式树根节点 + * @brief 琛ㄨ揪寮忔爲鏍硅妭鐐 */ class LIBSYNTAX_EXPORT ExprProgram : public IExprInstance, public std::enable_shared_from_this { private: @@ -90,7 +90,7 @@ namespace ast_basic { public: ExprProgram(const QString &root); - // 通过 IExprInstance 继承 + // 閫氳繃 IExprInstance 缁ф壙 std::shared_ptr definedRule() const override; QString filePath() const override; QList> tokens() const override; diff --git a/libSyntax/ast_gen.h b/libSyntax/ast_gen.h index f8f078f..87acdee 100644 --- a/libSyntax/ast_gen.h +++ b/libSyntax/ast_gen.h @@ -7,7 +7,7 @@ namespace ast_gen { /** - * @brief 语法解析器 + * @brief 璇硶瑙f瀽鍣 */ class LIBSYNTAX_EXPORT SyntaxParser { private: @@ -17,17 +17,17 @@ namespace ast_gen { SyntaxParser(std::shared_ptr rule); /** - * @brief 依据源码语法规则,解析源码链表 - * @param wods 源码链表 - * @return 所有解析路径,按照错误数量排序:少->多 + * @brief 渚濇嵁婧愮爜璇硶瑙勫垯锛岃В鏋愭簮鐮侀摼琛 + * @param wods 婧愮爜閾捐〃 + * @return 鎵鏈夎В鏋愯矾寰勶紝鎸夌収閿欒鏁伴噺鎺掑簭锛氬皯->澶 */ QList> parse(std::shared_ptr words); /** - * @brief 依据指定解析路径,提取程序解构 - * @param cursor 解析路径 - * @param root 表达式树预制根节点 - * @return 完善后的根节点 + * @brief 渚濇嵁鎸囧畾瑙f瀽璺緞锛屾彁鍙栫▼搴忚В鏋 + * @param cursor 瑙f瀽璺緞 + * @param root 琛ㄨ揪寮忔爲棰勫埗鏍硅妭鐐 + * @return 瀹屽杽鍚庣殑鏍硅妭鐐 */ std::shared_ptr getAst( std::shared_ptr cursor, std::shared_ptr root); diff --git a/libSyntax/ast_novel.cpp b/libSyntax/ast_novel.cpp index ad3fa37..72f9428 100644 --- a/libSyntax/ast_novel.cpp +++ b/libSyntax/ast_novel.cpp @@ -19,7 +19,7 @@ QString TextSection::content() const { } QString TextSection::signature() const { - return u8"::section"; + return "::section"; } PointRefers::PointRefers(std::shared_ptr rule_bind) @@ -50,12 +50,12 @@ void PointRefers::setPointRefer(const QString& refer) { } QString PointRefers::referSignature() const { - return storyRefer() + u8"&" + sliceRefer() + u8"&" + pointRefer(); + return storyRefer() + "&" + sliceRefer() + "&" + pointRefer(); } QString PointRefers::signature() const { - QString signature = u8"@" + referSignature(); - return parent().lock()->signature() + u8"&" + signature; + QString signature = "@" + referSignature(); + return parent().lock()->signature() + "&" + signature; } @@ -71,7 +71,7 @@ void PointDefines::setName(const QString& nm) { } QString PointDefines::signature() const { - return parent().lock()->signature() + u8"&" + name(); + return parent().lock()->signature() + "&" + name(); } @@ -103,7 +103,7 @@ Document::Document(std::shared_ptr rule_bind) : AbstractImpl(rule_bind) { } QString Document::signature() const { - return QString(u8"::document<%1>").arg(path()); + return QString("::document<%1>").arg(path()); } VolumeDefine::VolumeDefine(std::shared_ptr rule_bind) @@ -133,7 +133,7 @@ void ArticleDefine::setName(const QString& nm) { } QString ArticleDefine::signature() const { - return parent().lock()->signature() + u8"&" + name(); + return parent().lock()->signature() + "&" + name(); } RankDeclare::RankDeclare(std::shared_ptr rule) @@ -148,12 +148,12 @@ void RankDeclare::setRank(int nums) { } QString RankDeclare::signature() const { - return u8"::rank"; + return "::rank"; } FragmentSlice::FragmentSlice(std::shared_ptr rule) :AbstractImpl(rule) { - _slice_name = QString(u8"名称未定义_%1").arg((uint64_t)this); + _slice_name = QString("鍚嶇О鏈畾涔塤%1").arg((uint64_t)this); } QString FragmentSlice::name() const { @@ -165,7 +165,7 @@ void FragmentSlice::setName(const QString& nm) { } QString FragmentSlice::signature() const { - return parent().lock()->signature() + u8"&" + name(); + return parent().lock()->signature() + "&" + name(); } std::shared_ptr NGlobalElement::bindExpression() const { @@ -185,7 +185,7 @@ QString NGlobalElement::path() const { } QString NGlobalElement::signature() const { - return u8"::program"; + return "::program"; } std::weak_ptr NGlobalElement::parent() const { @@ -217,7 +217,7 @@ void GlobalElement::clearCache() { } std::shared_ptr GlobalElement::appendToCache(std::shared_ptr named_node) { - auto mixed_key = QString(u8"%1<%2>").arg(named_node->signature()).arg(named_node->typeMark()); + auto mixed_key = QString("%1<%2>").arg(named_node->signature()).arg(named_node->typeMark()); if (node_cache.contains(mixed_key)) return node_cache[mixed_key]; node_cache[mixed_key] = named_node; @@ -225,7 +225,7 @@ std::shared_ptr GlobalElement::appendToCache(std::shared_pt } std::shared_ptr GlobalElement::getNamedNodeBy(int paramType, const QString& signature) const { - auto mixed_key = QString(u8"%1<%2>").arg(signature).arg(paramType); + auto mixed_key = QString("%1<%2>").arg(signature).arg(paramType); if (!node_cache.contains(mixed_key)) return nullptr; return node_cache[mixed_key]; @@ -240,11 +240,11 @@ bool GlobalElement::isAnonymous() const { } QString GlobalElement::signature() const { - return u8"::global"; + return "::global"; } QString GlobalElement::path() const { - return u8""; + return ""; } std::weak_ptr GlobalElement::parent() const { diff --git a/libSyntax/ast_novel.h b/libSyntax/ast_novel.h index 4194582..b5b5d0c 100644 --- a/libSyntax/ast_novel.h +++ b/libSyntax/ast_novel.h @@ -9,64 +9,64 @@ namespace ast_gen { class TokenAccess; /** - * @brief 解析上下文 + * @brief 瑙f瀽涓婁笅鏂 */ class SyntaxElement { public: virtual ~SyntaxElement() = default; /** - * 绑定表达式实体. + * 缁戝畾琛ㄨ揪寮忓疄浣. * - * \return 表达式实例 + * \return 琛ㄨ揪寮忓疄渚 */ virtual std::shared_ptr bindExpression() const = 0; /** - * @brief 类型标记 + * @brief 绫诲瀷鏍囪 * @return */ virtual int typeMark() const = 0; /** - * 是否属于匿名节点. + * 鏄惁灞炰簬鍖垮悕鑺傜偣. * - * \return 匿名标志 + * \return 鍖垮悕鏍囧織 */ virtual bool isAnonymous() const = 0; /** - * @brief 文件路径 + * @brief 鏂囦欢璺緞 * @return */ virtual QString path() const = 0; /** - * @brief 元素签名,如果是匿名元素返回空字符串 + * @brief 鍏冪礌绛惧悕锛屽鏋滄槸鍖垮悕鍏冪礌杩斿洖绌哄瓧绗︿覆 * @return */ virtual QString signature() const = 0; /** - * @brief 获取父元素 - * @return 未设置parent,返回nullptr + * @brief 鑾峰彇鐖跺厓绱 + * @return 鏈缃畃arent锛岃繑鍥瀗ullptr */ virtual std::weak_ptr parent() const = 0; /** - * @brief 重置父指针. + * @brief 閲嶇疆鐖舵寚閽. * * \param inst */ virtual void setParent(std::shared_ptr inst) = 0; /** - * @brief 定义元素自身的Token集合 + * @brief 瀹氫箟鍏冪礌鑷韩鐨凾oken闆嗗悎 * @return */ virtual QList> selfTokens() const = 0; }; /** - * @brief 语法元素整理访问接口 + * @brief 璇硶鍏冪礌鏁寸悊璁块棶鎺ュ彛 */ class LIBSYNTAX_EXPORT ElementAccess { private: @@ -79,14 +79,14 @@ namespace ast_gen { QList> children() const; /** - * @brief 获取该元素下所有Token定义 + * @brief 鑾峰彇璇ュ厓绱犱笅鎵鏈塗oken瀹氫箟 * @return */ virtual QList> tokens() const; }; /** - * @brief Token元素访问接口 + * @brief Token鍏冪礌璁块棶鎺ュ彛 */ class LIBSYNTAX_EXPORT TokenAccess { private: @@ -100,7 +100,7 @@ namespace ast_gen { }; /** - * @brief 根元素定义 + * @brief 鏍瑰厓绱犲畾涔 */ class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement { private: @@ -114,10 +114,10 @@ namespace ast_gen { virtual void clearCache(); virtual std::shared_ptr appendToCache(std::shared_ptr named_node); /** - * @brief 通过节点签名获取定义节点 - * @param signature 完全签名 + * @brief 閫氳繃鑺傜偣绛惧悕鑾峰彇瀹氫箟鑺傜偣 + * @param signature 瀹屽叏绛惧悕 * @return - * @throws 没有指定节点抛出异常 + * @throws 娌℃湁鎸囧畾鑺傜偣鎶涘嚭寮傚父 */ virtual std::shared_ptr getNamedNodeBy(int paramType, const QString& signature) const; virtual void addChild(std::shared_ptr citem); @@ -132,7 +132,7 @@ namespace ast_gen { virtual void setParent(std::shared_ptr inst) override; virtual QList> selfTokens() const override; - // 通过 SyntaxElement 继承 + // 閫氳繃 SyntaxElement 缁ф壙 virtual std::shared_ptr bindExpression() const override; }; } @@ -182,7 +182,7 @@ namespace example_novel { parent_store.reset(); } - // 通过 SyntaxElement 继承 + // 閫氳繃 SyntaxElement 缁ф壙 virtual int typeMark() const override { return (int) type; } @@ -213,23 +213,23 @@ namespace example_novel { }; /** - * @brief 文字段落 + * @brief 鏂囧瓧娈佃惤 */ class LIBSYNTAX_EXPORT TextSection : public AbstractImpl { public: TextSection(std::shared_ptr rule_bind); /** - * @brief 段落内容 + * @brief 娈佃惤鍐呭 */ QString content() const; - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; }; /** - * @brief 故事情节 + * @brief 鏁呬簨鎯呰妭 */ class LIBSYNTAX_EXPORT FragmentSlice : public AbstractImpl { private: @@ -241,12 +241,12 @@ namespace example_novel { QString name() const; void setName(const QString& nm); - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 QString signature() const override; }; /** - * @brief 节点引用定义 + * @brief 鑺傜偣寮曠敤瀹氫箟 */ class LIBSYNTAX_EXPORT PointRefers : public AbstractImpl { private: @@ -266,12 +266,12 @@ namespace example_novel { QString referSignature() const; - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; }; /** - * @brief 节点定义 + * @brief 鑺傜偣瀹氫箟 */ class LIBSYNTAX_EXPORT PointDefines : public AbstractImpl { private: @@ -283,12 +283,12 @@ namespace example_novel { QString name() const; void setName(const QString& nm); - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; }; /** - * @brief 章节定义 + * @brief 绔犺妭瀹氫箟 */ class LIBSYNTAX_EXPORT ArticleDefine : public AbstractImpl { public: @@ -297,7 +297,7 @@ namespace example_novel { QString name() const; void setName(const QString& nm); - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; private: @@ -305,7 +305,7 @@ namespace example_novel { }; /** - * @brief 卷宗定义 + * @brief 鍗峰畻瀹氫箟 */ class LIBSYNTAX_EXPORT VolumeDefine : public AbstractImpl { public: @@ -314,7 +314,7 @@ namespace example_novel { QString name() const; void setName(const QString& nm); - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; private: @@ -322,7 +322,7 @@ namespace example_novel { }; /** - * @brief 故事定义 + * @brief 鏁呬簨瀹氫箟 */ class LIBSYNTAX_EXPORT StoryDefine : public AbstractImpl { public: @@ -334,7 +334,7 @@ namespace example_novel { void setSort(int value); int sort() const; - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; private: @@ -343,7 +343,7 @@ namespace example_novel { }; /** - * @brief 排序声明 + * @brief 鎺掑簭澹版槑 */ class LIBSYNTAX_EXPORT RankDeclare : public AbstractImpl { private: @@ -354,29 +354,29 @@ namespace example_novel { int rankNumber() const; void setRank(int nums); - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 QString signature() const override; }; /** - * @brief 文档定义 + * @brief 鏂囨。瀹氫箟 */ class LIBSYNTAX_EXPORT Document : public AbstractImpl { public: Document(std::shared_ptr rule_bind); - // 通过 AbstractImpl 继承 + // 閫氳繃 AbstractImpl 缁ф壙 virtual QString signature() const override; }; /** - * @brief 全局根元素 + * @brief 鍏ㄥ眬鏍瑰厓绱 */ class LIBSYNTAX_EXPORT NGlobalElement : public ast_basic::ExprProgram, public ast_gen::SyntaxElement { public: NGlobalElement(const QString &root); - // 通过 SyntaxElement 继承 + // 閫氳繃 SyntaxElement 缁ф壙 std::shared_ptr bindExpression() const override; int typeMark() const override; bool isAnonymous() const override; diff --git a/libSyntax/libSyntax.vcxproj b/libSyntax/libSyntax.vcxproj index b45711e..e05b690 100644 --- a/libSyntax/libSyntax.vcxproj +++ b/libSyntax/libSyntax.vcxproj @@ -27,6 +27,7 @@ DynamicLibrary v143 + Unicode DynamicLibrary @@ -73,8 +74,9 @@ libWords.lib;%(AdditionalDependencies);$(Qt_LIBS_) - Default + stdcpp20 false + /utf-8 %(AdditionalOptions) @@ -123,6 +125,7 @@ + diff --git a/libSyntax/libSyntax.vcxproj.filters b/libSyntax/libSyntax.vcxproj.filters index 21e292a..d22f228 100644 --- a/libSyntax/libSyntax.vcxproj.filters +++ b/libSyntax/libSyntax.vcxproj.filters @@ -53,6 +53,9 @@ Header Files + + Header Files + diff --git a/libSyntax/libsyntax.cpp b/libSyntax/libsyntax.cpp index 685e57c..dcf1ac8 100644 --- a/libSyntax/libsyntax.cpp +++ b/libSyntax/libsyntax.cpp @@ -29,36 +29,11 @@ QList> Any::parse(std::shared_ptrpresent() + u8"|"; + members_content += it->present() + "|"; } return members_content.mid(0, members_content.size() - 1); } -QList lib_syntax::Any::invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const { - QList lines; - - lines << QString(u8"if (%1->mustStop())").arg(in_cursor_name); - lines << QString(u8" return QList>() << %1; ").arg(in_cursor_name); - - lines << QString(u8"QList> result_list_%1;").arg((uint64_t) this); - for (auto rule : children()) { - auto segments = rule->invokeSegments(in_cursor_name, QString(u8"result_list_%1").arg((uint64_t) this)); - if (segments.size() == 1) { - lines.append(segments); - } - else { - lines << u8"{"; - lines.append(segments); - lines << u8"}"; - } - } - lines << QString(u8"%2 = result_list_%1;").arg((uint64_t) this).arg(out_list_name); -} - -QString lib_syntax::Any::implementSegments(const QString& in_cursor_name, QList& lines) const { - return QString(); -} - Seqs::Seqs(const QList> mbrs) : mbrs_store(mbrs) { } QList> Seqs::children() const { @@ -96,28 +71,6 @@ QString Seqs::present() const { return content.mid(0, content.size() - 1); } -QList lib_syntax::Seqs::invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const { - QList lines; - - lines << QString(u8"if (%1->mustStop())").arg(in_cursor_name); - lines << QString(u8" return QList>() << %1; ").arg(in_cursor_name); - - int times = 0; - lines << QString(u8"QList> bridge_list{ %1 };").arg(in_cursor_name); - for (auto rule : children()) { - auto segments = rule->invokeSegments(in_cursor_name, QString(u8"result_list_%1").arg((uint64_t) this)); - if (segments.size() == 1) { - lines.append(segments); - } - else { - lines << u8"{"; - lines.append(segments); - lines << u8"}"; - } - } - lines << QString(u8"%2 = result_list_%1;").arg((uint64_t) this).arg(out_list_name); -} - Rept::Rept(std::shared_ptr rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) { } @@ -131,11 +84,11 @@ QList> Rept::parse(std::shared_ptr> results; QList> bridge_list{ cursor }; - // 最小重复次数匹配 + // 鏈灏忛噸澶嶆鏁板尮閰 for (auto idx = 0; idx < min_match; ++idx) { QList> current_list; - // 迭代每一次可能匹配 + // 杩唬姣忎竴娆″彲鑳藉尮閰 std::for_each(bridge_list.begin(), bridge_list.end(), [&](std::shared_ptr curs) { if (curs->mustStop()) @@ -148,31 +101,31 @@ QList> Rept::parse(std::shared_ptr ins) { return ins->mustStop(); }); - // 清除匹配失败分支 + // 娓呴櫎鍖归厤澶辫触鍒嗘敮 for (auto idx = 0; idx < bridge_list.size(); ++idx) if (bridge_list.at(idx)->mustStop()) bridge_list.removeAt(idx--); - // 不满足最小匹配 + // 涓嶆弧瓒虫渶灏忓尮閰 if (!bridge_list.size()) return results; - // 尝试重复匹配最大次数 + // 灏濊瘯閲嶅鍖归厤鏈澶ф鏁 for (auto idx = min_match; idx < max_match; ++idx) { QList> current_list; - // 匹配迭代一次 + // 鍖归厤杩唬涓娆 std::for_each(bridge_list.begin(), bridge_list.end(), [&](std::shared_ptr ins) { current_list.append(this->rule_peer->parse(ins)); }); - // 移除失败分支 + // 绉婚櫎澶辫触鍒嗘敮 for (auto idx = 0; idx < current_list.size(); ++idx) { auto rst_branch = current_list.at(idx); if (rst_branch->mustStop() && rst_branch->currentWords()) { @@ -197,13 +150,13 @@ QList> Rept::parse(std::shared_ptrrule_peer->present() + QString(u8")*"); + return "(" + this->rule_peer->present() + QString(")*"); else if (min_match == 1 && max_match == INT_MAX) - return u8"(" + this->rule_peer->present() + QString(u8")+"); + return "(" + this->rule_peer->present() + QString(")+"); else if (min_match == 0 && max_match == 1) - return u8"(" + this->rule_peer->present() + QString(u8")?"); + return "(" + this->rule_peer->present() + QString(")?"); - return u8"(" + this->rule_peer->present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match); + return "(" + this->rule_peer->present() + QString("){%1, %2}").arg(min_match).arg(max_match); } SyntaxException::SyntaxException(const QString& message) { @@ -217,12 +170,6 @@ QString SyntaxException::message() const { ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_name), mark_store(expr_mark) { } -std::shared_ptr ExprRule::reloadRule(std::shared_ptr rule) const { - auto ninst = this->make_copy(); - ninst->child_store = rule; - return ninst; -} - QString ExprRule::name() const { return name_store; } @@ -231,62 +178,7 @@ int ExprRule::typeMark() const { return this->mark_store; } -QList> ExprRule::children() const { - return QList>() << this->child_store; -} - #include -QString ExprRule::present() const { - return child_store->present(); -} - -QList lib_syntax::ExprRule::invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const { - return QList() << QString(u8"%1=exprs_%2(%3); //%4") - .arg(out_list_name).arg((uint64_t) this).arg(in_cursor_name).arg(name()); -} - -QString lib_syntax::ExprRule::implementSegments(const QString& in_cursor_name, QList& lines) const { - lines << u8"if (" + in_cursor_name + "->mustStop())"; - lines << u8" return QList>() << " + in_cursor_name + ";"; - lines << u8"auto t_this = " + in_cursor_name + "->currentToken();"; - lines << u8"auto w_this = " + in_cursor_name + "->currentWords();"; - lines << u8"auto split_begin = std::make_shared>(shared_from_this(), t_this);"; - lines << u8"auto ncursor = std::make_shared(" + in_cursor_name + ");"; - lines << u8"ncursor->setCurrent(split_begin, w_this);"; - lines << u8"ncursor->enterExprs();"; - lines << u8"auto nbranch = this->child_store->parse(ncursor);"; - lines << u8"decltype(nbranch) list_ok;"; - lines << u8"// 选择完全匹配成功的"; - lines << u8"std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok)," - "[](std::shared_ptr ins) { return !ins->exprsErrorCount(); });"; - lines << u8"if (!list_ok.size()) {"; - lines << u8" // 选择被修正的"; - lines << u8" std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok)," - "[](std::shared_ptr ins) { return !ins->mustStop(); });"; - lines << u8" // 匹配失败的"; - lines << u8" if (!list_ok.size())"; - lines << u8" list_ok = nbranch;"; - lines << u8"}"; - lines << u8"decltype(list_ok) branch_procs;"; - lines << u8"std::for_each(list_ok.begin(), list_ok.end(), [&](std::shared_ptr curs) {"; - lines << u8" if (curs->mustStop()) {"; - lines << u8" branch_procs.append(curs);"; - lines << u8" }"; - lines << u8" else {"; - lines << u8" auto t_end = curs->currentToken();"; - lines << u8" auto w_end = curs->currentWords();"; - lines << u8" auto ecursor = std::make_shared(curs);"; - lines << u8" ecursor->quitExprs();"; - lines << u8" auto split_end = std::make_shared>(split_begin, t_end);"; - lines << u8" ecursor->setCurrent(split_end, w_end);"; - lines << u8" branch_procs.append(ecursor);"; - lines << u8" }"; - lines << u8"});"; - lines << u8"return branch_procs;"; - - return QString(u8"exprs_%1").arg((uint64_t) this); -} - MatchCursor::MatchCursor(const QString& path) :_file_path(path) { } MatchCursor::MatchCursor(std::shared_ptr other_ptr) diff --git a/libSyntax/libsyntax.h b/libSyntax/libsyntax.h index c3c2ffa..bcacf30 100644 --- a/libSyntax/libsyntax.h +++ b/libSyntax/libsyntax.h @@ -11,7 +11,7 @@ namespace lib_syntax { /** - * @brief 语法异常 + * @brief 璇硶寮傚父 */ class LIBSYNTAX_EXPORT SyntaxException { private: @@ -28,9 +28,9 @@ namespace lib_syntax { - // 基础语法解析接口 ==================================================================================== + // 鍩虹璇硶瑙f瀽鎺ュ彛 ==================================================================================== /** - * @brief 解析上下文接口 + * @brief 瑙f瀽涓婁笅鏂囨帴鍙 */ class LIBSYNTAX_EXPORT MatchCursor { public: @@ -68,64 +68,43 @@ namespace lib_syntax { QString _file_path; std::shared_ptr _prev_cursor = nullptr; - QList _total_errors; // 所有解析错误 - QList> _exprs_errors; // 当前表达式解析错误 - std::shared_ptr _current_token = nullptr; // 当前Token - std::shared_ptr _remains_word = nullptr; // 剩余词语 + QList _total_errors; // 鎵鏈夎В鏋愰敊璇 + QList> _exprs_errors; // 褰撳墠琛ㄨ揪寮忚В鏋愰敊璇 + std::shared_ptr _current_token = nullptr; // 褰撳墠Token + std::shared_ptr _remains_word = nullptr; // 鍓╀綑璇嶈 bool parse_stop() const; }; /** - * @brief 基础语法匹配规则接口 + * @brief 鍩虹璇硶鍖归厤瑙勫垯鎺ュ彛 */ class IBasicRule { public: /** - * 返回匹配语法规则的词法序列表达 - * - * \return 词法表达序列 - */ - virtual QString present() const = 0; - - /** - * @brief 解析 - * @param cursor 解析游标 - * @return 返回结果<匹配分支> - */ - virtual QList> parse(std::shared_ptr cursor) const = 0; - - /** - * @brief 子规则 + * @brief 瀛愯鍒 * @return */ virtual QList> children() const = 0; /** - * @brief 翻译此规则 - * @param in_cursor_name 指定输入cursor名称 - * @param out_list_name 指定输出list名称 - * - * - * out_list_name = element_xxxxxx(in_cursor_name); - * …… - * @return 返回执行语句段 - */ - virtual QList invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const = 0; + * 杩斿洖鍖归厤璇硶瑙勫垯鐨勮瘝娉曞簭鍒楄〃杈 + * + * \return 璇嶆硶琛ㄨ揪搴忓垪 + */ + virtual QString present() const = 0; /** - * @brief 定义和实现语句 - * @param in_cursor_name 输入 - * @param lines 规则实现语句 - * - * @return 表达式规则名称 - */ - virtual QString implementSegments(const QString& in_cursor_name, QList& lines) const = 0; + * @brief 瑙f瀽 + * @param cursor 瑙f瀽娓告爣 + * @return 杩斿洖缁撴灉<鍖归厤鍒嗘敮> + */ + virtual QList> parse(std::shared_ptr cursor) const = 0; }; - // 组合语法实体解析 ===================================================================================== + // 缁勫悎璇硶瀹炰綋瑙f瀽 ===================================================================================== /** - * @brief 语法规则或匹配 + * @brief 璇硶瑙勫垯鎴栧尮閰 */ class LIBSYNTAX_EXPORT Any : public IBasicRule, public std::enable_shared_from_this { private: @@ -139,13 +118,10 @@ namespace lib_syntax { virtual QList> children() const override; virtual QList> parse(std::shared_ptr cursor) const override; virtual QString present() const override; - - virtual QList invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const override; - virtual QString implementSegments(const QString& in_cursor_name, QList& lines) const override; }; /** - * @brief 语法规则序列匹配 + * @brief 璇硶瑙勫垯搴忓垪鍖归厤 */ class LIBSYNTAX_EXPORT Seqs : public IBasicRule, public std::enable_shared_from_this { private: @@ -159,13 +135,10 @@ namespace lib_syntax { virtual QList> children() const override; virtual QList> parse(std::shared_ptr cursor) const override; virtual QString present() const override; - - virtual QList invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const override; - virtual QString implementSegments(const QString& in_cursor_name, QList& lines) const override; }; /** - * @brief 语法规则重复匹配 + * @brief 璇硶瑙勫垯閲嶅鍖归厤 */ class LIBSYNTAX_EXPORT Rept : public IBasicRule, public std::enable_shared_from_this { private: @@ -183,7 +156,7 @@ namespace lib_syntax { }; /** - * @brief 对应语法表达式解析规则 + * @brief 瀵瑰簲璇硶琛ㄨ揪寮忚В鏋愯鍒 */ class LIBSYNTAX_EXPORT ExprRule : public IBasicRule, public std::enable_shared_from_this { public: @@ -191,18 +164,9 @@ namespace lib_syntax { virtual QString name() const; virtual int typeMark() const; - virtual std::shared_ptr reloadRule(std::shared_ptr rule) const; - - // IBasicRule interface - public: - virtual QList> children() const override; - virtual QString present() const override; - virtual QList invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const override; - virtual QString implementSegments(const QString& in_cursor_name, QList &lines) const override; protected: - std::shared_ptr child_store; - virtual std::shared_ptr make_copy() const = 0; + virtual QList> expr_rule_parse(std::shared_ptr cursor) const = 0; private: QString name_store; @@ -212,10 +176,11 @@ namespace lib_syntax { /** - * @brief token匹配 + * @brief token鍖归厤 */ - template XProc = nullptr> - class TokenMatch : public IBasicRule, public std::enable_shared_from_this> { + template XProc = nullptr> + requires std::derived_from + class TokenMatch : public IBasicRule, public std::enable_shared_from_this> { private: std::shared_ptr _define_peers; @@ -225,15 +190,15 @@ namespace lib_syntax { // IBasicRule interface public: virtual QString present() const override { - return QString(u8"%1").arg(this->_define_peers->reviseWords()); + return QString("%1").arg(this->_define_peers->reviseWords()); } virtual QList> parse(std::shared_ptr current) const override { auto w_this = current->currentWords(); if (!w_this) { auto clone_ins = std::make_shared(current); - clone_ins->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,缺失\"%1\">") + clone_ins->logExprsError(QString("Syntax[0x00001]璇硶鍖归厤閿欒锛岀己澶盶"%1\">") .arg(this->_define_peers->reviseWords()).arg(current->filePath())); - clone_ins->logExprsError(QString(u8"Syntax[0x00001]输入流提前结束,<%1>").arg(current->filePath())); + clone_ins->logExprsError(QString("Syntax[0x00001]杈撳叆娴佹彁鍓嶇粨鏉,<%1>").arg(current->filePath())); return QList>() << clone_ins; } @@ -242,7 +207,7 @@ namespace lib_syntax { auto match_result = _define_peers->analysis(w_this); if (std::get<0>(match_result)) { - auto chain = std::make_shared>(std::get<0>(match_result), t_this); + auto chain = std::make_shared>(std::get<0>(match_result), t_this); auto remains = w_this->nextWord(); if (std::get<1>(match_result)) { remains = std::make_shared(std::get<1>(match_result), remains); @@ -254,42 +219,42 @@ namespace lib_syntax { } else { QList> retvals; - // 少一个 + // 灏戜竴涓 { auto short_one = std::make_shared(current); - short_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,缺失\"%1\">") + short_one->logExprsError(QString("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\">") + error_one->logExprsError(QString("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(), - QString(u8"%2_%1").arg((uint64_t) error_one.get()).arg(this->_define_peers->reviseWords()), + QString("%2_%1").arg((uint64_t) error_one.get()).arg(this->_define_peers->reviseWords()), w_this->file(), this->_define_peers); - auto tkchain = std::make_shared>(tkins, t_this); + auto tkchain = std::make_shared>(tkins, t_this); error_one->setCurrent(tkchain, w_this->nextWord()); retvals << error_one; } - // 多一个 + // 澶氫竴涓 if (w_this->nextWord()) { auto nx_word = w_this->nextWord(); auto nx_result = this->_define_peers->analysis(nx_word); if (std::get<0>(nx_result)) { - auto chain = std::make_shared>(std::get<0>(nx_result), t_this); + auto chain = std::make_shared>(std::get<0>(nx_result), t_this); auto remains = nx_word->nextWord(); if (std::get<1>(nx_result)) { remains = std::make_shared(std::get<1>(nx_result), remains); } auto clone_ins = std::make_shared(current); - clone_ins->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,请删除\"%1\">") + clone_ins->logExprsError(QString("Syntax[0x00001]璇硶鍖归厤閿欒锛岃鍒犻櫎\"%1\">") .arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file())); clone_ins->setCurrent(chain, remains); retvals << clone_ins; @@ -305,12 +270,20 @@ namespace lib_syntax { }; /** - * 语法元素解析规则. + * @brief 鍩虹妯℃澘鍖栬娉曞厓绱犺В鏋愯鍒. */ - template class ElementRule : public ExprRule { + template class ElementRule : public ExprRule { public: - ElementRule(const QString& rule_name, int expr_mark) - :ExprRule(rule_name, expr_mark) { } + ElementRule(const QString& rule_name, std::shared_ptr children) + :ExprRule(rule_name, mark), _children_store(children) { } + + virtual QList> children() const { + return QList>() << this->_children_store; + } + + virtual QString present() const { + return this->_children_store->present(); + } virtual QList> parse(std::shared_ptr cursor) const override { if (cursor->mustStop()) @@ -324,16 +297,16 @@ namespace lib_syntax { ncursor->setCurrent(split_begin, w_this); ncursor->enterExprs(); - auto nbranch = this->child_store->parse(ncursor); + auto nbranch = this->expr_rule_parse(ncursor); decltype(nbranch) list_ok; - // 选择完全匹配成功的 + // 閫夋嫨瀹屽叏鍖归厤鎴愬姛鐨 std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok), [](std::shared_ptr ins) { return !ins->exprsErrorCount(); }); if (!list_ok.size()) { - // 选择被修正的 + // 閫夋嫨琚慨姝g殑 std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok), [](std::shared_ptr ins) { return !ins->mustStop(); }); - // 匹配失败的 + // 鍖归厤澶辫触鐨 if (!list_ok.size()) list_ok = nbranch; } @@ -354,14 +327,12 @@ namespace lib_syntax { ecursor->setCurrent(split_end, w_end); branch_procs.append(ecursor); } - }); + }); return branch_procs; } protected: - virtual std::shared_ptr make_copy() const { - return std::make_shared>(this->name(), this->typeMark()); - } + std::shared_ptr _children_store; }; } // namespace lib_syntax \ No newline at end of file diff --git a/libSyntax/libtokens.h b/libSyntax/libtokens.h index dcd77ab..ce80dd3 100644 --- a/libSyntax/libtokens.h +++ b/libSyntax/libtokens.h @@ -10,7 +10,7 @@ namespace ast_basic { namespace lib_token { /** - * @brief 解析异常 + * @brief 瑙f瀽寮傚父 */ class LIBSYNTAX_EXPORT TokenException { private: @@ -26,53 +26,53 @@ namespace lib_token { - // token 解析机制抽象接口========================================== + // token 瑙f瀽鏈哄埗鎶借薄鎺ュ彛========================================== /** - * @brief token类型定义 + * @brief token绫诲瀷瀹氫箟 */ class ITokenDefine { public: /** - * @brief 整数类型标识. + * @brief 鏁存暟绫诲瀷鏍囪瘑. * * \return */ virtual int typeMark() const = 0; /** - * @brief 解析机制关键定义 + * @brief 瑙f瀽鏈哄埗鍏抽敭瀹氫箟 * @return */ virtual QString regex() const = 0; }; /** - * @brief token解析结果 + * @brief token瑙f瀽缁撴灉 */ class IToken : public lib_words::IWordBase, public std::enable_shared_from_this { public: /** - * @brief token解析机制关联 + * @brief token瑙f瀽鏈哄埗鍏宠仈 * @return */ virtual std::shared_ptr defines() const = 0; }; - // 拓展基础接口========================= + // 鎷撳睍鍩虹鎺ュ彛========================= /** - * @brief token解析机制定义 + * @brief token瑙f瀽鏈哄埗瀹氫箟 */ class ITokenProcess : public ITokenDefine { public: /** - * @brief 解析机制名称 + * @brief 瑙f瀽鏈哄埗鍚嶇О * @return */ virtual QString reviseWords() const = 0; /** - * @brief 解析词语处理过程 - * @param content 词语 - * @return tuple 解析结果 + * @brief 瑙f瀽璇嶈澶勭悊杩囩▼ + * @param content 璇嶈 + * @return tuple 瑙f瀽缁撴灉 */ virtual std::tuple, std::shared_ptr> analysis(std::shared_ptr content) const = 0; @@ -80,20 +80,20 @@ namespace lib_token { /** - * @brief 动作Token + * @brief 鍔ㄤ綔Token */ class IActionToken : public lib_token::IToken { public: /** - * @brief 上一个Token + * @brief 涓婁竴涓猅oken * @return */ virtual std::shared_ptr prevToken() const = 0; /** - * @brief 落实本Token内容到指定expr,返回完善状态后的expr - * @param expr 目标表达式实例 - * @return 完善后的表达式 + * @brief 钀藉疄鏈琓oken鍐呭鍒版寚瀹歟xpr锛岃繑鍥炲畬鍠勭姸鎬佸悗鐨別xpr + * @param expr 鐩爣琛ㄨ揪寮忓疄渚 + * @return 瀹屽杽鍚庣殑琛ㄨ揪寮 */ virtual std::shared_ptr makeSure(std::shared_ptr expr) = 0; }; diff --git a/libSyntax/syntax_novel.cpp b/libSyntax/syntax_novel.cpp index 289a940..68943b2 100644 --- a/libSyntax/syntax_novel.cpp +++ b/libSyntax/syntax_novel.cpp @@ -14,13 +14,13 @@ auto refers = std::make_shared(); // @ auto declare = std::make_shared(); // # auto split_mark = std::make_shared(); // & -auto rank_key = std::make_shared(u8"排序", 0xAEu); // 排序 -auto story_key = std::make_shared(u8"故事", 0xAAu); // 故事 +auto rank_key = std::make_shared("鎺掑簭", 0xAEu); // 鎺掑簭 +auto story_key = std::make_shared("鏁呬簨", 0xAAu); // 鏁呬簨 auto numbers = std::make_shared(); // [0-9]+ -auto slice_key = std::make_shared(u8"剧情", 0xAFu); // 剧情 -auto point_key = std::make_shared(u8"节点", 0xABu); // 节点 -auto volume_key = std::make_shared(u8"分卷", 0xACu); // 分卷 -auto article_key = std::make_shared(u8"章节", 0xADu); // 章节 +auto slice_key = std::make_shared("鍓ф儏", 0xAFu); // 鍓ф儏 +auto point_key = std::make_shared("鑺傜偣", 0xABu); // 鑺傜偣 +auto volume_key = std::make_shared("鍒嗗嵎", 0xACu); // 鍒嗗嵎 +auto article_key = std::make_shared("绔犺妭", 0xADu); // 绔犺妭 auto vtext = std::make_shared(); // ^([^\\{\\}@&]+) auto name_text = std::make_shared(); // ^([^\\{\\}@&]+) @@ -32,6 +32,8 @@ void apntk(std::shared_ptr expr, std::shared_ptr expr->addToken(t); } +// MakeRule +#define MK(type) std::make_shared() // MatchRule #define MER(E, XProc, t) std::make_shared>(t) #define MR(E, t) MER(E, apntk, t) @@ -47,22 +49,42 @@ void apntk(std::shared_ptr expr, std::shared_ptr -auto decl_expr = ElementRule(u8"decl_section", (int) NovelNode::TextSection).reloadRule( - MultiR(std::make_shared(Rules{ - MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark) - })) -); +class DeclExpr : public ElementRule { +public: + DeclExpr() : ElementRule( + "decl_section", + MultiR(std::make_shared(Rules{ + MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark) + }))) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + void point_nmset(std::shared_ptr inst, std::shared_ptr token) { inst->addToken(token); inst->setName(token->content()); } -auto point_decl = ElementRule(u8"point_define", (int) NovelNode::PointDefines).reloadRule( - std::make_shared(Rules{ - MR(PointDefines, leftb), MR(PointDefines, point_key), MER(PointDefines, point_nmset, name_text) } << - OptR(decl_expr) << - MR(PointDefines, rightb) -)); +class PointSyntax : public ElementRule { +public: + PointSyntax() : ElementRule( + "point_define", std::make_shared(Rules{ + MR(PointDefines, leftb), MR(PointDefines, point_key), MER(PointDefines, point_nmset, name_text) } << + OptR(MK(DeclExpr)) << + MR(PointDefines, rightb) + )) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + void point_ref_story_set(std::shared_ptr inst, std::shared_ptr token) { inst->addToken(token); @@ -76,80 +98,166 @@ void point_ref_point_set(std::shared_ptr inst, std::shared_ptraddToken(token); inst->setPointRefer(token->content()); } -auto point_refer = ElementRule(u8"point_refer", (int) NovelNode::PointRefers).reloadRule( - std::make_shared(Rules{ +class ReferSyntax : public ElementRule { +public: + ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers>( + "point_refer", std::make_shared(Rules{ MR(PointRefers, leftb), MR(PointRefers, refers), MR(PointRefers, point_key), MER(PointRefers, point_ref_story_set, name_text), MR(PointRefers, split_mark), MER(PointRefers, point_ref_slice_set, name_text), MR(PointRefers, split_mark), MER(PointRefers, point_ref_point_set, name_text) } << - OptR(decl_expr) << - MR(PointRefers, rightb) -)); + OptR(MK(DeclExpr)) << + MR(PointRefers, rightb))) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + void slice_nm_set(std::shared_ptr inst, std::shared_ptr token) { inst->addToken(token); inst->setName(token->content()); } -auto slice_define = ElementRule(u8"slice_define", (int) NovelNode::FragmentSlice).reloadRule( - std::make_shared(Rules{ - MR(FragmentSlice, leftb), MR(FragmentSlice, slice_key), MER(FragmentSlice, slice_nm_set, name_text) } << - OptMulR(std::make_shared(Rules{ point_decl, point_refer, decl_expr })) << - MR(FragmentSlice, rightb) -)); +class SliceSyntax : public ElementRule { +public: + SliceSyntax() : ElementRule( + "slice_define", std::make_shared(Rules{ + MR(FragmentSlice, leftb), MR(FragmentSlice, slice_key), MER(FragmentSlice, slice_nm_set, name_text) } << + OptMulR(std::make_shared(Rules{ MK(PointSyntax), MK(ReferSyntax), MK(DeclExpr) })) << + MR(FragmentSlice, rightb))) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + void story_nmset(std::shared_ptr inst, std::shared_ptr token) { inst->addToken(token); inst->setName(token->content()); } -auto story_define = ElementRule(u8"story_define", (int) NovelNode::StoryDefine).reloadRule( - std::make_shared(Rules{ +class StorySyntax : public ElementRule { +public: + StorySyntax() : ElementRule( + "story_define", + std::make_shared(Rules{ MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, name_text) } << - OptMulR(std::make_shared(Rules{ slice_define, decl_expr })) << - MR(StoryDefine, rightb) -)); -// =================================================================== + OptMulR(std::make_shared(Rules{ MK(SliceSyntax), MK(DeclExpr) })) << + MR(StoryDefine, rightb) + )) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + + + void article_nset(std::shared_ptrinst, std::shared_ptr token) { inst->addToken(token); inst->setName(token->content()); } -auto article_decl = ElementRule(u8"article_define", (int) NovelNode::ArticleDefine).reloadRule( - std::make_shared(Rules{ +class ArticleSyntax : public ElementRule { +public: + ArticleSyntax() : ElementRule( + "article_define", std::make_shared(Rules{ MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, name_text) } << - OptMulR(std::make_shared(Rules{ point_refer, decl_expr })) << - MR(ArticleDefine, rightb) -)); + OptMulR(std::make_shared(Rules{ MK(ReferSyntax), MK(DeclExpr) })) << + MR(ArticleDefine, rightb) + )) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + + void volume_nset(std::shared_ptr inst, std::shared_ptr token) { inst->addToken(token); inst->setName(token->content()); } -auto volume_decl = ElementRule(u8"volume_define", (int) NovelNode::VolumeDefine).reloadRule( - std::make_shared(Rules{ +class VolumeSyntax : public ElementRule { +public: + VolumeSyntax() : ElementRule( + "volume_define", std::make_shared(Rules{ MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, name_text) } << - OptMulR(std::make_shared(Rules{ decl_expr, article_decl })) << - MR(VolumeDefine, rightb) -)); + OptMulR(std::make_shared(Rules{ MK(DeclExpr), MK(ArticleSyntax) })) << + MR(VolumeDefine, rightb) + )) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + + + void rank_set(std::shared_ptr inst, std::shared_ptr token) { inst->addToken(token); inst->setRank(token->content().toInt()); } -auto rank_define = ElementRule(u8"rank_define", (int) NovelNode::RankDeclaration).reloadRule( - std::make_shared(Rules{ +class RankSyntax : public ElementRule { +public: + RankSyntax() : ElementRule( + "rank_define", std::make_shared(Rules{ MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, numbers) } - )); + )) { } -auto document_define = ElementRule(u8"decls-doc", (int) NovelNode::Document).reloadRule( - std::make_shared( - Rules{ - std::make_shared(rank_define, 0, 1), - MultiR(std::make_shared(Rules{story_define, volume_decl})) + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); } -)); +}; + + + +class DocumentSyntax : public ElementRule { +public: + DocumentSyntax() : ElementRule( + "decls-doc", std::make_shared(Rules{ + OptR(MK(RankSyntax)), MultiR(std::make_shared(Rules{ MK(StorySyntax), MK(VolumeSyntax) })) + })) { } + + // 閫氳繃 ElementRule 缁ф壙 + QList> expr_rule_parse(std::shared_ptr cursor) const override { + return _children_store->parse(cursor); + } +}; + +class MM : public lib_syntax::IBasicRule { + // 閫氳繃 IBasicRule 缁ф壙 + QList> children() const override { + return QList>(); + } + QString present() const override { + return QString(); + } + QList> parse(std::shared_ptr cursor) const override { + return QList>(); + } +}; + +#include "syntax_templets.h" +std::shared_ptr NovalSyntax::getSyntaxTree() { + lib_composit::TypeList vv; + + return std::make_shared(); +} -std::shared_ptr NovalSyntax::getSyntaxTree() { return document_define; } //std::shared_ptr NovalSyntax::tidy(std::shared_ptr root, QList> children) //{ // build_objecttree(root, children); @@ -176,7 +284,7 @@ std::shared_ptr NovalSyntax::getSyntaxTree() { return document_d // if (!child->isAnonymous()) { // auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child); // if (check_result) -// throw new SyntaxException(QString(u8"SyntaxError[0x0004]系统中包含同类型重名命名节点:%1(%3,%4)") +// throw new SyntaxException(QString("SyntaxError[0x0004]绯荤粺涓寘鍚悓绫诲瀷閲嶅悕鍛藉悕鑺傜偣锛%1(%3,%4)") // .arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path())); // } // diff --git a/libSyntax/syntax_novel.h b/libSyntax/syntax_novel.h index ed6086b..f03b257 100644 --- a/libSyntax/syntax_novel.h +++ b/libSyntax/syntax_novel.h @@ -10,7 +10,7 @@ namespace example_novel { class LIBSYNTAX_EXPORT NovalSyntax { public: /** - * @brief 获取novel语法解析树 + * @brief 鑾峰彇novel璇硶瑙f瀽鏍 * @return */ static std::shared_ptr getSyntaxTree(); diff --git a/libSyntax/syntax_templets.h b/libSyntax/syntax_templets.h new file mode 100644 index 0000000..ad7ece2 --- /dev/null +++ b/libSyntax/syntax_templets.h @@ -0,0 +1,51 @@ +#pragma once + +#include "libsyntax.h" +#include + +namespace lib_composit { + template class TypeList; + + template<> class TypeList<> { + public: + static QList> getRules() { + return QList>(); + } + }; + + template + requires std::derived_from + class TypeList : public TypeList { + public: + static QList> getRules() { + auto list = TypeList::getRules(); + list.prepend(std::make_shared()); + return list; + } + }; + + template + class AnyR : public lib_syntax::Any, public TypeList { + public: + AnyR() : Any(TypeList::getRules()){ } + }; + + template + class SeqsR : public lib_syntax::Seqs, public TypeList { + public: + SeqsR() : Seqs(TypeList::getRules()){ } + }; + + template + class ReptR : public lib_syntax::Rept { + public: + ReptR() : Rept(std::make_shared(), min, max){ } + }; + + template xproc = nullptr> + requires std::derived_from && std::derived_from + class TokenR : public lib_syntax::TokenMatch { + public: + TokenR() : TokenMatch(std::make_shared()){ } + }; +} diff --git a/libSyntax/tokens_impl.h b/libSyntax/tokens_impl.h index 727a85a..1ff4ae4 100644 --- a/libSyntax/tokens_impl.h +++ b/libSyntax/tokens_impl.h @@ -9,9 +9,9 @@ namespace lib_token { using TokenProcs = void(*)(std::shared_ptr expr, std::shared_ptr t); - // 基础token实现 =================================================== + // 鍩虹token瀹炵幇 =================================================== /** - * @brief 词法解析成果 + * @brief 璇嶆硶瑙f瀽鎴愭灉 */ class TokenContent : public IToken { private: @@ -21,13 +21,13 @@ namespace lib_token { public: /** - * @brief 词法解析内容 - * @param r 行 - * @param c 列 - * @param pos 位置 - * @param t 内容 + * @brief 璇嶆硶瑙f瀽鍐呭 + * @param r 琛 + * @param c 鍒 + * @param pos 浣嶇疆 + * @param t 鍐呭 * @param p path - * @param paramType 类型定义 + * @param paramType 绫诲瀷瀹氫箟 */ TokenContent(int r, int c, uint64_t pos, const QString& t, const QString& p, std::shared_ptr paramType); @@ -46,7 +46,7 @@ namespace lib_token { /** - * @brief 动作Token + * @brief 鍔ㄤ綔Token */ template proc> class ActionToken : public IActionToken { @@ -64,7 +64,7 @@ namespace lib_token { return expr; } - // 通过 IActionToken 继承 + // 閫氳繃 IActionToken 缁ф壙 QString file() const override { return _bind_content->file(); } @@ -89,7 +89,7 @@ namespace lib_token { }; /** - * @brief 表达式起始token + * @brief 琛ㄨ揪寮忚捣濮媡oken */ template class ExprBeginToken : public IActionToken { private: @@ -107,25 +107,25 @@ namespace lib_token { return this->_parent_expr; } - // 通过 IActionToken 继承 + // 閫氳繃 IActionToken 缁ф壙 QString file() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprBeginToken"); } uint64_t position() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprBeginToken"); } QString content() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprBeginToken"); } int row() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprBeginToken"); } int column() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprBeginToken"); } std::shared_ptr defines() const override { @@ -145,7 +145,7 @@ namespace lib_token { }; /** - * @brief 表达式终止token + * @brief 琛ㄨ揪寮忕粓姝oken */ template class ExprEndToken : public IActionToken { private: @@ -156,21 +156,21 @@ namespace lib_token { ExprEndToken(std::shared_ptr> start, std::shared_ptr prev) : _prev_token(prev), _self_start(start){ } - // 通过 IActionToken 继承 + // 閫氳繃 IActionToken 缁ф壙 QString file() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprEndToken"); } uint64_t position() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprEndToken"); } QString content() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprEndToken"); } int row() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprEndToken"); } int column() const override { - throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken"); + throw new lib_token::TokenException("涓嶅簲璇ョ洿鎺ヨ闂瓻xprEndToken"); } std::shared_ptr defines() const override { return nullptr; diff --git a/libSyntax/tokens_novel.cpp b/libSyntax/tokens_novel.cpp index 3fd7f75..c15df00 100644 --- a/libSyntax/tokens_novel.cpp +++ b/libSyntax/tokens_novel.cpp @@ -7,14 +7,14 @@ using namespace lib_token; using namespace lib_words; -QString LeftBracket::reviseWords() const { return u8"{"; } +QString LeftBracket::reviseWords() const { return "{"; } int LeftBracket::typeMark() const { return 0x01000000; } -QString LeftBracket::regex() const { return u8"{"; } +QString LeftBracket::regex() const { return "{"; } std::tuple, std::shared_ptr> LeftBracket::analysis(std::shared_ptr content) const { @@ -34,23 +34,23 @@ LeftBracket::analysis(std::shared_ptr content) const { return std::make_tuple(token_inst, nullptr); } -QString RightBracket::reviseWords() const { return u8"}"; } +QString RightBracket::reviseWords() const { return "}"; } int RightBracket::typeMark() const { return 0x02000000; } -QString RightBracket::regex() const { return u8"}"; } +QString RightBracket::regex() const { return "}"; } -QString ReferMark::reviseWords() const { return u8"@"; } +QString ReferMark::reviseWords() const { return "@"; } int ReferMark::typeMark() const { return 0x03000000; } -QString ReferMark::regex() const { return u8"@"; } +QString ReferMark::regex() const { return "@"; } Keywords::Keywords(const QString& val, uint type_code) : means_store(val), type_code(type_code) {} @@ -74,14 +74,14 @@ Keywords::analysis(std::shared_ptr content) const { return std::make_tuple(token_inst, nullptr); } -QString Numbers::reviseWords() const { return u8"正整数"; } +QString Numbers::reviseWords() const { return "姝f暣鏁"; } int Numbers::typeMark() const { return 0x07000000; } -QString Numbers::regex() const { return u8"^([0-9]+)$"; } +QString Numbers::regex() const { return "^([0-9]+)$"; } std::tuple, std::shared_ptr> Numbers::analysis(std::shared_ptr content) const { @@ -95,14 +95,14 @@ Numbers::analysis(std::shared_ptr content) const { return std::make_tuple(tinst, nullptr); } -QString VTextSection::reviseWords() const { return u8"文本"; } +QString VTextSection::reviseWords() const { return "鏂囨湰"; } int VTextSection::typeMark() const { return 0x09000000; } -QString VTextSection::regex() const { return u8"^([^\\{\\}@&]+)"; } +QString VTextSection::regex() const { return "^([^\\{\\}@&]+)"; } std::tuple, std::shared_ptr> VTextSection::analysis(std::shared_ptr content) const { @@ -125,23 +125,23 @@ VTextSection::analysis(std::shared_ptr content) const { return std::make_tuple(tinst, nullptr); } -QString Split::reviseWords() const { return u8"&"; } +QString Split::reviseWords() const { return "&"; } int Split::typeMark() const { return 0x05000000; } -QString Split::regex() const { return u8"&"; } +QString Split::regex() const { return "&"; } -QString NameSection::reviseWords() const { return u8"名称"; } +QString NameSection::reviseWords() const { return "鍚嶇О"; } int NameSection::typeMark() const { return 0x08000000; } -QString NameSection::regex() const { return u8"^([^\\{\\}@&]+)"; } +QString NameSection::regex() const { return "^([^\\{\\}@&]+)"; } std::tuple, std::shared_ptr > NameSection::analysis(std::shared_ptr content) const @@ -166,7 +166,7 @@ NameSection::analysis(std::shared_ptr content) const } QString DeclareSymbo::reviseWords() const { - return u8"#"; + return "#"; } int DeclareSymbo::typeMark() const @@ -176,7 +176,7 @@ int DeclareSymbo::typeMark() const QString DeclareSymbo::regex() const { - return u8"#"; + return "#"; } std::tuple, std::shared_ptr> @@ -190,7 +190,7 @@ DeclareSymbo::analysis(std::shared_ptr content) const auto remains = content->content().mid(regex().size()); auto tinst = std::make_shared(content->row(), content->column(), content->position(), - u8"#", content->file(), shared_from_this()); + "#", content->file(), shared_from_this()); if (remains.length()) { auto t_remains = std::make_shared(content->row(), content->column()+1, content->position() + regex().length(), remains, content->file()); diff --git a/libSyntax/tokens_novel.h b/libSyntax/tokens_novel.h index bfe0cb2..742eb94 100644 --- a/libSyntax/tokens_novel.h +++ b/libSyntax/tokens_novel.h @@ -88,7 +88,7 @@ namespace example_novel { class LIBSYNTAX_EXPORT DeclareSymbo : public lib_token::ITokenProcess, public std::enable_shared_from_this { public: - // 通过 TokenDefine 继承 + // 閫氳繃 TokenDefine 缁ф壙 virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; diff --git a/libToken/libtoken.cpp b/libToken/libtoken.cpp deleted file mode 100644 index 4b379f0..0000000 --- a/libToken/libtoken.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "libtoken.h" - -#include -#include - -using namespace lib_token; - diff --git a/libToken/libtoken.h b/libToken/libtoken.h deleted file mode 100644 index 39718f3..0000000 --- a/libToken/libtoken.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "libtoken_global.h" -#include -#include -#include - -namespace lib_token { - - -} // namespace lib_token \ No newline at end of file diff --git a/libWords/libWords.vcxproj b/libWords/libWords.vcxproj index ce4290d..3f108d8 100644 --- a/libWords/libWords.vcxproj +++ b/libWords/libWords.vcxproj @@ -63,6 +63,12 @@ + + + /utf-8 %(AdditionalOptions) + stdcpp20 + + true diff --git a/libWords/libwords.cpp b/libWords/libwords.cpp index 887b1f5..20c5b75 100644 --- a/libWords/libwords.cpp +++ b/libWords/libwords.cpp @@ -33,7 +33,7 @@ QList> lib_words::WordReader::extract_primary(Q #include QList> WordReader::parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const { - QRegExp split_char(u8"\\s"); + QRegExp split_char("\\s"); auto words = line_text.split(split_char, QString::SplitBehavior::SkipEmptyParts); QList> primary_words; diff --git a/libWords/libwords.h b/libWords/libwords.h index 8d8ae3d..50610b2 100644 --- a/libWords/libwords.h +++ b/libWords/libwords.h @@ -8,32 +8,32 @@ namespace lib_words { /** - * @brief 源码词语实例 + * @brief 婧愮爜璇嶈瀹炰緥 */ class IWordBase { public: /** - * @brief 源文件路径 + * @brief 婧愭枃浠惰矾寰 * @return */ virtual QString file() const = 0; /** - * @brief 获取位置 - * @return 起始位置 + * @brief 鑾峰彇浣嶇疆 + * @return 璧峰浣嶇疆 */ virtual uint64_t position() const = 0; /** - * @brief 词语内容 + * @brief 璇嶈鍐呭 * @return */ virtual QString content() const = 0; /** - * @brief 源码行号 + * @brief 婧愮爜琛屽彿 * @return */ virtual int row() const = 0; /** - * @brief 源码列号 + * @brief 婧愮爜鍒楀彿 * @return */ virtual int column() const = 0; @@ -42,7 +42,7 @@ namespace lib_words { class IPrimitiveWord : public IWordBase { public: /** - * @brief 下一个单词. + * @brief 涓嬩竴涓崟璇. * * \return */ @@ -50,7 +50,7 @@ namespace lib_words { }; /** - * @brief 未解析原始词语 + * @brief 鏈В鏋愬師濮嬭瘝璇 */ class LIBWORDS_EXPORT WordContent : public IPrimitiveWord { private: @@ -79,7 +79,7 @@ namespace lib_words { public: WordImpl(std::shared_ptr content, std::shared_ptr next); - // 通过 IWordBase 继承 + // 閫氳繃 IWordBase 缁ф壙 QString file() const override; virtual uint64_t position() const override; QString content() const override; @@ -90,7 +90,7 @@ namespace lib_words { /** - * @brief 源码词语获取器 + * @brief 婧愮爜璇嶈鑾峰彇鍣 */ class LIBWORDS_EXPORT WordReader { private: @@ -99,7 +99,7 @@ namespace lib_words { public: /** - * @brief 提取源码中所有的词语 + * @brief 鎻愬彇婧愮爜涓墍鏈夌殑璇嶈 */ std::shared_ptr wordsFrom(QTextStream& tin, const QString& path) const; }; @@ -107,7 +107,7 @@ namespace lib_words { /** - * @brief 解析异常 + * @brief 瑙f瀽寮傚父 */ class LIBWORDS_EXPORT WordsException { private: