调整代码
This commit is contained in:
parent
32e5ea34ed
commit
f96bc43e97
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@
|
|||
<TargetName>nsc</TargetName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<IncludePath>$(SolutionDir)libToken;$(SolutionDir)libSyntax;$(SolutionDir)ArgsParser;$(SolutionDir)libParse;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)libWords;$(SolutionDir)libSyntax;$(SolutionDir)ArgsParser;$(SolutionDir)libParse;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
|
||||
<TargetName>nsc</TargetName>
|
||||
</PropertyGroup>
|
||||
|
|
@ -80,7 +80,7 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Link>
|
||||
<AdditionalDependencies>libToken.lib;libSyntax.lib;libParse.lib;ArgsParser.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
|
||||
<AdditionalDependencies>libWords.lib;libSyntax.lib;libParse.lib;ArgsParser.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include <QFileInfoList>
|
||||
#include <ast_gen.h>
|
||||
#include <ast_novel.h>
|
||||
#include <libtoken.h>
|
||||
#include <libtokens.h>
|
||||
#include <syntax_novel.h>
|
||||
#include <tokens_novel.h>
|
||||
#include <QDir>
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ std::shared_ptr<lib_syntax::IContext> NovelParser::parserContext() const
|
|||
|
||||
QList<std::shared_ptr<const ast_basic::IExprInst>> NovelParser::parse(const QFileInfoList source_list) const {
|
||||
const_cast<NovelParser*>(this)->context = std::make_shared<ast_gen::GlobalElement>(u8"С˵");
|
||||
auto word_reader = std::make_shared<lib_token::WordReader>();
|
||||
auto word_reader = std::make_shared<lib_words::WordReader>();
|
||||
|
||||
auto time_stamp = QTime::currentTime();
|
||||
for (auto& file : source_list) {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@
|
|||
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<IncludePath>$(SolutionDir)libToken\;$(SolutionDir)libSyntax\;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)libWords\;$(SolutionDir)libSyntax\;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
|
|
@ -78,7 +78,7 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Link>
|
||||
<AdditionalDependencies>libToken.lib;libSyntax.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
|
||||
<AdditionalDependencies>libWords.lib;libSyntax.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <memory>
|
||||
#include <QList>
|
||||
#include <libtoken.h>
|
||||
#include "libtokens.h"
|
||||
#include "libsyntax.h"
|
||||
|
||||
namespace ast_basic {
|
||||
|
|
|
|||
|
|
@ -182,8 +182,8 @@ namespace lib_syntax {
|
|||
return std::make_shared<ElementRule<example_novel::TextSection>>(name(), typeMark());
|
||||
}
|
||||
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override {
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const override {
|
||||
std::shared_ptr<ast_basic::IExprInst> elm_ast = this->newEmptyInstance();
|
||||
auto text_present = this->token_present();
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@
|
|||
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<IncludePath>$(SolutionDir)libToken\;$(IncludePath)</IncludePath>
|
||||
<IncludePath>$(SolutionDir)libWords\;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
|
|
@ -78,7 +78,7 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Link>
|
||||
<AdditionalDependencies>libToken.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
|
||||
<AdditionalDependencies>libWords.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
|
||||
|
|
@ -108,7 +108,9 @@
|
|||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="libtokens.cpp" />
|
||||
<ClCompile Include="syntax_novel.cpp" />
|
||||
<ClCompile Include="tokens_novel.cpp" />
|
||||
<ClInclude Include="ast_basic.h" />
|
||||
<ClInclude Include="ast_gen.h" />
|
||||
<ClInclude Include="ast_novel.h" />
|
||||
|
|
@ -118,7 +120,9 @@
|
|||
<ClCompile Include="ast_gen.cpp" />
|
||||
<ClCompile Include="ast_novel.cpp" />
|
||||
<ClCompile Include="libsyntax.cpp" />
|
||||
<ClInclude Include="libtokens.h" />
|
||||
<ClInclude Include="syntax_novel.h" />
|
||||
<ClInclude Include="tokens_novel.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
|
||||
|
|
|
|||
|
|
@ -44,6 +44,12 @@
|
|||
<ClInclude Include="syntax_novel.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="libtokens.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="tokens_novel.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ast_basic.cpp">
|
||||
|
|
@ -58,5 +64,11 @@
|
|||
<ClCompile Include="syntax_novel.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="libtokens.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="tokens_novel.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
#include "libsyntax.h"
|
||||
#include "ast_basic.h"
|
||||
#include <tuple>
|
||||
#include <QDebug>
|
||||
|
||||
using namespace lib_syntax;
|
||||
using namespace std;
|
||||
using namespace lib_token;
|
||||
using namespace lib_words;
|
||||
using namespace ast_basic;
|
||||
|
||||
Rept::Rept(std::shared_ptr<const IBasicRule> rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "libsyntax_global.h"
|
||||
#include <libtoken.h>
|
||||
#include "libtokens.h"
|
||||
#include <memory>
|
||||
#include <QtCore/QList>
|
||||
#include <tuple>
|
||||
#include <functional>
|
||||
namespace ast_basic {
|
||||
|
|
@ -87,8 +88,8 @@ namespace lib_syntax {
|
|||
* @param head 列表头
|
||||
* @return 返回结果<匹配完成新列表头,匹配长度>
|
||||
*/
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const = 0;
|
||||
|
||||
/**
|
||||
* 返回匹配语法规则的词法序列表达
|
||||
|
|
@ -115,7 +116,7 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const IBasicRule>> children() const override { return QList<std::shared_ptr<const IBasicRule>>(); }
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>> parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override {
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>> parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> 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<WordImpl>(std::get<1>(match_result), head->nextWord()));
|
||||
return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared<lib_words::WordImpl>(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<std::shared_ptr<const IBasicRule>> mbrs_store;
|
||||
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const IBasicRule>, std::shared_ptr<const lib_token::IWordBase>> rule_select(std::shared_ptr<const lib_token::IWordBase> head) const;
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const IBasicRule>, std::shared_ptr<const lib_words::IWordBase>> rule_select(std::shared_ptr<const lib_words::IWordBase> head) const;
|
||||
|
||||
public:
|
||||
Any(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
||||
|
|
@ -164,8 +165,8 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -182,8 +183,8 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -201,8 +202,8 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -225,8 +226,8 @@ namespace lib_syntax {
|
|||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const lib_syntax::IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_words::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_words::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
|
||||
protected:
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
#pragma once
|
||||
|
||||
#include "libsyntax_global.h"
|
||||
#include <libwords.h>
|
||||
|
||||
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<token/null,remains> 解析结果
|
||||
*/
|
||||
virtual std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> content) const = 0;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief token解析结果
|
||||
*/
|
||||
class IToken : public lib_words::IWordBase {
|
||||
public:
|
||||
/**
|
||||
* @brief token解析机制关联
|
||||
* @return
|
||||
*/
|
||||
virtual std::shared_ptr<const ITokenDefine> define() const = 0;
|
||||
};
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
#include "ast_basic.h"
|
||||
#include "ast_gen.h"
|
||||
#include "libsyntax.h"
|
||||
#include <libtoken.h>
|
||||
#include "libtokens.h"
|
||||
#include <tokens_novel.h>
|
||||
|
||||
namespace example_novel {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
#include "tokens_novel.h"
|
||||
#include <QtCore/QRegExp>
|
||||
|
||||
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<const ITokenDefine> paramType)
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
#include "libToken_global.h"
|
||||
#include "libtoken.h"
|
||||
#include "libtokens.h"
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
|
|
@ -36,38 +34,38 @@ namespace lib_token {
|
|||
}
|
||||
|
||||
namespace example_novel {
|
||||
class LIBTOKEN_EXPORT LeftBracket : public lib_token::ITokenDefine, public std::enable_shared_from_this<LeftBracket> {
|
||||
class LIBSYNTAX_EXPORT LeftBracket : public lib_token::ITokenDefine, public std::enable_shared_from_this<LeftBracket> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_token::IWordBase> content) const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> 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<Keywords> {
|
||||
class LIBSYNTAX_EXPORT Keywords : public lib_token::ITokenDefine, public std::enable_shared_from_this<Keywords> {
|
||||
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<const lib_token::IToken>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_token::IWordBase> content) const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> content) const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT Numbers : public lib_token::ITokenDefine, public std::enable_shared_from_this<Numbers> {
|
||||
class LIBSYNTAX_EXPORT Numbers : public lib_token::ITokenDefine, public std::enable_shared_from_this<Numbers> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_token::IWordBase> content) const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> content) const override;
|
||||
};
|
||||
|
||||
|
||||
class LIBTOKEN_EXPORT NameSection : public lib_token::ITokenDefine, public std::enable_shared_from_this<NameSection> {
|
||||
class LIBSYNTAX_EXPORT NameSection : public lib_token::ITokenDefine, public std::enable_shared_from_this<NameSection> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_token::IWordBase> content) const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> content) const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT VTextSection : public lib_token::ITokenDefine, public std::enable_shared_from_this<VTextSection> {
|
||||
class LIBSYNTAX_EXPORT VTextSection : public lib_token::ITokenDefine, public std::enable_shared_from_this<VTextSection> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_token::IWordBase> content) const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> content) const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT DeclareSymbo : public lib_token::ITokenDefine, public std::enable_shared_from_this<DeclareSymbo> {
|
||||
class LIBSYNTAX_EXPORT DeclareSymbo : public lib_token::ITokenDefine, public std::enable_shared_from_this<DeclareSymbo> {
|
||||
public:
|
||||
// ͨ¹ý TokenDefine ¼Ì³Ð
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_token::IWordBase> content) const override;
|
||||
virtual std::tuple<std::shared_ptr<const lib_token::IToken>, std::shared_ptr<const lib_words::IWordBase>>
|
||||
analysis(std::shared_ptr<const lib_words::IWordBase> content) const override;
|
||||
|
||||
};
|
||||
} // namespace example_novel
|
||||
|
|
@ -5,115 +5,3 @@
|
|||
|
||||
using namespace lib_token;
|
||||
|
||||
std::shared_ptr<const IWordBase> WordReader::wordsFrom(const QString& path) const {
|
||||
auto primary_words = extract_primary(path);
|
||||
if (!primary_words.size())
|
||||
return nullptr;
|
||||
|
||||
std::shared_ptr<const IWordBase> prev_ptr = std::make_shared<const WordImpl>(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<const WordImpl>(content_ptr, prev_ptr);
|
||||
}
|
||||
|
||||
return prev_ptr;
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const IWordBase>> 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<std::shared_ptr<const IWordBase>> 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 <QRegExp>
|
||||
QList<std::shared_ptr<const IWordBase>> 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<std::shared_ptr<const IWordBase>> primary_words;
|
||||
int columns_offset = 0;
|
||||
for (auto& w : words) {
|
||||
auto column_start = line_text.indexOf(w, columns_offset);
|
||||
auto token = std::make_shared<WordContent>(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<const IWordBase> WordContent::nextWord() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
WordImpl::WordImpl(std::shared_ptr<const IWordBase> content, std::shared_ptr<const IWordBase> 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<const IWordBase> WordImpl::nextWord() const {
|
||||
return next_ptr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,152 +6,6 @@
|
|||
#include <memory>
|
||||
|
||||
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<const IWordBase> 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<const IWordBase> nextWord() const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT WordImpl : public IWordBase {
|
||||
private:
|
||||
std::shared_ptr<const IWordBase> content_ptr;
|
||||
std::shared_ptr<const IWordBase> next_ptr;
|
||||
|
||||
public:
|
||||
WordImpl(std::shared_ptr<const IWordBase> content, std::shared_ptr<const IWordBase> 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<const IWordBase> 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<const ITokenDefine> 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<token/null,remains> 解析结果
|
||||
*/
|
||||
virtual std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IWordBase>> analysis(std::shared_ptr<const IWordBase> content) const = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 源码词语获取器
|
||||
*/
|
||||
class LIBTOKEN_EXPORT WordReader {
|
||||
private:
|
||||
QList<std::shared_ptr<const IWordBase>> parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const;
|
||||
QList<std::shared_ptr<const IWordBase>> extract_primary(const QString& path) const;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 提取源码中所有的词语
|
||||
*/
|
||||
std::shared_ptr<const IWordBase> wordsFrom(const QString &path) const;
|
||||
};
|
||||
|
||||
} // namespace lib_token
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{386F6D42-C6EB-4973-9511-181472391B21}</ProjectGuid>
|
||||
<Keyword>QtVS_v304</Keyword>
|
||||
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">10.0</WindowsTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">10.0</WindowsTargetPlatformVersion>
|
||||
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt_defaults.props')">
|
||||
<Import Project="$(QtMsBuild)\qt_defaults.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
|
||||
<QtInstall>5.12.11_msvc2017_64</QtInstall>
|
||||
<QtModules>core</QtModules>
|
||||
<QtBuildConfig>release</QtBuildConfig>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
|
||||
<QtInstall>5.12.11_msvc2017_64</QtInstall>
|
||||
<QtModules>core</QtModules>
|
||||
<QtBuildConfig>debug</QtBuildConfig>
|
||||
</PropertyGroup>
|
||||
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
|
||||
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." />
|
||||
</Target>
|
||||
<ImportGroup Label="ExtensionSettings" />
|
||||
<ImportGroup Label="Shared" />
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="$(QtMsBuild)\Qt.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
<Import Project="$(QtMsBuild)\Qt.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
|
||||
<ClCompile>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<PreprocessorDefinitions>LIBWORDS_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
|
||||
<ClCompile>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<PreprocessorDefinitions>LIBWORDS_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="libwords_global.h" />
|
||||
<ClInclude Include="libwords.h" />
|
||||
<ClCompile Include="libwords.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
|
||||
<Import Project="$(QtMsBuild)\qt.targets" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>qml;cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Form Files">
|
||||
<UniqueIdentifier>{99349809-55BA-4b9d-BF79-8FDBB0286EB3}</UniqueIdentifier>
|
||||
<Extensions>ui</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Translation Files">
|
||||
<UniqueIdentifier>{639EADAA-A684-42e4-A9AD-28FC9BCB8F7C}</UniqueIdentifier>
|
||||
<Extensions>ts</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="libwords_global.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClCompile Include="libwords.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClInclude Include="libwords.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<QtTouchProperty>
|
||||
</QtTouchProperty>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<QtTouchProperty>
|
||||
</QtTouchProperty>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
#include "libwords.h"
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QTextStream>
|
||||
|
||||
using namespace lib_words;
|
||||
|
||||
|
||||
std::shared_ptr<const IWordBase> WordReader::wordsFrom(const QString& path) const {
|
||||
auto primary_words = extract_primary(path);
|
||||
if (!primary_words.size())
|
||||
return nullptr;
|
||||
|
||||
std::shared_ptr<const IWordBase> prev_ptr = std::make_shared<const WordImpl>(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<const WordImpl>(content_ptr, prev_ptr);
|
||||
}
|
||||
|
||||
return prev_ptr;
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const IWordBase>> 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<std::shared_ptr<const IWordBase>> 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 <QRegExp>
|
||||
QList<std::shared_ptr<const IWordBase>> 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<std::shared_ptr<const IWordBase>> primary_words;
|
||||
int columns_offset = 0;
|
||||
for (auto& w : words) {
|
||||
auto column_start = line_text.indexOf(w, columns_offset);
|
||||
auto token = std::make_shared<WordContent>(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<const IWordBase> content, std::shared_ptr<const IWordBase> 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<const IWordBase> 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<const IWordBase> WordContent::nextWord() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
#pragma once
|
||||
|
||||
#include "libwords_global.h"
|
||||
#include <memory>
|
||||
#include <QtCore/QString>
|
||||
|
||||
|
||||
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<const IWordBase> 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<const IWordBase> nextWord() const override;
|
||||
};
|
||||
|
||||
class LIBWORDS_EXPORT WordImpl : public IWordBase {
|
||||
private:
|
||||
std::shared_ptr<const IWordBase> content_ptr;
|
||||
std::shared_ptr<const IWordBase> next_ptr;
|
||||
|
||||
public:
|
||||
WordImpl(std::shared_ptr<const IWordBase> content, std::shared_ptr<const IWordBase> 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<const IWordBase> nextWord() const override;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief 源码词语获取器
|
||||
*/
|
||||
class LIBWORDS_EXPORT WordReader {
|
||||
private:
|
||||
QList<std::shared_ptr<const IWordBase>> parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const;
|
||||
QList<std::shared_ptr<const IWordBase>> extract_primary(const QString& path) const;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief 提取源码中所有的词语
|
||||
*/
|
||||
std::shared_ptr<const IWordBase> wordsFrom(const QString& path) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 解析异常
|
||||
*/
|
||||
class LIBWORDS_EXPORT WordsException {
|
||||
private:
|
||||
QString msg_store;
|
||||
|
||||
public:
|
||||
WordsException(const QString& message);
|
||||
virtual QString message() const;
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#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
|
||||
Loading…
Reference in New Issue