Compare commits
4 Commits
864236562d
...
7a07e0b266
| Author | SHA1 | Date |
|---|---|---|
|
|
7a07e0b266 | |
|
|
cbc2b2ccb6 | |
|
|
00d800bd7f | |
|
|
0c4f0d0a15 |
|
|
@ -70,6 +70,14 @@ int main(int argc, char* argv[]) {
|
|||
try {
|
||||
auto parser = std::make_shared<NovelParser>();
|
||||
access_ptr = parser->parse(files);
|
||||
|
||||
auto errors_list = std::dynamic_pointer_cast<const ast_gen::GlobalElement>(access_ptr->element())->errors();
|
||||
if (errors_list.size()) {
|
||||
for (auto& err : errors_list) {
|
||||
qDebug().noquote() << err;
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
catch (lib_syntax::SyntaxException* e) {
|
||||
qDebug().noquote() << e->message();
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ QString NovelParser::version() const
|
|||
}
|
||||
|
||||
std::shared_ptr<const ast_gen::ElementAccess> NovelParser::parse(const QFileInfoList source_list) const {
|
||||
QList<std::shared_ptr<const ast_basic::Expression>> forst_root;
|
||||
QList<std::shared_ptr<const ast_basic::ExprInst>> forst_root;
|
||||
auto word_reader = std::make_shared<lib_token::WordReader>();
|
||||
auto context = std::make_shared<ast_gen::GlobalElement>(u8"小说");
|
||||
|
||||
|
|
@ -38,9 +38,10 @@ std::shared_ptr<const ast_gen::ElementAccess> NovelParser::parse(const QFileInfo
|
|||
for (auto& file : source_list) {
|
||||
context->setCurrentFile(file.canonicalFilePath());
|
||||
auto words = word_reader->wordsFrom(file.canonicalFilePath());
|
||||
auto exprs_result = this->syntax_defines->parse(context, words);
|
||||
forst_root.append(std::get<0>(exprs_result));
|
||||
this->syntax_defines->parse(context, words);
|
||||
}
|
||||
|
||||
forst_root = context->getDocs();
|
||||
auto current_stamp = QTime::currentTime();
|
||||
qDebug().noquote() << QString(u8"%词法解析+语法解析消耗时间:%1 ms。").arg(time_stamp.msecsTo(current_stamp));
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace example_novel {
|
|||
class NovelParser
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<const lib_syntax::ExpressionRule> syntax_defines;
|
||||
std::shared_ptr<const lib_syntax::ExprRule> syntax_defines;
|
||||
QList<std::shared_ptr<const lib_parse::CheckProvider>> checker_list;
|
||||
std::shared_ptr<const lib_parse::Analyzer> analyzer_ref;
|
||||
|
||||
|
|
|
|||
|
|
@ -48,4 +48,5 @@ bool VisitorControl::visitWith(std::shared_ptr<const ElementAccess> syntax_elm,
|
|||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ using namespace ast_basic;
|
|||
using namespace lib_token;
|
||||
using namespace lib_syntax;
|
||||
|
||||
ExpressionElement::ExpressionElement(std::shared_ptr<const ExpressionRule> bind) : _expr_rule(bind) {}
|
||||
ExpressionElement::ExpressionElement(std::shared_ptr<const ExprRule> bind) : _expr_rule(bind) {}
|
||||
|
||||
std::shared_ptr<const ExpressionRule> ExpressionElement::definedRule() const {
|
||||
std::shared_ptr<const ExprRule> ExpressionElement::definedRule() const {
|
||||
return _expr_rule;
|
||||
}
|
||||
|
||||
|
|
@ -26,11 +26,11 @@ void ExpressionElement::addToken(std::shared_ptr<const IToken> token_inst) {
|
|||
this->tokens_bind.append(token_inst);
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const Expression>> ExpressionElement::children() const {
|
||||
QList<std::shared_ptr<const ExprInst>> ExpressionElement::children() const {
|
||||
return this->children_store;
|
||||
}
|
||||
|
||||
void ExpressionElement::addChild(std::shared_ptr<const Expression> inst) {
|
||||
void ExpressionElement::addChild(std::shared_ptr<const ExprInst> inst) {
|
||||
this->children_store.append(inst);
|
||||
}
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ void ExpressionContext::setCurrentFile(const QString& path) { this->current_file
|
|||
|
||||
QString ExpressionContext::currentFile() const { return this->current_file_path; }
|
||||
|
||||
std::shared_ptr<Expression> ExpressionContext::currentInst() const
|
||||
std::shared_ptr<ExprInst> ExpressionContext::currentInst() const
|
||||
{
|
||||
if (expression_stack.size())
|
||||
return expression_stack.last();
|
||||
|
|
@ -52,13 +52,13 @@ std::shared_ptr<Expression> ExpressionContext::currentInst() const
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
void ExpressionContext::pushInst(std::shared_ptr<Expression> current_inst)
|
||||
void ExpressionContext::pushInst(std::shared_ptr<ExprInst> current_inst)
|
||||
{
|
||||
if (!expression_stack.size() || expression_stack.last() != current_inst)
|
||||
expression_stack.append(current_inst);
|
||||
}
|
||||
|
||||
std::shared_ptr<Expression> ExpressionContext::popInst()
|
||||
std::shared_ptr<ExprInst> ExpressionContext::popInst()
|
||||
{
|
||||
auto lastx = expression_stack.takeLast();
|
||||
return lastx;
|
||||
|
|
@ -80,26 +80,35 @@ std::shared_ptr<const BaseRule> ExpressionContext::popExpressionRule()
|
|||
return rule_stack.takeLast();
|
||||
}
|
||||
|
||||
void ExpressionContext::appendParseErrors(int start, const QString& e) {
|
||||
this->errors_storage.append(std::make_tuple(start, e));
|
||||
void ExpressionContext::appendParseErrors(const QString& file_path, int start, const QString& e) {
|
||||
this->errors_storage.append(std::make_tuple(file_path, start, e));
|
||||
}
|
||||
|
||||
QStringList ExpressionContext::errors() const {
|
||||
QStringList values;
|
||||
for (auto& tp : this->errors_storage)
|
||||
values.append(std::get<1>(tp));
|
||||
values.append(QString(u8"%2\n\tÎļþ£¨%1£©").arg(std::get<0>(tp)).arg(std::get<2>(tp)));
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
void ExpressionContext::clearErrors(int start) {
|
||||
void ExpressionContext::clearErrors(const QString &file_path, int start) {
|
||||
for (int idx = 0; idx < this->errors_storage.size(); ++idx) {
|
||||
auto &tp = errors_storage[idx];
|
||||
if(std::get<0>(tp) >= start)
|
||||
if(std::get<0>(tp) == file_path && std::get<1>(tp) >= start)
|
||||
errors_storage.removeAt(idx--);
|
||||
}
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> ExpressionContext::currentExpressionRuleStack() const {
|
||||
return rule_stack;
|
||||
}
|
||||
|
||||
|
||||
void ExpressionContext::appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) {
|
||||
this->document_store.append(inst);
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const ast_basic::ExprInst>> ExpressionContext::getDocs() const {
|
||||
return this->document_store;
|
||||
}
|
||||
|
|
@ -10,15 +10,15 @@ namespace ast_basic {
|
|||
/**
|
||||
* @brief 抽象语法树集合节点/表达式节点
|
||||
*/
|
||||
class Expression {
|
||||
class ExprInst {
|
||||
public:
|
||||
virtual ~Expression() = default;
|
||||
virtual ~ExprInst() = default;
|
||||
|
||||
/**
|
||||
* @brief 获取表达式的解析规则
|
||||
* @return
|
||||
*/
|
||||
virtual std::shared_ptr<const lib_syntax::ExpressionRule> definedRule() const = 0;
|
||||
virtual std::shared_ptr<const lib_syntax::ExprRule> definedRule() const = 0;
|
||||
//=====================================================
|
||||
/**
|
||||
* 获取语法节点的源码文件路径.
|
||||
|
|
@ -49,13 +49,13 @@ namespace ast_basic {
|
|||
* @brief 子表达式集合
|
||||
* @return
|
||||
*/
|
||||
virtual QList<std::shared_ptr<const Expression>> children() const = 0;
|
||||
virtual QList<std::shared_ptr<const ExprInst>> children() const = 0;
|
||||
/**
|
||||
* @brief 添加子表达式.
|
||||
*
|
||||
* \param inst 子表达式实例
|
||||
*/
|
||||
virtual void addChild(std::shared_ptr<const Expression> inst) = 0;
|
||||
virtual void addChild(std::shared_ptr<const ExprInst> inst) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -63,33 +63,34 @@ namespace ast_basic {
|
|||
/**
|
||||
* @brief 表达式节点
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::Expression, public std::enable_shared_from_this<ExpressionElement> {
|
||||
class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::ExprInst, public std::enable_shared_from_this<ExpressionElement> {
|
||||
private:
|
||||
std::shared_ptr<const lib_syntax::ExpressionRule> _expr_rule;
|
||||
QList<std::shared_ptr<const Expression>> children_store;
|
||||
std::shared_ptr<const lib_syntax::ExprRule> _expr_rule;
|
||||
QList<std::shared_ptr<const ExprInst>> children_store;
|
||||
QList<std::shared_ptr<const lib_token::IToken>> tokens_bind;
|
||||
|
||||
public:
|
||||
ExpressionElement(std::shared_ptr<const lib_syntax::ExpressionRule> bind);
|
||||
ExpressionElement(std::shared_ptr<const lib_syntax::ExprRule> bind);
|
||||
|
||||
// 通过 Expression 继承
|
||||
std::shared_ptr<const lib_syntax::ExpressionRule> definedRule() const override;
|
||||
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;
|
||||
void tokensReset(const QList<std::shared_ptr<const lib_token::IToken>>& list) override;
|
||||
void addToken(std::shared_ptr<const lib_token::IToken> token_inst) override;
|
||||
|
||||
QList<std::shared_ptr<const Expression>> children() const override;
|
||||
void addChild(std::shared_ptr<const Expression> inst) override;
|
||||
QList<std::shared_ptr<const ExprInst>> children() const override;
|
||||
void addChild(std::shared_ptr<const ExprInst> inst) override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT ExpressionContext : public lib_syntax::ParseContext, public std::enable_shared_from_this<ExpressionContext> {
|
||||
private:
|
||||
QList<std::shared_ptr<const lib_syntax::BaseRule>> rule_stack;
|
||||
QList<std::shared_ptr<Expression>> expression_stack;
|
||||
QList<std::shared_ptr<ExprInst>> expression_stack;
|
||||
QList<std::shared_ptr<const ast_basic::ExprInst>> document_store;
|
||||
QString current_file_path;
|
||||
QList<std::tuple<int, QString>> errors_storage;
|
||||
QList<std::tuple<QString, int, QString>> errors_storage;
|
||||
|
||||
public:
|
||||
ExpressionContext();
|
||||
|
|
@ -98,17 +99,20 @@ namespace ast_basic {
|
|||
virtual QString currentFile() const;
|
||||
|
||||
// 通过 ParseContext 继承
|
||||
std::shared_ptr<ast_basic::Expression> currentInst() const override;
|
||||
void pushInst(std::shared_ptr<ast_basic::Expression> current_inst) override;
|
||||
std::shared_ptr<ast_basic::Expression> popInst() override;
|
||||
std::shared_ptr<ast_basic::ExprInst> currentInst() const override;
|
||||
void pushInst(std::shared_ptr<ast_basic::ExprInst> current_inst) override;
|
||||
std::shared_ptr<ast_basic::ExprInst> popInst() override;
|
||||
|
||||
std::shared_ptr<const lib_syntax::BaseRule> currentExpressionRule() const override;
|
||||
void pushExpressionRule(std::shared_ptr<const lib_syntax::BaseRule> inst) override;
|
||||
std::shared_ptr<const lib_syntax::BaseRule> popExpressionRule() override;
|
||||
virtual QList<std::shared_ptr<const lib_syntax::BaseRule>> currentExpressionRuleStack() const;
|
||||
|
||||
void appendParseErrors(int start, const QString& error_msg) override;
|
||||
virtual void appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) override;
|
||||
virtual QList<std::shared_ptr<const ast_basic::ExprInst>> getDocs() const override;
|
||||
|
||||
void appendParseErrors(const QString& file_path, int start, const QString& error_msg) override;
|
||||
QStringList errors() const override;
|
||||
void clearErrors(int start) override;
|
||||
void clearErrors(const QString& file_path, int start) override;
|
||||
};
|
||||
}
|
||||
|
|
@ -39,7 +39,7 @@ void ast_gen::GlobalElement::setParent(std::shared_ptr<const SyntaxElement> inst
|
|||
|
||||
QList<std::shared_ptr<const TokenAccess>> GlobalElement::selfTokens() const { return QList<std::shared_ptr<const TokenAccess>>(); }
|
||||
|
||||
std::shared_ptr<const ast_basic::Expression> ast_gen::GlobalElement::bindExpression() const
|
||||
std::shared_ptr<const ast_basic::ExprInst> ast_gen::GlobalElement::bindExpression() const
|
||||
{
|
||||
return bind_exprs;
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@ void ast_gen::GlobalElement::cacheLoad()
|
|||
}
|
||||
|
||||
void GlobalElement::addChild(std::shared_ptr<ast_gen::SyntaxElement> citem) {
|
||||
auto convx = std::dynamic_pointer_cast<ast_basic::Expression>(citem);
|
||||
auto convx = std::dynamic_pointer_cast<ast_basic::ExprInst>(citem);
|
||||
bind_exprs->addChild(convx);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace ast_gen
|
|||
*
|
||||
* \return 表达式实例
|
||||
*/
|
||||
virtual std::shared_ptr<const ast_basic::Expression> bindExpression() const = 0;
|
||||
virtual std::shared_ptr<const ast_basic::ExprInst> bindExpression() const = 0;
|
||||
|
||||
/**
|
||||
* @brief 类型标记
|
||||
|
|
@ -107,7 +107,7 @@ namespace ast_gen
|
|||
QString names_store;
|
||||
QHash<QString, std::shared_ptr<const SyntaxElement>> node_cache;
|
||||
|
||||
std::shared_ptr<ast_basic::Expression> bind_exprs = std::make_shared<ast_basic::ExpressionElement>(nullptr);
|
||||
std::shared_ptr<ast_basic::ExprInst> bind_exprs = std::make_shared<ast_basic::ExpressionElement>(nullptr);
|
||||
|
||||
public:
|
||||
static GlobalElement* UniquePtr;
|
||||
|
|
@ -135,7 +135,7 @@ namespace ast_gen
|
|||
virtual QList<std::shared_ptr<const TokenAccess>> selfTokens() const override;
|
||||
|
||||
// 通过 SyntaxElement 继承
|
||||
virtual std::shared_ptr<const ast_basic::Expression> bindExpression() const override;
|
||||
virtual std::shared_ptr<const ast_basic::ExprInst> bindExpression() const override;
|
||||
virtual void cacheLoad() override;
|
||||
};
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
using namespace example_novel;
|
||||
using namespace lib_syntax;
|
||||
|
||||
TextSection::TextSection(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
TextSection::TextSection(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind) {}
|
||||
|
||||
QString TextSection::content() const
|
||||
|
|
@ -30,7 +30,7 @@ void TextSection::cacheLoad()
|
|||
context_store = text;
|
||||
}
|
||||
|
||||
FragmentRefers::FragmentRefers(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
FragmentRefers::FragmentRefers(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind) {}
|
||||
|
||||
QString FragmentRefers::storyRefer() const { return story_refs; }
|
||||
|
|
@ -59,7 +59,7 @@ QString FragmentRefers::signature() const {
|
|||
}
|
||||
|
||||
|
||||
FragmentDefine::FragmentDefine(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
FragmentDefine::FragmentDefine(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind) {}
|
||||
|
||||
QString FragmentDefine::name() const { return name_store; }
|
||||
|
|
@ -78,7 +78,7 @@ bool FragmentDefine::isAnonymous() const
|
|||
QString FragmentDefine::signature() const { return parent()->signature() + u8"&" + name(); }
|
||||
|
||||
|
||||
StoryDefine::StoryDefine(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
StoryDefine::StoryDefine(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind), sort_index(0) {}
|
||||
|
||||
QString StoryDefine::name() const { return name_store; }
|
||||
|
|
@ -104,7 +104,7 @@ bool StoryDefine::isAnonymous() const
|
|||
QString StoryDefine::signature() const { return name(); }
|
||||
|
||||
#include "syntax_novel.h"
|
||||
Document::Document(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
Document::Document(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind) {}
|
||||
|
||||
int Document::typeMark() const { return (int)NovelNode::Document; }
|
||||
|
|
@ -120,7 +120,7 @@ void Document::cacheLoad()
|
|||
{
|
||||
}
|
||||
|
||||
AbstractImpl::AbstractImpl(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
AbstractImpl::AbstractImpl(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: ExpressionElement(rule_bind) { parent_store.reset(); }
|
||||
|
||||
QList<std::shared_ptr<const ast_gen::TokenAccess> > AbstractImpl::selfTokens() const {
|
||||
|
|
@ -145,7 +145,7 @@ void AbstractImpl::setParent(std::shared_ptr<const ast_gen::SyntaxElement> inst)
|
|||
|
||||
|
||||
// ͨ¹ý SyntaxElement ¼Ì³Ð
|
||||
std::shared_ptr<const ast_basic::Expression> AbstractImpl::bindExpression() const {
|
||||
std::shared_ptr<const ast_basic::ExprInst> AbstractImpl::bindExpression() const {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
|
|
@ -154,7 +154,7 @@ QString AbstractImpl::path() const
|
|||
return ast_basic::ExpressionElement::filePath();
|
||||
}
|
||||
|
||||
VolumeDefine::VolumeDefine(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
VolumeDefine::VolumeDefine(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind) {}
|
||||
|
||||
QString VolumeDefine::name() const { return name_store; }
|
||||
|
|
@ -173,7 +173,7 @@ bool VolumeDefine::isAnonymous() const
|
|||
|
||||
QString VolumeDefine::signature() const { return name(); }
|
||||
|
||||
ArticleDefine::ArticleDefine(std::shared_ptr<const ExpressionRule> rule_bind)
|
||||
ArticleDefine::ArticleDefine(std::shared_ptr<const ExprRule> rule_bind)
|
||||
: AbstractImpl(rule_bind) {}
|
||||
|
||||
QString ArticleDefine::name() const { return name_store; }
|
||||
|
|
@ -192,7 +192,7 @@ bool ArticleDefine::isAnonymous() const
|
|||
|
||||
QString ArticleDefine::signature() const { return parent()->signature() + u8"&" + name(); }
|
||||
|
||||
RankDeclare::RankDeclare(std::shared_ptr<const ExpressionRule> rule)
|
||||
RankDeclare::RankDeclare(std::shared_ptr<const ExprRule> rule)
|
||||
: AbstractImpl(rule)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,10 +21,10 @@ namespace example_novel
|
|||
std::weak_ptr<const SyntaxElement> parent_store;
|
||||
|
||||
public:
|
||||
explicit AbstractImpl(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
explicit AbstractImpl(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
// ͨ¹ý SyntaxElement ¼Ì³Ð
|
||||
virtual std::shared_ptr<const ast_basic::Expression> bindExpression() const override;
|
||||
virtual std::shared_ptr<const ast_basic::ExprInst> bindExpression() const override;
|
||||
QString path() const override;
|
||||
virtual std::shared_ptr<const ast_gen::SyntaxElement> parent() const override;
|
||||
virtual void setParent(std::shared_ptr<const ast_gen::SyntaxElement> inst) override;
|
||||
|
|
@ -34,7 +34,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT TextSection : public AbstractImpl {
|
||||
public:
|
||||
TextSection(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
TextSection(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
QString content() const;
|
||||
|
||||
|
|
@ -51,7 +51,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT FragmentRefers : public AbstractImpl {
|
||||
public:
|
||||
FragmentRefers(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
FragmentRefers(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
QString storyRefer() const;
|
||||
QString fragmentRefer() const;
|
||||
|
|
@ -70,7 +70,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT FragmentDefine : public AbstractImpl {
|
||||
public:
|
||||
FragmentDefine(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
FragmentDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
QString name() const;
|
||||
|
||||
|
|
@ -87,7 +87,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT ArticleDefine : public AbstractImpl {
|
||||
public:
|
||||
ArticleDefine(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
ArticleDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
QString name() const;
|
||||
|
||||
|
|
@ -104,7 +104,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT VolumeDefine : public AbstractImpl {
|
||||
public:
|
||||
VolumeDefine(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
VolumeDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
QString name() const;
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT StoryDefine : public AbstractImpl {
|
||||
public:
|
||||
StoryDefine(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
StoryDefine(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
QString name() const;
|
||||
void setSort(int value);
|
||||
|
|
@ -141,7 +141,7 @@ namespace example_novel
|
|||
|
||||
class LIBSYNTAX_EXPORT Document : public AbstractImpl {
|
||||
public:
|
||||
Document(std::shared_ptr<const lib_syntax::ExpressionRule> rule_bind);
|
||||
Document(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
// SyntaxElement interface
|
||||
public:
|
||||
|
|
@ -155,7 +155,7 @@ namespace example_novel
|
|||
private:
|
||||
int page_rank = 0;
|
||||
public:
|
||||
RankDeclare(std::shared_ptr<const lib_syntax::ExpressionRule> rule);
|
||||
RankDeclare(std::shared_ptr<const lib_syntax::ExprRule> rule);
|
||||
|
||||
int rankNumber() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -11,23 +11,28 @@ TokenMatch::TokenMatch(shared_ptr<const ITokenDefine> define) : define_peer(defi
|
|||
|
||||
QList<std::shared_ptr<const BaseRule>> TokenMatch::children() const { return QList<std::shared_ptr<const BaseRule>>(); }
|
||||
|
||||
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
if (!head)
|
||||
throw new InputTerminal(rt_inst->currentFile());
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
if (!head) {
|
||||
rt_inst->appendParseErrors(rt_inst->currentFile(), - 1, QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(rt_inst->currentFile()));
|
||||
return std::make_tuple(BaseRule::MatchResult::Fail, head);
|
||||
}
|
||||
|
||||
auto match_result = define_peer->analysis(head);
|
||||
if (std::get<0>(match_result)) {
|
||||
rt_inst->currentInst()->addToken(std::get<0>(match_result));
|
||||
}
|
||||
else {
|
||||
throw new MismatchException(head);
|
||||
rt_inst->appendParseErrors(rt_inst->currentFile(), head->position(),
|
||||
QString(u8"Syntax[0x00001]语法匹配错误,不能识别token:%1<%2,%3>(%4)")
|
||||
.arg(head->content()).arg(head->row()).arg(head->column()).arg(head->file()));
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, head);
|
||||
}
|
||||
|
||||
if (std::get<1>(match_result)) {
|
||||
return std::make_tuple(nullptr, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
|
||||
}
|
||||
else {
|
||||
return std::make_tuple(nullptr, head->nextWord());
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, head->nextWord());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -39,34 +44,40 @@ 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<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> Rept::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Rept::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
auto temp_head = head;
|
||||
|
||||
// min-match
|
||||
for (auto idx = 0; idx < min_match; ++idx) {
|
||||
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));
|
||||
|
||||
temp_head = std::get<1>(result_gen);
|
||||
switch (std::get<0>(result_gen)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, temp_head);
|
||||
case BaseRule::MatchResult::Part:
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, std::get<1>(result_gen));
|
||||
default:
|
||||
temp_head = std::get<1>(result_gen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// max-match
|
||||
for (auto idx = min_match; idx < max_match; ++idx) {
|
||||
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));
|
||||
if (!temp_head)
|
||||
break;
|
||||
|
||||
auto result_gen = rule_peer->parse(rt_inst, temp_head);
|
||||
switch (std::get<0>(result_gen)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
case BaseRule::MatchResult::Part:
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, temp_head);
|
||||
default:
|
||||
temp_head = std::get<1>(result_gen);
|
||||
}
|
||||
catch (SyntaxException* ex) {
|
||||
delete ex;
|
||||
return std::make_tuple(nullptr, temp_head);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(nullptr, temp_head);
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, temp_head);
|
||||
}
|
||||
|
||||
QString Rept::token_present() const
|
||||
|
|
@ -78,18 +89,25 @@ 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<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> Seqs::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Seqs::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
auto temp_head = head;
|
||||
|
||||
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));
|
||||
switch (std::get<0>(rst_gene)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, temp_head);
|
||||
case BaseRule::MatchResult::Part:
|
||||
return rst_gene;
|
||||
case BaseRule::MatchResult::Success:
|
||||
temp_head = std::get<1>(rst_gene);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(nullptr, temp_head);
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, temp_head);
|
||||
}
|
||||
|
||||
QString Seqs::token_present() const
|
||||
|
|
@ -108,44 +126,43 @@ QList<std::shared_ptr<const BaseRule>> Any::children() const { return mbrs_store
|
|||
class words_span {
|
||||
public:
|
||||
int row_span, column_span;
|
||||
words_span(int rspan, int cspan):row_span(rspan), column_span(cspan){}
|
||||
words_span(int rspan, int cspan) :row_span(rspan), column_span(cspan) {}
|
||||
bool operator>(const words_span& other) {
|
||||
if(row_span > other.row_span)
|
||||
if (row_span > other.row_span)
|
||||
return true;
|
||||
if(row_span == other.row_span)
|
||||
if (row_span == other.row_span)
|
||||
return column_span > other.column_span;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> Any::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::function<words_span(std::shared_ptr<const IWordBase>, std::shared_ptr<const IWordBase>)> measure_span =
|
||||
[&](std::shared_ptr<const IWordBase> anchor, std::shared_ptr<const IWordBase> head)->words_span {
|
||||
return words_span(anchor->row() - head->row(), anchor->column() - head->column());
|
||||
};
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<std::shared_ptr<const BaseRule>, uint64_t> temp_result = std::make_tuple(nullptr, 0);
|
||||
auto rule_present = this->token_present();
|
||||
|
||||
std::tuple<std::shared_ptr<const BaseRule>, words_span> temp_result = std::make_tuple(mbrs_store.first(), words_span(0,0));
|
||||
for (auto& fork : mbrs_store) {
|
||||
try {
|
||||
auto gen = fork->parse(rt_inst, head);
|
||||
|
||||
auto gen = fork->parse(rt_inst, head);
|
||||
switch (std::get<0>(gen)) {
|
||||
// 遇到成功的直接返回解析结果
|
||||
if (std::get<0>(gen))
|
||||
rt_inst->currentInst()->addChild(std::get<0>(gen));
|
||||
return std::make_tuple(nullptr, std::get<1>(gen));
|
||||
case BaseRule::MatchResult::Success:
|
||||
return gen;
|
||||
case BaseRule::MatchResult::Fail: {
|
||||
if (!std::get<0>(temp_result))
|
||||
temp_result = std::make_tuple(fork, 0);
|
||||
}break;
|
||||
case BaseRule::MatchResult::Part: {
|
||||
auto span = std::get<1>(gen)->position() - head->position();
|
||||
if (span >= std::get<1>(temp_result))
|
||||
temp_result = std::make_tuple(fork, span);
|
||||
}
|
||||
// 语法错误的会进行比较
|
||||
catch (MismatchException* ex) {
|
||||
auto current_span = measure_span(ex->targetWord(), head);
|
||||
|
||||
if (current_span > std::get<1>(temp_result))
|
||||
temp_result = std::make_tuple(fork, current_span);
|
||||
|
||||
delete ex;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 分析最匹配的分支
|
||||
return std::get<0>(temp_result)->parse(rt_inst, head);
|
||||
rt_inst->clearErrors(rt_inst->currentFile(), head->position());
|
||||
auto temp = std::get<0>(temp_result)->parse(rt_inst, head);
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, std::get<1>(temp));
|
||||
}
|
||||
|
||||
QString Any::token_present() const
|
||||
|
|
@ -162,60 +179,65 @@ SyntaxException::SyntaxException(const QString& message) { this->msg_store = mes
|
|||
|
||||
QString SyntaxException::message() const { return msg_store; }
|
||||
|
||||
ExpressionRule::ExpressionRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
|
||||
this->filter_proc = [](const TokenSeqs& seqs) { return seqs; };
|
||||
ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_name) {
|
||||
this->mark_store = expr_mark;
|
||||
}
|
||||
|
||||
std::shared_ptr<const ExpressionRule> ExpressionRule::reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule) {
|
||||
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const BaseRule> rule) {
|
||||
auto ninst = makeCopy();
|
||||
ninst->child_store = rule;
|
||||
ninst->filter_proc = filter;
|
||||
return ninst;
|
||||
}
|
||||
|
||||
QString ExpressionRule::name() const { return name_store; }
|
||||
QString ExprRule::name() const { return name_store; }
|
||||
|
||||
int ExpressionRule::typeMark() const { return this->mark_store; }
|
||||
int ExprRule::typeMark() const { return this->mark_store; }
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> ExpressionRule::children() const {
|
||||
QList<std::shared_ptr<const BaseRule>> ExprRule::children() const {
|
||||
return QList<std::shared_ptr<const BaseRule>>() << this->child_store;
|
||||
}
|
||||
|
||||
std::tuple<std::shared_ptr<const Expression>, std::shared_ptr<const IWordBase>> ExpressionRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::shared_ptr<Expression> elm_ast = this->newEmptyInstance();
|
||||
#include <ast_novel.h>
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>>
|
||||
ExprRule::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::shared_ptr<ExprInst> elm_ast = this->newEmptyInstance();
|
||||
auto text_present = this->token_present();
|
||||
|
||||
rt_inst->pushExpressionRule(this->shared_from_this());
|
||||
rt_inst->pushInst(elm_ast);
|
||||
|
||||
try {
|
||||
auto rstg = child_store->parse(rt_inst, head);
|
||||
auto rstg = child_store->parse(rt_inst, head);
|
||||
auto tokens_decl = elm_ast->tokens();
|
||||
|
||||
auto tokens_decl = this->filter_proc(elm_ast->tokens());
|
||||
elm_ast->tokensReset(tokens_decl);
|
||||
switch (std::get<0>(rstg)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
case BaseRule::MatchResult::Part:
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
break;
|
||||
case BaseRule::MatchResult::Success: {
|
||||
if (!std::dynamic_pointer_cast<example_novel::Document>(elm_ast)) {
|
||||
auto start_pos = tokens_decl.first()->position();
|
||||
rt_inst->clearErrors(rt_inst->currentFile(), start_pos);
|
||||
}
|
||||
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
return std::make_tuple(elm_ast, std::get<1>(rstg));
|
||||
}
|
||||
catch (...) {
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
throw;
|
||||
|
||||
if (rt_inst->currentInst()) {
|
||||
rt_inst->currentInst()->addChild(elm_ast);
|
||||
}
|
||||
else {
|
||||
rt_inst->appendDoc(elm_ast);
|
||||
}
|
||||
}break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return rstg;
|
||||
}
|
||||
|
||||
QString ExpressionRule::token_present() const {
|
||||
return QString(u8"(%1)").arg(child_store->token_present());
|
||||
QString ExprRule::token_present() const {
|
||||
return child_store->token_present();
|
||||
}
|
||||
|
||||
MismatchException::MismatchException(std::shared_ptr<const lib_token::IWordBase> inst) :SyntaxException(
|
||||
QString(u8"Syntax[0x00001]语法匹配错误,不能识别token:%1<%2,%3>(%4)")
|
||||
.arg(inst->content()).arg(inst->row()).arg(inst->column()).arg(inst->file())), target(inst) {}
|
||||
|
||||
std::shared_ptr<const IWordBase>MismatchException::targetWord() const {
|
||||
return this->target;
|
||||
}
|
||||
|
||||
InputTerminal::InputTerminal(const QString& file_path)
|
||||
:SyntaxException(QString(u8"Syntax[0x0000]token流(%1)提前终止").arg(file_path)) {}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include <tuple>
|
||||
#include <functional>
|
||||
namespace ast_basic {
|
||||
class Expression;
|
||||
class ExprInst;
|
||||
class ExpressionElement;
|
||||
}
|
||||
|
||||
|
|
@ -38,18 +38,21 @@ namespace lib_syntax {
|
|||
virtual void setCurrentFile(const QString &path) = 0;
|
||||
virtual QString currentFile() const = 0;
|
||||
|
||||
virtual void appendParseErrors(int start, const QString &error_msg) = 0;
|
||||
virtual void appendParseErrors(const QString & file_path, int start, const QString &error_msg) = 0;
|
||||
virtual QStringList errors() const = 0;
|
||||
virtual void clearErrors(int start) = 0;
|
||||
virtual void clearErrors(const QString &file_path, int start) = 0;
|
||||
|
||||
virtual void appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) = 0;
|
||||
virtual QList<std::shared_ptr<const ast_basic::ExprInst>> getDocs() const = 0;
|
||||
|
||||
/**
|
||||
* \brief 当前表达式元素.
|
||||
*
|
||||
* \return 返回当前表达式
|
||||
*/
|
||||
virtual std::shared_ptr<ast_basic::Expression> currentInst() const = 0;
|
||||
virtual void pushInst(std::shared_ptr<ast_basic::Expression> current_inst) = 0;
|
||||
virtual std::shared_ptr<ast_basic::Expression> popInst() = 0;
|
||||
virtual std::shared_ptr<ast_basic::ExprInst> currentInst() const = 0;
|
||||
virtual void pushInst(std::shared_ptr<ast_basic::ExprInst> current_inst) = 0;
|
||||
virtual std::shared_ptr<ast_basic::ExprInst> popInst() = 0;
|
||||
|
||||
virtual std::shared_ptr<const BaseRule> currentExpressionRule() const = 0;
|
||||
virtual void pushExpressionRule(std::shared_ptr<const BaseRule> inst) = 0;
|
||||
|
|
@ -84,7 +87,7 @@ namespace lib_syntax {
|
|||
* @param head 列表头
|
||||
* @return 返回结果<匹配完成新列表头,匹配长度>
|
||||
*/
|
||||
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
virtual std::tuple<BaseRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0;
|
||||
|
||||
/**
|
||||
|
|
@ -109,7 +112,7 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
virtual std::tuple<BaseRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
|
@ -129,7 +132,7 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
virtual std::tuple<BaseRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
|
@ -147,7 +150,7 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
virtual std::tuple<BaseRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
|
@ -166,7 +169,7 @@ namespace lib_syntax {
|
|||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
virtual std::tuple<BaseRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
|
@ -175,27 +178,26 @@ namespace lib_syntax {
|
|||
/**
|
||||
* @brief 对应语法表达式解析规则
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT ExpressionRule : public lib_syntax::BaseRule, public std::enable_shared_from_this<ExpressionRule> {
|
||||
class LIBSYNTAX_EXPORT ExprRule : public lib_syntax::BaseRule, public std::enable_shared_from_this<ExprRule> {
|
||||
public:
|
||||
typedef QList<std::shared_ptr<const lib_token::IToken>> TokenSeqs;
|
||||
ExpressionRule(const QString& rule_name, int expr_mark);
|
||||
ExprRule(const QString& rule_name, int expr_mark);
|
||||
|
||||
virtual std::shared_ptr<const ExpressionRule> reloadRule(std::function<TokenSeqs(const TokenSeqs&)> filter, std::shared_ptr<const BaseRule> rule);
|
||||
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const BaseRule> rule);
|
||||
virtual QString name() const;
|
||||
virtual int typeMark() const;
|
||||
|
||||
virtual std::shared_ptr<ast_basic::Expression> newEmptyInstance() const = 0;
|
||||
virtual std::shared_ptr<ExpressionRule> makeCopy() const = 0;
|
||||
virtual std::shared_ptr<ast_basic::ExprInst> newEmptyInstance() const = 0;
|
||||
virtual std::shared_ptr<ExprRule> makeCopy() const = 0;
|
||||
|
||||
// BaseRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const lib_syntax::BaseRule>> children() const override;
|
||||
virtual std::tuple<std::shared_ptr<const ast_basic::Expression>, std::shared_ptr<const lib_token::IWordBase>>
|
||||
virtual std::tuple<BaseRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
|
||||
private:
|
||||
std::function<TokenSeqs(const TokenSeqs&)> filter_proc;
|
||||
std::shared_ptr<const lib_syntax::BaseRule> child_store;
|
||||
QString name_store;
|
||||
int mark_store;
|
||||
|
|
@ -205,35 +207,18 @@ namespace lib_syntax {
|
|||
* 语法元素解析规则.
|
||||
*/
|
||||
template<class ExprType>
|
||||
class ElementRule : public ExpressionRule {
|
||||
class ElementRule : public ExprRule {
|
||||
public:
|
||||
ElementRule(const QString& rule_name, int expr_mark)
|
||||
:ExpressionRule(rule_name, expr_mark){}
|
||||
:ExprRule(rule_name, expr_mark){}
|
||||
|
||||
virtual std::shared_ptr<ast_basic::Expression> newEmptyInstance() const {
|
||||
return std::dynamic_pointer_cast<ast_basic::Expression>(
|
||||
virtual std::shared_ptr<ast_basic::ExprInst> newEmptyInstance() const {
|
||||
return std::dynamic_pointer_cast<ast_basic::ExprInst>(
|
||||
std::make_shared<ExprType>(this->shared_from_this()));
|
||||
}
|
||||
|
||||
virtual std::shared_ptr<ExpressionRule> makeCopy() const {
|
||||
virtual std::shared_ptr<ExprRule> makeCopy() const {
|
||||
return std::make_shared<ElementRule<ExprType>>(name(), typeMark());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class MismatchException : public SyntaxException {
|
||||
private:
|
||||
std::shared_ptr<const lib_token::IWordBase> target;
|
||||
|
||||
public:
|
||||
MismatchException(std::shared_ptr<const lib_token::IWordBase> inst);
|
||||
virtual ~MismatchException() = default;
|
||||
|
||||
virtual std::shared_ptr<const lib_token::IWordBase> targetWord() const;
|
||||
};
|
||||
|
||||
class InputTerminal : public SyntaxException {
|
||||
public:
|
||||
InputTerminal(const QString &file_path);
|
||||
};
|
||||
} // namespace lib_syntax
|
||||
|
|
@ -23,9 +23,6 @@ auto split_mark = std::make_shared<Split>(); //
|
|||
auto vtext = std::make_shared<VTextSection>(); // ^([^\\{\\}\\n@&]+)
|
||||
auto name_text = std::make_shared<NameSection>(); // ^([^:\\{\\}\\n@&][^\\{\\}\\n@&]*)
|
||||
|
||||
auto newl = std::make_shared<NewLine>();
|
||||
|
||||
|
||||
|
||||
|
||||
// rule-parts ===============================================================================
|
||||
|
|
@ -40,76 +37,61 @@ auto newl = std::make_shared<NewLine>();
|
|||
#define MultiR(rule) std::make_shared<const Rept>(rule, 1, INT_MAX)
|
||||
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> LinesMerge(const QList<std::shared_ptr<const BaseRule>>& mbrs) {
|
||||
QList<std::shared_ptr<const BaseRule>> values_ret;
|
||||
for (auto& item : mbrs) {
|
||||
values_ret << item << OptMulT(newl);
|
||||
}
|
||||
return values_ret;
|
||||
}
|
||||
QList<std::shared_ptr<const BaseRule>> LinesMerge(std::shared_ptr<const BaseRule> item) {
|
||||
return QList<std::shared_ptr<const BaseRule>>() << item << OptMulT(newl);
|
||||
}
|
||||
|
||||
// remove-return
|
||||
auto remove_nl = [](const ExpressionRule::TokenSeqs& p)->ExpressionRule::TokenSeqs {
|
||||
ExpressionRule::TokenSeqs result;
|
||||
for (auto& n : p) {
|
||||
if (n->define()->typeMark() == newl->typeMark())
|
||||
continue;
|
||||
result.append(n);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
auto decl_comp = std::make_shared<const Any>(Rules{ MR(numbers), MR(vtext), MR(name_text), MR(split_mark) });
|
||||
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule(remove_nl,
|
||||
std::make_shared<const Seqs>(LinesMerge(MultiR(decl_comp))
|
||||
auto decl_expr = ElementRule<TextSection>(u8"decl_section", (int)NovelExprs::DESC_SECTION).reloadRule(
|
||||
MultiR(decl_comp));
|
||||
|
||||
auto fragment_decl = ElementRule<FragmentDefine>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(frag_key), MR(name_text) } <<
|
||||
OptMulR(decl_expr) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto fragment_decl = ElementRule<FragmentDefine>(u8"fragment_define", (int)NovelExprs::FRAG_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(frag_key), MR(name_text) }) <<
|
||||
OptMulR(decl_expr) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
|
||||
auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(refers), MR(frag_key), MR(name_text), MR(split_mark), MR(name_text) }) <<
|
||||
OptMulR(decl_expr) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto fragment_refer = ElementRule<FragmentRefers>(u8"fragment_refer", (int)NovelExprs::FRAG_REFERS).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(refers), MR(frag_key), MR(name_text), MR(split_mark), MR(name_text) } <<
|
||||
OptMulR(decl_expr) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto fragment_comp = std::make_shared<const Any>(Rules{ fragment_decl, fragment_refer, decl_expr });
|
||||
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(story_key), MR(name_text) }) <<
|
||||
OptMulR(fragment_comp) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto story_define = ElementRule<StoryDefine>(u8"story_define", (int)NovelExprs::STORY_DEFINES).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(story_key), MR(name_text) } <<
|
||||
OptMulR(fragment_comp) <<
|
||||
MR(rightb)
|
||||
));
|
||||
// ===================================================================
|
||||
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(article_key), MR(name_text) }) <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto article_decl = ElementRule<ArticleDefine>(u8"article_define", (int)NovelExprs::ARTICLE_DEFINE).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(article_key), MR(name_text) } <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ fragment_refer, decl_expr })) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
LinesMerge(Rules{ MR(leftb), MR(volume_key), MR(name_text) }) <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
|
||||
LinesMerge(MR(rightb))
|
||||
));
|
||||
auto volume_decl = ElementRule<VolumeDefine>(u8"volume_define", (int)NovelExprs::VOLUME_DEFINE).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(leftb), MR(volume_key), MR(name_text) } <<
|
||||
OptMulR(std::make_shared<const Any>(Rules{ decl_expr, article_decl })) <<
|
||||
MR(rightb)
|
||||
));
|
||||
|
||||
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule(remove_nl, std::make_shared<const Seqs>(
|
||||
Rules{ MR(declare), MR(rank_key), MR(numbers), OptMulT(newl)}
|
||||
));
|
||||
auto rank_define = ElementRule<RankDeclare>(u8"rank_define", (int)NovelNode::RankDeclaration).reloadRule(
|
||||
std::make_shared<const Seqs>(
|
||||
Rules{ MR(declare), MR(rank_key), MR(numbers) }
|
||||
));
|
||||
|
||||
auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).reloadRule(remove_nl, 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}))
|
||||
}
|
||||
));
|
||||
auto document_define = ElementRule<Document>(u8"decls-doc", (int)NovelExprs::DOC_DEFINES).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}))
|
||||
}
|
||||
));
|
||||
|
||||
std::shared_ptr<const ExpressionRule> NovalSyntax::getParseTree() { return document_define; }
|
||||
std::shared_ptr<const ExprRule> NovalSyntax::getParseTree() { 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)
|
||||
{
|
||||
cache_load(root, children);
|
||||
|
|
@ -136,7 +118,7 @@ void NovalSyntax::node_register(std::shared_ptr<const ast_gen::SyntaxElement> ro
|
|||
for (auto& child : children) {
|
||||
if (!child->isAnonymous()) {
|
||||
auto check_result = ast_gen::GlobalElement::UniquePtr->appendToCache(child);
|
||||
if(check_result)
|
||||
if (check_result)
|
||||
throw new lib_syntax::SyntaxException(QString(u8"SyntaxError[0x0004]系统中包含同类型重名命名节点:%1<type:%2>(%3,%4)")
|
||||
.arg(child->signature()).arg(child->typeMark()).arg(child->path()).arg(check_result->path()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace example_novel {
|
|||
* @brief 获取novel语法解析树
|
||||
* @return
|
||||
*/
|
||||
static std::shared_ptr<const lib_syntax::ExpressionRule> getParseTree();
|
||||
static std::shared_ptr<const lib_syntax::ExprRule> getParseTree();
|
||||
|
||||
static std::shared_ptr<const ast_gen::SyntaxElement> tidy(std::shared_ptr<ast_gen::SyntaxElement> root, QList<std::shared_ptr<ast_gen::SyntaxElement>> docs);
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ QList<std::shared_ptr<const IWordBase>> WordReader::extract_from(const QString&
|
|||
while (!tin.atEnd()) {
|
||||
uint64_t relative_offset = line_number;
|
||||
relative_offset = relative_offset << 32;
|
||||
auto line = tin.readLine() + "\n";
|
||||
auto line = tin.readLine();
|
||||
ret_list.append(this->parse_line(relative_offset, line_number++, line, path));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -93,15 +93,6 @@ Keywords::analysis(std::shared_ptr<const IWordBase> content) const {
|
|||
return std::make_tuple(token_inst, nullptr);
|
||||
}
|
||||
|
||||
QString NewLine::typeName() const { return u8"new-line"; }
|
||||
|
||||
int NewLine::typeMark() const
|
||||
{
|
||||
return 0x04000000;
|
||||
}
|
||||
|
||||
QString NewLine::regex() const { return u8"\n"; }
|
||||
|
||||
QString Numbers::typeName() const { return u8"numbers"; }
|
||||
|
||||
int Numbers::typeMark() const
|
||||
|
|
|
|||
|
|
@ -60,13 +60,6 @@ namespace example_novel {
|
|||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT NewLine : public LeftBracket {
|
||||
public:
|
||||
virtual QString typeName() const override;
|
||||
virtual int typeMark() const override;
|
||||
virtual QString regex() const override;
|
||||
};
|
||||
|
||||
class LIBTOKEN_EXPORT Split : public LeftBracket {
|
||||
public:
|
||||
virtual QString typeName() const override;
|
||||
|
|
|
|||
Loading…
Reference in New Issue