加速架构改造完成

This commit is contained in:
codeboss 2024-06-19 01:09:45 +08:00
parent c49c4c4230
commit 3c4662415f
10 changed files with 94 additions and 27 deletions

View File

@ -20,6 +20,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libParse", "libParse\libPar
{EF557F71-99AA-4F2B-A5F5-1A4518A11C19} = {EF557F71-99AA-4F2B-A5F5-1A4518A11C19} {EF557F71-99AA-4F2B-A5F5-1A4518A11C19} = {EF557F71-99AA-4F2B-A5F5-1A4518A11C19}
EndProjectSection EndProjectSection
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{8548B2EE-059F-40ED-B393-9513E6095930}"
ProjectSection(SolutionItems) = preProject
报告20240618-1929.diagsession = 报告20240618-1929.diagsession
EndProjectSection
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 Debug|x64 = Debug|x64

View File

@ -6,12 +6,12 @@
<LocalDebuggerCommandArguments>--path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>--path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.5896562Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.6534384Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.6990325Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.7471721Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>

View File

@ -29,9 +29,9 @@ std::shared_ptr<const ast_gen::ElementAccess> NovelParser::parse(const QFileInfo
auto time_stamp = QTime::currentTime(); auto time_stamp = QTime::currentTime();
for (auto& file : source_list) { for (auto& file : source_list) {
auto tokens = lex_reader->extractFrom(file.canonicalFilePath()); auto tokens = lex_reader->tokensWithin(file.canonicalFilePath());
auto exprs_result = this->syntax_defines->parse(context, tokens); auto exprs_result = this->syntax_defines->parse(context, tokens);
forst_root.append(exprs_result); forst_root.append(std::get<0>(exprs_result));
} }
auto current_stamp = QTime::currentTime(); auto current_stamp = QTime::currentTime();
qDebug() << QString(u8"词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp)); qDebug() << QString(u8"词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));

View File

@ -5,12 +5,12 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.8553531Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.9815477Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.9178555Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:04.0440621Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>

View File

@ -1,4 +1,5 @@
#include "ast_basic.h" #include "ast_basic.h"
#include <QDebug>
using namespace ast_basic; using namespace ast_basic;
using namespace lib_token; using namespace lib_token;

View File

@ -5,12 +5,12 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.7302802Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.7784470Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.8084783Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.8565020Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>

View File

@ -22,15 +22,14 @@ std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> TokenMatch
// std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::Token>> // std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::Token>>
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const { std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const {
if (!head) if (!head)
throw new SyntaxException(u8"Syntax[0x0000]token流提前终止"); throw std::make_shared<InputTerminal>();
if (head->define()->name() == define_peer->name()) { if (head->define()->name() == define_peer->name()) {
rt_inst->currentInst()->addToken(head); rt_inst->currentInst()->addToken(head);
return std::make_tuple(nullptr, head->nextToken()); return std::make_tuple(nullptr, head->nextToken());
} }
throw new SyntaxException(QString(u8"Syntax[0x00001]语法匹配错误不能识别token%1<%2,%3>") throw std::make_shared<MismatchException>(head);
.arg(head->content()).arg(head->row()).arg(head->column()));
} }
QString TokenMatch::token_present() const { QString TokenMatch::token_present() const {
@ -96,7 +95,10 @@ std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> Rept
temp_head = std::get<1>(result_gen); temp_head = std::get<1>(result_gen);
} }
catch (...) { catch (std::shared_ptr<MismatchException> ex) {
return std::make_tuple(nullptr, temp_head);
}
catch (std::shared_ptr<InputTerminal> ex) {
return std::make_tuple(nullptr, temp_head); return std::make_tuple(nullptr, temp_head);
} }
} }
@ -197,8 +199,34 @@ std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> Any::match
} }
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> Any::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const { std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> Any::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const {
auto temp = rule_select(head); std::function<int(std::shared_ptr<const Token>, std::shared_ptr<const Token>)> measure_span =
return std::get<2>(temp)->parse(rt_inst, head); [&](std::shared_ptr<const Token> anchor, std::shared_ptr<const Token> head)->int {
if (anchor == head)
return 1;
return measure_span(anchor, head->nextToken()) + 1;
};
std::tuple<std::shared_ptr<const BaseRule>, int> temp_result = std::make_tuple(mbrs_store.first(), 0);
for (auto& fork : mbrs_store) {
try {
auto gen = fork->parse(rt_inst, head);
// 遇到成功的直接返回解析结果
if (std::get<0>(gen))
rt_inst->currentInst()->addChild(std::get<0>(gen));
return std::make_tuple(nullptr, std::get<1>(gen));
}
// 语法错误的会进行比较
catch (std::shared_ptr<MismatchException> ex) {
auto current_span = measure_span(ex->targetToken(), head);
if (current_span > std::get<1>(temp_result))
temp_result = std::make_tuple(fork, current_span);
}
}
// 分析最匹配的分支
return std::get<0>(temp_result)->parse(rt_inst, head);
} }
QString Any::token_present() const QString Any::token_present() const
@ -240,19 +268,37 @@ std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> Expression
} }
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> ExpressionRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const { std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> ExpressionRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const {
std::shared_ptr<ast_basic::Expression> elm_ast = this->newEmptyInstance(); std::shared_ptr<Expression> elm_ast = this->newEmptyInstance();
rt_inst->pushExpressionRule(this->shared_from_this()); rt_inst->pushExpressionRule(this->shared_from_this());
rt_inst->pushInst(elm_ast); rt_inst->pushInst(elm_ast);
try {
auto rstg = child_store->parse(rt_inst, head); auto rstg = child_store->parse(rt_inst, head);
auto tokens_decl = this->filter_proc(elm_ast->tokens()); auto tokens_decl = this->filter_proc(elm_ast->tokens());
elm_ast->tokensReset(tokens_decl); elm_ast->tokensReset(tokens_decl);
rt_inst->popInst();
rt_inst->popExpressionRule(); rt_inst->popExpressionRule();
return std::make_tuple(rt_inst->popInst(), std::get<1>(rstg)); return std::make_tuple(elm_ast, std::get<1>(rstg));
}
catch (...) {
rt_inst->popInst();
rt_inst->popExpressionRule();
throw;
}
} }
QString ExpressionRule::token_present() const { QString ExpressionRule::token_present() const {
return QString(u8"(%1)").arg(child_store->token_present()); return QString(u8"(%1)").arg(child_store->token_present());
} }
MismatchException::MismatchException(std::shared_ptr<const lib_token::Token> inst) :SyntaxException(
QString(u8"Syntax[0x00001]语法匹配错误不能识别token%1<%2,%3>").arg(inst->content()).arg(inst->row()).arg(inst->column())), target(inst) {}
std::shared_ptr<const Token>MismatchException::targetToken() const {
return this->target;
}
InputTerminal::InputTerminal() :SyntaxException(u8"Syntax[0x0000]token流提前终止") {}

View File

@ -11,6 +11,8 @@ namespace ast_basic {
} }
namespace lib_syntax { namespace lib_syntax {
class BaseRule;
/** /**
* @brief * @brief
*/ */
@ -25,8 +27,6 @@ namespace lib_syntax {
virtual QString message() const; virtual QString message() const;
}; };
class BaseRule;
// 基础语法解析接口 =================================================================================================== // 基础语法解析接口 ===================================================================================================
/** /**
* @brief * @brief
@ -107,7 +107,7 @@ namespace lib_syntax {
// BaseRule interface // BaseRule interface
public: public:
virtual QList<std::shared_ptr<const BaseRule>> children() const override; virtual QList<std::shared_ptr<const BaseRule>> children() const override;
virtual std::tuple<MatchResult, uint, std::shared_ptr<const lib_token::Token>> match(std::shared_ptr<const lib_token::Token> remains_head) const override; virtual std::tuple<MatchResult, uint, std::shared_ptr<const lib_token::Token>> match(std::shared_ptr<const lib_token::Token> head) const override;
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::Token>> virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::Token>>
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::Token> head) const override; parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::Token> head) const override;
virtual QString token_present() const override; virtual QString token_present() const override;
@ -223,4 +223,20 @@ namespace lib_syntax {
} }
}; };
class MismatchException : public SyntaxException {
private:
std::shared_ptr<const lib_token::Token> target;
public:
MismatchException(std::shared_ptr<const lib_token::Token> inst);
virtual ~MismatchException() = default;
virtual std::shared_ptr<const lib_token::Token> targetToken() const;
};
class InputTerminal : public SyntaxException {
public:
InputTerminal();
};
} // namespace lib_syntax } // namespace lib_syntax

View File

@ -112,8 +112,7 @@ std::shared_ptr<const ast_gen::SyntaxElement> example_novel::NovalSyntax::tidy(s
} }
void example_novel::NovalSyntax::cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children) void example_novel::NovalSyntax::cache_load(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
{ {
std::const_pointer_cast<ast_gen::SyntaxElement>(root)->cacheLoad(); root->cacheLoad();
for (auto& cinst : children) { for (auto& cinst : children) {
cinst->setParent(root); cinst->setParent(root);

View File

@ -2,12 +2,12 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup /> <PropertyGroup />
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:54.9646551Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.8877508Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtLastBackgroundBuild>2024-06-18T03:59:55.0271675Z</QtLastBackgroundBuild> <QtLastBackgroundBuild>2024-06-18T15:36:03.9502420Z</QtLastBackgroundBuild>
<QtTouchProperty> <QtTouchProperty>
</QtTouchProperty> </QtTouchProperty>
</PropertyGroup> </PropertyGroup>