From 1f9bdf91b490d24dfd15eabe639312a4b630fc75 Mon Sep 17 00:00:00 2001 From: vl241552 <vincent.lorrain@cea.fr> Date: Tue, 25 Jul 2023 14:15:11 +0000 Subject: [PATCH] create a more general class for tokens and AST nodes that can be used for graph matching --- .../include/nodeTester/ConditionalLexer.hpp | 82 +++++++++---------- aidge/_Core/include/utilsParsing/AstNode.hpp | 69 ++++++++++++++++ .../include/utilsParsing/ParsingToken.hpp | 66 +++++++++++++++ 3 files changed, 175 insertions(+), 42 deletions(-) create mode 100644 aidge/_Core/include/utilsParsing/AstNode.hpp create mode 100644 aidge/_Core/include/utilsParsing/ParsingToken.hpp diff --git a/aidge/_Core/include/nodeTester/ConditionalLexer.hpp b/aidge/_Core/include/nodeTester/ConditionalLexer.hpp index f21e0f15..286244db 100644 --- a/aidge/_Core/include/nodeTester/ConditionalLexer.hpp +++ b/aidge/_Core/include/nodeTester/ConditionalLexer.hpp @@ -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; }; diff --git a/aidge/_Core/include/utilsParsing/AstNode.hpp b/aidge/_Core/include/utilsParsing/AstNode.hpp new file mode 100644 index 00000000..28d17a54 --- /dev/null +++ b/aidge/_Core/include/utilsParsing/AstNode.hpp @@ -0,0 +1,69 @@ + + +#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_ diff --git a/aidge/_Core/include/utilsParsing/ParsingToken.hpp b/aidge/_Core/include/utilsParsing/ParsingToken.hpp new file mode 100644 index 00000000..78045cf3 --- /dev/null +++ b/aidge/_Core/include/utilsParsing/ParsingToken.hpp @@ -0,0 +1,66 @@ + +#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 -- GitLab