encoding update

This commit is contained in:
codeboss 2025-02-11 22:32:10 +08:00
parent 22f2d1adf2
commit 735a9fb210
21 changed files with 458 additions and 442 deletions

View File

@ -14,7 +14,7 @@ std::shared_ptr<const ExprRule> ExprInstance::definedRule() const {
QString ExprInstance::filePath() const {
if (!tokens_bind.size())
throw new SyntaxException(u8"InternalError[0x0002]一个空的非法无效节点");
throw new SyntaxException("InternalError[0x0002]一个空的非法无效节点");
return tokens_bind.first()->file();
}

View File

@ -9,55 +9,55 @@ namespace lib_syntax {
class ExprRule;
}
// 抽象语法树结构 ====================================================================
// 抽象语法树结构 ====================================================================
namespace ast_basic {
/**
* @brief /
* @brief /
*/
class IExprInstance {
public:
/**
* @brief
* @brief
* @return
*/
virtual std::shared_ptr<const lib_syntax::ExprRule> definedRule() const = 0;
//=====================================================
/**
* .
* .
*
* \return
* \return
*/
virtual QString filePath() const = 0;
/**
* token序列.
* token序列.
*
* \return token序列
* \return token序列
*/
virtual QList<std::shared_ptr<const lib_token::IToken>> tokens() const = 0;
/**
* token实例.
* token实例.
*
* \param token_inst
* \param token_inst
*/
virtual void addToken(std::shared_ptr<const lib_token::IToken> token_inst) = 0;
//=====================================================
/**
* @brief
* @brief
* @return
*/
virtual QList<std::shared_ptr<const IExprInstance>> children() const = 0;
/**
* @brief .
* @brief .
*
* \param inst
* \param inst
*/
virtual void addChild(std::shared_ptr<const IExprInstance> inst) = 0;
};
// 基础语法树构成 ==========================================================================================
// 基础语法树构成 ==========================================================================================
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT ExprInstance : public IExprInstance, public std::enable_shared_from_this<ExprInstance> {
private:
@ -68,7 +68,7 @@ namespace ast_basic {
public:
ExprInstance(std::shared_ptr<const lib_syntax::ExprRule> bind);
// 通过 Expression 继承
// 通过 Expression 继承
std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override;
QString filePath() const override;
@ -80,7 +80,7 @@ namespace ast_basic {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT ExprProgram : public IExprInstance, public std::enable_shared_from_this<ExprProgram> {
private:
@ -90,7 +90,7 @@ namespace ast_basic {
public:
ExprProgram(const QString &root);
// 通过 IExprInstance 继承
// 通过 IExprInstance 继承
std::shared_ptr<const lib_syntax::ExprRule> definedRule() const override;
QString filePath() const override;
QList<std::shared_ptr<const lib_token::IToken>> tokens() const override;

View File

@ -7,7 +7,7 @@
namespace ast_gen {
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT SyntaxParser {
private:
@ -17,17 +17,17 @@ namespace ast_gen {
SyntaxParser(std::shared_ptr<const lib_syntax::IBasicRule> rule);
/**
* @brief
* @param wods
* @return ->
* @brief
* @param wods
* @return ->
*/
QList<std::shared_ptr<const lib_syntax::MatchCursor>> parse(std::shared_ptr<const lib_words::IPrimitiveWord> words);
/**
* @brief
* @param cursor
* @param root
* @return
* @brief
* @param cursor
* @param root
* @return
*/
std::shared_ptr<ast_basic::IExprInstance> getAst(
std::shared_ptr<const lib_syntax::MatchCursor> cursor, std::shared_ptr<ast_basic::IExprInstance> root);

View File

@ -19,7 +19,7 @@ QString TextSection::content() const {
}
QString TextSection::signature() const {
return u8"::section";
return "::section";
}
PointRefers::PointRefers(std::shared_ptr<const ExprRule> rule_bind)
@ -50,12 +50,12 @@ void PointRefers::setPointRefer(const QString& refer) {
}
QString PointRefers::referSignature() const {
return storyRefer() + u8"&" + sliceRefer() + u8"&" + pointRefer();
return storyRefer() + "&" + sliceRefer() + "&" + pointRefer();
}
QString PointRefers::signature() const {
QString signature = u8"@" + referSignature();
return parent().lock()->signature() + u8"&" + signature;
QString signature = "@" + referSignature();
return parent().lock()->signature() + "&" + signature;
}
@ -71,7 +71,7 @@ void PointDefines::setName(const QString& nm) {
}
QString PointDefines::signature() const {
return parent().lock()->signature() + u8"&" + name();
return parent().lock()->signature() + "&" + name();
}
@ -103,7 +103,7 @@ Document::Document(std::shared_ptr<const ExprRule> rule_bind)
: AbstractImpl(rule_bind) { }
QString Document::signature() const {
return QString(u8"::document<%1>").arg(path());
return QString("::document<%1>").arg(path());
}
VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind)
@ -133,7 +133,7 @@ void ArticleDefine::setName(const QString& nm) {
}
QString ArticleDefine::signature() const {
return parent().lock()->signature() + u8"&" + name();
return parent().lock()->signature() + "&" + name();
}
RankDeclare::RankDeclare(std::shared_ptr<const ExprRule> rule)
@ -148,12 +148,12 @@ void RankDeclare::setRank(int nums) {
}
QString RankDeclare::signature() const {
return u8"::rank";
return "::rank";
}
FragmentSlice::FragmentSlice(std::shared_ptr<const ExprRule> rule)
:AbstractImpl(rule) {
_slice_name = QString(u8"Ãû³ÆÎ´¶¨Òå_%1").arg((uint64_t)this);
_slice_name = QString("名称未定义_%1").arg((uint64_t)this);
}
QString FragmentSlice::name() const {
@ -165,7 +165,7 @@ void FragmentSlice::setName(const QString& nm) {
}
QString FragmentSlice::signature() const {
return parent().lock()->signature() + u8"&" + name();
return parent().lock()->signature() + "&" + name();
}
std::shared_ptr<const IExprInstance> NGlobalElement::bindExpression() const {
@ -185,7 +185,7 @@ QString NGlobalElement::path() const {
}
QString NGlobalElement::signature() const {
return u8"::program";
return "::program";
}
std::weak_ptr<const SyntaxElement> NGlobalElement::parent() const {
@ -217,7 +217,7 @@ void GlobalElement::clearCache() {
}
std::shared_ptr<const SyntaxElement> GlobalElement::appendToCache(std::shared_ptr<const SyntaxElement> named_node) {
auto mixed_key = QString(u8"%1<%2>").arg(named_node->signature()).arg(named_node->typeMark());
auto mixed_key = QString("%1<%2>").arg(named_node->signature()).arg(named_node->typeMark());
if (node_cache.contains(mixed_key))
return node_cache[mixed_key];
node_cache[mixed_key] = named_node;
@ -225,7 +225,7 @@ std::shared_ptr<const SyntaxElement> GlobalElement::appendToCache(std::shared_pt
}
std::shared_ptr<const SyntaxElement> GlobalElement::getNamedNodeBy(int paramType, const QString& signature) const {
auto mixed_key = QString(u8"%1<%2>").arg(signature).arg(paramType);
auto mixed_key = QString("%1<%2>").arg(signature).arg(paramType);
if (!node_cache.contains(mixed_key))
return nullptr;
return node_cache[mixed_key];
@ -240,11 +240,11 @@ bool GlobalElement::isAnonymous() const {
}
QString GlobalElement::signature() const {
return u8"::global";
return "::global";
}
QString GlobalElement::path() const {
return u8"";
return "";
}
std::weak_ptr<const SyntaxElement> GlobalElement::parent() const {

View File

@ -9,64 +9,64 @@ namespace ast_gen {
class TokenAccess;
/**
* @brief
* @brief
*/
class SyntaxElement {
public:
virtual ~SyntaxElement() = default;
/**
* .
* .
*
* \return
* \return
*/
virtual std::shared_ptr<const ast_basic::IExprInstance> bindExpression() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual int typeMark() const = 0;
/**
* .
* .
*
* \return
* \return
*/
virtual bool isAnonymous() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual QString path() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual QString signature() const = 0;
/**
* @brief
* @return parentnullptr
* @brief
* @return parentnullptr
*/
virtual std::weak_ptr<const SyntaxElement> parent() const = 0;
/**
* @brief .
* @brief .
*
* \param inst
*/
virtual void setParent(std::shared_ptr<const SyntaxElement> inst) = 0;
/**
* @brief Token集合
* @brief Token集合
* @return
*/
virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const = 0;
};
/**
* @brief 访
* @brief 访
*/
class LIBSYNTAX_EXPORT ElementAccess {
private:
@ -79,14 +79,14 @@ namespace ast_gen {
QList<std::shared_ptr<const ElementAccess>> children() const;
/**
* @brief Token定义
* @brief Token定义
* @return
*/
virtual QList<std::shared_ptr<const TokenAccess>> tokens() const;
};
/**
* @brief Token元素访问接口
* @brief Token元素访问接口
*/
class LIBSYNTAX_EXPORT TokenAccess {
private:
@ -100,7 +100,7 @@ namespace ast_gen {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT GlobalElement : public SyntaxElement {
private:
@ -114,10 +114,10 @@ namespace ast_gen {
virtual void clearCache();
virtual std::shared_ptr<const ast_gen::SyntaxElement> appendToCache(std::shared_ptr<const ast_gen::SyntaxElement> named_node);
/**
* @brief
* @param signature
* @brief
* @param signature
* @return
* @throws
* @throws
*/
virtual std::shared_ptr<const ast_gen::SyntaxElement> getNamedNodeBy(int paramType, const QString& signature) const;
virtual void addChild(std::shared_ptr<ast_gen::SyntaxElement> citem);
@ -132,7 +132,7 @@ namespace ast_gen {
virtual void setParent(std::shared_ptr<const SyntaxElement> inst) override;
virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const override;
// 通过 SyntaxElement 继承
// 通过 SyntaxElement 继承
virtual std::shared_ptr<const ast_basic::IExprInstance> bindExpression() const override;
};
}
@ -182,7 +182,7 @@ namespace example_novel {
parent_store.reset();
}
// 通过 SyntaxElement 继承
// 通过 SyntaxElement 继承
virtual int typeMark() const override {
return (int) type;
}
@ -213,23 +213,23 @@ namespace example_novel {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT TextSection : public AbstractImpl<NovelNode::TextSection, false> {
public:
TextSection(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
/**
* @brief
* @brief
*/
QString content() const;
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT FragmentSlice : public AbstractImpl<NovelNode::FragmentSlice, true> {
private:
@ -241,12 +241,12 @@ namespace example_novel {
QString name() const;
void setName(const QString& nm);
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
QString signature() const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT PointRefers : public AbstractImpl<NovelNode::PointRefers, false> {
private:
@ -266,12 +266,12 @@ namespace example_novel {
QString referSignature() const;
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT PointDefines : public AbstractImpl<NovelNode::PointDefines, true> {
private:
@ -283,12 +283,12 @@ namespace example_novel {
QString name() const;
void setName(const QString& nm);
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT ArticleDefine : public AbstractImpl<NovelNode::ArticleDefine, true> {
public:
@ -297,7 +297,7 @@ namespace example_novel {
QString name() const;
void setName(const QString& nm);
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
private:
@ -305,7 +305,7 @@ namespace example_novel {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT VolumeDefine : public AbstractImpl<NovelNode::VolumeDefine, true> {
public:
@ -314,7 +314,7 @@ namespace example_novel {
QString name() const;
void setName(const QString& nm);
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
private:
@ -322,7 +322,7 @@ namespace example_novel {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT StoryDefine : public AbstractImpl<NovelNode::StoryDefine, true> {
public:
@ -334,7 +334,7 @@ namespace example_novel {
void setSort(int value);
int sort() const;
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
private:
@ -343,7 +343,7 @@ namespace example_novel {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT RankDeclare : public AbstractImpl<NovelNode::RankDeclaration, false> {
private:
@ -354,29 +354,29 @@ namespace example_novel {
int rankNumber() const;
void setRank(int nums);
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
QString signature() const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT Document : public AbstractImpl<NovelNode::Document, false> {
public:
Document(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
// 通过 AbstractImpl 继承
// 通过 AbstractImpl 继承
virtual QString signature() const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT NGlobalElement : public ast_basic::ExprProgram, public ast_gen::SyntaxElement {
public:
NGlobalElement(const QString &root);
// 通过 SyntaxElement 继承
// 通过 SyntaxElement 继承
std::shared_ptr<const ast_basic::IExprInstance> bindExpression() const override;
int typeMark() const override;
bool isAnonymous() const override;

View File

@ -27,6 +27,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
@ -73,8 +74,9 @@
<AdditionalDependencies>libWords.lib;%(AdditionalDependencies);$(Qt_LIBS_)</AdditionalDependencies>
</Link>
<ClCompile>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<PreprocessToFile>false</PreprocessToFile>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -123,6 +125,7 @@
<ClCompile Include="libsyntax.cpp" />
<ClInclude Include="libtokens.h" />
<ClInclude Include="syntax_novel.h" />
<ClInclude Include="syntax_templets.h" />
<ClInclude Include="tokens_impl.h" />
<ClInclude Include="tokens_novel.h" />
</ItemGroup>

View File

@ -53,6 +53,9 @@
<ClInclude Include="tokens_impl.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="syntax_templets.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ast_basic.cpp">

View File

@ -29,36 +29,11 @@ QList<std::shared_ptr<const MatchCursor>> Any::parse(std::shared_ptr<const Match
QString Any::present() const {
QString members_content;
for (auto& it : children()) {
members_content += it->present() + u8"|";
members_content += it->present() + "|";
}
return members_content.mid(0, members_content.size() - 1);
}
QList<QString> lib_syntax::Any::invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const {
QList<QString> lines;
lines << QString(u8"if (%1->mustStop())").arg(in_cursor_name);
lines << QString(u8" return QList<std::shared_ptr<const MatchCursor>>() << %1; ").arg(in_cursor_name);
lines << QString(u8"QList<std::shared_ptr<const MatchCursor>> result_list_%1;").arg((uint64_t) this);
for (auto rule : children()) {
auto segments = rule->invokeSegments(in_cursor_name, QString(u8"result_list_%1").arg((uint64_t) this));
if (segments.size() == 1) {
lines.append(segments);
}
else {
lines << u8"{";
lines.append(segments);
lines << u8"}";
}
}
lines << QString(u8"%2 = result_list_%1;").arg((uint64_t) this).arg(out_list_name);
}
QString lib_syntax::Any::implementSegments(const QString& in_cursor_name, QList<QString>& lines) const {
return QString();
}
Seqs::Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) { }
QList<std::shared_ptr<const IBasicRule>> Seqs::children() const {
@ -96,28 +71,6 @@ QString Seqs::present() const {
return content.mid(0, content.size() - 1);
}
QList<QString> lib_syntax::Seqs::invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const {
QList<QString> lines;
lines << QString(u8"if (%1->mustStop())").arg(in_cursor_name);
lines << QString(u8" return QList<std::shared_ptr<const MatchCursor>>() << %1; ").arg(in_cursor_name);
int times = 0;
lines << QString(u8"QList<std::shared_ptr<const MatchCursor>> bridge_list{ %1 };").arg(in_cursor_name);
for (auto rule : children()) {
auto segments = rule->invokeSegments(in_cursor_name, QString(u8"result_list_%1").arg((uint64_t) this));
if (segments.size() == 1) {
lines.append(segments);
}
else {
lines << u8"{";
lines.append(segments);
lines << u8"}";
}
}
lines << QString(u8"%2 = result_list_%1;").arg((uint64_t) this).arg(out_list_name);
}
Rept::Rept(std::shared_ptr<const IBasicRule> rule, int min, int max)
: rule_peer(rule), min_match(min), max_match(max) { }
@ -131,11 +84,11 @@ QList<std::shared_ptr<const MatchCursor>> Rept::parse(std::shared_ptr<const Matc
QList<std::shared_ptr<const MatchCursor>> results;
QList<std::shared_ptr<const MatchCursor>> bridge_list{ cursor };
// 最小重复次数匹配
// 最小重复次数匹配
for (auto idx = 0; idx < min_match; ++idx) {
QList<std::shared_ptr<const MatchCursor>> current_list;
// 迭代每一次可能匹配
// 迭代每一次可能匹配
std::for_each(bridge_list.begin(), bridge_list.end(),
[&](std::shared_ptr<const MatchCursor> curs) {
if (curs->mustStop())
@ -148,31 +101,31 @@ QList<std::shared_ptr<const MatchCursor>> Rept::parse(std::shared_ptr<const Matc
bridge_list = current_list;
}
// 归并失败分支
// 归并失败分支
std::copy_if(bridge_list.begin(), bridge_list.end(), std::back_inserter(results),
[&](std::shared_ptr<const MatchCursor> ins) { return ins->mustStop(); });
// 清除匹配失败分支
// 清除匹配失败分支
for (auto idx = 0; idx < bridge_list.size(); ++idx)
if (bridge_list.at(idx)->mustStop())
bridge_list.removeAt(idx--);
// 不满足最小匹配
// 不满足最小匹配
if (!bridge_list.size())
return results;
// 尝试重复匹配最大次数
// 尝试重复匹配最大次数
for (auto idx = min_match; idx < max_match; ++idx) {
QList<std::shared_ptr<const MatchCursor>> current_list;
// 匹配迭代一次
// 匹配迭代一次
std::for_each(bridge_list.begin(), bridge_list.end(),
[&](std::shared_ptr<const MatchCursor> ins) {
current_list.append(this->rule_peer->parse(ins));
});
// 移除失败分支
// 移除失败分支
for (auto idx = 0; idx < current_list.size(); ++idx) {
auto rst_branch = current_list.at(idx);
if (rst_branch->mustStop() && rst_branch->currentWords()) {
@ -197,13 +150,13 @@ QList<std::shared_ptr<const MatchCursor>> Rept::parse(std::shared_ptr<const Matc
QString Rept::present() const {
if (min_match == 0 && max_match == INT_MAX)
return u8"(" + this->rule_peer->present() + QString(u8")*");
return "(" + this->rule_peer->present() + QString(")*");
else if (min_match == 1 && max_match == INT_MAX)
return u8"(" + this->rule_peer->present() + QString(u8")+");
return "(" + this->rule_peer->present() + QString(")+");
else if (min_match == 0 && max_match == 1)
return u8"(" + this->rule_peer->present() + QString(u8")?");
return "(" + this->rule_peer->present() + QString(")?");
return u8"(" + this->rule_peer->present() + QString(u8"){%1, %2}").arg(min_match).arg(max_match);
return "(" + this->rule_peer->present() + QString("){%1, %2}").arg(min_match).arg(max_match);
}
SyntaxException::SyntaxException(const QString& message) {
@ -217,12 +170,6 @@ QString SyntaxException::message() const {
ExprRule::ExprRule(const QString& rule_name, int expr_mark)
: name_store(rule_name), mark_store(expr_mark) { }
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const IBasicRule> rule) const {
auto ninst = this->make_copy();
ninst->child_store = rule;
return ninst;
}
QString ExprRule::name() const {
return name_store;
}
@ -231,62 +178,7 @@ int ExprRule::typeMark() const {
return this->mark_store;
}
QList<std::shared_ptr<const IBasicRule>> ExprRule::children() const {
return QList<std::shared_ptr<const IBasicRule>>() << this->child_store;
}
#include <ast_novel.h>
QString ExprRule::present() const {
return child_store->present();
}
QList<QString> lib_syntax::ExprRule::invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const {
return QList<QString>() << QString(u8"%1=exprs_%2(%3); //%4")
.arg(out_list_name).arg((uint64_t) this).arg(in_cursor_name).arg(name());
}
QString lib_syntax::ExprRule::implementSegments(const QString& in_cursor_name, QList<QString>& lines) const {
lines << u8"if (" + in_cursor_name + "->mustStop())";
lines << u8" return QList<std::shared_ptr<const MatchCursor>>() << " + in_cursor_name + ";";
lines << u8"auto t_this = " + in_cursor_name + "->currentToken();";
lines << u8"auto w_this = " + in_cursor_name + "->currentWords();";
lines << u8"auto split_begin = std::make_shared<lib_token::ExprBeginToken<ExprType>>(shared_from_this(), t_this);";
lines << u8"auto ncursor = std::make_shared<MatchCursor>(" + in_cursor_name + ");";
lines << u8"ncursor->setCurrent(split_begin, w_this);";
lines << u8"ncursor->enterExprs();";
lines << u8"auto nbranch = this->child_store->parse(ncursor);";
lines << u8"decltype(nbranch) list_ok;";
lines << u8"// 选择完全匹配成功的";
lines << u8"std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok),"
"[](std::shared_ptr<const MatchCursor> ins) { return !ins->exprsErrorCount(); });";
lines << u8"if (!list_ok.size()) {";
lines << u8" // 选择被修正的";
lines << u8" std::copy_if(nbranch.begin(), nbranch.end(), std::back_inserter(list_ok),"
"[](std::shared_ptr<const MatchCursor> ins) { return !ins->mustStop(); });";
lines << u8" // 匹配失败的";
lines << u8" if (!list_ok.size())";
lines << u8" list_ok = nbranch;";
lines << u8"}";
lines << u8"decltype(list_ok) branch_procs;";
lines << u8"std::for_each(list_ok.begin(), list_ok.end(), [&](std::shared_ptr<const MatchCursor> curs) {";
lines << u8" if (curs->mustStop()) {";
lines << u8" branch_procs.append(curs);";
lines << u8" }";
lines << u8" else {";
lines << u8" auto t_end = curs->currentToken();";
lines << u8" auto w_end = curs->currentWords();";
lines << u8" auto ecursor = std::make_shared<MatchCursor>(curs);";
lines << u8" ecursor->quitExprs();";
lines << u8" auto split_end = std::make_shared<lib_token::ExprEndToken<ExprType>>(split_begin, t_end);";
lines << u8" ecursor->setCurrent(split_end, w_end);";
lines << u8" branch_procs.append(ecursor);";
lines << u8" }";
lines << u8"});";
lines << u8"return branch_procs;";
return QString(u8"exprs_%1").arg((uint64_t) this);
}
MatchCursor::MatchCursor(const QString& path) :_file_path(path) { }
MatchCursor::MatchCursor(std::shared_ptr<const MatchCursor> other_ptr)

View File

@ -11,7 +11,7 @@
namespace lib_syntax {
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT SyntaxException {
private:
@ -28,9 +28,9 @@ namespace lib_syntax {
// 基础语法解析接口 ====================================================================================
// 基础语法解析接口 ====================================================================================
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT MatchCursor {
public:
@ -68,64 +68,43 @@ namespace lib_syntax {
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; // 剩余词语
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;
};
/**
* @brief
* @brief
*/
class IBasicRule {
public:
/**
*
*
* \return
*/
virtual QString present() const = 0;
/**
* @brief
* @param cursor
* @return <>
*/
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
/**
* @brief
* @brief
* @return
*/
virtual QList<std::shared_ptr<const IBasicRule>> children() const = 0;
/**
* @brief
* @param in_cursor_name cursor名称
* @param out_list_name list名称
*
*
* out_list_name = element_xxxxxx(in_cursor_name);
*
* @return
*/
virtual QList<QString> invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const = 0;
*
*
* \return
*/
virtual QString present() const = 0;
/**
* @brief
* @param in_cursor_name
* @param lines
*
* @return
*/
virtual QString implementSegments(const QString& in_cursor_name, QList<QString>& lines) const = 0;
* @brief
* @param cursor
* @return <>
*/
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
};
// 组合语法实体解析 =====================================================================================
// 组合语法实体解析 =====================================================================================
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT Any : public IBasicRule, public std::enable_shared_from_this<Any> {
private:
@ -139,13 +118,10 @@ namespace lib_syntax {
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
virtual QString present() const override;
virtual QList<QString> invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const override;
virtual QString implementSegments(const QString& in_cursor_name, QList<QString>& lines) const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT Seqs : public IBasicRule, public std::enable_shared_from_this<Seqs> {
private:
@ -159,13 +135,10 @@ namespace lib_syntax {
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override;
virtual QString present() const override;
virtual QList<QString> invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const override;
virtual QString implementSegments(const QString& in_cursor_name, QList<QString>& lines) const override;
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT Rept : public IBasicRule, public std::enable_shared_from_this<Rept> {
private:
@ -183,7 +156,7 @@ namespace lib_syntax {
};
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT ExprRule : public IBasicRule, public std::enable_shared_from_this<ExprRule> {
public:
@ -191,18 +164,9 @@ namespace lib_syntax {
virtual QString name() const;
virtual int typeMark() const;
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const IBasicRule> rule) const;
// IBasicRule interface
public:
virtual QList<std::shared_ptr<const IBasicRule>> children() const override;
virtual QString present() const override;
virtual QList<QString> invokeSegments(const QString& in_cursor_name, const QString& out_list_name) const override;
virtual QString implementSegments(const QString& in_cursor_name, QList<QString> &lines) const override;
protected:
std::shared_ptr<const IBasicRule> child_store;
virtual std::shared_ptr<ExprRule> make_copy() const = 0;
virtual QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const = 0;
private:
QString name_store;
@ -212,10 +176,11 @@ namespace lib_syntax {
/**
* @brief token匹配
* @brief token匹配
*/
template<typename T, lib_token::TokenProcs<T> XProc = nullptr>
class TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch<T, XProc>> {
template<typename ELEM, lib_token::TokenProcs<ELEM> XProc = nullptr>
requires std::derived_from<ELEM, ast_basic::IExprInstance>
class TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch<ELEM, XProc>> {
private:
std::shared_ptr<const lib_token::ITokenProcess> _define_peers;
@ -225,15 +190,15 @@ namespace lib_syntax {
// IBasicRule interface
public:
virtual QString present() const override {
return QString(u8"%1").arg(this->_define_peers->reviseWords());
return QString("%1").arg(this->_define_peers->reviseWords());
}
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>>")
clone_ins->logExprsError(QString("Syntax[0x00001]语法匹配错误,缺失\"%1\"<file<%2>>")
.arg(this->_define_peers->reviseWords()).arg(current->filePath()));
clone_ins->logExprsError(QString(u8"Syntax[0x00001]输入流提前结束,<%1>").arg(current->filePath()));
clone_ins->logExprsError(QString("Syntax[0x00001]输入流提前结束,<%1>").arg(current->filePath()));
return QList<std::shared_ptr<const MatchCursor>>() << clone_ins;
}
@ -242,7 +207,7 @@ namespace lib_syntax {
auto match_result = _define_peers->analysis(w_this);
if (std::get<0>(match_result)) {
auto chain = std::make_shared<lib_token::ActionToken<T, XProc>>(std::get<0>(match_result), t_this);
auto chain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(std::get<0>(match_result), t_this);
auto remains = w_this->nextWord();
if (std::get<1>(match_result)) {
remains = std::make_shared<lib_words::WordImpl>(std::get<1>(match_result), remains);
@ -254,42 +219,42 @@ namespace lib_syntax {
}
else {
QList<std::shared_ptr<const MatchCursor>> retvals;
// 少一个
// 少一个
{
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("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;
}
// 错一个
// 错一个
{
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("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"%2_%1").arg((uint64_t) error_one.get()).arg(this->_define_peers->reviseWords()),
QString("%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);
auto tkchain = std::make_shared<lib_token::ActionToken<ELEM, 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)) {
auto chain = std::make_shared<lib_token::ActionToken<T, XProc>>(std::get<0>(nx_result), t_this);
auto chain = std::make_shared<lib_token::ActionToken<ELEM, XProc>>(std::get<0>(nx_result), t_this);
auto remains = nx_word->nextWord();
if (std::get<1>(nx_result)) {
remains = std::make_shared<lib_words::WordImpl>(std::get<1>(nx_result), remains);
}
auto clone_ins = std::make_shared<MatchCursor>(current);
clone_ins->logExprsError(QString(u8"Syntax[0x00001]语法匹配错误,请删除\"%1\"<row:%2,col:%3,file<%4>>")
clone_ins->logExprsError(QString("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;
@ -305,12 +270,20 @@ namespace lib_syntax {
};
/**
* .
* @brief .
*/
template<class ExprType> class ElementRule : public ExprRule {
template<class ExprType, int mark> class ElementRule : public ExprRule {
public:
ElementRule(const QString& rule_name, int expr_mark)
:ExprRule(rule_name, expr_mark) { }
ElementRule(const QString& rule_name, std::shared_ptr<const IBasicRule> children)
:ExprRule(rule_name, mark), _children_store(children) { }
virtual QList<std::shared_ptr<const IBasicRule>> children() const {
return QList<std::shared_ptr<const IBasicRule>>() << this->_children_store;
}
virtual QString present() const {
return this->_children_store->present();
}
virtual QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override {
if (cursor->mustStop())
@ -324,16 +297,16 @@ namespace lib_syntax {
ncursor->setCurrent(split_begin, w_this);
ncursor->enterExprs();
auto nbranch = this->child_store->parse(ncursor);
auto nbranch = this->expr_rule_parse(ncursor);
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;
}
@ -354,14 +327,12 @@ namespace lib_syntax {
ecursor->setCurrent(split_end, w_end);
branch_procs.append(ecursor);
}
});
});
return branch_procs;
}
protected:
virtual std::shared_ptr<ExprRule> make_copy() const {
return std::make_shared<ElementRule<ExprType>>(this->name(), this->typeMark());
}
std::shared_ptr<const IBasicRule> _children_store;
};
} // namespace lib_syntax

View File

@ -10,7 +10,7 @@ namespace ast_basic {
namespace lib_token {
/**
* @brief
* @brief
*/
class LIBSYNTAX_EXPORT TokenException {
private:
@ -26,53 +26,53 @@ namespace lib_token {
// token 解析机制抽象接口==========================================
// token 解析机制抽象接口==========================================
/**
* @brief token类型定义
* @brief token类型定义
*/
class ITokenDefine {
public:
/**
* @brief .
* @brief .
*
* \return
*/
virtual int typeMark() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual QString regex() const = 0;
};
/**
* @brief token解析结果
* @brief token解析结果
*/
class IToken : public lib_words::IWordBase, public std::enable_shared_from_this<IToken> {
public:
/**
* @brief token解析机制关联
* @brief token解析机制关联
* @return
*/
virtual std::shared_ptr<const ITokenDefine> defines() const = 0;
};
// 拓展基础接口=========================
// 拓展基础接口=========================
/**
* @brief token解析机制定义
* @brief token解析机制定义
*/
class ITokenProcess : public ITokenDefine {
public:
/**
* @brief
* @brief
* @return
*/
virtual QString reviseWords() const = 0;
/**
* @brief
* @param content
* @return tuple<token/null,remains>
* @brief
* @param content
* @return tuple<token/null,remains>
*/
virtual std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const lib_words::IPrimitiveWord>>
analysis(std::shared_ptr<const lib_words::IPrimitiveWord> content) const = 0;
@ -80,20 +80,20 @@ namespace lib_token {
/**
* @brief Token
* @brief Token
*/
class IActionToken : public lib_token::IToken {
public:
/**
* @brief Token
* @brief Token
* @return
*/
virtual std::shared_ptr<const IActionToken> prevToken() const = 0;
/**
* @brief Token内容到指定exprexpr
* @param expr
* @return
* @brief Token内容到指定exprexpr
* @param expr
* @return
*/
virtual std::shared_ptr<ast_basic::IExprInstance> makeSure(std::shared_ptr<ast_basic::IExprInstance> expr) = 0;
};

View File

@ -14,13 +14,13 @@ auto refers = std::make_shared<ReferMark>(); // @
auto declare = std::make_shared<DeclareSymbo>(); // #
auto split_mark = std::make_shared<Split>(); // &
auto rank_key = std::make_shared<Keywords>(u8"排序", 0xAEu); // 排序
auto story_key = std::make_shared<Keywords>(u8"故事", 0xAAu); // 故事
auto rank_key = std::make_shared<Keywords>("排序", 0xAEu); // 排序
auto story_key = std::make_shared<Keywords>("故事", 0xAAu); // 故事
auto numbers = std::make_shared<Numbers>(); // [0-9]+
auto slice_key = std::make_shared<Keywords>(u8"剧情", 0xAFu); // 剧情
auto point_key = std::make_shared<Keywords>(u8"节点", 0xABu); // 节点
auto volume_key = std::make_shared<Keywords>(u8"分卷", 0xACu); // 分卷
auto article_key = std::make_shared<Keywords>(u8"章节", 0xADu); // 章节
auto slice_key = std::make_shared<Keywords>("剧情", 0xAFu); // 剧情
auto point_key = std::make_shared<Keywords>("节点", 0xABu); // 节点
auto volume_key = std::make_shared<Keywords>("分卷", 0xACu); // 分卷
auto article_key = std::make_shared<Keywords>("章节", 0xADu); // 章节
auto vtext = std::make_shared<VTextSection>(); // ^([^\\{\\}@&]+)
auto name_text = std::make_shared<NameSection>(); // ^([^\\{\\}@&]+)
@ -32,6 +32,8 @@ void apntk(std::shared_ptr<ExprT> expr, std::shared_ptr<const lib_token::IToken>
expr->addToken(t);
}
// MakeRule
#define MK(type) std::make_shared<const type>()
// MatchRule
#define MER(E, XProc, t) std::make_shared<const TokenMatch<E, XProc>>(t)
#define MR(E, t) MER(E, apntk<E>, t)
@ -47,22 +49,42 @@ void apntk(std::shared_ptr<ExprT> expr, std::shared_ptr<const lib_token::IToken>
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int) NovelNode::TextSection).reloadRule(
MultiR(std::make_shared<const Any>(Rules{
MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark)
}))
);
class DeclExpr : public ElementRule<TextSection, (int) NovelNode::TextSection> {
public:
DeclExpr() : ElementRule<TextSection, (int) NovelNode::TextSection>(
"decl_section",
MultiR(std::make_shared<const Any>(Rules{
MR(TextSection, numbers), MR(TextSection, vtext), MR(TextSection, refers), MR(TextSection, split_mark)
}))) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void point_nmset(std::shared_ptr<PointDefines> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto point_decl = ElementRule<PointDefines>(u8"point_define", (int) NovelNode::PointDefines).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(PointDefines, leftb), MR(PointDefines, point_key), MER(PointDefines, point_nmset, name_text) } <<
OptR(decl_expr) <<
MR(PointDefines, rightb)
));
class PointSyntax : public ElementRule<PointDefines, (int) NovelNode::PointDefines> {
public:
PointSyntax() : ElementRule<PointDefines, (int) NovelNode::PointDefines>(
"point_define", std::make_shared<const Seqs>(Rules{
MR(PointDefines, leftb), MR(PointDefines, point_key), MER(PointDefines, point_nmset, name_text) } <<
OptR(MK(DeclExpr)) <<
MR(PointDefines, rightb)
)) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void point_ref_story_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
@ -76,80 +98,166 @@ void point_ref_point_set(std::shared_ptr<PointRefers> inst, std::shared_ptr<cons
inst->addToken(token);
inst->setPointRefer(token->content());
}
auto point_refer = ElementRule<PointRefers>(u8"point_refer", (int) NovelNode::PointRefers).reloadRule(
std::make_shared<const Seqs>(Rules{
class ReferSyntax : public ElementRule<PointRefers, (int) NovelNode::PointRefers> {
public:
ReferSyntax() : ElementRule < PointRefers, (int) NovelNode::PointRefers>(
"point_refer", std::make_shared<const Seqs>(Rules{
MR(PointRefers, leftb), MR(PointRefers, refers), MR(PointRefers, point_key),
MER(PointRefers, point_ref_story_set, name_text),
MR(PointRefers, split_mark),
MER(PointRefers, point_ref_slice_set, name_text),
MR(PointRefers, split_mark),
MER(PointRefers, point_ref_point_set, name_text) } <<
OptR(decl_expr) <<
MR(PointRefers, rightb)
));
OptR(MK(DeclExpr)) <<
MR(PointRefers, rightb))) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void slice_nm_set(std::shared_ptr<FragmentSlice> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto slice_define = ElementRule<FragmentSlice>(u8"slice_define", (int) NovelNode::FragmentSlice).reloadRule(
std::make_shared<const Seqs>(Rules{
MR(FragmentSlice, leftb), MR(FragmentSlice, slice_key), MER(FragmentSlice, slice_nm_set, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ point_decl, point_refer, decl_expr })) <<
MR(FragmentSlice, rightb)
));
class SliceSyntax : public ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice> {
public:
SliceSyntax() : ElementRule<FragmentSlice, (int) NovelNode::FragmentSlice>(
"slice_define", std::make_shared<const Seqs>(Rules{
MR(FragmentSlice, leftb), MR(FragmentSlice, slice_key), MER(FragmentSlice, slice_nm_set, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ MK(PointSyntax), MK(ReferSyntax), MK(DeclExpr) })) <<
MR(FragmentSlice, rightb))) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void story_nmset(std::shared_ptr<StoryDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int) NovelNode::StoryDefine).reloadRule(
std::make_shared<const Seqs>(Rules{
class StorySyntax : public ElementRule<StoryDefine, (int) NovelNode::StoryDefine> {
public:
StorySyntax() : ElementRule<StoryDefine, (int) NovelNode::StoryDefine>(
"story_define",
std::make_shared<const Seqs>(Rules{
MR(StoryDefine, leftb), MR(StoryDefine, story_key), MER(StoryDefine, story_nmset, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ slice_define, decl_expr })) <<
MR(StoryDefine, rightb)
));
// ===================================================================
OptMulR(std::make_shared<const Any>(Rules{ MK(SliceSyntax), MK(DeclExpr) })) <<
MR(StoryDefine, rightb)
)) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void article_nset(std::shared_ptr<ArticleDefine>inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int) NovelNode::ArticleDefine).reloadRule(
std::make_shared<const Seqs>(Rules{
class ArticleSyntax : public ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine> {
public:
ArticleSyntax() : ElementRule<ArticleDefine, (int) NovelNode::ArticleDefine>(
"article_define", std::make_shared<const Seqs>(Rules{
MR(ArticleDefine, leftb), MR(ArticleDefine, article_key), MER(ArticleDefine, article_nset, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ point_refer, decl_expr })) <<
MR(ArticleDefine, rightb)
));
OptMulR(std::make_shared<const Any>(Rules{ MK(ReferSyntax), MK(DeclExpr) })) <<
MR(ArticleDefine, rightb)
)) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void volume_nset(std::shared_ptr<VolumeDefine> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setName(token->content());
}
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int) NovelNode::VolumeDefine).reloadRule(
std::make_shared<const Seqs>(Rules{
class VolumeSyntax : public ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine> {
public:
VolumeSyntax() : ElementRule<VolumeDefine, (int) NovelNode::VolumeDefine>(
"volume_define", std::make_shared<const Seqs>(Rules{
MR(VolumeDefine, leftb), MR(VolumeDefine, volume_key), MER(VolumeDefine, volume_nset, name_text) } <<
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
MR(VolumeDefine, rightb)
));
OptMulR(std::make_shared<const Any>(Rules{ MK(DeclExpr), MK(ArticleSyntax) })) <<
MR(VolumeDefine, rightb)
)) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
void rank_set(std::shared_ptr<RankDeclare> inst, std::shared_ptr<const lib_token::IToken> token) {
inst->addToken(token);
inst->setRank(token->content().toInt());
}
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int) NovelNode::RankDeclaration).reloadRule(
std::make_shared<const Seqs>(Rules{
class RankSyntax : public ElementRule<RankDeclare, (int) NovelNode::RankDeclaration> {
public:
RankSyntax() : ElementRule<RankDeclare, (int) NovelNode::RankDeclaration>(
"rank_define", std::make_shared<const Seqs>(Rules{
MR(RankDeclare, declare), MR(RankDeclare, rank_key), MER(RankDeclare, rank_set, numbers) }
));
)) { }
auto document_define = ElementRule<Document>(u8"decls-doc", (int) NovelNode::Document).reloadRule(
std::make_shared<const Seqs>(
Rules{
std::make_shared<const Rept>(rank_define, 0, 1),
MultiR(std::make_shared<const Any>(Rules{story_define, volume_decl}))
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
));
};
class DocumentSyntax : public ElementRule<Document, (int) NovelNode::Document> {
public:
DocumentSyntax() : ElementRule<Document, (int) NovelNode::Document>(
"decls-doc", std::make_shared<const Seqs>(Rules{
OptR(MK(RankSyntax)), MultiR(std::make_shared<const Any>(Rules{ MK(StorySyntax), MK(VolumeSyntax) }))
})) { }
// 通过 ElementRule 继承
QList<std::shared_ptr<const MatchCursor>> expr_rule_parse(std::shared_ptr<const MatchCursor> cursor) const override {
return _children_store->parse(cursor);
}
};
class MM : public lib_syntax::IBasicRule {
// 通过 IBasicRule 继承
QList<std::shared_ptr<const IBasicRule>> children() const override {
return QList<std::shared_ptr<const IBasicRule>>();
}
QString present() const override {
return QString();
}
QList<std::shared_ptr<const MatchCursor>> parse(std::shared_ptr<const MatchCursor> cursor) const override {
return QList<std::shared_ptr<const MatchCursor>>();
}
};
#include "syntax_templets.h"
std::shared_ptr<const ExprRule> NovalSyntax::getSyntaxTree() {
lib_composit::TypeList<MM, MM> vv;
return std::make_shared<DocumentSyntax>();
}
std::shared_ptr<const ExprRule> NovalSyntax::getSyntaxTree() { return document_define; }
//std::shared_ptr<const ast_gen::SyntaxElement> NovalSyntax::tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> children)
//{
// build_objecttree(root, children);
@ -176,7 +284,7 @@ std::shared_ptr<const ExprRule> NovalSyntax::getSyntaxTree() { return document_d
// if (!child->isAnonymous()) {
// auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child);
// if (check_result)
// throw new SyntaxException(QString(u8"SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type%2>(%3,%4)")
// throw new SyntaxException(QString("SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type%2>(%3,%4)")
// .arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path()));
// }
//

View File

@ -10,7 +10,7 @@ namespace example_novel {
class LIBSYNTAX_EXPORT NovalSyntax {
public:
/**
* @brief novel语法解析树
* @brief novel语法解析树
* @return
*/
static std::shared_ptr<const lib_syntax::ExprRule> getSyntaxTree();

View File

@ -0,0 +1,51 @@
#pragma once
#include "libsyntax.h"
#include <concepts>
namespace lib_composit {
template<typename... TYPES> class TypeList;
template<> class TypeList<> {
public:
static QList<std::shared_ptr<const lib_syntax::IBasicRule>> getRules() {
return QList<std::shared_ptr<const lib_syntax::IBasicRule>>();
}
};
template<typename T, typename... Rets>
requires std::derived_from<T, lib_syntax::IBasicRule>
class TypeList<T, Rets...> : public TypeList<Rets...> {
public:
static QList<std::shared_ptr<const lib_syntax::IBasicRule>> getRules() {
auto list = TypeList<Rets...>::getRules();
list.prepend(std::make_shared<const T>());
return list;
}
};
template<typename... TYPES>
class AnyR : public lib_syntax::Any, public TypeList<TYPES...> {
public:
AnyR() : Any(TypeList<TYPES...>::getRules()){ }
};
template<typename... TYPES>
class SeqsR : public lib_syntax::Seqs, public TypeList<TYPES...> {
public:
SeqsR() : Seqs(TypeList<TYPES...>::getRules()){ }
};
template<typename TYPE, int min, int max>
class ReptR : public lib_syntax::Rept {
public:
ReptR() : Rept(std::make_shared<TYPE>(), min, max){ }
};
template<typename E, typename T, lib_token::TokenProcs<E> xproc = nullptr>
requires std::derived_from<E, ast_basic::IExprInstance> && std::derived_from<T, lib_token::ITokenProcess>
class TokenR : public lib_syntax::TokenMatch<E, xproc> {
public:
TokenR() : TokenMatch<E, xproc>(std::make_shared<T>()){ }
};
}

View File

@ -9,9 +9,9 @@ namespace lib_token {
using TokenProcs = void(*)(std::shared_ptr<ExprT> expr, std::shared_ptr<const lib_token::IToken> t);
// 基础token实现 ===================================================
// 基础token实现 ===================================================
/**
* @brief
* @brief
*/
class TokenContent : public IToken {
private:
@ -21,13 +21,13 @@ namespace lib_token {
public:
/**
* @brief
* @param r
* @param c
* @param pos
* @param t
* @brief
* @param r
* @param c
* @param pos
* @param t
* @param p path
* @param paramType
* @param paramType
*/
TokenContent(int r, int c, uint64_t pos, const QString& t, const QString& p, std::shared_ptr<const ITokenDefine> paramType);
@ -46,7 +46,7 @@ namespace lib_token {
/**
* @brief Token
* @brief Token
*/
template<typename ExprT, TokenProcs<ExprT> proc>
class ActionToken : public IActionToken {
@ -64,7 +64,7 @@ namespace lib_token {
return expr;
}
// 通过 IActionToken 继承
// 通过 IActionToken 继承
QString file() const override {
return _bind_content->file();
}
@ -89,7 +89,7 @@ namespace lib_token {
};
/**
* @brief token
* @brief token
*/
template<typename NExpr> class ExprBeginToken : public IActionToken {
private:
@ -107,25 +107,25 @@ namespace lib_token {
return this->_parent_expr;
}
// 通过 IActionToken 继承
// 通过 IActionToken 继承
QString file() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken");
throw new lib_token::TokenException("不应该直接访问ExprBeginToken");
}
uint64_t position() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken");
throw new lib_token::TokenException("不应该直接访问ExprBeginToken");
}
QString content() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken");
throw new lib_token::TokenException("不应该直接访问ExprBeginToken");
}
int row() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken");
throw new lib_token::TokenException("不应该直接访问ExprBeginToken");
}
int column() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprBeginToken");
throw new lib_token::TokenException("不应该直接访问ExprBeginToken");
}
std::shared_ptr<const ITokenDefine> defines() const override {
@ -145,7 +145,7 @@ namespace lib_token {
};
/**
* @brief token
* @brief token
*/
template<typename ExprT> class ExprEndToken : public IActionToken {
private:
@ -156,21 +156,21 @@ namespace lib_token {
ExprEndToken(std::shared_ptr<ExprBeginToken<ExprT>> start, std::shared_ptr<const IActionToken> prev)
: _prev_token(prev), _self_start(start){ }
// 通过 IActionToken 继承
// 通过 IActionToken 继承
QString file() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
throw new lib_token::TokenException("不应该直接访问ExprEndToken");
}
uint64_t position() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
throw new lib_token::TokenException("不应该直接访问ExprEndToken");
}
QString content() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
throw new lib_token::TokenException("不应该直接访问ExprEndToken");
}
int row() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
throw new lib_token::TokenException("不应该直接访问ExprEndToken");
}
int column() const override {
throw new lib_token::TokenException(u8"不应该直接访问ExprEndToken");
throw new lib_token::TokenException("不应该直接访问ExprEndToken");
}
std::shared_ptr<const ITokenDefine> defines() const override {
return nullptr;

View File

@ -7,14 +7,14 @@ using namespace lib_token;
using namespace lib_words;
QString LeftBracket::reviseWords() const { return u8"{"; }
QString LeftBracket::reviseWords() const { return "{"; }
int LeftBracket::typeMark() const
{
return 0x01000000;
}
QString LeftBracket::regex() const { return u8"{"; }
QString LeftBracket::regex() const { return "{"; }
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
LeftBracket::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
@ -34,23 +34,23 @@ 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 "}"; }
int RightBracket::typeMark() const
{
return 0x02000000;
}
QString RightBracket::regex() const { return u8"}"; }
QString RightBracket::regex() const { return "}"; }
QString ReferMark::reviseWords() const { return u8"@"; }
QString ReferMark::reviseWords() const { return "@"; }
int ReferMark::typeMark() const
{
return 0x03000000;
}
QString ReferMark::regex() const { return u8"@"; }
QString ReferMark::regex() const { return "@"; }
Keywords::Keywords(const QString& val, uint type_code) : means_store(val), type_code(type_code) {}
@ -74,14 +74,14 @@ Keywords::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
return std::make_tuple(token_inst, nullptr);
}
QString Numbers::reviseWords() const { return u8"ÕýÕûÊý"; }
QString Numbers::reviseWords() const { return "正整数"; }
int Numbers::typeMark() const
{
return 0x07000000;
}
QString Numbers::regex() const { return u8"^([0-9]+)$"; }
QString Numbers::regex() const { return "^([0-9]+)$"; }
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
Numbers::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
@ -95,14 +95,14 @@ Numbers::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
return std::make_tuple(tinst, nullptr);
}
QString VTextSection::reviseWords() const { return u8"Îı¾"; }
QString VTextSection::reviseWords() const { return "文本"; }
int VTextSection::typeMark() const
{
return 0x09000000;
}
QString VTextSection::regex() const { return u8"^([^\\{\\}@&]+)"; }
QString VTextSection::regex() const { return "^([^\\{\\}@&]+)"; }
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
VTextSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
@ -125,23 +125,23 @@ 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 "&"; }
int Split::typeMark() const
{
return 0x05000000;
}
QString Split::regex() const { return u8"&"; }
QString Split::regex() const { return "&"; }
QString NameSection::reviseWords() const { return u8"Ãû³Æ"; }
QString NameSection::reviseWords() const { return "名称"; }
int NameSection::typeMark() const
{
return 0x08000000;
}
QString NameSection::regex() const { return u8"^([^\\{\\}@&]+)"; }
QString NameSection::regex() const { return "^([^\\{\\}@&]+)"; }
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord> >
NameSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const
@ -166,7 +166,7 @@ NameSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const
}
QString DeclareSymbo::reviseWords() const {
return u8"#";
return "#";
}
int DeclareSymbo::typeMark() const
@ -176,7 +176,7 @@ int DeclareSymbo::typeMark() const
QString DeclareSymbo::regex() const
{
return u8"#";
return "#";
}
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
@ -190,7 +190,7 @@ DeclareSymbo::analysis(std::shared_ptr<const IPrimitiveWord> content) const
auto remains = content->content().mid(regex().size());
auto tinst = std::make_shared<TokenContent>(content->row(), content->column(), content->position(),
u8"#", content->file(), shared_from_this());
"#", content->file(), shared_from_this());
if (remains.length()) {
auto t_remains = std::make_shared<WordContent>(content->row(), content->column()+1,
content->position() + regex().length(), remains, content->file());

View File

@ -88,7 +88,7 @@ namespace example_novel {
class LIBSYNTAX_EXPORT DeclareSymbo : public lib_token::ITokenProcess, public std::enable_shared_from_this<DeclareSymbo> {
public:
// 通过 TokenDefine 继承
// 通过 TokenDefine 继承
virtual QString reviseWords() const override;
virtual int typeMark() const override;
virtual QString regex() const override;

View File

@ -1,7 +0,0 @@
#include "libtoken.h"
#include <QFile>
#include <QTextStream>
using namespace lib_token;

View File

@ -1,11 +0,0 @@
#pragma once
#include "libtoken_global.h"
#include <QList>
#include <QString>
#include <memory>
namespace lib_token {
} // namespace lib_token

View File

@ -63,6 +63,12 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>

View File

@ -33,7 +33,7 @@ QList<std::shared_ptr<const IWordBase>> lib_words::WordReader::extract_primary(Q
#include <QRegExp>
QList<std::shared_ptr<const IWordBase>> WordReader::parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const {
QRegExp split_char(u8"\\s");
QRegExp split_char("\\s");
auto words = line_text.split(split_char, QString::SplitBehavior::SkipEmptyParts);
QList<std::shared_ptr<const IWordBase>> primary_words;

View File

@ -8,32 +8,32 @@
namespace lib_words {
/**
* @brief
* @brief
*/
class IWordBase {
public:
/**
* @brief
* @brief
* @return
*/
virtual QString file() const = 0;
/**
* @brief
* @return
* @brief
* @return
*/
virtual uint64_t position() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual QString content() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual int row() const = 0;
/**
* @brief
* @brief
* @return
*/
virtual int column() const = 0;
@ -42,7 +42,7 @@ namespace lib_words {
class IPrimitiveWord : public IWordBase {
public:
/**
* @brief .
* @brief .
*
* \return
*/
@ -50,7 +50,7 @@ namespace lib_words {
};
/**
* @brief
* @brief
*/
class LIBWORDS_EXPORT WordContent : public IPrimitiveWord {
private:
@ -79,7 +79,7 @@ namespace lib_words {
public:
WordImpl(std::shared_ptr<const IWordBase> content, std::shared_ptr<const IPrimitiveWord> next);
// 通过 IWordBase 继承
// 通过 IWordBase 继承
QString file() const override;
virtual uint64_t position() const override;
QString content() const override;
@ -90,7 +90,7 @@ namespace lib_words {
/**
* @brief
* @brief
*/
class LIBWORDS_EXPORT WordReader {
private:
@ -99,7 +99,7 @@ namespace lib_words {
public:
/**
* @brief
* @brief
*/
std::shared_ptr<const IPrimitiveWord> wordsFrom(QTextStream& tin, const QString& path) const;
};
@ -107,7 +107,7 @@ namespace lib_words {
/**
* @brief
* @brief
*/
class LIBWORDS_EXPORT WordsException {
private: