Skip to content
Snippets Groups Projects
Commit 1f9bdf91 authored by vincent  lorrain's avatar vincent lorrain
Browse files

create a more general class for tokens and AST nodes that can be used for graph matching

parent cee31c9c
No related branches found
No related tags found
No related merge requests found
......@@ -60,48 +60,46 @@ enum class ConditionalTokenTypes
*/
class ConditionalToken
{
public:
/**
* @brief Token container
* @param type one of the token type
* @param lexeme String representing aditional information of the token
*/
ConditionalToken(const ConditionalTokenTypes type , const std::string lexeme );
/**
* @brief get the lexeme
* @return std::string
*/
const std::string getLexeme(void);
/**
* @brief get the token type
*
* @return ConditionalTokenTypes
*/
const ConditionalTokenTypes getType(void);
/**
* @brief copy the token
* @return deep copy of the token
*/
std::shared_ptr<Aidge::ConditionalToken> copy();
std::ostringstream rep(void);
private:
/**
* @brief additional information of the token
*/
const std::string mLexeme;
/**
* @brief type of the token
* @see ConditionalTokenTypes
*/
const ConditionalTokenTypes mType;
public:
/**
* @brief Token container
* @param type one of the token type
* @param lexeme String representing aditional information of the token
*/
ConditionalToken(const ConditionalTokenTypes type , const std::string lexeme );
/**
* @brief get the lexeme
* @return std::string
*/
const std::string getLexeme(void);
/**
* @brief get the token type
*
* @return ConditionalTokenTypes
*/
const ConditionalTokenTypes getType(void);
/**
* @brief copy the token
* @return deep copy of the token
*/
std::shared_ptr<Aidge::ConditionalToken> copy();
std::ostringstream rep(void);
private:
/**
* @brief additional information of the token
*/
const std::string mLexeme;
/**
* @brief type of the token
* @see ConditionalTokenTypes
*/
const ConditionalTokenTypes mType;
};
......
#ifndef _AIDGE_AST_NODE_H_
#define _AIDGE_AST_NODE_H_
#include <string>
#include <type_traits>
#include <vector>
#include <memory>
#include "utilsParsing/ParsingToken.hpp"
namespace Aidge{
template <typename EnumType>
class AstNode: public std::enable_shared_from_this<AstNode>
{
static_assert(std::is_enum<EnumType>::value, "AstNode EnumType must be an enum type");
public:
AstNode(std::shared_ptr<ParsingToken<EnumType>> token,std::vector<std::shared_ptr<AstNode>> child ={}):mToken(token),mChild(child){}
/**
* @brief get the type of the token
* @return the type
*/
EnumType getType() const{
return mToken->getType();
}
/**
* @brief get the lexeme of the token
* @return the lexeme
*/
std::string getValue() const{
return mToken->getLexeme();
}
/**
* @brief get the child of the node
* @return child
*/
const std::vector<std::shared_ptr<AstNode>>& getChilds() const {
return mChild;
}
/**
* @brief test if the node is a leaf in the tree
* @return true if a leaf
*/
bool isLeaf() const {
return mChild.size() == 0;
}
/**
* @brief get the number of child
* @return the number of child
*/
std::size_t nbChild() const{
return mChild.size();
}
private:
/**
* @brief the token of the node
*/
const std::shared_ptr<ParsingToken<EnumType>> mToken;
/**
* @brief list of child
*/
const std::vector<std::shared_ptr<AstNode>> mChild;
};
}
#endif //_AIDGE_AST_NODE_H_
#ifndef _AIDGE_PARSING_TOKEN_H_
#define _AIDGE_PARSING_TOKEN_H_
#include <string>
#include <type_traits>
namespace Aidge{
template <typename EnumType>
class ParsingToken: public std::enable_shared_from_this<ParsingToken>
{
static_assert(std::is_enum<EnumType>::value, "ParsingToken EnumType must be an enum type");
public:
/**
* @brief Token container
* @param type one of the token type
* @param lexeme String representing aditional information of the token
*/
ParsingToken(const EnumType type , const std::string lexeme )mLexeme(lexeme),mType(type){}
/**
* @brief get the lexeme
* @return std::string
*/
const std::string getLexeme(void){
return mLexeme;
}
/**
* @brief get the token type
*
* @return ParsingToken
*/
const EnumType getType(void){
return mType;
}
/**
* @brief copy the token
* @return deep copy of the token
*/
std::shared_ptr<Aidge::ParsingToken> copy();
//TODO
std::ostringstream rep(void){
std::ostringstream out;
out << " Token (" << mLexeme <<")" << "\n";
return out;
}
private:
/**
* @brief additional information of the token
*/
const std::string mLexeme;
/**
* @brief type of the token
* @see ConditionalTokenTypes
*/
const EnumType mType;
};
}
#endif //_AIDGE_PARSING_TOKEN_H_
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment