From f96bc43e97aca43b4c1654438a7057622250144d Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Sun, 2 Feb 2025 20:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WsNovelParser.sln | 24 ++-- WsNovelParser/WsNovelParser.vcxproj | 4 +- WsNovelParser/main.cpp | 2 +- WsNovelParser/novelparser.cpp | 2 +- libParse/libParse.vcxproj | 4 +- libSyntax/ast_basic.h | 2 +- libSyntax/ast_novel.h | 4 +- libSyntax/libSyntax.vcxproj | 8 +- libSyntax/libSyntax.vcxproj.filters | 12 ++ libSyntax/libsyntax.cpp | 2 + libSyntax/libsyntax.h | 29 ++--- libSyntax/libtokens.cpp | 9 ++ libSyntax/libtokens.h | 64 ++++++++++ libSyntax/syntax_novel.h | 2 +- {libToken => libSyntax}/tokens_novel.cpp | 2 + {libToken => libSyntax}/tokens_novel.h | 46 ++++--- libToken/libtoken.cpp | 112 ----------------- libToken/libtoken.h | 146 ----------------------- libWords/libWords.vcxproj | 107 +++++++++++++++++ libWords/libWords.vcxproj.filters | 36 ++++++ libWords/libWords.vcxproj.user | 12 ++ libWords/libwords.cpp | 121 +++++++++++++++++++ libWords/libwords.h | 115 ++++++++++++++++++ libWords/libwords_global.h | 13 ++ 24 files changed, 556 insertions(+), 322 deletions(-) create mode 100644 libSyntax/libtokens.cpp create mode 100644 libSyntax/libtokens.h rename {libToken => libSyntax}/tokens_novel.cpp (99%) rename {libToken => libSyntax}/tokens_novel.h (67%) create mode 100644 libWords/libWords.vcxproj create mode 100644 libWords/libWords.vcxproj.filters create mode 100644 libWords/libWords.vcxproj.user create mode 100644 libWords/libwords.cpp create mode 100644 libWords/libwords.h create mode 100644 libWords/libwords_global.h diff --git a/WsNovelParser.sln b/WsNovelParser.sln index 3577028..e20c69d 100644 --- a/WsNovelParser.sln +++ b/WsNovelParser.sln @@ -7,16 +7,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WsNovelParser", "WsNovelPar ProjectSection(ProjectDependencies) = postProject {1FF80476-26C9-42FB-BFF6-D587C4941964} = {1FF80476-26C9-42FB-BFF6-D587C4941964} {C3AADEB5-3695-4DF4-B8E1-D37F928F3B2F} = {C3AADEB5-3695-4DF4-B8E1-D37F928F3B2F} - {DAB406C7-174A-47C3-893C-343079396350} = {DAB406C7-174A-47C3-893C-343079396350} {EF557F71-99AA-4F2B-A5F5-1A4518A11C19} = {EF557F71-99AA-4F2B-A5F5-1A4518A11C19} EndProjectSection EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libToken", "libToken\libToken.vcxproj", "{DAB406C7-174A-47C3-893C-343079396350}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libSyntax", "libSyntax\libSyntax.vcxproj", "{EF557F71-99AA-4F2B-A5F5-1A4518A11C19}" - ProjectSection(ProjectDependencies) = postProject - {DAB406C7-174A-47C3-893C-343079396350} = {DAB406C7-174A-47C3-893C-343079396350} - EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libParse", "libParse\libParse.vcxproj", "{C3AADEB5-3695-4DF4-B8E1-D37F928F3B2F}" ProjectSection(ProjectDependencies) = postProject @@ -38,6 +32,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WsNovelManager", "WsNovelMa {1FF80476-26C9-42FB-BFF6-D587C4941964} = {1FF80476-26C9-42FB-BFF6-D587C4941964} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libWords", "libWords\libWords.vcxproj", "{386F6D42-C6EB-4973-9511-181472391B21}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -54,14 +50,6 @@ Global {1EF577E8-D92D-4926-9207-1567137BB672}.Release|x64.Build.0 = Release|x64 {1EF577E8-D92D-4926-9207-1567137BB672}.Release|x86.ActiveCfg = Release|x64 {1EF577E8-D92D-4926-9207-1567137BB672}.Release|x86.Build.0 = Release|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Debug|x64.ActiveCfg = Debug|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Debug|x64.Build.0 = Debug|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Debug|x86.ActiveCfg = Debug|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Debug|x86.Build.0 = Debug|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Release|x64.ActiveCfg = Release|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Release|x64.Build.0 = Release|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Release|x86.ActiveCfg = Release|x64 - {DAB406C7-174A-47C3-893C-343079396350}.Release|x86.Build.0 = Release|x64 {EF557F71-99AA-4F2B-A5F5-1A4518A11C19}.Debug|x64.ActiveCfg = Debug|x64 {EF557F71-99AA-4F2B-A5F5-1A4518A11C19}.Debug|x64.Build.0 = Debug|x64 {EF557F71-99AA-4F2B-A5F5-1A4518A11C19}.Debug|x86.ActiveCfg = Debug|x64 @@ -102,6 +90,14 @@ Global {DD802A96-BBB6-47CD-9B64-3582FD1805F3}.Release|x64.Build.0 = Release|x64 {DD802A96-BBB6-47CD-9B64-3582FD1805F3}.Release|x86.ActiveCfg = Release|x64 {DD802A96-BBB6-47CD-9B64-3582FD1805F3}.Release|x86.Build.0 = Release|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Debug|x64.ActiveCfg = Debug|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Debug|x64.Build.0 = Debug|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Debug|x86.ActiveCfg = Debug|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Debug|x86.Build.0 = Debug|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Release|x64.ActiveCfg = Release|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Release|x64.Build.0 = Release|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Release|x86.ActiveCfg = Release|x64 + {386F6D42-C6EB-4973-9511-181472391B21}.Release|x86.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WsNovelParser/WsNovelParser.vcxproj b/WsNovelParser/WsNovelParser.vcxproj index 717df7d..9c89375 100644 --- a/WsNovelParser/WsNovelParser.vcxproj +++ b/WsNovelParser/WsNovelParser.vcxproj @@ -66,7 +66,7 @@ nsc - $(SolutionDir)libToken;$(SolutionDir)libSyntax;$(SolutionDir)ArgsParser;$(SolutionDir)libParse;$(IncludePath) + $(SolutionDir)libWords;$(SolutionDir)libSyntax;$(SolutionDir)ArgsParser;$(SolutionDir)libParse;$(IncludePath) $(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath) nsc @@ -80,7 +80,7 @@ - libToken.lib;libSyntax.lib;libParse.lib;ArgsParser.lib;%(AdditionalDependencies);$(Qt_LIBS_) + libWords.lib;libSyntax.lib;libParse.lib;ArgsParser.lib;%(AdditionalDependencies);$(Qt_LIBS_) diff --git a/WsNovelParser/main.cpp b/WsNovelParser/main.cpp index 63d1586..31c3abb 100644 --- a/WsNovelParser/main.cpp +++ b/WsNovelParser/main.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/WsNovelParser/novelparser.cpp b/WsNovelParser/novelparser.cpp index f994df3..8376fe9 100644 --- a/WsNovelParser/novelparser.cpp +++ b/WsNovelParser/novelparser.cpp @@ -36,7 +36,7 @@ std::shared_ptr NovelParser::parserContext() const QList> NovelParser::parse(const QFileInfoList source_list) const { const_cast(this)->context = std::make_shared(u8"С˵"); - auto word_reader = std::make_shared(); + auto word_reader = std::make_shared(); auto time_stamp = QTime::currentTime(); for (auto& file : source_list) { diff --git a/libParse/libParse.vcxproj b/libParse/libParse.vcxproj index f13e397..02b20d9 100644 --- a/libParse/libParse.vcxproj +++ b/libParse/libParse.vcxproj @@ -65,7 +65,7 @@ $(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath) - $(SolutionDir)libToken\;$(SolutionDir)libSyntax\;$(IncludePath) + $(SolutionDir)libWords\;$(SolutionDir)libSyntax\;$(IncludePath) $(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath) @@ -78,7 +78,7 @@ - libToken.lib;libSyntax.lib;%(AdditionalDependencies);$(Qt_LIBS_) + libWords.lib;libSyntax.lib;%(AdditionalDependencies);$(Qt_LIBS_) diff --git a/libSyntax/ast_basic.h b/libSyntax/ast_basic.h index 6a73f79..200e4c9 100644 --- a/libSyntax/ast_basic.h +++ b/libSyntax/ast_basic.h @@ -2,7 +2,7 @@ #include #include -#include +#include "libtokens.h" #include "libsyntax.h" namespace ast_basic { diff --git a/libSyntax/ast_novel.h b/libSyntax/ast_novel.h index aad8d59..e758a3c 100644 --- a/libSyntax/ast_novel.h +++ b/libSyntax/ast_novel.h @@ -182,8 +182,8 @@ namespace lib_syntax { return std::make_shared>(name(), typeMark()); } - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override { + virtual std::tuple> + parse(std::shared_ptr rt_inst, std::shared_ptr head) const override { std::shared_ptr elm_ast = this->newEmptyInstance(); auto text_present = this->token_present(); diff --git a/libSyntax/libSyntax.vcxproj b/libSyntax/libSyntax.vcxproj index 460ffac..99945be 100644 --- a/libSyntax/libSyntax.vcxproj +++ b/libSyntax/libSyntax.vcxproj @@ -65,7 +65,7 @@ $(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath) - $(SolutionDir)libToken\;$(IncludePath) + $(SolutionDir)libWords\;$(IncludePath) $(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath) @@ -78,7 +78,7 @@ - libToken.lib;%(AdditionalDependencies);$(Qt_LIBS_) + libWords.lib;%(AdditionalDependencies);$(Qt_LIBS_) @@ -108,7 +108,9 @@ + + @@ -118,7 +120,9 @@ + + diff --git a/libSyntax/libSyntax.vcxproj.filters b/libSyntax/libSyntax.vcxproj.filters index 9f5397c..51732a2 100644 --- a/libSyntax/libSyntax.vcxproj.filters +++ b/libSyntax/libSyntax.vcxproj.filters @@ -44,6 +44,12 @@ Header Files + + Header Files + + + Header Files + @@ -58,5 +64,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file diff --git a/libSyntax/libsyntax.cpp b/libSyntax/libsyntax.cpp index 4dfcc5c..d0bf1fa 100644 --- a/libSyntax/libsyntax.cpp +++ b/libSyntax/libsyntax.cpp @@ -1,10 +1,12 @@ #include "libsyntax.h" #include "ast_basic.h" +#include #include using namespace lib_syntax; using namespace std; 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) {} diff --git a/libSyntax/libsyntax.h b/libSyntax/libsyntax.h index 96185b0..e91a065 100644 --- a/libSyntax/libsyntax.h +++ b/libSyntax/libsyntax.h @@ -1,8 +1,9 @@ #pragma once #include "libsyntax_global.h" -#include +#include "libtokens.h" #include +#include #include #include namespace ast_basic { @@ -87,8 +88,8 @@ namespace lib_syntax { * @param head ÁбíÍ· * @return ·µ»Ø½á¹û<Æ¥ÅäÍê³ÉÐÂÁбíÍ·,Æ¥Å䳤¶È> */ - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const = 0; + virtual std::tuple> + parse(std::shared_ptr rt_inst, std::shared_ptr head) const = 0; /** * ·µ»ØÆ¥ÅäÓï·¨¹æÔòµÄ´Ê·¨ÐòÁбí´ï @@ -115,7 +116,7 @@ namespace lib_syntax { // 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 { + 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); @@ -130,7 +131,7 @@ namespace lib_syntax { } if (std::get<1>(match_result)) { - return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared(std::get<1>(match_result), head->nextWord())); + 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()); @@ -156,7 +157,7 @@ namespace lib_syntax { private: QList> mbrs_store; - std::tuple, std::shared_ptr> rule_select(std::shared_ptr head) const; + std::tuple, std::shared_ptr> rule_select(std::shared_ptr head) const; public: Any(const QList> mbrs); @@ -164,8 +165,8 @@ namespace lib_syntax { // IBasicRule interface public: virtual QList> children() const override; - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + virtual std::tuple> + parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -182,8 +183,8 @@ namespace lib_syntax { // IBasicRule interface public: virtual QList> children() const override; - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + virtual std::tuple> + parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -201,8 +202,8 @@ namespace lib_syntax { // IBasicRule interface public: virtual QList> children() const override; - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + virtual std::tuple> + parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; }; @@ -225,8 +226,8 @@ namespace lib_syntax { // IBasicRule interface public: virtual QList> children() const override; - virtual std::tuple> - parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; + virtual std::tuple> + parse(std::shared_ptr rt_inst, std::shared_ptr head) const override; virtual QString token_present() const override; protected: diff --git a/libSyntax/libtokens.cpp b/libSyntax/libtokens.cpp new file mode 100644 index 0000000..cde63a1 --- /dev/null +++ b/libSyntax/libtokens.cpp @@ -0,0 +1,9 @@ +#include "libtokens.h" + +using namespace lib_token; + +TokenException::TokenException(const QString& message) : msg_store(message) { } + +QString TokenException::message() const { + return msg_store; +} diff --git a/libSyntax/libtokens.h b/libSyntax/libtokens.h new file mode 100644 index 0000000..5287712 --- /dev/null +++ b/libSyntax/libtokens.h @@ -0,0 +1,64 @@ +#pragma once + +#include "libsyntax_global.h" +#include + +namespace lib_token { + + /** + * @brief ½âÎöÒì³£ + */ + class LIBSYNTAX_EXPORT TokenException { + private: + QString msg_store; + + public: + TokenException(const QString& message); + virtual QString message() const; + }; + + class IToken; + /** + * @brief token½âÎö»úÖÆ¶¨Òå + */ + class ITokenDefine { + public: + /** + * @brief ½âÎö»úÖÆÃû³Æ + * @return + */ + virtual QString reviseWords() const = 0; + /** + * @brief ÕûÊýÀàÐͱêʶ. + * + * \return + */ + virtual int typeMark() const = 0; + /** + * @brief ½âÎö»úÖÆ¹Ø¼ü¶¨Òå + * @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½âÎö½á¹û + */ + class IToken : public lib_words::IWordBase { + public: + /** + * @brief token½âÎö»úÖÆ¹ØÁª + * @return + */ + virtual std::shared_ptr define() const = 0; + }; +} \ No newline at end of file diff --git a/libSyntax/syntax_novel.h b/libSyntax/syntax_novel.h index 340cf33..d221d85 100644 --- a/libSyntax/syntax_novel.h +++ b/libSyntax/syntax_novel.h @@ -3,7 +3,7 @@ #include "ast_basic.h" #include "ast_gen.h" #include "libsyntax.h" -#include +#include "libtokens.h" #include namespace example_novel { diff --git a/libToken/tokens_novel.cpp b/libSyntax/tokens_novel.cpp similarity index 99% rename from libToken/tokens_novel.cpp rename to libSyntax/tokens_novel.cpp index eb02bf7..1cfda99 100644 --- a/libToken/tokens_novel.cpp +++ b/libSyntax/tokens_novel.cpp @@ -1,7 +1,9 @@ #include "tokens_novel.h" +#include using namespace example_novel; using namespace lib_token; +using namespace lib_words; TokenContent::TokenContent(int r, int c, uint64_t pos, const QString& t, const QString& p, std::shared_ptr paramType) diff --git a/libToken/tokens_novel.h b/libSyntax/tokens_novel.h similarity index 67% rename from libToken/tokens_novel.h rename to libSyntax/tokens_novel.h index 8325685..931cb36 100644 --- a/libToken/tokens_novel.h +++ b/libSyntax/tokens_novel.h @@ -1,8 +1,6 @@ #pragma once - -#include "libToken_global.h" -#include "libtoken.h" +#include "libtokens.h" #include #include #include @@ -36,38 +34,38 @@ namespace lib_token { } namespace example_novel { - class LIBTOKEN_EXPORT LeftBracket : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT LeftBracket : public lib_token::ITokenDefine, 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 LIBTOKEN_EXPORT RightBracket : public LeftBracket { + class LIBSYNTAX_EXPORT RightBracket : public LeftBracket { public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; }; - class LIBTOKEN_EXPORT ReferMark : public LeftBracket { + class LIBSYNTAX_EXPORT ReferMark : public LeftBracket { public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; }; - class LIBTOKEN_EXPORT Split : public LeftBracket { + class LIBSYNTAX_EXPORT Split : public LeftBracket { public: virtual QString reviseWords() const override; virtual int typeMark() const override; virtual QString regex() const override; }; - class LIBTOKEN_EXPORT Keywords : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT Keywords : public lib_token::ITokenDefine, public std::enable_shared_from_this { private: QString means_store; int type_code; @@ -80,49 +78,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 LIBTOKEN_EXPORT Numbers : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT Numbers : public lib_token::ITokenDefine, 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 LIBTOKEN_EXPORT NameSection : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT NameSection : public lib_token::ITokenDefine, 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 LIBTOKEN_EXPORT VTextSection : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT VTextSection : public lib_token::ITokenDefine, 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 LIBTOKEN_EXPORT DeclareSymbo : public lib_token::ITokenDefine, public std::enable_shared_from_this { + class LIBSYNTAX_EXPORT DeclareSymbo : public lib_token::ITokenDefine, 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/libToken/libtoken.cpp b/libToken/libtoken.cpp index 943f7fa..4b379f0 100644 --- a/libToken/libtoken.cpp +++ b/libToken/libtoken.cpp @@ -5,115 +5,3 @@ using namespace lib_token; -std::shared_ptr WordReader::wordsFrom(const QString& path) const { - auto primary_words = extract_primary(path); - if (!primary_words.size()) - return nullptr; - - std::shared_ptr prev_ptr = std::make_shared(primary_words.last(), nullptr); - for (auto idx = primary_words.size() - 2; idx >= 0; --idx) { - auto content_ptr = primary_words[idx]; - prev_ptr = std::make_shared(content_ptr, prev_ptr); - } - - return prev_ptr; -} - -QList> WordReader::extract_primary(const QString& path) const { - QFile file(path); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - throw new TokenException(u8"Lex[0x0000]Ö¸¶¨ÎļþÎÞ·¨´ò¿ª£º" + path); - } - - QTextStream tin(&file); - tin.setCodec("UTF-8"); - - QList> ret_list; - int line_number = 1; - while (!tin.atEnd()) { - uint64_t relative_offset = line_number; - relative_offset = relative_offset << 32; - auto line = tin.readLine(); - ret_list.append(this->parse_line(relative_offset, line_number++, line, path)); - } - - return ret_list; -} - -#include -QList> WordReader::parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const { - QRegExp split_char(u8"\\s"); - auto words = line_text.split(split_char, QString::SplitBehavior::SkipEmptyParts); - - QList> primary_words; - int columns_offset = 0; - for (auto& w : words) { - auto column_start = line_text.indexOf(w, columns_offset); - auto token = std::make_shared(row, column_start + 1, start_pos + column_start, w, path); - primary_words << token; - - columns_offset = column_start + w.length(); - } - - return primary_words; -} - -TokenException::TokenException(const QString& message) : msg_store(message) { } - -QString TokenException::message() const { - return msg_store; -} - -WordContent::WordContent(int r, int c, uint64_t pos, const QString& t, const QString& p) - : row_n(r), col_n(c), doc_offset(pos), text_n(t), path_p(p) { } - -QString WordContent::file() const { - return path_p; -} - -uint64_t lib_token::WordContent::position() const { - return doc_offset; -} - -QString WordContent::content() const { - return text_n; -} - -int WordContent::row() const { - return row_n; -} - -int WordContent::column() const { - return col_n; -} - -std::shared_ptr WordContent::nextWord() const { - return nullptr; -} - -WordImpl::WordImpl(std::shared_ptr content, std::shared_ptr next) - : content_ptr(content), next_ptr(next) { } - -QString WordImpl::file() const { - return content_ptr->file(); -} - -uint64_t lib_token::WordImpl::position() const { - return content_ptr->position(); -} - -QString WordImpl::content() const { - return content_ptr->content(); -} - -int WordImpl::row() const { - return content_ptr->row(); -} - -int WordImpl::column() const { - return content_ptr->column(); -} - -std::shared_ptr WordImpl::nextWord() const { - return next_ptr; -} diff --git a/libToken/libtoken.h b/libToken/libtoken.h index 2f7d6ef..39718f3 100644 --- a/libToken/libtoken.h +++ b/libToken/libtoken.h @@ -6,152 +6,6 @@ #include namespace lib_token { - /** - * @brief Ô´Âë´ÊÓïʵÀý - */ - class IWordBase { - public: - /** - * @brief Ô´Îļþ·¾¶ - * @return - */ - virtual QString file() const = 0; - /** - * @brief »ñȡλÖà - * @return ÆðʼλÖà - */ - virtual uint64_t position() const = 0; - /** - * @brief ´ÊÓïÄÚÈÝ - * @return - */ - virtual QString content() const = 0; - /** - * @brief Ô´ÂëÐкŠ- * @return - */ - virtual int row() const = 0; - /** - * @brief Ô´ÂëÁкŠ- * @return - */ - virtual int column() const = 0; - /** - * @brief ÏÂÒ»¸öµ¥´Ê. - * - * \return - */ - virtual std::shared_ptr nextWord() const = 0; - }; - /** - * @brief δ½âÎöԭʼ´ÊÓï - */ - class WordContent : public IWordBase { - private: - int row_n, col_n; - uint64_t doc_offset; - QString text_n, path_p; - - public: - WordContent(int r, int c, uint64_t pos, const QString& t, const QString& p); - - // 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; - }; - - class LIBTOKEN_EXPORT WordImpl : public IWordBase { - private: - std::shared_ptr content_ptr; - std::shared_ptr next_ptr; - - public: - WordImpl(std::shared_ptr content, std::shared_ptr next); - - // ͨ¹ý IWordBase ¼Ì³Ð - QString file() const override; - virtual uint64_t position() const override; - QString content() const override; - int row() const override; - int column() const override; - std::shared_ptr nextWord() const override; - }; - - /** - * @brief ½âÎöÒì³£ - */ - class LIBTOKEN_EXPORT TokenException { - private: - QString msg_store; - - public: - TokenException(const QString& message); - virtual QString message() const; - }; - class ITokenDefine; - /** - * @brief token½âÎö½á¹û - */ - class IToken : public IWordBase { - public: - /** - * @brief token½âÎö»úÖÆ¹ØÁª - * @return - */ - virtual std::shared_ptr define() const = 0; - }; - - /** - * @brief token½âÎö»úÖÆ¶¨Òå - */ - class ITokenDefine { - public: - /** - * @brief ½âÎö»úÖÆÃû³Æ - * @return - */ - virtual QString reviseWords() const = 0; - /** - * @brief ÕûÊýÀàÐͱêʶ. - * - * \return - */ - virtual int typeMark() const = 0; - /** - * @brief ½âÎö»úÖÆ¹Ø¼ü¶¨Òå - * @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 Ô´Âë´ÊÓï»ñÈ¡Æ÷ - */ - class LIBTOKEN_EXPORT WordReader { - private: - QList> parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const; - QList> extract_primary(const QString& path) const; - - public: - /** - * @brief ÌáȡԴÂëÖÐËùÓеĴÊÓï - */ - std::shared_ptr wordsFrom(const QString &path) const; - }; } // namespace lib_token \ No newline at end of file diff --git a/libWords/libWords.vcxproj b/libWords/libWords.vcxproj new file mode 100644 index 0000000..ce4290d --- /dev/null +++ b/libWords/libWords.vcxproj @@ -0,0 +1,107 @@ + + + + + Release + x64 + + + Debug + x64 + + + + {386F6D42-C6EB-4973-9511-181472391B21} + QtVS_v304 + 10.0 + 10.0 + $(MSBuildProjectDirectory)\QtMsBuild + + + + DynamicLibrary + v143 + false + true + Unicode + + + DynamicLibrary + v143 + true + Unicode + + + + + + + 5.12.11_msvc2017_64 + core + release + + + 5.12.11_msvc2017_64 + core + debug + + + + + + + + + + + + + + + + + + + + + + true + LIBWORDS_LIB;%(PreprocessorDefinitions) + Level3 + true + true + true + true + + + Windows + false + true + true + + + + + true + LIBWORDS_LIB;%(PreprocessorDefinitions) + Level3 + true + true + + + Windows + true + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libWords/libWords.vcxproj.filters b/libWords/libWords.vcxproj.filters new file mode 100644 index 0000000..eddb843 --- /dev/null +++ b/libWords/libWords.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + qml;cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {99349809-55BA-4b9d-BF79-8FDBB0286EB3} + ui + + + {639EADAA-A684-42e4-A9AD-28FC9BCB8F7C} + ts + + + + + Header Files + + + Source Files + + + Header Files + + + \ No newline at end of file diff --git a/libWords/libWords.vcxproj.user b/libWords/libWords.vcxproj.user new file mode 100644 index 0000000..41a709a --- /dev/null +++ b/libWords/libWords.vcxproj.user @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/libWords/libwords.cpp b/libWords/libwords.cpp new file mode 100644 index 0000000..c9409af --- /dev/null +++ b/libWords/libwords.cpp @@ -0,0 +1,121 @@ +#include "libwords.h" +#include +#include + +using namespace lib_words; + + +std::shared_ptr WordReader::wordsFrom(const QString& path) const { + auto primary_words = extract_primary(path); + if (!primary_words.size()) + return nullptr; + + std::shared_ptr prev_ptr = std::make_shared(primary_words.last(), nullptr); + for (auto idx = primary_words.size() - 2; idx >= 0; --idx) { + auto content_ptr = primary_words[idx]; + prev_ptr = std::make_shared(content_ptr, prev_ptr); + } + + return prev_ptr; +} + +QList> WordReader::extract_primary(const QString& path) const { + QFile file(path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + throw new WordsException(u8"Lex[0x0000]Ö¸¶¨ÎļþÎÞ·¨´ò¿ª£º" + path); + } + + QTextStream tin(&file); + tin.setCodec("UTF-8"); + + QList> ret_list; + int line_number = 1; + while (!tin.atEnd()) { + uint64_t relative_offset = line_number; + relative_offset = relative_offset << 32; + auto line = tin.readLine(); + ret_list.append(this->parse_line(relative_offset, line_number++, line, path)); + } + + return ret_list; +} + +#include +QList> WordReader::parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const { + QRegExp split_char(u8"\\s"); + auto words = line_text.split(split_char, QString::SplitBehavior::SkipEmptyParts); + + QList> primary_words; + int columns_offset = 0; + for (auto& w : words) { + auto column_start = line_text.indexOf(w, columns_offset); + auto token = std::make_shared(row, column_start + 1, start_pos + column_start, w, path); + primary_words << token; + + columns_offset = column_start + w.length(); + } + + return primary_words; +} + +WordImpl::WordImpl(std::shared_ptr content, std::shared_ptr next) + : content_ptr(content), next_ptr(next) { } + +QString WordImpl::file() const { + return content_ptr->file(); +} + +uint64_t WordImpl::position() const { + return content_ptr->position(); +} + +QString WordImpl::content() const { + return content_ptr->content(); +} + +int WordImpl::row() const { + return content_ptr->row(); +} + +int WordImpl::column() const { + return content_ptr->column(); +} + +std::shared_ptr WordImpl::nextWord() const { + return next_ptr; +} + +WordsException::WordsException(const QString& message) + : msg_store(message) { } + +QString WordsException::message() const { + return msg_store; +} + + +WordContent::WordContent(int r, int c, uint64_t pos, const QString& t, const QString& p) + : row_n(r), col_n(c), doc_offset(pos), text_n(t), path_p(p) { } + +QString WordContent::file() const { + return path_p; +} + +uint64_t WordContent::position() const { + return doc_offset; +} + +QString WordContent::content() const { + return text_n; +} + +int WordContent::row() const { + return row_n; +} + +int WordContent::column() const { + return col_n; +} + +std::shared_ptr WordContent::nextWord() const { + return nullptr; +} diff --git a/libWords/libwords.h b/libWords/libwords.h new file mode 100644 index 0000000..734c0d6 --- /dev/null +++ b/libWords/libwords.h @@ -0,0 +1,115 @@ +#pragma once + +#include "libwords_global.h" +#include +#include + + +namespace lib_words { + /** + * @brief Ô´Âë´ÊÓïʵÀý + */ + class IWordBase { + public: + /** + * @brief Ô´Îļþ·¾¶ + * @return + */ + virtual QString file() const = 0; + /** + * @brief »ñȡλÖà + * @return ÆðʼλÖà + */ + virtual uint64_t position() const = 0; + /** + * @brief ´ÊÓïÄÚÈÝ + * @return + */ + virtual QString content() const = 0; + /** + * @brief Ô´ÂëÐкŠ+ * @return + */ + virtual int row() const = 0; + /** + * @brief Ô´ÂëÁкŠ+ * @return + */ + virtual int column() const = 0; + /** + * @brief ÏÂÒ»¸öµ¥´Ê. + * + * \return + */ + virtual std::shared_ptr nextWord() const = 0; + }; + + /** + * @brief δ½âÎöԭʼ´ÊÓï + */ + class LIBWORDS_EXPORT WordContent : public IWordBase { + private: + int row_n, col_n; + uint64_t doc_offset; + QString text_n, path_p; + + public: + WordContent(int r, int c, uint64_t pos, const QString& t, const QString& p); + + // 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; + }; + + class LIBWORDS_EXPORT WordImpl : public IWordBase { + private: + std::shared_ptr content_ptr; + std::shared_ptr next_ptr; + + public: + WordImpl(std::shared_ptr content, std::shared_ptr next); + + // ͨ¹ý IWordBase ¼Ì³Ð + QString file() const override; + virtual uint64_t position() const override; + QString content() const override; + int row() const override; + int column() const override; + std::shared_ptr nextWord() const override; + }; + + + /** + * @brief Ô´Âë´ÊÓï»ñÈ¡Æ÷ + */ + class LIBWORDS_EXPORT WordReader { + private: + QList> parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const; + QList> extract_primary(const QString& path) const; + + public: + /** + * @brief ÌáȡԴÂëÖÐËùÓеĴÊÓï + */ + std::shared_ptr wordsFrom(const QString& path) const; + }; + + + + /** + * @brief ½âÎöÒì³£ + */ + class LIBWORDS_EXPORT WordsException { + private: + QString msg_store; + + public: + WordsException(const QString& message); + virtual QString message() const; + }; +} \ No newline at end of file diff --git a/libWords/libwords_global.h b/libWords/libwords_global.h new file mode 100644 index 0000000..c926c14 --- /dev/null +++ b/libWords/libwords_global.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +#ifndef BUILD_STATIC +# if defined(LIBWORDS_LIB) +# define LIBWORDS_EXPORT Q_DECL_EXPORT +# else +# define LIBWORDS_EXPORT Q_DECL_IMPORT +# endif +#else +# define LIBWORDS_EXPORT +#endif