185 lines
5.3 KiB
C
185 lines
5.3 KiB
C
|
#pragma once
|
|||
|
|
|||
|
#include "libtokens.h"
|
|||
|
#include "ast_basic.h"
|
|||
|
|
|||
|
namespace lib_token {
|
|||
|
|
|||
|
template<typename ExprT>
|
|||
|
using TokenProcs = void(*)(std::shared_ptr<ExprT> expr, std::shared_ptr<const lib_token::IToken> t);
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>tokenʵ<6E><CAB5> ===================================================
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
|
|||
|
*/
|
|||
|
class TokenContent : public IToken {
|
|||
|
private:
|
|||
|
int row_n, col_n, doc_offset;
|
|||
|
QString text_n, path_p;
|
|||
|
std::shared_ptr<const ITokenDefine> type_def;
|
|||
|
|
|||
|
public:
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param r <EFBFBD><EFBFBD>
|
|||
|
* @param c <EFBFBD><EFBFBD>
|
|||
|
* @param pos λ<EFBFBD><EFBFBD>
|
|||
|
* @param t <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param p path
|
|||
|
* @param paramType <EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
TokenContent(int r, int c, uint64_t pos, const QString& t, const QString& p, std::shared_ptr<const ITokenDefine> paramType);
|
|||
|
|
|||
|
// WordBase interface
|
|||
|
public:
|
|||
|
virtual QString file() const override;
|
|||
|
virtual uint64_t position() const override;
|
|||
|
virtual QString content() const override;
|
|||
|
virtual int row() const override;
|
|||
|
virtual int column() const override;
|
|||
|
|
|||
|
// Token interface
|
|||
|
public:
|
|||
|
virtual std::shared_ptr<const ITokenDefine> defines() const override;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Token
|
|||
|
*/
|
|||
|
template<typename ExprT, TokenProcs<ExprT> proc>
|
|||
|
class ActionToken : public IActionToken {
|
|||
|
private:
|
|||
|
std::shared_ptr<const IToken> _bind_content;
|
|||
|
std::shared_ptr<const IActionToken> _bind_previous;
|
|||
|
|
|||
|
public:
|
|||
|
ActionToken(std::shared_ptr<const IToken> content, std::shared_ptr<const IActionToken> prev)
|
|||
|
: _bind_content(content), _bind_previous(prev) { }
|
|||
|
|
|||
|
virtual std::shared_ptr<ast_basic::IExprInstance> makeSure(std::shared_ptr<ast_basic::IExprInstance> expr) {
|
|||
|
auto expr_inst = std::dynamic_pointer_cast<ExprT>(expr);
|
|||
|
proc(expr_inst, shared_from_this());
|
|||
|
return expr;
|
|||
|
}
|
|||
|
|
|||
|
// ͨ<><CDA8> IActionToken <20>̳<EFBFBD>
|
|||
|
QString file() const override {
|
|||
|
return _bind_content->file();
|
|||
|
}
|
|||
|
uint64_t position() const override {
|
|||
|
return _bind_content->position();
|
|||
|
}
|
|||
|
QString content() const override {
|
|||
|
return _bind_content->content();
|
|||
|
}
|
|||
|
int row() const override {
|
|||
|
return _bind_content->row();
|
|||
|
}
|
|||
|
int column() const override {
|
|||
|
return _bind_content->column();
|
|||
|
}
|
|||
|
std::shared_ptr<const ITokenDefine> defines() const override {
|
|||
|
return _bind_content->defines();
|
|||
|
}
|
|||
|
std::shared_ptr<const IActionToken> prevToken() const override {
|
|||
|
return this->_bind_previous;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>ʼtoken
|
|||
|
*/
|
|||
|
template<typename NExpr> class ExprBeginToken : public IActionToken {
|
|||
|
private:
|
|||
|
std::shared_ptr<const IActionToken> _prev_token = nullptr;
|
|||
|
std::shared_ptr<const lib_syntax::ExprRule> _self_rule = nullptr;
|
|||
|
|
|||
|
std::shared_ptr<ast_basic::IExprInstance> _parent_expr = nullptr;
|
|||
|
std::shared_ptr<NExpr> _self_expr = nullptr;
|
|||
|
|
|||
|
public:
|
|||
|
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(){
|
|||
|
return this->_parent_expr;
|
|||
|
}
|
|||
|
|
|||
|
// ͨ<><CDA8> IActionToken <20>̳<EFBFBD>
|
|||
|
QString file() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprBeginToken");
|
|||
|
}
|
|||
|
|
|||
|
uint64_t position() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprBeginToken");
|
|||
|
}
|
|||
|
|
|||
|
QString content() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprBeginToken");
|
|||
|
}
|
|||
|
|
|||
|
int row() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprBeginToken");
|
|||
|
}
|
|||
|
|
|||
|
int column() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprBeginToken");
|
|||
|
}
|
|||
|
|
|||
|
std::shared_ptr<const ITokenDefine> defines() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprBeginToken");
|
|||
|
}
|
|||
|
|
|||
|
std::shared_ptr<const IActionToken> prevToken() const override {
|
|||
|
return _prev_token;
|
|||
|
}
|
|||
|
|
|||
|
std::shared_ptr<ast_basic::IExprInstance> makeSure(std::shared_ptr<ast_basic::IExprInstance> expr) override {
|
|||
|
this->_parent_expr = expr;
|
|||
|
this->_self_expr = std::make_shared<NExpr>(_self_rule);
|
|||
|
this->_parent_expr->addChild(this->_self_expr);
|
|||
|
return this->_self_expr;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>ֹtoken
|
|||
|
*/
|
|||
|
template<typename ExprT> 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){ }
|
|||
|
|
|||
|
// ͨ<><CDA8> IActionToken <20>̳<EFBFBD>
|
|||
|
QString file() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprEndToken");
|
|||
|
}
|
|||
|
uint64_t position() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprEndToken");
|
|||
|
}
|
|||
|
QString content() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprEndToken");
|
|||
|
}
|
|||
|
int row() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprEndToken");
|
|||
|
}
|
|||
|
int column() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprEndToken");
|
|||
|
}
|
|||
|
std::shared_ptr<const ITokenDefine> defines() const override {
|
|||
|
throw new lib_token::TokenException(u8"<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>ExprEndToken");
|
|||
|
}
|
|||
|
std::shared_ptr<const IActionToken> prevToken() const override {
|
|||
|
return _prev_token;
|
|||
|
}
|
|||
|
std::shared_ptr<ast_basic::IExprInstance> makeSure(std::shared_ptr<ast_basic::IExprInstance> expr) override {
|
|||
|
return this->_self_start->parentExpr();
|
|||
|
}
|
|||
|
};
|
|||
|
}
|