渐进模板化
This commit is contained in:
parent
735a9fb210
commit
805169bad1
|
@ -272,7 +272,9 @@ namespace lib_syntax {
|
|||
/**
|
||||
* @brief 基础模板化语法元素解析规则.
|
||||
*/
|
||||
template<class ExprType, int mark> class ElementRule : public ExprRule {
|
||||
template<class ExprType, int mark>
|
||||
requires std::derived_from<ExprType, ast_basic::IExprInstance>
|
||||
class ElementRule : public ExprRule {
|
||||
public:
|
||||
ElementRule(const QString& rule_name, std::shared_ptr<const IBasicRule> children)
|
||||
:ExprRule(rule_name, mark), _children_store(children) { }
|
||||
|
|
|
@ -8,20 +8,20 @@ using namespace lib_token;
|
|||
using namespace ast_basic;
|
||||
|
||||
// token-avaliable ==========================================================================
|
||||
auto leftb = std::make_shared<LeftBracket>(); // {
|
||||
auto rightb = std::make_shared<RightBracket>(); // }
|
||||
auto refers = std::make_shared<ReferMark>(); // @
|
||||
auto leftb = std::make_shared<LBracket>(); // {
|
||||
auto rightb = std::make_shared<RBracket>(); // }
|
||||
auto refers = std::make_shared<ReferMk>(); // @
|
||||
auto declare = std::make_shared<DeclareSymbo>(); // #
|
||||
auto split_mark = std::make_shared<Split>(); // &
|
||||
auto split_mark = std::make_shared<SplitMk>(); // &
|
||||
|
||||
auto rank_key = std::make_shared<Keywords>("排序", 0xAEu); // 排序
|
||||
auto story_key = std::make_shared<Keywords>("故事", 0xAAu); // 故事
|
||||
auto rank_key = std::make_shared<RankWord>(); // 排序
|
||||
auto story_key = std::make_shared<StoryWord>(); // 故事
|
||||
auto numbers = std::make_shared<Numbers>(); // [0-9]+
|
||||
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 slice_key = std::make_shared<SliceWord>(); // 剧情
|
||||
auto point_key = std::make_shared<PointWord>(); // 节点
|
||||
auto volume_key = std::make_shared<VolumeWord>(); // 分卷
|
||||
auto article_key = std::make_shared<ArticleWord>(); // 章节
|
||||
auto vtext = std::make_shared<NormalText>(); // ^([^\\{\\}@&]+)
|
||||
auto name_text = std::make_shared<NameSection>(); // ^([^\\{\\}@&]+)
|
||||
|
||||
|
||||
|
@ -297,3 +297,40 @@ std::shared_ptr<const ExprRule> NovalSyntax::getSyntaxTree() {
|
|||
// node_register(child, next_child_items);
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
|
||||
#include "syntax_templets.h"
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
using namespace lib_composit;
|
||||
|
||||
template<typename E, typename T> class TokenRn : public TokenR<E, T, apntk> { };
|
||||
template<typename R> class MultiRx : public ReptR<R, 1, INT_MAX> { };
|
||||
|
||||
void rules() {
|
||||
MultiRx<AnyR<
|
||||
TokenRn<TextSection, Numbers>,
|
||||
TokenRn<TextSection, NormalText>,
|
||||
TokenRn<TextSection, ReferMk>,
|
||||
TokenRn<TextSection, SplitMk>
|
||||
>> dcc;
|
||||
|
||||
AnyR<TokenRn<TextSection, Numbers>, TokenRn<TextSection, NormalText>, TokenRn<TextSection, ReferMk>, TokenRn<TextSection, SplitMk>> mmm;
|
||||
ReptR<decltype(mmm), 1, INT_MAX> decl;
|
||||
}
|
||||
|
|
|
@ -91,7 +91,9 @@ namespace lib_token {
|
|||
/**
|
||||
* @brief 表达式起始token
|
||||
*/
|
||||
template<typename NExpr> class ExprBeginToken : public IActionToken {
|
||||
template<typename NExpr>
|
||||
requires std::derived_from<NExpr, ast_basic::IExprInstance>
|
||||
class ExprBeginToken : public IActionToken {
|
||||
private:
|
||||
std::shared_ptr<const IActionToken> _prev_token = nullptr;
|
||||
std::shared_ptr<const lib_syntax::ExprRule> _self_rule = nullptr;
|
||||
|
@ -103,7 +105,7 @@ namespace lib_token {
|
|||
ExprBeginToken(std::shared_ptr<const lib_syntax::ExprRule> rule, std::shared_ptr<const IActionToken> prev)
|
||||
:_prev_token(prev), _self_rule(rule) { }
|
||||
|
||||
std::shared_ptr<ast_basic::IExprInstance> parentExpr(){
|
||||
std::shared_ptr<ast_basic::IExprInstance> parentExpr() {
|
||||
return this->_parent_expr;
|
||||
}
|
||||
|
||||
|
@ -147,14 +149,16 @@ namespace lib_token {
|
|||
/**
|
||||
* @brief 表达式终止token
|
||||
*/
|
||||
template<typename ExprT> class ExprEndToken : public IActionToken {
|
||||
template<typename ExprT>
|
||||
requires std::derived_from<ExprT, ast_basic::IExprInstance>
|
||||
class ExprEndToken : public IActionToken {
|
||||
private:
|
||||
std::shared_ptr<const IActionToken> _prev_token = nullptr;
|
||||
std::shared_ptr<ExprBeginToken<ExprT>> _self_start = nullptr;
|
||||
|
||||
public:
|
||||
ExprEndToken(std::shared_ptr<ExprBeginToken<ExprT>> start, std::shared_ptr<const IActionToken> prev)
|
||||
: _prev_token(prev), _self_start(start){ }
|
||||
: _prev_token(prev), _self_start(start) { }
|
||||
|
||||
// 通过 IActionToken 继承
|
||||
QString file() const override {
|
||||
|
|
|
@ -7,17 +7,17 @@ using namespace lib_token;
|
|||
using namespace lib_words;
|
||||
|
||||
|
||||
QString LeftBracket::reviseWords() const { return "{"; }
|
||||
QString LBracket::reviseWords() const { return "{"; }
|
||||
|
||||
int LeftBracket::typeMark() const
|
||||
int LBracket::typeMark() const
|
||||
{
|
||||
return 0x01000000;
|
||||
}
|
||||
|
||||
QString LeftBracket::regex() const { return "{"; }
|
||||
QString LBracket::regex() const { return "{"; }
|
||||
|
||||
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
|
||||
LeftBracket::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
||||
LBracket::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
||||
auto text = content->content();
|
||||
if (!text.startsWith(regex()))
|
||||
return std::make_tuple(nullptr, content);
|
||||
|
@ -34,37 +34,37 @@ LeftBracket::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
|||
return std::make_tuple(token_inst, nullptr);
|
||||
}
|
||||
|
||||
QString RightBracket::reviseWords() const { return "}"; }
|
||||
QString RBracket::reviseWords() const { return "}"; }
|
||||
|
||||
int RightBracket::typeMark() const
|
||||
int RBracket::typeMark() const
|
||||
{
|
||||
return 0x02000000;
|
||||
}
|
||||
|
||||
QString RightBracket::regex() const { return "}"; }
|
||||
QString RBracket::regex() const { return "}"; }
|
||||
|
||||
QString ReferMark::reviseWords() const { return "@"; }
|
||||
QString ReferMk::reviseWords() const { return "@"; }
|
||||
|
||||
int ReferMark::typeMark() const
|
||||
int ReferMk::typeMark() const
|
||||
{
|
||||
return 0x03000000;
|
||||
}
|
||||
|
||||
QString ReferMark::regex() const { return "@"; }
|
||||
QString ReferMk::regex() const { return "@"; }
|
||||
|
||||
Keywords::Keywords(const QString& val, uint type_code) : means_store(val), type_code(type_code) {}
|
||||
_keywords::_keywords(const QString& val, uint type_code) : means_store(val), type_code(type_code) {}
|
||||
|
||||
QString Keywords::reviseWords() const { return means_store; }
|
||||
QString _keywords::reviseWords() const { return means_store; }
|
||||
|
||||
int Keywords::typeMark() const
|
||||
int _keywords::typeMark() const
|
||||
{
|
||||
return 0x06000000 | (0x00ffffff & type_code);
|
||||
}
|
||||
|
||||
QString Keywords::regex() const { return means_store; }
|
||||
QString _keywords::regex() const { return means_store; }
|
||||
|
||||
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
|
||||
Keywords::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
||||
_keywords::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
||||
if (content->content() != regex()) {
|
||||
return std::make_tuple(nullptr, content);
|
||||
}
|
||||
|
@ -95,17 +95,17 @@ Numbers::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
|||
return std::make_tuple(tinst, nullptr);
|
||||
}
|
||||
|
||||
QString VTextSection::reviseWords() const { return "文本"; }
|
||||
QString NormalText::reviseWords() const { return "文本"; }
|
||||
|
||||
int VTextSection::typeMark() const
|
||||
int NormalText::typeMark() const
|
||||
{
|
||||
return 0x09000000;
|
||||
}
|
||||
|
||||
QString VTextSection::regex() const { return "^([^\\{\\}@&]+)"; }
|
||||
QString NormalText::regex() const { return "^([^\\{\\}@&]+)"; }
|
||||
|
||||
std::tuple<std::shared_ptr<const IToken>, std::shared_ptr<const IPrimitiveWord>>
|
||||
VTextSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
||||
NormalText::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
||||
auto text = content->content();
|
||||
QRegExp regx(regex());
|
||||
if (regx.indexIn(text) == -1) {
|
||||
|
@ -125,14 +125,14 @@ VTextSection::analysis(std::shared_ptr<const IPrimitiveWord> content) const {
|
|||
return std::make_tuple(tinst, nullptr);
|
||||
}
|
||||
|
||||
QString Split::reviseWords() const { return "&"; }
|
||||
QString SplitMk::reviseWords() const { return "&"; }
|
||||
|
||||
int Split::typeMark() const
|
||||
int SplitMk::typeMark() const
|
||||
{
|
||||
return 0x05000000;
|
||||
}
|
||||
|
||||
QString Split::regex() const { return "&"; }
|
||||
QString SplitMk::regex() const { return "&"; }
|
||||
|
||||
QString NameSection::reviseWords() const { return "名称"; }
|
||||
|
||||
|
@ -198,3 +198,15 @@ DeclareSymbo::analysis(std::shared_ptr<const IPrimitiveWord> content) const
|
|||
}
|
||||
return std::make_tuple(tinst, nullptr);
|
||||
}
|
||||
|
||||
inline example_novel::RankWord::RankWord() : _keywords("排序", 0xAEu) { }
|
||||
|
||||
inline example_novel::StoryWord::StoryWord() : _keywords("故事", 0xAAu) { }
|
||||
|
||||
inline example_novel::SliceWord::SliceWord() : _keywords("剧情", 0xAFu) { }
|
||||
|
||||
inline example_novel::PointWord::PointWord() : _keywords("节点", 0xABu) { }
|
||||
|
||||
inline example_novel::VolumeWord::VolumeWord() : _keywords("分卷", 0xACu) { }
|
||||
|
||||
inline example_novel::ArticleWord::ArticleWord() : _keywords("章节", 0xADu) { }
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
|
||||
namespace example_novel {
|
||||
class LIBSYNTAX_EXPORT LeftBracket : public lib_token::ITokenProcess, public std::enable_shared_from_this<LeftBracket> {
|
||||
class LIBSYNTAX_EXPORT LBracket : public lib_token::ITokenProcess, public std::enable_shared_from_this<LBracket> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
|
@ -17,34 +17,34 @@ namespace example_novel {
|
|||
analysis(std::shared_ptr<const lib_words::IPrimitiveWord> content) const override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT RightBracket : public LeftBracket {
|
||||
class LIBSYNTAX_EXPORT RBracket : public LBracket {
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT ReferMark : public LeftBracket {
|
||||
class LIBSYNTAX_EXPORT ReferMk : public LBracket {
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT Split : public LeftBracket {
|
||||
class LIBSYNTAX_EXPORT SplitMk : public LBracket {
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT Keywords : public lib_token::ITokenProcess, public std::enable_shared_from_this<Keywords> {
|
||||
class LIBSYNTAX_EXPORT _keywords : public lib_token::ITokenProcess, public std::enable_shared_from_this<_keywords> {
|
||||
private:
|
||||
QString means_store;
|
||||
int type_code;
|
||||
|
||||
public:
|
||||
Keywords(const QString& val, uint type_code);
|
||||
_keywords(const QString& val, uint type_code);
|
||||
|
||||
// TokenDefine interface
|
||||
public:
|
||||
|
@ -55,6 +55,32 @@ namespace example_novel {
|
|||
analysis(std::shared_ptr<const lib_words::IPrimitiveWord> content) const override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT RankWord : public _keywords {
|
||||
public:
|
||||
RankWord();
|
||||
};
|
||||
class LIBSYNTAX_EXPORT StoryWord : public _keywords {
|
||||
public:
|
||||
StoryWord();
|
||||
};
|
||||
class LIBSYNTAX_EXPORT SliceWord : public _keywords {
|
||||
public:
|
||||
SliceWord();
|
||||
};
|
||||
class LIBSYNTAX_EXPORT PointWord : public _keywords {
|
||||
public:
|
||||
PointWord();
|
||||
};
|
||||
class LIBSYNTAX_EXPORT VolumeWord : public _keywords {
|
||||
public:
|
||||
VolumeWord();
|
||||
};
|
||||
class LIBSYNTAX_EXPORT ArticleWord : public _keywords {
|
||||
public:
|
||||
ArticleWord();
|
||||
};
|
||||
|
||||
|
||||
class LIBSYNTAX_EXPORT Numbers : public lib_token::ITokenProcess, public std::enable_shared_from_this<Numbers> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
|
@ -76,7 +102,7 @@ namespace example_novel {
|
|||
analysis(std::shared_ptr<const lib_words::IPrimitiveWord> content) const override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT VTextSection : public lib_token::ITokenProcess, public std::enable_shared_from_this<VTextSection> {
|
||||
class LIBSYNTAX_EXPORT NormalText : public lib_token::ITokenProcess, public std::enable_shared_from_this<NormalText> {
|
||||
// TokenDefine interface
|
||||
public:
|
||||
virtual QString reviseWords() const override;
|
||||
|
|
Loading…
Reference in New Issue