This commit is contained in:
codeboss 2025-02-08 13:40:36 +08:00
parent 88bc1de7a4
commit ffb558b545
18 changed files with 333 additions and 65 deletions

120
CoreTest/CoreTest.vcxproj Normal file
View File

@ -0,0 +1,120 @@
<?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="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{9B3EEB2A-26D7-4B2C-B890-505715283400}</ProjectGuid>
<Keyword>QtVS_v304</Keyword>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|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)' == 'Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<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)' == 'Debug|x64'" Label="QtSettings">
<QtInstall>5.12.11_msvc2017_64</QtInstall>
<QtModules>core</QtModules>
<QtBuildConfig>debug</QtBuildConfig>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
<QtInstall>5.12.11_msvc2017_64</QtInstall>
<QtModules>core</QtModules>
<QtBuildConfig>release</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)' == '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>
<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>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<IncludePath>$(SolutionDir)libSyntax;$(SolutionDir)libWords;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<IncludePath>$(SolutionDir)libSyntax;$(SolutionDir)libWords;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Link>
<AdditionalDependencies>libWords.lib;libSyntax.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Link>
<AdditionalDependencies>libWords.lib;libSyntax.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>false</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="syntax_example.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
<Import Project="$(QtMsBuild)\qt.targets" />
</ImportGroup>
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,33 @@
<?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>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="syntax_example.txt" />
</ItemGroup>
</Project>

View File

@ -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>

39
CoreTest/main.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <QtCore/QCoreApplication>
#include <libwords.h>
#include <qdebug.h>
#include <QFile>
#include <libsyntax.h>
#include <ast_gen.h>
#include <syntax_novel.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile in(u8"D:\\Projects\\Cpp\\WsNovelParser\\CoreTest\\syntax_example.txt");
in.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream tt(&in);
lib_words::WordReader reader;
auto vwords = reader.wordsFrom(tt, u8"D:\\Projects\\Cpp\\WsNovelParser\\CoreTest\\syntax_example.txt");
auto words = vwords;
while (words) {
qDebug() << words->content();
words = words->nextWord();
}
ast_gen::SyntaxParser parser(example_novel::NovalSyntax::getSyntaxTree());
auto rst = parser.parse(vwords);
for (auto one : rst) {
qDebug() << u8"===================================================";
qDebug().noquote() << one->totalErrors();
auto vtoken = one->currentToken();
while (vtoken) {
if(vtoken->defines())
qDebug() << vtoken->content();
vtoken = vtoken->prevToken();
}
}
return a.exec();
}

View File

@ -0,0 +1,8 @@
#<23><><EFBFBD>ע 1
<><C2B9><EFBFBD> ¹<><C2B9><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>1
}
<><C2B9><EFBFBD> ¹<><C2B9><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>2
¹<><C2B9><EFBFBD>½י<C2BD><D799><EFBFBD><C2B6>ה}

View File

@ -11,6 +11,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WsNovelParser", "WsNovelPar
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libSyntax", "libSyntax\libSyntax.vcxproj", "{EF557F71-99AA-4F2B-A5F5-1A4518A11C19}"
ProjectSection(ProjectDependencies) = postProject
{386F6D42-C6EB-4973-9511-181472391B21} = {386F6D42-C6EB-4973-9511-181472391B21}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libParse", "libParse\libParse.vcxproj", "{C3AADEB5-3695-4DF4-B8E1-D37F928F3B2F}"
ProjectSection(ProjectDependencies) = postProject
@ -34,6 +37,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WsNovelManager", "WsNovelMa
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libWords", "libWords\libWords.vcxproj", "{386F6D42-C6EB-4973-9511-181472391B21}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CoreTest", "CoreTest\CoreTest.vcxproj", "{9B3EEB2A-26D7-4B2C-B890-505715283400}"
ProjectSection(ProjectDependencies) = postProject
{386F6D42-C6EB-4973-9511-181472391B21} = {386F6D42-C6EB-4973-9511-181472391B21}
{EF557F71-99AA-4F2B-A5F5-1A4518A11C19} = {EF557F71-99AA-4F2B-A5F5-1A4518A11C19}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -98,6 +107,14 @@ Global
{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
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Debug|x64.ActiveCfg = Debug|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Debug|x64.Build.0 = Debug|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Debug|x86.ActiveCfg = Debug|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Debug|x86.Build.0 = Debug|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Release|x64.ActiveCfg = Release|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Release|x64.Build.0 = Release|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Release|x86.ActiveCfg = Release|x64
{9B3EEB2A-26D7-4B2C-B890-505715283400}.Release|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -81,7 +81,7 @@ int main(int argc, char* argv[]) {
}
access_ptr = parser->validsApply(docs);
}
catch (lib_syntax::SyntaxException* e) {
catch (SyntaxException* e) {
qDebug().noquote() << e->message();
delete e;
exit(0);

View File

@ -11,7 +11,7 @@ using namespace lib_parse;
NovelParser::NovelParser()
{
this->syntax_defines = NovalSyntax::getParseTree();
this->syntax_defines = NovalSyntax::getSyntaxTree();
checker_list << std::make_shared<FragmentExistsCheck>();
checker_list << std::make_shared<StoryOrderCheck>();
checker_list << std::make_shared<FragmentGraphCheck>();
@ -29,12 +29,12 @@ QString NovelParser::version() const
return "1.0.0";
}
std::shared_ptr<lib_syntax::IContext> NovelParser::parserContext() const
std::shared_ptr<IContext> NovelParser::parserContext() const
{
return context;
}
QList<std::shared_ptr<const ast_basic::IExprInst>> NovelParser::parse(const QFileInfoList source_list) const {
QList<std::shared_ptr<const IExprInstance>> 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_words::WordReader>();
@ -45,14 +45,14 @@ QList<std::shared_ptr<const ast_basic::IExprInst>> NovelParser::parse(const QFil
this->syntax_defines->parse(context, words);
}
QList<std::shared_ptr<const ast_basic::IExprInst>> forst_root = context->getDocInsts();
QList<std::shared_ptr<const IExprInstance>> forst_root = context->getDocInsts();
auto current_stamp = QTime::currentTime();
qDebug().noquote() << QString(u8"%词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
return forst_root;
}
std::shared_ptr<const ast_gen::ElementAccess> NovelParser::validsApply(QList<std::shared_ptr<const ast_basic::IExprInst>> forst_root) const {
std::shared_ptr<const ast_gen::ElementAccess> NovelParser::validsApply(QList<std::shared_ptr<const IExprInstance>> forst_root) const {
auto time_stamp = QTime::currentTime();
QList<std::shared_ptr<ast_gen::SyntaxElement>> docs_node;
for (auto& it : forst_root) {

View File

@ -13,7 +13,7 @@ class NovelParser
{
private:
std::shared_ptr<ast_gen::GlobalElement> context = nullptr;
std::shared_ptr<const lib_syntax::ExprRule> syntax_defines;
std::shared_ptr<const ExprRule> syntax_defines;
QList<std::shared_ptr<const lib_parse::CheckProvider>> checker_list;
std::shared_ptr<const lib_parse::Analyzer> analyzer_ref;
@ -22,7 +22,7 @@ public:
QList<std::shared_ptr<const example_novel::FragmentGraphHelper>> fragmentsSorted() const;
virtual QString version() const;
std::shared_ptr<lib_syntax::IContext> parserContext() const;
QList<std::shared_ptr<const ast_basic::IExprInst>> parse(const QFileInfoList souurce_list) const;
std::shared_ptr<const ast_gen::ElementAccess> validsApply(QList<std::shared_ptr<const ast_basic::IExprInst>> docs_list) const;
std::shared_ptr<IContext> parserContext() const;
QList<std::shared_ptr<const IExprInstance>> parse(const QFileInfoList souurce_list) const;
std::shared_ptr<const ast_gen::ElementAccess> validsApply(QList<std::shared_ptr<const IExprInstance>> docs_list) const;
};

View File

@ -6,11 +6,11 @@ using namespace lib_token;
using namespace lib_syntax;
using namespace lib_words;
SyntaxParser::SyntaxParser(std::shared_ptr<IBasicRule> rule)
SyntaxParser::SyntaxParser(std::shared_ptr<const IBasicRule> rule)
: _rule_bind(rule) { }
QList<std::shared_ptr<const MatchCursor>> SyntaxParser::parse(std::shared_ptr<IPrimitiveWord> words) {
auto cursor = std::make_shared<MatchCursor>();
QList<std::shared_ptr<const MatchCursor>> SyntaxParser::parse(std::shared_ptr<const IPrimitiveWord> words) {
auto cursor = std::make_shared<MatchCursor>(words->file());
cursor->setCurrent(nullptr, words);
auto list = this->_rule_bind->parse(cursor);

View File

@ -11,17 +11,17 @@ namespace ast_gen {
*/
class LIBSYNTAX_EXPORT SyntaxParser {
private:
std::shared_ptr<lib_syntax::IBasicRule> _rule_bind = nullptr;
std::shared_ptr<const lib_syntax::IBasicRule> _rule_bind = nullptr;
public:
SyntaxParser(std::shared_ptr<lib_syntax::IBasicRule> rule);
SyntaxParser(std::shared_ptr<const lib_syntax::IBasicRule> rule);
/**
* @brief
* @param wods
* @return ->
*/
QList<std::shared_ptr<const lib_syntax::MatchCursor>> parse(std::shared_ptr<lib_words::IPrimitiveWord> words);
QList<std::shared_ptr<const lib_syntax::MatchCursor>> parse(std::shared_ptr<const lib_words::IPrimitiveWord> words);
/**
* @brief

View File

@ -61,7 +61,7 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<IncludePath>$(SolutionDir)libToken\;$(IncludePath)</IncludePath>
<IncludePath>$(SolutionDir)libWords\;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration)\;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
@ -70,7 +70,7 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Link>
<AdditionalDependencies>libToken.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
<AdditionalDependencies>libWords.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
</Link>
<ClCompile>
<LanguageStandard>Default</LanguageStandard>

View File

@ -16,13 +16,12 @@ QList<std::shared_ptr<const IBasicRule>> Any::children() const {
}
QList<std::shared_ptr<const MatchCursor>> Any::parse(std::shared_ptr<const MatchCursor> cursor) const {
QList<std::shared_ptr<const MatchCursor>> result_list;
if (cursor->mustStop())
result_list << cursor;
else
for (auto rx : this->children())
result_list.append(rx->parse(cursor));
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
QList<std::shared_ptr<const MatchCursor>> result_list;
for (auto rx : this->children())
result_list.append(rx->parse(cursor));
return result_list;
}
@ -42,8 +41,10 @@ QList<std::shared_ptr<const IBasicRule>> Seqs::children() const {
}
QList<std::shared_ptr<const MatchCursor>> Seqs::parse(std::shared_ptr<const MatchCursor> cursor) const {
QList<std::shared_ptr<const MatchCursor>> results;
if (cursor->mustStop())
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
QList<std::shared_ptr<const MatchCursor>> results;
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
for (auto rule : this->children()) {
QList<std::shared_ptr<const MatchCursor>> current_result;
@ -78,8 +79,10 @@ QList<std::shared_ptr<const IBasicRule>> Rept::children() const {
}
QList<std::shared_ptr<const MatchCursor>> Rept::parse(std::shared_ptr<const MatchCursor> cursor) const {
QList<std::shared_ptr<const MatchCursor>> results;
if (cursor->mustStop())
return QList<std::shared_ptr<const MatchCursor>>() << cursor;
QList<std::shared_ptr<const MatchCursor>> results;
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
// 最小重复次数匹配
for (auto idx = 0; idx < min_match; ++idx) {
@ -123,14 +126,21 @@ QList<std::shared_ptr<const MatchCursor>> Rept::parse(std::shared_ptr<const Matc
});
// 移除失败分支
for (auto idx = 0; idx<current_list.size(); ++idx) {
for (auto idx = 0; idx < current_list.size(); ++idx) {
auto rst_branch = current_list.at(idx);
if (rst_branch->mustStop()) {
if (rst_branch->mustStop() && rst_branch->currentWords()) {
results.append(rst_branch->previous());
current_list.removeAt(idx--);
}
if (rst_branch->mustStop() && !rst_branch->currentWords()) {
results.append(rst_branch);
current_list.removeAt(idx--);
}
}
if (!current_list.size())
break;
bridge_list = current_list;
}
@ -183,19 +193,24 @@ QString ExprRule::present() const {
return child_store->present();
}
MatchCursor::MatchCursor() { }
MatchCursor::MatchCursor(const QString& path) :_file_path(path) { }
lib_syntax::MatchCursor::MatchCursor(std::shared_ptr<const MatchCursor> other_ptr)
MatchCursor::MatchCursor(std::shared_ptr<const MatchCursor> other_ptr)
: _prev_cursor(other_ptr),
_file_path(other_ptr->_file_path),
_total_errors(other_ptr->_total_errors),
_exprs_errors(other_ptr->_exprs_errors),
_current_token(other_ptr->_current_token),
_remains_word(other_ptr->_remains_word) { }
std::shared_ptr<const MatchCursor> lib_syntax::MatchCursor::previous() const {
std::shared_ptr<const MatchCursor> MatchCursor::previous() const {
return _prev_cursor;
}
QString MatchCursor::filePath() const {
return _file_path;
}
void MatchCursor::enterExprs() {
auto new_expr = std::make_shared<ErrsPack>();
this->_exprs_errors.push_back(new_expr);
@ -211,11 +226,13 @@ void MatchCursor::quitExprs() {
}
bool MatchCursor::mustStop() const {
return exprsErrorCount() >= 2;
return exprsErrorCount() >= 2 || parse_stop();
}
int MatchCursor::exprsErrorCount() const {
return this->_exprs_errors.last()->errorCount();
if (this->_exprs_errors.size())
return this->_exprs_errors.last()->errorCount();
return 0;
}
int MatchCursor::totalErrorCount() const {
@ -239,6 +256,10 @@ std::shared_ptr<const IPrimitiveWord> MatchCursor::currentWords() const {
return this->_remains_word;
}
bool lib_syntax::MatchCursor::parse_stop() const {
return !currentWords();
}
void MatchCursor::ErrsPack::addError(const QString& msg) {
this->_error_collection.append(msg);
}

View File

@ -44,11 +44,12 @@ namespace lib_syntax {
QList<QString> _error_collection;
};
MatchCursor();
MatchCursor(const QString& path);
MatchCursor(std::shared_ptr<const MatchCursor> other_ptr);
virtual ~MatchCursor() = default;
virtual std::shared_ptr<const MatchCursor> previous() const;
virtual QString filePath() const;
virtual void enterExprs();
virtual void logExprsError(const QString& msg);
@ -64,12 +65,15 @@ namespace lib_syntax {
virtual std::shared_ptr<const lib_words::IPrimitiveWord> currentWords() const;
private:
QString _file_path;
std::shared_ptr<const MatchCursor> _prev_cursor = nullptr;
QList<QString> _total_errors; // 所有解析错误
QList<std::shared_ptr<ErrsPack>> _exprs_errors; // 当前表达式解析错误
std::shared_ptr<const lib_token::IActionToken> _current_token = nullptr; // 当前Token
std::shared_ptr<const lib_words::IPrimitiveWord> _remains_word = nullptr; // 剩余词语
bool parse_stop() const;
};
/**
@ -151,7 +155,7 @@ namespace lib_syntax {
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
virtual QString present() const override;
};
/**
* @brief
*/
@ -197,6 +201,15 @@ namespace lib_syntax {
}
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> current) const override {
auto w_this = current->currentWords();
if (!w_this) {
auto clone_ins = std::make_shared<MatchCursor>(current);
clone_ins->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,缺失\"%1\"<file<%2>>")
.arg(this->_define_peers->reviseWords()).arg(current->filePath()));
clone_ins->logExprsError(QString(u8"Syntax[0x00001]输入流提前结束,<%1>").arg(current->filePath()));
return QList<std::shared_ptr<const MatchCursor>>() << clone_ins;
}
auto t_this = current->currentToken();
auto match_result = _define_peers->analysis(w_this);
@ -216,7 +229,7 @@ namespace lib_syntax {
// 少一个
{
auto short_one = std::make_shared<MatchCursor>(current);
short_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,缺失\"%1\"<row:%2,col:%3,file:%4>")
short_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,缺失\"%1\"<row:%2,col:%3,file<%4>>")
.arg(this->_define_peers->reviseWords())
.arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
retvals << short_one;
@ -225,18 +238,19 @@ namespace lib_syntax {
// 错一个
{
auto error_one = std::make_shared<MatchCursor>(current);
error_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,请修正\"%1\"<row:%2,col:%3,file:%4>")
error_one->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,请修正\"%1\"<row:%2,col:%3,file<%4>>")
.arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
auto tkins = std::make_shared<lib_token::TokenContent>(
w_this->row(), w_this->column(), w_this->position(),
QString(u8"名称错误_%1").arg((uint64_t)error_one.get()), w_this->file(), this->_define_peers);
QString(u8"%2_%1").arg((uint64_t) error_one.get()).arg(this->_define_peers->reviseWords()),
w_this->file(), this->_define_peers);
auto tkchain = std::make_shared<lib_token::ActionToken<T, XProc>>(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)) {
@ -247,6 +261,8 @@ namespace lib_syntax {
}
auto clone_ins = std::make_shared<MatchCursor>(current);
clone_ins->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,请删除\"%1\"<row:%2,col:%3,file<%4>>")
.arg(w_this->content()).arg(w_this->row()).arg(w_this->column()).arg(w_this->file()));
clone_ins->setCurrent(chain, remains);
retvals << clone_ins;
}
@ -281,8 +297,18 @@ namespace lib_syntax {
ncursor->enterExprs();
auto nbranch = this->child_store->parse(ncursor);
decltype(nbranch) branch_procs;
std::for_each(nbranch.begin(), nbranch.end(), [&](std::shared_ptr<const MatchCursor> curs) {
decltype(nbranch) list_ok;
std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok),
[](std::shared_ptr<const MatchCursor> ins) { return !ins->exprsErrorCount(); });
if (!list_ok.size()) {
std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok),
[](std::shared_ptr<const MatchCursor> ins) { return !ins->mustStop(); });
if (!list_ok.size())
list_ok = nbranch;
}
decltype(list_ok) branch_procs;
std::for_each(list_ok.begin(), list_ok.end(), [&](std::shared_ptr<const MatchCursor> curs) {
if (curs->mustStop()) {
branch_procs.append(curs);
}
@ -301,7 +327,7 @@ namespace lib_syntax {
return branch_procs;
}
protected:
virtual std::shared_ptr<ExprRule> make_copy() const {
return std::make_shared<ElementRule<ExprType>>(this->name(), this->typeMark());

View File

@ -129,7 +129,7 @@ namespace lib_token {
}
std::shared_ptr<const ITokenDefine> defines() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken");
return nullptr;
}
std::shared_ptr<const IActionToken> prevToken() const override {
@ -173,7 +173,7 @@ namespace lib_token {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
}
std::shared_ptr<const ITokenDefine> defines() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
return nullptr;
}
std::shared_ptr<const IActionToken> prevToken() const override {
return _prev_token;

View File

@ -7,7 +7,7 @@ using namespace lib_token;
using namespace lib_words;
QString LeftBracket::reviseWords() const { return u8"'{'"; }
QString LeftBracket::reviseWords() const { return u8"{"; }
int LeftBracket::typeMark() const
{
@ -34,7 +34,7 @@ LeftBracket::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
return std::make_tuple(token_inst, nullptr);
}
QString RightBracket::reviseWords() const { return u8"'}'"; }
QString RightBracket::reviseWords() const { return u8"}"; }
int RightBracket::typeMark() const
{
@ -43,7 +43,7 @@ int RightBracket::typeMark() const
QString RightBracket::regex() const { return u8"}"; }
QString ReferMark::reviseWords() const { return u8"'@'"; }
QString ReferMark::reviseWords() const { return u8"@"; }
int ReferMark::typeMark() const
{
@ -54,7 +54,7 @@ QString ReferMark::regex() const { return u8"@"; }
Keywords::Keywords(const QString& val, uint type_code) : means_store(val), type_code(type_code) {}
QString Keywords::reviseWords() const { return "'"+means_store+"'"; }
QString Keywords::reviseWords() const { return means_store; }
int Keywords::typeMark() const
{
@ -125,7 +125,7 @@ VTextSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
return std::make_tuple(tinst, nullptr);
}
QString Split::reviseWords() const { return u8"'&'"; }
QString Split::reviseWords() const { return u8"&"; }
int Split::typeMark() const
{
@ -166,7 +166,7 @@ NameSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const
}
QString DeclareSymbo::reviseWords() const {
return u8"'ÉùÃ÷·û'";
return u8"#";
}
int DeclareSymbo::typeMark() const

View File

@ -5,8 +5,8 @@
using namespace lib_words;
std::shared_ptr<const IPrimitiveWord> WordReader::wordsFrom(const QString& path) const {
auto primary_words = extract_primary(path);
std::shared_ptr<const IPrimitiveWord> WordReader::wordsFrom(QTextStream &tin, const QString& path) const {
auto primary_words = extract_primary(tin, path);
if (!primary_words.size())
return nullptr;
@ -18,23 +18,14 @@ std::shared_ptr<const IPrimitiveWord> WordReader::wordsFrom(const QString& path)
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>> lib_words::WordReader::extract_primary(QTextStream& tin, const QString& f) const{
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));
ret_list.append(this->parse_line(relative_offset, line_number++, line, f));
}
return ret_list;

View File

@ -3,6 +3,7 @@
#include "libwords_global.h"
#include <memory>
#include <QtCore/QString>
#include <QtCore/QTextStream>
namespace lib_words {
@ -94,13 +95,13 @@ namespace lib_words {
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;
QList<std::shared_ptr<const IWordBase>> extract_primary(QTextStream& in, const QString &f_path) const;
public:
/**
* @brief
*/
std::shared_ptr<const IPrimitiveWord> wordsFrom(const QString& path) const;
std::shared_ptr<const IPrimitiveWord> wordsFrom(QTextStream& tin, const QString& path) const;
};