Compare commits
3 Commits
ac99860469
...
3c4662415f
| Author | SHA1 | Date |
|---|---|---|
|
|
3c4662415f | |
|
|
c49c4c4230 | |
|
|
856a81c2c9 |
|
|
@ -20,6 +20,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libParse", "libParse\libPar
|
|||
{EF557F71-99AA-4F2B-A5F5-1A4518A11C19} = {EF557F71-99AA-4F2B-A5F5-1A4518A11C19}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{8548B2EE-059F-40ED-B393-9513E6095930}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
报告20240618-1929.diagsession = 报告20240618-1929.diagsession
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
<LocalDebuggerCommandArguments>--path "D:\手作小说\科学+修仙+创造世界"</LocalDebuggerCommandArguments>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -29,9 +29,9 @@ std::shared_ptr<const ast_gen::ElementAccess> NovelParser::parse(const QFileInfo
|
|||
|
||||
auto time_stamp = QTime::currentTime();
|
||||
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);
|
||||
forst_root.append(exprs_result);
|
||||
forst_root.append(std::get<0>(exprs_result));
|
||||
}
|
||||
auto current_stamp = QTime::currentTime();
|
||||
qDebug() << QString(u8"词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@
|
|||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "ast_basic.h"
|
||||
#include <QDebug>
|
||||
|
||||
using namespace ast_basic;
|
||||
using namespace lib_token;
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@
|
|||
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -10,27 +10,26 @@ TokenMatch::TokenMatch(shared_ptr<const TokenDefine> define) : define_peer(defin
|
|||
|
||||
QList<std::shared_ptr<const BaseRule>> TokenMatch::children() const { return QList<std::shared_ptr<const BaseRule>>(); }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, uint> TokenMatch::match(const QList<std::shared_ptr<const Token>>& stream) const {
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> TokenMatch::match(std::shared_ptr<const Token> remains_head) const {
|
||||
QString token_seqs = this->token_present();
|
||||
if (stream.size() && define_peer->name() == stream.first()->define()->name())
|
||||
return std::make_tuple(MatchResult::Success, 1);
|
||||
if (remains_head && remains_head->define()->name() == define_peer->name())
|
||||
return std::make_tuple(MatchResult::Success, 1, remains_head->nextToken());
|
||||
|
||||
return std::make_tuple(MatchResult::Fail, 0, nullptr);
|
||||
// auto mis_match = define_peer->name();
|
||||
// auto real_match = stream.first()->define()->name();
|
||||
return std::make_tuple(MatchResult::Fail, 0);
|
||||
}
|
||||
// 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 {
|
||||
if (!head)
|
||||
throw std::make_shared<InputTerminal>();
|
||||
|
||||
std::shared_ptr<const Expression> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
if (stream.size()) {
|
||||
auto current = stream.first();
|
||||
if (current->define()->name() == define_peer->name()){
|
||||
rt_inst->currentInst()->addToken(current);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
throw new SyntaxException(QString(u8"Syntax[0x00001]语法匹配错误,不能识别token:%1<%2,%3>")
|
||||
.arg(current->content()).arg(current->row()).arg(current->column()));
|
||||
if (head->define()->name() == define_peer->name()) {
|
||||
rt_inst->currentInst()->addToken(head);
|
||||
return std::make_tuple(nullptr, head->nextToken());
|
||||
}
|
||||
throw new SyntaxException(u8"Syntax[0x0000]token流提前终止");
|
||||
|
||||
throw std::make_shared<MismatchException>(head);
|
||||
}
|
||||
|
||||
QString TokenMatch::token_present() const {
|
||||
|
|
@ -41,70 +40,70 @@ Rept::Rept(std::shared_ptr<const BaseRule> rule, int min, int max) : rule_peer(r
|
|||
|
||||
QList<std::shared_ptr<const BaseRule>> Rept::children() const { return QList<std::shared_ptr<const BaseRule>>() << rule_peer; }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, uint> Rept::match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> Rept::match(std::shared_ptr<const Token> list_head) const {
|
||||
auto token_offset = 0;
|
||||
QString token_seqs = this->token_present();
|
||||
auto temp_head = list_head;
|
||||
|
||||
// min-match
|
||||
for (auto idx = 0; idx < min_match; ++idx) {
|
||||
auto result = rule_peer->match(stream.mid(token_offset));
|
||||
auto result = rule_peer->match(temp_head);
|
||||
token_offset += std::get<1>(result);
|
||||
temp_head = std::get<2>(result);
|
||||
|
||||
if (std::get<0>(result) != MatchResult::Success) {
|
||||
return std::make_tuple(token_offset ? MatchResult::Part : MatchResult::Fail, token_offset);
|
||||
return std::make_tuple(token_offset ? MatchResult::Part : MatchResult::Fail, token_offset, temp_head);
|
||||
}
|
||||
}
|
||||
|
||||
// max-match
|
||||
for (auto idx = min_match; idx < max_match; ++idx) {
|
||||
auto result = rule_peer->match(stream.mid(token_offset));
|
||||
auto result = rule_peer->match(temp_head);
|
||||
|
||||
switch (std::get<0>(result)) {
|
||||
case MatchResult::Fail:
|
||||
case MatchResult::Part:
|
||||
return std::make_tuple(MatchResult::Success, token_offset);
|
||||
return std::make_tuple(MatchResult::Success, token_offset, temp_head);
|
||||
default:
|
||||
temp_head = std::get<2>(result);
|
||||
token_offset += std::get<1>(result);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(MatchResult::Success, token_offset);
|
||||
return std::make_tuple(MatchResult::Success, token_offset, temp_head);
|
||||
}
|
||||
|
||||
std::shared_ptr<const Expression> Rept::parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
auto token_offset = 0;
|
||||
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> Rept::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const {
|
||||
auto temp_head = head;
|
||||
|
||||
// min-match
|
||||
for (auto idx = 0; idx < min_match; ++idx) {
|
||||
auto result = rule_peer->match(stream.mid(token_offset));
|
||||
auto result_gen = rule_peer->parse(rt_inst, temp_head);
|
||||
if (std::get<0>(result_gen))
|
||||
rt_inst->currentInst()->addChild(std::get<0>(result_gen));
|
||||
|
||||
auto result_gen = rule_peer->parse(rt_inst, stream.mid(token_offset));
|
||||
if(result_gen)
|
||||
rt_inst->currentInst()->addChild(result_gen);
|
||||
|
||||
token_offset += std::get<1>(result);
|
||||
temp_head = std::get<1>(result_gen);
|
||||
}
|
||||
|
||||
// max-match
|
||||
for (auto idx = min_match; idx < max_match; ++idx) {
|
||||
auto result = rule_peer->match(stream.mid(token_offset));
|
||||
try {
|
||||
auto result_gen = rule_peer->parse(rt_inst, temp_head);
|
||||
if (std::get<0>(result_gen))
|
||||
rt_inst->currentInst()->addChild(std::get<0>(result_gen));
|
||||
|
||||
switch (std::get<0>(result)) {
|
||||
case MatchResult::Fail:
|
||||
case MatchResult::Part:
|
||||
return nullptr;
|
||||
default:
|
||||
break;
|
||||
temp_head = std::get<1>(result_gen);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
auto result_gen = rule_peer->parse(rt_inst, stream.mid(token_offset));
|
||||
if (result_gen)
|
||||
rt_inst->currentInst()->addChild(result_gen);
|
||||
|
||||
token_offset += std::get<1>(result);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return std::make_tuple(nullptr, temp_head);
|
||||
}
|
||||
|
||||
QString Rept::token_present() const
|
||||
|
|
@ -116,38 +115,41 @@ Seqs::Seqs(const QList<std::shared_ptr<const BaseRule>> mbrs) : mbrs_store(mbrs)
|
|||
|
||||
QList<std::shared_ptr<const BaseRule>> Seqs::children() const { return mbrs_store; }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, uint> Seqs::match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> Seqs::match(std::shared_ptr<const Token> list_head) const {
|
||||
auto token_offset = 0;
|
||||
QString token_seqs = this->token_present();
|
||||
auto temp_head = list_head;
|
||||
|
||||
for (auto& r : mbrs_store) {
|
||||
auto v_token_seqs = r->token_present();
|
||||
auto result = r->match(stream.mid(token_offset));
|
||||
auto result = r->match(list_head);
|
||||
token_offset += std::get<1>(result);
|
||||
temp_head = std::get<2>(result);
|
||||
|
||||
switch (std::get<0>(result)) {
|
||||
case MatchResult::Fail:
|
||||
case MatchResult::Part:
|
||||
return std::make_tuple(token_offset ? MatchResult::Part : MatchResult::Fail, token_offset);
|
||||
return std::make_tuple(token_offset ? MatchResult::Part : MatchResult::Fail, token_offset, temp_head);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(MatchResult::Success, token_offset);
|
||||
return std::make_tuple(MatchResult::Success, token_offset, temp_head);
|
||||
}
|
||||
|
||||
std::shared_ptr<const Expression> Seqs::parse(std::shared_ptr<ParseContext> rt_inst,const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
auto token_offset = 0;
|
||||
for (auto& r : mbrs_store) {
|
||||
auto rst_gene = r->parse(rt_inst, stream.mid(token_offset));
|
||||
if(rst_gene)
|
||||
rt_inst->currentInst()->addChild(rst_gene);
|
||||
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const Token>> Seqs::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const Token> head) const {
|
||||
auto temp_head = head;
|
||||
|
||||
auto result = r->match(stream.mid(token_offset));
|
||||
token_offset += std::get<1>(result);
|
||||
for (auto& r : mbrs_store) {
|
||||
auto rst_gene = r->parse(rt_inst, temp_head);
|
||||
temp_head = std::get<1>(rst_gene);
|
||||
|
||||
if (std::get<0>(rst_gene))
|
||||
rt_inst->currentInst()->addChild(std::get<0>(rst_gene));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return std::make_tuple(nullptr, temp_head);
|
||||
}
|
||||
|
||||
QString Seqs::token_present() const
|
||||
|
|
@ -158,29 +160,29 @@ QString Seqs::token_present() const
|
|||
return QString(u8"(%1)").arg(content);
|
||||
}
|
||||
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const BaseRule>>
|
||||
Any::rule_select(const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const Token>>
|
||||
Any::rule_select(std::shared_ptr<const Token> head) const {
|
||||
QString token_seqs = this->token_present();
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const BaseRule>> temp = std::make_tuple(MatchResult::Fail, 0, nullptr);
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const Token>> temp = std::make_tuple(MatchResult::Fail, 0, nullptr, nullptr);
|
||||
|
||||
for (auto& r : mbrs_store) {
|
||||
auto mbr_seqs = r->token_present();
|
||||
auto result = r->match(stream);
|
||||
auto result = r->match(head);
|
||||
if (std::get<0>(result) == MatchResult::Success)
|
||||
return std::make_tuple(std::get<0>(result), std::get<1>(result), r);
|
||||
return std::make_tuple(std::get<0>(result), std::get<1>(result), r, std::get<2>(result));
|
||||
|
||||
else if (std::get<0>(result) == MatchResult::Part) {
|
||||
if (std::get<0>(temp) == MatchResult::Fail || std::get<1>(result) > std::get<1>(temp))
|
||||
temp = std::make_tuple(MatchResult::Part, std::get<1>(result), r);
|
||||
temp = std::make_tuple(MatchResult::Part, std::get<1>(result), r, std::get<2>(result));
|
||||
else
|
||||
temp = std::make_tuple(MatchResult::Part, std::get<1>(temp), std::get<2>(temp));
|
||||
temp = std::make_tuple(MatchResult::Part, std::get<1>(temp), std::get<2>(temp), std::get<3>(temp));
|
||||
}
|
||||
|
||||
else if (std::get<0>(temp) == MatchResult::Fail) {
|
||||
if (!std::get<2>(temp) || std::get<1>(result) > std::get<1>(temp))
|
||||
temp = std::make_tuple(MatchResult::Fail, std::get<1>(result), r);
|
||||
temp = std::make_tuple(MatchResult::Fail, std::get<1>(result), r, std::get<2>(result));
|
||||
else
|
||||
temp = std::make_tuple(MatchResult::Fail, std::get<1>(temp), std::get<2>(temp));
|
||||
temp = std::make_tuple(MatchResult::Fail, std::get<1>(temp), std::get<2>(temp), std::get<3>(temp));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -191,14 +193,40 @@ Any::Any(const QList<std::shared_ptr<const BaseRule>> mbrs) : mbrs_store(mbrs) {
|
|||
|
||||
QList<std::shared_ptr<const BaseRule>> Any::children() const { return mbrs_store; }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, uint> Any::match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
auto item = rule_select(stream);
|
||||
return std::make_tuple(std::get<0>(item), std::get<1>(item));
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> Any::match(std::shared_ptr<const Token> list_head) const {
|
||||
auto item = rule_select(list_head);
|
||||
return std::make_tuple(std::get<0>(item), std::get<1>(item), std::get<3>(item));
|
||||
}
|
||||
|
||||
std::shared_ptr<const Expression> Any::parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
auto temp = rule_select(stream);
|
||||
return std::get<2>(temp)->parse(rt_inst, stream);
|
||||
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::function<int(std::shared_ptr<const Token>, std::shared_ptr<const Token>)> measure_span =
|
||||
[&](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
|
||||
|
|
@ -215,44 +243,62 @@ SyntaxException::SyntaxException(const QString& message) { this->msg_store = mes
|
|||
|
||||
QString SyntaxException::message() const { return msg_store; }
|
||||
|
||||
lib_syntax::ExpressionRule::ExpressionRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
|
||||
ExpressionRule::ExpressionRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
|
||||
this->filter_proc = [](const TokenSeqs& seqs) { return seqs; };
|
||||
this->mark_store = expr_mark;
|
||||
}
|
||||
|
||||
std::shared_ptr<const ExpressionRule> lib_syntax::ExpressionRule::reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule) {
|
||||
std::shared_ptr<const ExpressionRule> ExpressionRule::reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule) {
|
||||
auto ninst = makeCopy();
|
||||
ninst->child_store = rule;
|
||||
ninst->filter_proc = filter;
|
||||
return ninst;
|
||||
}
|
||||
|
||||
QString lib_syntax::ExpressionRule::name() const { return name_store; }
|
||||
QString ExpressionRule::name() const { return name_store; }
|
||||
|
||||
int lib_syntax::ExpressionRule::typeMark() const { return this->mark_store; }
|
||||
int ExpressionRule::typeMark() const { return this->mark_store; }
|
||||
|
||||
QList<std::shared_ptr<const lib_syntax::BaseRule>> lib_syntax::ExpressionRule::children() const {
|
||||
QList<std::shared_ptr<const BaseRule>> ExpressionRule::children() const {
|
||||
return QList<std::shared_ptr<const BaseRule>>() << this->child_store;
|
||||
}
|
||||
|
||||
std::tuple<BaseRule::MatchResult, uint> lib_syntax::ExpressionRule::match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
return child_store->match(stream);
|
||||
std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const Token>> ExpressionRule::match(std::shared_ptr<const Token> list_head) const {
|
||||
return child_store->match(list_head);
|
||||
}
|
||||
|
||||
std::shared_ptr<const ast_basic::Expression> lib_syntax::ExpressionRule::parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const {
|
||||
std::shared_ptr<ast_basic::Expression> elm_ast = this->newEmptyInstance();
|
||||
|
||||
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<Expression> elm_ast = this->newEmptyInstance();
|
||||
|
||||
rt_inst->pushExpressionRule(this->shared_from_this());
|
||||
rt_inst->pushInst(elm_ast);
|
||||
|
||||
child_store->parse(rt_inst, stream);
|
||||
auto tokens_decl = this->filter_proc(elm_ast->tokens());
|
||||
elm_ast->tokensReset(tokens_decl);
|
||||
try {
|
||||
auto rstg = child_store->parse(rt_inst, head);
|
||||
|
||||
rt_inst->popExpressionRule();
|
||||
return rt_inst->popInst();
|
||||
auto tokens_decl = this->filter_proc(elm_ast->tokens());
|
||||
elm_ast->tokensReset(tokens_decl);
|
||||
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
return std::make_tuple(elm_ast, std::get<1>(rstg));
|
||||
}
|
||||
catch (...) {
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
QString lib_syntax::ExpressionRule::token_present() const {
|
||||
QString ExpressionRule::token_present() const {
|
||||
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流提前终止") {}
|
||||
|
|
@ -11,6 +11,8 @@ namespace ast_basic {
|
|||
}
|
||||
|
||||
namespace lib_syntax {
|
||||
class BaseRule;
|
||||
|
||||
/**
|
||||
* @brief 语法异常
|
||||
*/
|
||||
|
|
@ -25,8 +27,6 @@ namespace lib_syntax {
|
|||
virtual QString message() const;
|
||||
};
|
||||
|
||||
|
||||
class BaseRule;
|
||||
// 基础语法解析接口 ===================================================================================================
|
||||
/**
|
||||
* @brief 解析上下文接口
|
||||
|
|
@ -72,16 +72,18 @@ namespace lib_syntax {
|
|||
|
||||
/**
|
||||
* @brief token流匹配
|
||||
* @return Êײ¿¶ÔÆë£¬Æ¥Åätoken¼¯ºÏ
|
||||
* @return 首部对齐,匹配token集合<匹配结果,匹配长度,剩余列表头指针>
|
||||
*/
|
||||
virtual std::tuple<MatchResult, uint> match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const = 0;
|
||||
virtual std::tuple<MatchResult, uint, std::shared_ptr<const lib_token::Token>> match(std::shared_ptr<const lib_token::Token> remains_head) const = 0;
|
||||
|
||||
/**
|
||||
* @brief 解析
|
||||
* @param stream
|
||||
* @return
|
||||
* @param rt_inst 解析上下文
|
||||
* @param head 列表头
|
||||
* @return 返回结果<匹配完成新列表头,匹配长度>
|
||||
*/
|
||||
virtual std::shared_ptr<const ast_basic::Expression> parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const = 0;
|
||||
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 = 0;
|
||||
|
||||
/**
|
||||
* 返回匹配语法规则的词法序列表达
|
||||
|
|
@ -105,8 +107,9 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<MatchResult, uint> match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::shared_ptr<const ast_basic::Expression> parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) 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>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::Token> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -117,7 +120,7 @@ namespace lib_syntax {
|
|||
private:
|
||||
QList<std::shared_ptr<const BaseRule>> mbrs_store;
|
||||
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const BaseRule>> rule_select(const QList<std::shared_ptr<const lib_token::Token>>& stream) const;
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const lib_token::Token>> rule_select(std::shared_ptr<const lib_token::Token> head) const;
|
||||
|
||||
public:
|
||||
Any(const QList<std::shared_ptr<const BaseRule>> mbrs);
|
||||
|
|
@ -125,8 +128,9 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<MatchResult, uint> match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::shared_ptr<const ast_basic::Expression> parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::tuple<MatchResult, uint, std::shared_ptr<const lib_token::Token>> match(std::shared_ptr<const lib_token::Token> list_head) const override;
|
||||
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;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -143,8 +147,9 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<MatchResult, uint> match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::shared_ptr<const ast_basic::Expression> parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::tuple<MatchResult, uint, std::shared_ptr<const lib_token::Token>> match(std::shared_ptr<const lib_token::Token> list_head) const override;
|
||||
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;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -162,8 +167,9 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<MatchResult, uint> match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::shared_ptr<const ast_basic::Expression> parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::tuple<MatchResult, uint, std::shared_ptr<const lib_token::Token>> match(std::shared_ptr<const lib_token::Token> list_head) const override;
|
||||
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;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -186,8 +192,9 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const lib_syntax::BaseRule>> children() const override;
|
||||
virtual std::tuple<MatchResult, uint> match(const QList<std::shared_ptr<const lib_token::Token>>& stream) const override;
|
||||
virtual std::shared_ptr<const ast_basic::Expression> parse(std::shared_ptr<ParseContext> rt_inst, const QList<std::shared_ptr<const lib_token::Token>>& stream) 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<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;
|
||||
virtual QString token_present() const override;
|
||||
|
||||
private:
|
||||
|
|
@ -216,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
|
||||
|
|
@ -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)
|
||||
{
|
||||
std::const_pointer_cast<ast_gen::SyntaxElement>(root)->cacheLoad();
|
||||
|
||||
root->cacheLoad();
|
||||
for (auto& cinst : children) {
|
||||
cinst->setParent(root);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@
|
|||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
<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>
|
||||
</PropertyGroup>
|
||||
|
|
|
|||
|
|
@ -7,60 +7,74 @@ using namespace lib_token;
|
|||
|
||||
TokenReader::TokenReader(const QList<std::shared_ptr<const TokenDefine>> rulers) : rules_store(rulers) {}
|
||||
|
||||
QList<std::shared_ptr<const lib_token::Token>> lib_token::TokenReader::extractFrom(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");
|
||||
std::shared_ptr<const Token> lib_token::TokenReader::tokensWithin(const QString& path) const {
|
||||
auto content_list = extract_from(path);
|
||||
if (!content_list.size())
|
||||
return nullptr;
|
||||
|
||||
QList<std::shared_ptr<const Token>> ret_list;
|
||||
int line_number = 1;
|
||||
while (!tin.atEnd()) {
|
||||
auto line = tin.readLine() + "\n";
|
||||
ret_list.append(this->parse_line(line_number++, line, path));
|
||||
}
|
||||
std::shared_ptr<const Token> prev_ptr = std::make_shared<const TokenImpl>(content_list.last(), nullptr);
|
||||
for (auto idx = content_list.size() - 2; idx >=0; --idx) {
|
||||
auto content_ptr = content_list[idx];
|
||||
prev_ptr = std::make_shared<const TokenImpl>(content_ptr, prev_ptr);
|
||||
}
|
||||
|
||||
return ret_list;
|
||||
return prev_ptr;
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const Token>> TokenReader::extract_from(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 Token>> ret_list;
|
||||
int line_number = 1;
|
||||
while (!tin.atEnd()) {
|
||||
auto line = tin.readLine() + "\n";
|
||||
ret_list.append(this->parse_line(line_number++, line, path));
|
||||
}
|
||||
|
||||
return ret_list;
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const Token>> TokenReader::parse_line(int row, const QString& line_text, const QString& path) const {
|
||||
auto words = line_text.split(" ", QString::SplitBehavior::SkipEmptyParts);
|
||||
auto words = line_text.split(" ", QString::SplitBehavior::SkipEmptyParts);
|
||||
|
||||
QList<std::shared_ptr<const WordBase>> primary_words;
|
||||
int columns_offset = 0;
|
||||
for (auto& w : words) {
|
||||
auto column_start = line_text.indexOf(w, columns_offset);
|
||||
auto token = std::make_shared<WordPeaks>(row, column_start + 1, w, path);
|
||||
primary_words << token;
|
||||
QList<std::shared_ptr<const WordBase>> primary_words;
|
||||
int columns_offset = 0;
|
||||
for (auto& w : words) {
|
||||
auto column_start = line_text.indexOf(w, columns_offset);
|
||||
auto token = std::make_shared<WordPeaks>(row, column_start + 1, w, path);
|
||||
primary_words << token;
|
||||
|
||||
columns_offset = column_start + w.length();
|
||||
}
|
||||
columns_offset = column_start + w.length();
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const Token>> rets_tokens;
|
||||
for (auto idx = 0; idx < primary_words.size(); ++idx) {
|
||||
auto word = primary_words[idx];
|
||||
auto result = parse_token(word);
|
||||
rets_tokens.append(std::get<0>(result));
|
||||
QList<std::shared_ptr<const Token>> rets_tokens;
|
||||
for (auto idx = 0; idx < primary_words.size(); ++idx) {
|
||||
auto word = primary_words[idx];
|
||||
auto result = parse_token(word);
|
||||
rets_tokens.append(std::get<0>(result));
|
||||
|
||||
auto remains = std::get<1>(result);
|
||||
if (remains)
|
||||
primary_words.insert(idx + 1, remains);
|
||||
}
|
||||
auto remains = std::get<1>(result);
|
||||
if (remains)
|
||||
primary_words.insert(idx + 1, remains);
|
||||
}
|
||||
|
||||
return rets_tokens;
|
||||
return rets_tokens;
|
||||
}
|
||||
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>> TokenReader::parse_token(std::shared_ptr<const WordBase> word) const {
|
||||
for (auto& it : this->rules_store) {
|
||||
auto result = it->analysis(word);
|
||||
if (std::get<0>(result)) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
for (auto& it : this->rules_store) {
|
||||
auto result = it->analysis(word);
|
||||
if (std::get<0>(result)) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
throw new TokenException(QString(u8"Lex[0x0001]指定词语无法解析:%1,<%2,%3>").arg(word->content()).arg(word->row()).arg(word->column()));
|
||||
throw new TokenException(QString(u8"Lex[0x0001]指定词语无法解析:%1,<%2,%3>").arg(word->content()).arg(word->row()).arg(word->column()));
|
||||
}
|
||||
|
||||
TokenException::TokenException(const QString& message) : msg_store(message) {}
|
||||
|
|
@ -77,15 +91,53 @@ int WordPeaks::row() const { return row_n; }
|
|||
|
||||
int WordPeaks::column() const { return col_n; }
|
||||
|
||||
TokenInst::TokenInst(int r, int c, const QString& t, const QString& p, std::shared_ptr<const TokenDefine> type)
|
||||
: row_n(r), col_n(c), text_n(t), path_p(p), type_def(type) {}
|
||||
TokenContent::TokenContent(int r, int c, const QString& t, const QString& p, std::shared_ptr<const TokenDefine> type)
|
||||
: row_n(r), col_n(c), text_n(t), path_p(p), type_def(type) {}
|
||||
|
||||
QString TokenInst::file() const { return path_p; }
|
||||
QString TokenContent::file() const { return path_p; }
|
||||
|
||||
QString TokenInst::content() const { return text_n; }
|
||||
QString TokenContent::content() const { return text_n; }
|
||||
|
||||
int TokenInst::row() const { return row_n; }
|
||||
int TokenContent::row() const { return row_n; }
|
||||
|
||||
int TokenInst::column() const { return col_n; }
|
||||
int TokenContent::column() const { return col_n; }
|
||||
|
||||
std::shared_ptr<const TokenDefine> TokenInst::define() const { return this->type_def; }
|
||||
std::shared_ptr<const TokenDefine> TokenContent::define() const { return this->type_def; }
|
||||
|
||||
std::shared_ptr<const Token> TokenContent::nextToken() const
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
TokenImpl::TokenImpl(std::shared_ptr<const Token> content, std::shared_ptr<const Token> next)
|
||||
: content_ptr(content), next_element(next) {}
|
||||
|
||||
QString TokenImpl::file() const
|
||||
{
|
||||
return content_ptr->file();
|
||||
}
|
||||
|
||||
QString TokenImpl::content() const
|
||||
{
|
||||
return content_ptr->content();
|
||||
}
|
||||
|
||||
int TokenImpl::row() const
|
||||
{
|
||||
return content_ptr->row();
|
||||
}
|
||||
|
||||
int TokenImpl::column() const
|
||||
{
|
||||
return content_ptr->column();
|
||||
}
|
||||
|
||||
std::shared_ptr<const TokenDefine> TokenImpl::define() const
|
||||
{
|
||||
return content_ptr->define();
|
||||
}
|
||||
|
||||
std::shared_ptr<const Token> TokenImpl::nextToken() const
|
||||
{
|
||||
return next_element;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,140 +6,159 @@
|
|||
#include <memory>
|
||||
|
||||
namespace lib_token {
|
||||
class TokenDefine;
|
||||
class TokenDefine;
|
||||
/**
|
||||
* @brief 解析异常
|
||||
*/
|
||||
class LIBTOKEN_EXPORT TokenException {
|
||||
private:
|
||||
QString msg_store;
|
||||
|
||||
/**
|
||||
* @brief 源码词语实例
|
||||
*/
|
||||
class WordBase {
|
||||
public:
|
||||
/**
|
||||
* @brief 源文件路径
|
||||
* @return
|
||||
*/
|
||||
virtual QString file() const = 0;
|
||||
/**
|
||||
* @brief 词语内容
|
||||
* @return
|
||||
*/
|
||||
virtual QString content() const = 0;
|
||||
/**
|
||||
* @brief 源码行号
|
||||
* @return
|
||||
*/
|
||||
virtual int row() const = 0;
|
||||
/**
|
||||
* @brief 源码列号
|
||||
* @return
|
||||
*/
|
||||
virtual int column() const = 0;
|
||||
};
|
||||
public:
|
||||
TokenException(const QString& message);
|
||||
virtual QString message() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief token解析结果
|
||||
*/
|
||||
class Token : public WordBase {
|
||||
public:
|
||||
/**
|
||||
* @brief token解析机制关联
|
||||
* @return
|
||||
*/
|
||||
virtual std::shared_ptr<const TokenDefine> define() const = 0;
|
||||
};
|
||||
/**
|
||||
* @brief 源码词语实例
|
||||
*/
|
||||
class WordBase {
|
||||
public:
|
||||
/**
|
||||
* @brief 源文件路径
|
||||
* @return
|
||||
*/
|
||||
virtual QString file() const = 0;
|
||||
/**
|
||||
* @brief 词语内容
|
||||
* @return
|
||||
*/
|
||||
virtual QString content() const = 0;
|
||||
/**
|
||||
* @brief 源码行号
|
||||
* @return
|
||||
*/
|
||||
virtual int row() const = 0;
|
||||
/**
|
||||
* @brief 源码列号
|
||||
* @return
|
||||
*/
|
||||
virtual int column() const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief token解析机制定义
|
||||
*/
|
||||
class TokenDefine {
|
||||
public:
|
||||
/**
|
||||
* @brief 解析机制名称
|
||||
* @return
|
||||
*/
|
||||
virtual QString name() const = 0;
|
||||
/**
|
||||
* @brief 解析机制关键定义
|
||||
* @return
|
||||
*/
|
||||
virtual QString regex() const = 0;
|
||||
/**
|
||||
* @brief token解析结果
|
||||
*/
|
||||
class Token : public WordBase {
|
||||
public:
|
||||
/**
|
||||
* @brief token解析机制关联
|
||||
* @return
|
||||
*/
|
||||
virtual std::shared_ptr<const TokenDefine> define() const = 0;
|
||||
virtual std::shared_ptr<const Token> nextToken() const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 解析词语处理过程
|
||||
* @param content 词语
|
||||
* @return tuple<token/null,remains> 解析结果
|
||||
*/
|
||||
virtual std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>> analysis(std::shared_ptr<const WordBase> content) const = 0;
|
||||
};
|
||||
/**
|
||||
* @brief token解析机制定义
|
||||
*/
|
||||
class TokenDefine {
|
||||
public:
|
||||
/**
|
||||
* @brief 解析机制名称
|
||||
* @return
|
||||
*/
|
||||
virtual QString name() const = 0;
|
||||
/**
|
||||
* @brief 解析机制关键定义
|
||||
* @return
|
||||
*/
|
||||
virtual QString regex() const = 0;
|
||||
|
||||
/**
|
||||
* @brief 解析异常
|
||||
*/
|
||||
class LIBTOKEN_EXPORT TokenException {
|
||||
private:
|
||||
QString msg_store;
|
||||
/**
|
||||
* @brief 解析词语处理过程
|
||||
* @param content 词语
|
||||
* @return tuple<token/null,remains> 解析结果
|
||||
*/
|
||||
virtual std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>> analysis(std::shared_ptr<const WordBase> content) const = 0;
|
||||
};
|
||||
|
||||
public:
|
||||
TokenException(const QString& message);
|
||||
virtual QString message() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 未解析原始词语
|
||||
*/
|
||||
class LIBTOKEN_EXPORT WordPeaks : public WordBase {
|
||||
private:
|
||||
int row_n, col_n;
|
||||
QString text_n, path_p;
|
||||
/**
|
||||
* @brief 未解析原始词语
|
||||
*/
|
||||
class WordPeaks : public WordBase {
|
||||
private:
|
||||
int row_n, col_n;
|
||||
QString text_n, path_p;
|
||||
|
||||
public:
|
||||
WordPeaks(int r, int c, const QString& t, const QString& p);
|
||||
public:
|
||||
WordPeaks(int r, int c, const QString& t, const QString& p);
|
||||
|
||||
// WordBase interface
|
||||
public:
|
||||
virtual QString file() const override;
|
||||
virtual QString content() const override;
|
||||
virtual int row() const override;
|
||||
virtual int column() const override;
|
||||
};
|
||||
// WordBase interface
|
||||
public:
|
||||
virtual QString file() const override;
|
||||
virtual QString content() const override;
|
||||
virtual int row() const override;
|
||||
virtual int column() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 词法解析成果
|
||||
*/
|
||||
class LIBTOKEN_EXPORT TokenInst : public Token {
|
||||
private:
|
||||
int row_n, col_n;
|
||||
QString text_n, path_p;
|
||||
std::shared_ptr<const TokenDefine> type_def;
|
||||
/**
|
||||
* @brief 词法解析成果
|
||||
*/
|
||||
class TokenContent : public Token {
|
||||
private:
|
||||
int row_n, col_n;
|
||||
QString text_n, path_p;
|
||||
std::shared_ptr<const TokenDefine> type_def;
|
||||
|
||||
public:
|
||||
TokenInst(int r, int c, const QString& t, const QString& p, std::shared_ptr<const TokenDefine> type);
|
||||
public:
|
||||
TokenContent(int r, int c, const QString& t, const QString& p, std::shared_ptr<const TokenDefine> type);
|
||||
|
||||
// WordBase interface
|
||||
public:
|
||||
virtual QString file() const override;
|
||||
virtual QString content() const override;
|
||||
virtual int row() const override;
|
||||
virtual int column() const override;
|
||||
// WordBase interface
|
||||
public:
|
||||
virtual QString file() const override;
|
||||
virtual QString content() const override;
|
||||
virtual int row() const override;
|
||||
virtual int column() const override;
|
||||
|
||||
// Token interface
|
||||
public:
|
||||
virtual std::shared_ptr<const TokenDefine> define() const override;
|
||||
};
|
||||
// Token interface
|
||||
public:
|
||||
virtual std::shared_ptr<const TokenDefine> define() const override;
|
||||
virtual std::shared_ptr<const Token> nextToken() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 词法获取器
|
||||
*/
|
||||
class LIBTOKEN_EXPORT TokenReader {
|
||||
private:
|
||||
QList<std::shared_ptr<const TokenDefine>> rules_store;
|
||||
class TokenImpl : public Token {
|
||||
private:
|
||||
std::shared_ptr<const Token> content_ptr;
|
||||
std::shared_ptr<const Token> next_element;
|
||||
|
||||
QList<std::shared_ptr<const Token>> parse_line(int row, const QString& line_text, const QString& path) const;
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>> parse_token(std::shared_ptr<const WordBase> word) const;
|
||||
public:
|
||||
TokenImpl(std::shared_ptr<const Token> content, std::shared_ptr<const Token> next);
|
||||
|
||||
public:
|
||||
TokenReader(const QList<std::shared_ptr<const TokenDefine>> rulers);
|
||||
// 通过 Token 继承
|
||||
QString file() const override;
|
||||
QString content() const override;
|
||||
int row() const override;
|
||||
int column() const override;
|
||||
std::shared_ptr<const TokenDefine> define() const override;
|
||||
std::shared_ptr<const Token> nextToken() const override;
|
||||
};
|
||||
|
||||
QList<std::shared_ptr<const Token>> extractFrom(const QString& path) const;
|
||||
};
|
||||
/**
|
||||
* @brief 词法获取器
|
||||
*/
|
||||
class LIBTOKEN_EXPORT TokenReader {
|
||||
private:
|
||||
QList<std::shared_ptr<const TokenDefine>> rules_store;
|
||||
|
||||
QList<std::shared_ptr<const Token>> parse_line(int row, const QString& line_text, const QString& path) const;
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>> parse_token(std::shared_ptr<const WordBase> word) const;
|
||||
QList<std::shared_ptr<const Token>> extract_from(const QString& path) const;
|
||||
|
||||
public:
|
||||
TokenReader(const QList<std::shared_ptr<const TokenDefine>> rulers);
|
||||
std::shared_ptr<const Token> tokensWithin(const QString &path) const;
|
||||
};
|
||||
|
||||
} // namespace lib_token
|
||||
|
|
@ -1,22 +1,23 @@
|
|||
#include "tokens_novel.h"
|
||||
|
||||
using namespace example_novel;
|
||||
using namespace lib_token;
|
||||
|
||||
QString LeftBracket::name() const { return u8"left-bracket"; }
|
||||
|
||||
QString LeftBracket::regex() const { return u8"{"; }
|
||||
|
||||
std::tuple<std::shared_ptr<const lib_token::Token>, std::shared_ptr<const lib_token::WordBase>>
|
||||
LeftBracket::analysis(std::shared_ptr<const lib_token::WordBase> content) const {
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>>
|
||||
LeftBracket::analysis(std::shared_ptr<const WordBase> content) const {
|
||||
auto text = content->content();
|
||||
if (!text.startsWith(regex()))
|
||||
return std::make_tuple(nullptr, content);
|
||||
|
||||
auto token_inst = std::make_shared<lib_token::TokenInst>(content->row(), content->column(), content->content().mid(0, regex().length()),
|
||||
auto token_inst = std::make_shared<TokenContent>(content->row(), content->column(), content->content().mid(0, regex().length()),
|
||||
content->file(), shared_from_this());
|
||||
auto t_remains = content->content().mid(regex().length());
|
||||
if (t_remains.length() > 0) {
|
||||
auto remains = std::make_shared<lib_token::WordPeaks>(content->row(), content->column() + regex().length(), t_remains, content->file());
|
||||
auto remains = std::make_shared<WordPeaks>(content->row(), content->column() + regex().length(), t_remains, content->file());
|
||||
return std::make_tuple(token_inst, remains);
|
||||
}
|
||||
return std::make_tuple(token_inst, nullptr);
|
||||
|
|
@ -36,13 +37,13 @@ QString Keywords::name() const { return name_store; }
|
|||
|
||||
QString Keywords::regex() const { return value_store; }
|
||||
|
||||
std::tuple<std::shared_ptr<const lib_token::Token>, std::shared_ptr<const lib_token::WordBase>>
|
||||
Keywords::analysis(std::shared_ptr<const lib_token::WordBase> content) const {
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>>
|
||||
Keywords::analysis(std::shared_ptr<const WordBase> content) const {
|
||||
if (content->content() != regex()) {
|
||||
return std::make_tuple(nullptr, content);
|
||||
}
|
||||
|
||||
auto token_inst = std::make_shared<lib_token::TokenInst>(content->row(), content->column(), content->content(), content->file(), shared_from_this());
|
||||
auto token_inst = std::make_shared<TokenContent>(content->row(), content->column(), content->content(), content->file(), shared_from_this());
|
||||
return std::make_tuple(token_inst, nullptr);
|
||||
}
|
||||
|
||||
|
|
@ -54,14 +55,14 @@ QString Numbers::name() const { return u8"numbers"; }
|
|||
|
||||
QString Numbers::regex() const { return u8"^([0-9]+)$"; }
|
||||
|
||||
std::tuple<std::shared_ptr<const lib_token::Token>, std::shared_ptr<const lib_token::WordBase>>
|
||||
Numbers::analysis(std::shared_ptr<const lib_token::WordBase> content) const {
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>>
|
||||
Numbers::analysis(std::shared_ptr<const WordBase> content) const {
|
||||
auto text = content->content();
|
||||
QRegExp regx(regex());
|
||||
if (regx.indexIn(text) == -1)
|
||||
return std::make_tuple(nullptr, content);
|
||||
|
||||
auto tinst = std::make_shared<lib_token::TokenInst>(content->row(), content->column(), content->content(), content->file(), shared_from_this());
|
||||
auto tinst = std::make_shared<TokenContent>(content->row(), content->column(), content->content(), content->file(), shared_from_this());
|
||||
return std::make_tuple(tinst, nullptr);
|
||||
}
|
||||
|
||||
|
|
@ -69,8 +70,8 @@ QString VTextSection::name() const { return u8"text-section"; }
|
|||
|
||||
QString VTextSection::regex() const { return u8"^([^\\{\\}\\n@&]+)"; }
|
||||
|
||||
std::tuple<std::shared_ptr<const lib_token::Token>, std::shared_ptr<const lib_token::WordBase>>
|
||||
VTextSection::analysis(std::shared_ptr<const lib_token::WordBase> content) const {
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase>>
|
||||
VTextSection::analysis(std::shared_ptr<const WordBase> content) const {
|
||||
auto text = content->content();
|
||||
QRegExp regx(regex());
|
||||
if (regx.indexIn(text) == -1) {
|
||||
|
|
@ -80,9 +81,9 @@ VTextSection::analysis(std::shared_ptr<const lib_token::WordBase> content) const
|
|||
auto match = regx.cap(1);
|
||||
auto remains = content->content().mid(match.length());
|
||||
|
||||
auto tinst = std::make_shared<lib_token::TokenInst>(content->row(), content->column(), match, content->file(), shared_from_this());
|
||||
auto tinst = std::make_shared<TokenContent>(content->row(), content->column(), match, content->file(), shared_from_this());
|
||||
if (remains.length()) {
|
||||
auto t_remains = std::make_shared<lib_token::WordPeaks>(content->row(), content->column(), remains, content->file());
|
||||
auto t_remains = std::make_shared<WordPeaks>(content->row(), content->column(), remains, content->file());
|
||||
return std::make_tuple(tinst, t_remains);
|
||||
}
|
||||
return std::make_tuple(tinst, nullptr);
|
||||
|
|
@ -96,7 +97,7 @@ QString NameSection::name() const { return u8"name-section"; }
|
|||
|
||||
QString NameSection::regex() const { return u8"^([^:\\{\\}\\n@&][^\\{\\}\\n@&]*)"; }
|
||||
|
||||
std::tuple<std::shared_ptr<const lib_token::Token>, std::shared_ptr<const lib_token::WordBase> > NameSection::analysis(std::shared_ptr<const lib_token::WordBase> content) const
|
||||
std::tuple<std::shared_ptr<const Token>, std::shared_ptr<const WordBase> > NameSection::analysis(std::shared_ptr<const WordBase> content) const
|
||||
{
|
||||
auto text = content->content();
|
||||
QRegExp regx(regex());
|
||||
|
|
@ -107,9 +108,9 @@ std::tuple<std::shared_ptr<const lib_token::Token>, std::shared_ptr<const lib_to
|
|||
auto match = regx.cap(1);
|
||||
auto remains = content->content().mid(match.length());
|
||||
|
||||
auto tinst = std::make_shared<lib_token::TokenInst>(content->row(), content->column(), match, content->file(), shared_from_this());
|
||||
auto tinst = std::make_shared<TokenContent>(content->row(), content->column(), match, content->file(), shared_from_this());
|
||||
if (remains.length()) {
|
||||
auto t_remains = std::make_shared<lib_token::WordPeaks>(content->row(), content->column(), remains, content->file());
|
||||
auto t_remains = std::make_shared<WordPeaks>(content->row(), content->column(), remains, content->file());
|
||||
return std::make_tuple(tinst, t_remains);
|
||||
}
|
||||
return std::make_tuple(tinst, nullptr);
|
||||
|
|
|
|||
Loading…
Reference in New Issue