refactor rename
This commit is contained in:
parent
7a07e0b266
commit
19985a4bb7
|
|
@ -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::ExprInst>> forst_root;
|
||||
QList<std::shared_ptr<const ast_basic::IExprInst>> forst_root;
|
||||
auto word_reader = std::make_shared<lib_token::WordReader>();
|
||||
auto context = std::make_shared<ast_gen::GlobalElement>(u8"С˵");
|
||||
|
||||
|
|
|
|||
|
|
@ -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 ExprInst>> ExpressionElement::children() const {
|
||||
QList<std::shared_ptr<const IExprInst>> ExpressionElement::children() const {
|
||||
return this->children_store;
|
||||
}
|
||||
|
||||
void ExpressionElement::addChild(std::shared_ptr<const ExprInst> inst) {
|
||||
void ExpressionElement::addChild(std::shared_ptr<const IExprInst> 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<ExprInst> ExpressionContext::currentInst() const
|
||||
std::shared_ptr<IExprInst> ExpressionContext::currentExprInst() const
|
||||
{
|
||||
if (expression_stack.size())
|
||||
return expression_stack.last();
|
||||
|
|
@ -52,30 +52,30 @@ std::shared_ptr<ExprInst> ExpressionContext::currentInst() const
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
void ExpressionContext::pushInst(std::shared_ptr<ExprInst> current_inst)
|
||||
void ExpressionContext::pushExprInst(std::shared_ptr<IExprInst> current_inst)
|
||||
{
|
||||
if (!expression_stack.size() || expression_stack.last() != current_inst)
|
||||
expression_stack.append(current_inst);
|
||||
}
|
||||
|
||||
std::shared_ptr<ExprInst> ExpressionContext::popInst()
|
||||
std::shared_ptr<IExprInst> ExpressionContext::popExprInst()
|
||||
{
|
||||
auto lastx = expression_stack.takeLast();
|
||||
return lastx;
|
||||
}
|
||||
|
||||
std::shared_ptr<const BaseRule> ExpressionContext::currentExpressionRule() const {
|
||||
std::shared_ptr<const IBasicRule> ExpressionContext::currentExprRule() const {
|
||||
if (rule_stack.size())
|
||||
return rule_stack.last();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ExpressionContext::pushExpressionRule(std::shared_ptr<const BaseRule> inst) {
|
||||
void ExpressionContext::pushExprRule(std::shared_ptr<const IBasicRule> inst) {
|
||||
if (!rule_stack.size() || rule_stack.last() != inst)
|
||||
rule_stack.append(inst);
|
||||
}
|
||||
|
||||
std::shared_ptr<const BaseRule> ExpressionContext::popExpressionRule()
|
||||
std::shared_ptr<const IBasicRule> ExpressionContext::popExprRule()
|
||||
{
|
||||
return rule_stack.takeLast();
|
||||
}
|
||||
|
|
@ -100,15 +100,15 @@ void ExpressionContext::clearErrors(const QString &file_path, int start) {
|
|||
}
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> ExpressionContext::currentExpressionRuleStack() const {
|
||||
QList<std::shared_ptr<const IBasicRule>> ExpressionContext::currentExpressionRuleStack() const {
|
||||
return rule_stack;
|
||||
}
|
||||
|
||||
|
||||
void ExpressionContext::appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) {
|
||||
void ExpressionContext::appendDoc(std::shared_ptr<ast_basic::IExprInst> inst) {
|
||||
this->document_store.append(inst);
|
||||
}
|
||||
|
||||
QList<std::shared_ptr<const ast_basic::ExprInst>> ExpressionContext::getDocs() const {
|
||||
QList<std::shared_ptr<const ast_basic::IExprInst>> ExpressionContext::getDocs() const {
|
||||
return this->document_store;
|
||||
}
|
||||
|
|
@ -10,9 +10,9 @@ namespace ast_basic {
|
|||
/**
|
||||
* @brief 抽象语法树集合节点/表达式节点
|
||||
*/
|
||||
class ExprInst {
|
||||
class IExprInst {
|
||||
public:
|
||||
virtual ~ExprInst() = default;
|
||||
virtual ~IExprInst() = default;
|
||||
|
||||
/**
|
||||
* @brief 获取表达式的解析规则
|
||||
|
|
@ -49,13 +49,13 @@ namespace ast_basic {
|
|||
* @brief 子表达式集合
|
||||
* @return
|
||||
*/
|
||||
virtual QList<std::shared_ptr<const ExprInst>> children() const = 0;
|
||||
virtual QList<std::shared_ptr<const IExprInst>> children() const = 0;
|
||||
/**
|
||||
* @brief 添加子表达式.
|
||||
*
|
||||
* \param inst 子表达式实例
|
||||
*/
|
||||
virtual void addChild(std::shared_ptr<const ExprInst> inst) = 0;
|
||||
virtual void addChild(std::shared_ptr<const IExprInst> inst) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -63,10 +63,10 @@ namespace ast_basic {
|
|||
/**
|
||||
* @brief 表达式节点
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::ExprInst, public std::enable_shared_from_this<ExpressionElement> {
|
||||
class LIBSYNTAX_EXPORT ExpressionElement : public ast_basic::IExprInst, public std::enable_shared_from_this<ExpressionElement> {
|
||||
private:
|
||||
std::shared_ptr<const lib_syntax::ExprRule> _expr_rule;
|
||||
QList<std::shared_ptr<const ExprInst>> children_store;
|
||||
QList<std::shared_ptr<const IExprInst>> children_store;
|
||||
QList<std::shared_ptr<const lib_token::IToken>> tokens_bind;
|
||||
|
||||
public:
|
||||
|
|
@ -80,15 +80,15 @@ namespace ast_basic {
|
|||
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 ExprInst>> children() const override;
|
||||
void addChild(std::shared_ptr<const ExprInst> inst) override;
|
||||
QList<std::shared_ptr<const IExprInst>> children() const override;
|
||||
void addChild(std::shared_ptr<const IExprInst> inst) override;
|
||||
};
|
||||
|
||||
class LIBSYNTAX_EXPORT ExpressionContext : public lib_syntax::ParseContext, public std::enable_shared_from_this<ExpressionContext> {
|
||||
class LIBSYNTAX_EXPORT ExpressionContext : public lib_syntax::IContext, public std::enable_shared_from_this<ExpressionContext> {
|
||||
private:
|
||||
QList<std::shared_ptr<const lib_syntax::BaseRule>> rule_stack;
|
||||
QList<std::shared_ptr<ExprInst>> expression_stack;
|
||||
QList<std::shared_ptr<const ast_basic::ExprInst>> document_store;
|
||||
QList<std::shared_ptr<const lib_syntax::IBasicRule>> rule_stack;
|
||||
QList<std::shared_ptr<IExprInst>> expression_stack;
|
||||
QList<std::shared_ptr<const ast_basic::IExprInst>> document_store;
|
||||
QString current_file_path;
|
||||
QList<std::tuple<QString, int, QString>> errors_storage;
|
||||
|
||||
|
|
@ -98,18 +98,18 @@ namespace ast_basic {
|
|||
virtual void setCurrentFile(const QString& path);
|
||||
virtual QString currentFile() const;
|
||||
|
||||
// ͨ¹ý ParseContext ¼Ì³Ð
|
||||
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;
|
||||
// ͨ¹ý IContext ¼Ì³Ð
|
||||
std::shared_ptr<ast_basic::IExprInst> currentExprInst() const override;
|
||||
void pushExprInst(std::shared_ptr<ast_basic::IExprInst> current_inst) override;
|
||||
std::shared_ptr<ast_basic::IExprInst> popExprInst() 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;
|
||||
std::shared_ptr<const lib_syntax::IBasicRule> currentExprRule() const override;
|
||||
void pushExprRule(std::shared_ptr<const lib_syntax::IBasicRule> inst) override;
|
||||
std::shared_ptr<const lib_syntax::IBasicRule> popExprRule() override;
|
||||
virtual QList<std::shared_ptr<const lib_syntax::IBasicRule>> currentExpressionRuleStack() const;
|
||||
|
||||
virtual void appendDoc(std::shared_ptr<ast_basic::ExprInst> inst) override;
|
||||
virtual QList<std::shared_ptr<const ast_basic::ExprInst>> getDocs() const override;
|
||||
virtual void appendDoc(std::shared_ptr<ast_basic::IExprInst> inst) override;
|
||||
virtual QList<std::shared_ptr<const ast_basic::IExprInst>> getDocs() const override;
|
||||
|
||||
void appendParseErrors(const QString& file_path, int start, const QString& error_msg) override;
|
||||
QStringList errors() const 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::ExprInst> ast_gen::GlobalElement::bindExpression() const
|
||||
std::shared_ptr<const ast_basic::IExprInst> 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::ExprInst>(citem);
|
||||
auto convx = std::dynamic_pointer_cast<ast_basic::IExprInst>(citem);
|
||||
bind_exprs->addChild(convx);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace ast_gen
|
|||
*
|
||||
* \return 表达式实例
|
||||
*/
|
||||
virtual std::shared_ptr<const ast_basic::ExprInst> bindExpression() const = 0;
|
||||
virtual std::shared_ptr<const ast_basic::IExprInst> 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::ExprInst> bind_exprs = std::make_shared<ast_basic::ExpressionElement>(nullptr);
|
||||
std::shared_ptr<ast_basic::IExprInst> 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::ExprInst> bindExpression() const override;
|
||||
virtual std::shared_ptr<const ast_basic::IExprInst> bindExpression() const override;
|
||||
virtual void cacheLoad() override;
|
||||
};
|
||||
}
|
||||
|
|
@ -145,7 +145,7 @@ void AbstractImpl::setParent(std::shared_ptr<const ast_gen::SyntaxElement> inst)
|
|||
|
||||
|
||||
// 通过 SyntaxElement 继承
|
||||
std::shared_ptr<const ast_basic::ExprInst> AbstractImpl::bindExpression() const {
|
||||
std::shared_ptr<const ast_basic::IExprInst> AbstractImpl::bindExpression() const {
|
||||
return shared_from_this();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ namespace example_novel
|
|||
explicit AbstractImpl(std::shared_ptr<const lib_syntax::ExprRule> rule_bind);
|
||||
|
||||
// 通过 SyntaxElement 继承
|
||||
virtual std::shared_ptr<const ast_basic::ExprInst> bindExpression() const override;
|
||||
virtual std::shared_ptr<const ast_basic::IExprInst> 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;
|
||||
|
|
|
|||
|
|
@ -9,30 +9,30 @@ using namespace ast_basic;
|
|||
|
||||
TokenMatch::TokenMatch(shared_ptr<const ITokenDefine> define) : define_peer(define) {}
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> TokenMatch::children() const { return QList<std::shared_ptr<const BaseRule>>(); }
|
||||
QList<std::shared_ptr<const IBasicRule>> TokenMatch::children() const { return QList<std::shared_ptr<const IBasicRule>>(); }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> TokenMatch::parse(std::shared_ptr<IContext> 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);
|
||||
return std::make_tuple(IBasicRule::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));
|
||||
rt_inst->currentExprInst()->addToken(std::get<0>(match_result));
|
||||
}
|
||||
else {
|
||||
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);
|
||||
return std::make_tuple(IBasicRule::MatchResult::Part, head);
|
||||
}
|
||||
|
||||
if (std::get<1>(match_result)) {
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
|
||||
return std::make_tuple(IBasicRule::MatchResult::Success, std::make_shared<WordImpl>(std::get<1>(match_result), head->nextWord()));
|
||||
}
|
||||
else {
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, head->nextWord());
|
||||
return std::make_tuple(IBasicRule::MatchResult::Success, head->nextWord());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -40,21 +40,21 @@ QString TokenMatch::token_present() const {
|
|||
return QString(u8"<%1>").arg(this->define_peer->typeName());
|
||||
}
|
||||
|
||||
Rept::Rept(std::shared_ptr<const BaseRule> rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {}
|
||||
Rept::Rept(std::shared_ptr<const IBasicRule> rule, int min, int max) : rule_peer(rule), min_match(min), max_match(max) {}
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> Rept::children() const { return QList<std::shared_ptr<const BaseRule>>() << rule_peer; }
|
||||
QList<std::shared_ptr<const IBasicRule>> Rept::children() const { return QList<std::shared_ptr<const IBasicRule>>() << rule_peer; }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Rept::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> Rept::parse(std::shared_ptr<IContext> 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);
|
||||
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));
|
||||
case IBasicRule::MatchResult::Fail:
|
||||
return std::make_tuple(IBasicRule::MatchResult::Part, temp_head);
|
||||
case IBasicRule::MatchResult::Part:
|
||||
return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(result_gen));
|
||||
default:
|
||||
temp_head = std::get<1>(result_gen);
|
||||
break;
|
||||
|
|
@ -68,16 +68,16 @@ std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Rept::parse(
|
|||
|
||||
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);
|
||||
case IBasicRule::MatchResult::Fail:
|
||||
case IBasicRule::MatchResult::Part:
|
||||
return std::make_tuple(IBasicRule::MatchResult::Success, temp_head);
|
||||
default:
|
||||
temp_head = std::get<1>(result_gen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, temp_head);
|
||||
return std::make_tuple(IBasicRule::MatchResult::Success, temp_head);
|
||||
}
|
||||
|
||||
QString Rept::token_present() const
|
||||
|
|
@ -85,21 +85,21 @@ QString Rept::token_present() const
|
|||
return u8"(" + this->rule_peer->token_present() + QString(u8"{%1, %2}").arg(min_match).arg(max_match) + u8")";
|
||||
}
|
||||
|
||||
Seqs::Seqs(const QList<std::shared_ptr<const BaseRule>> mbrs) : mbrs_store(mbrs) {}
|
||||
Seqs::Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) {}
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> Seqs::children() const { return mbrs_store; }
|
||||
QList<std::shared_ptr<const IBasicRule>> Seqs::children() const { return mbrs_store; }
|
||||
|
||||
std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Seqs::parse(std::shared_ptr<ParseContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> Seqs::parse(std::shared_ptr<IContext> 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);
|
||||
switch (std::get<0>(rst_gene)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
return std::make_tuple(BaseRule::MatchResult::Part, temp_head);
|
||||
case BaseRule::MatchResult::Part:
|
||||
case IBasicRule::MatchResult::Fail:
|
||||
return std::make_tuple(IBasicRule::MatchResult::Part, temp_head);
|
||||
case IBasicRule::MatchResult::Part:
|
||||
return rst_gene;
|
||||
case BaseRule::MatchResult::Success:
|
||||
case IBasicRule::MatchResult::Success:
|
||||
temp_head = std::get<1>(rst_gene);
|
||||
break;
|
||||
default:
|
||||
|
|
@ -107,7 +107,7 @@ std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Seqs::parse(
|
|||
}
|
||||
}
|
||||
|
||||
return std::make_tuple(BaseRule::MatchResult::Success, temp_head);
|
||||
return std::make_tuple(IBasicRule::MatchResult::Success, temp_head);
|
||||
}
|
||||
|
||||
QString Seqs::token_present() const
|
||||
|
|
@ -118,10 +118,10 @@ QString Seqs::token_present() const
|
|||
return QString(u8"(%1)").arg(content);
|
||||
}
|
||||
|
||||
//std::tuple<BaseRule::MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const Token>>
|
||||
Any::Any(const QList<std::shared_ptr<const BaseRule>> mbrs) : mbrs_store(mbrs) {}
|
||||
//std::tuple<IBasicRule::MatchResult, uint, std::shared_ptr<const IBasicRule>, std::shared_ptr<const Token>>
|
||||
Any::Any(const QList<std::shared_ptr<const IBasicRule>> mbrs) : mbrs_store(mbrs) {}
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> Any::children() const { return mbrs_store; }
|
||||
QList<std::shared_ptr<const IBasicRule>> Any::children() const { return mbrs_store; }
|
||||
|
||||
class words_span {
|
||||
public:
|
||||
|
|
@ -135,21 +135,21 @@ public:
|
|||
return false;
|
||||
}
|
||||
};
|
||||
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);
|
||||
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::tuple<std::shared_ptr<const IBasicRule>, uint64_t> temp_result = std::make_tuple(nullptr, 0);
|
||||
auto rule_present = this->token_present();
|
||||
|
||||
for (auto& fork : mbrs_store) {
|
||||
auto gen = fork->parse(rt_inst, head);
|
||||
switch (std::get<0>(gen)) {
|
||||
// 遇到成功的直接返回解析结果
|
||||
case BaseRule::MatchResult::Success:
|
||||
case IBasicRule::MatchResult::Success:
|
||||
return gen;
|
||||
case BaseRule::MatchResult::Fail: {
|
||||
case IBasicRule::MatchResult::Fail: {
|
||||
if (!std::get<0>(temp_result))
|
||||
temp_result = std::make_tuple(fork, 0);
|
||||
}break;
|
||||
case BaseRule::MatchResult::Part: {
|
||||
case IBasicRule::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);
|
||||
|
|
@ -162,7 +162,7 @@ std::tuple<BaseRule::MatchResult, std::shared_ptr<const IWordBase>> Any::parse(s
|
|||
// 分析最匹配的分支
|
||||
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));
|
||||
return std::make_tuple(IBasicRule::MatchResult::Part, std::get<1>(temp));
|
||||
}
|
||||
|
||||
QString Any::token_present() const
|
||||
|
|
@ -183,7 +183,7 @@ ExprRule::ExprRule(const QString& rule_name, int expr_mark) : name_store(rule_na
|
|||
this->mark_store = expr_mark;
|
||||
}
|
||||
|
||||
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const BaseRule> rule) {
|
||||
std::shared_ptr<const ExprRule> ExprRule::reloadRule(std::shared_ptr<const IBasicRule> rule) {
|
||||
auto ninst = makeCopy();
|
||||
ninst->child_store = rule;
|
||||
return ninst;
|
||||
|
|
@ -193,39 +193,39 @@ QString ExprRule::name() const { return name_store; }
|
|||
|
||||
int ExprRule::typeMark() const { return this->mark_store; }
|
||||
|
||||
QList<std::shared_ptr<const BaseRule>> ExprRule::children() const {
|
||||
return QList<std::shared_ptr<const BaseRule>>() << this->child_store;
|
||||
QList<std::shared_ptr<const IBasicRule>> ExprRule::children() const {
|
||||
return QList<std::shared_ptr<const IBasicRule>>() << this->child_store;
|
||||
}
|
||||
|
||||
#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();
|
||||
std::tuple<IBasicRule::MatchResult, std::shared_ptr<const IWordBase>>
|
||||
ExprRule::parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const IWordBase> head) const {
|
||||
std::shared_ptr<IExprInst> elm_ast = this->newEmptyInstance();
|
||||
auto text_present = this->token_present();
|
||||
|
||||
rt_inst->pushExpressionRule(this->shared_from_this());
|
||||
rt_inst->pushInst(elm_ast);
|
||||
rt_inst->pushExprRule(this->shared_from_this());
|
||||
rt_inst->pushExprInst(elm_ast);
|
||||
|
||||
auto rstg = child_store->parse(rt_inst, head);
|
||||
auto tokens_decl = elm_ast->tokens();
|
||||
|
||||
switch (std::get<0>(rstg)) {
|
||||
case BaseRule::MatchResult::Fail:
|
||||
case BaseRule::MatchResult::Part:
|
||||
rt_inst->popInst();
|
||||
rt_inst->popExpressionRule();
|
||||
case IBasicRule::MatchResult::Fail:
|
||||
case IBasicRule::MatchResult::Part:
|
||||
rt_inst->popExprInst();
|
||||
rt_inst->popExprRule();
|
||||
break;
|
||||
case BaseRule::MatchResult::Success: {
|
||||
case IBasicRule::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();
|
||||
rt_inst->popExprInst();
|
||||
rt_inst->popExprRule();
|
||||
|
||||
if (rt_inst->currentInst()) {
|
||||
rt_inst->currentInst()->addChild(elm_ast);
|
||||
if (rt_inst->currentExprInst()) {
|
||||
rt_inst->currentExprInst()->addChild(elm_ast);
|
||||
}
|
||||
else {
|
||||
rt_inst->appendDoc(elm_ast);
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
#include <tuple>
|
||||
#include <functional>
|
||||
namespace ast_basic {
|
||||
class ExprInst;
|
||||
class IExprInst;
|
||||
class ExpressionElement;
|
||||
}
|
||||
|
||||
namespace lib_syntax {
|
||||
class BaseRule;
|
||||
class IBasicRule;
|
||||
|
||||
/**
|
||||
* @brief 语法异常
|
||||
|
|
@ -31,9 +31,9 @@ namespace lib_syntax {
|
|||
/**
|
||||
* @brief 解析上下文接口
|
||||
*/
|
||||
class ParseContext {
|
||||
class IContext {
|
||||
public:
|
||||
virtual ~ParseContext() = default;
|
||||
virtual ~IContext() = default;
|
||||
|
||||
virtual void setCurrentFile(const QString &path) = 0;
|
||||
virtual QString currentFile() const = 0;
|
||||
|
|
@ -42,35 +42,35 @@ namespace lib_syntax {
|
|||
virtual QStringList errors() const = 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;
|
||||
virtual void appendDoc(std::shared_ptr<ast_basic::IExprInst> inst) = 0;
|
||||
virtual QList<std::shared_ptr<const ast_basic::IExprInst>> getDocs() const = 0;
|
||||
|
||||
/**
|
||||
* \brief 当前表达式元素.
|
||||
*
|
||||
* \return 返回当前表达式
|
||||
*/
|
||||
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<ast_basic::IExprInst> currentExprInst() const = 0;
|
||||
virtual void pushExprInst(std::shared_ptr<ast_basic::IExprInst> current_inst) = 0;
|
||||
virtual std::shared_ptr<ast_basic::IExprInst> popExprInst() = 0;
|
||||
|
||||
virtual std::shared_ptr<const BaseRule> currentExpressionRule() const = 0;
|
||||
virtual void pushExpressionRule(std::shared_ptr<const BaseRule> inst) = 0;
|
||||
virtual std::shared_ptr<const BaseRule> popExpressionRule() = 0;
|
||||
virtual QList<std::shared_ptr<const BaseRule>> currentExpressionRuleStack() const = 0;
|
||||
virtual std::shared_ptr<const IBasicRule> currentExprRule() const = 0;
|
||||
virtual void pushExprRule(std::shared_ptr<const IBasicRule> inst) = 0;
|
||||
virtual std::shared_ptr<const IBasicRule> popExprRule() = 0;
|
||||
virtual QList<std::shared_ptr<const IBasicRule>> currentExpressionRuleStack() const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 基础语法匹配规则接口
|
||||
*/
|
||||
class BaseRule {
|
||||
class IBasicRule {
|
||||
public:
|
||||
virtual ~BaseRule() = default;
|
||||
virtual ~IBasicRule() = default;
|
||||
/**
|
||||
* @brief 子规则
|
||||
* @return
|
||||
*/
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const = 0;
|
||||
virtual QList<std::shared_ptr<const IBasicRule>> children() const = 0;
|
||||
|
||||
/**
|
||||
* @brief 匹配结果
|
||||
|
|
@ -87,8 +87,8 @@ namespace lib_syntax {
|
|||
* @param head 列表头
|
||||
* @return 返回结果<匹配完成新列表头,匹配长度>
|
||||
*/
|
||||
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;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const = 0;
|
||||
|
||||
/**
|
||||
* 返回匹配语法规则的词法序列表达
|
||||
|
|
@ -102,75 +102,75 @@ namespace lib_syntax {
|
|||
/**
|
||||
* @brief token匹配
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT TokenMatch : public BaseRule, public std::enable_shared_from_this<TokenMatch> {
|
||||
class LIBSYNTAX_EXPORT TokenMatch : public IBasicRule, public std::enable_shared_from_this<TokenMatch> {
|
||||
private:
|
||||
std::shared_ptr<const lib_token::ITokenDefine> define_peer;
|
||||
|
||||
public:
|
||||
TokenMatch(std::shared_ptr<const lib_token::ITokenDefine> define);
|
||||
|
||||
// BaseRule interface
|
||||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
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 QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 语法规则或匹配
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT Any : public BaseRule, public std::enable_shared_from_this<Any> {
|
||||
class LIBSYNTAX_EXPORT Any : public IBasicRule, public std::enable_shared_from_this<Any> {
|
||||
private:
|
||||
QList<std::shared_ptr<const BaseRule>> mbrs_store;
|
||||
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
|
||||
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const BaseRule>, std::shared_ptr<const lib_token::IWordBase>> rule_select(std::shared_ptr<const lib_token::IWordBase> head) const;
|
||||
std::tuple<MatchResult, uint, std::shared_ptr<const IBasicRule>, std::shared_ptr<const lib_token::IWordBase>> rule_select(std::shared_ptr<const lib_token::IWordBase> head) const;
|
||||
|
||||
public:
|
||||
Any(const QList<std::shared_ptr<const BaseRule>> mbrs);
|
||||
Any(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
||||
|
||||
// BaseRule interface
|
||||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
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 QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 语法规则序列匹配
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT Seqs : public BaseRule, public std::enable_shared_from_this<Seqs> {
|
||||
class LIBSYNTAX_EXPORT Seqs : public IBasicRule, public std::enable_shared_from_this<Seqs> {
|
||||
private:
|
||||
QList<std::shared_ptr<const BaseRule>> mbrs_store;
|
||||
QList<std::shared_ptr<const IBasicRule>> mbrs_store;
|
||||
|
||||
public:
|
||||
Seqs(const QList<std::shared_ptr<const BaseRule>> mbrs);
|
||||
Seqs(const QList<std::shared_ptr<const IBasicRule>> mbrs);
|
||||
|
||||
// BaseRule interface
|
||||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
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 QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 语法规则重复匹配
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT Rept : public BaseRule, public std::enable_shared_from_this<Rept> {
|
||||
class LIBSYNTAX_EXPORT Rept : public IBasicRule, public std::enable_shared_from_this<Rept> {
|
||||
private:
|
||||
std::shared_ptr<const BaseRule> rule_peer;
|
||||
std::shared_ptr<const IBasicRule> rule_peer;
|
||||
int min_match, max_match;
|
||||
|
||||
public:
|
||||
Rept(std::shared_ptr<const BaseRule> rule, int min, int max);
|
||||
Rept(std::shared_ptr<const IBasicRule> rule, int min, int max);
|
||||
|
||||
// BaseRule interface
|
||||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const BaseRule>> children() const override;
|
||||
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 QList<std::shared_ptr<const IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
};
|
||||
|
||||
|
|
@ -178,27 +178,27 @@ namespace lib_syntax {
|
|||
/**
|
||||
* @brief 对应语法表达式解析规则
|
||||
*/
|
||||
class LIBSYNTAX_EXPORT ExprRule : public lib_syntax::BaseRule, public std::enable_shared_from_this<ExprRule> {
|
||||
class LIBSYNTAX_EXPORT ExprRule : public lib_syntax::IBasicRule, public std::enable_shared_from_this<ExprRule> {
|
||||
public:
|
||||
typedef QList<std::shared_ptr<const lib_token::IToken>> TokenSeqs;
|
||||
ExprRule(const QString& rule_name, int expr_mark);
|
||||
|
||||
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const BaseRule> rule);
|
||||
virtual std::shared_ptr<const ExprRule> reloadRule(std::shared_ptr<const IBasicRule> rule);
|
||||
virtual QString name() const;
|
||||
virtual int typeMark() const;
|
||||
|
||||
virtual std::shared_ptr<ast_basic::ExprInst> newEmptyInstance() const = 0;
|
||||
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const = 0;
|
||||
virtual std::shared_ptr<ExprRule> makeCopy() const = 0;
|
||||
|
||||
// BaseRule interface
|
||||
// IBasicRule interface
|
||||
public:
|
||||
virtual QList<std::shared_ptr<const lib_syntax::BaseRule>> children() const override;
|
||||
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 QList<std::shared_ptr<const lib_syntax::IBasicRule>> children() const override;
|
||||
virtual std::tuple<IBasicRule::MatchResult, std::shared_ptr<const lib_token::IWordBase>>
|
||||
parse(std::shared_ptr<IContext> rt_inst, std::shared_ptr<const lib_token::IWordBase> head) const override;
|
||||
virtual QString token_present() const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<const lib_syntax::BaseRule> child_store;
|
||||
std::shared_ptr<const lib_syntax::IBasicRule> child_store;
|
||||
QString name_store;
|
||||
int mark_store;
|
||||
};
|
||||
|
|
@ -212,8 +212,8 @@ namespace lib_syntax {
|
|||
ElementRule(const QString& rule_name, int expr_mark)
|
||||
:ExprRule(rule_name, expr_mark){}
|
||||
|
||||
virtual std::shared_ptr<ast_basic::ExprInst> newEmptyInstance() const {
|
||||
return std::dynamic_pointer_cast<ast_basic::ExprInst>(
|
||||
virtual std::shared_ptr<ast_basic::IExprInst> newEmptyInstance() const {
|
||||
return std::dynamic_pointer_cast<ast_basic::IExprInst>(
|
||||
std::make_shared<ExprType>(this->shared_from_this()));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ auto name_text = std::make_shared<NameSection>(); // ^
|
|||
// MatchRule
|
||||
#define MR(x) std::make_shared<const TokenMatch>(x)
|
||||
// Buffer
|
||||
#define Rules QList<std::shared_ptr<const BaseRule>>
|
||||
#define Rules QList<std::shared_ptr<const IBasicRule>>
|
||||
// Option
|
||||
#define OptMulT(token) std::make_shared<const Rept>(MR(token), 0, INT_MAX)
|
||||
#define OptMulR(rule) std::make_shared<const Rept>(rule, 0, INT_MAX)
|
||||
|
|
|
|||
Loading…
Reference in New Issue