diff --git a/libs/sexpr/include/sexpr/sexpr_parser.h b/libs/sexpr/include/sexpr/sexpr_parser.h index 8781810a44..f918619960 100644 --- a/libs/sexpr/include/sexpr/sexpr_parser.h +++ b/libs/sexpr/include/sexpr/sexpr_parser.h @@ -19,6 +19,8 @@ #define SEXPR_PARSER_H_ #include "sexpr/sexpr.h" + +#include #include #include @@ -30,12 +32,13 @@ namespace SEXPR public: PARSER(); ~PARSER(); - SEXPR* Parse( const std::string &aString ); - SEXPR* ParseFromFile( const std::string &aFilename ); + std::unique_ptr Parse( const std::string& aString ); + std::unique_ptr ParseFromFile( const std::string& aFilename ); static std::string GetFileContents( const std::string &aFilename ); private: - SEXPR* parseString( const std::string& aString, std::string::const_iterator& it ); + std::unique_ptr parseString( + const std::string& aString, std::string::const_iterator& it ); static const std::string whitespaceCharacters; int m_lineNumber; }; diff --git a/libs/sexpr/sexpr_parser.cpp b/libs/sexpr/sexpr_parser.cpp index fa0a21432d..3c3decd784 100644 --- a/libs/sexpr/sexpr_parser.cpp +++ b/libs/sexpr/sexpr_parser.cpp @@ -26,7 +26,9 @@ #include #include #include + #include +#include namespace SEXPR { @@ -40,13 +42,13 @@ namespace SEXPR { } - SEXPR* PARSER::Parse( const std::string &aString ) + std::unique_ptr PARSER::Parse( const std::string& aString ) { std::string::const_iterator it = aString.begin(); return parseString( aString, it ); } - SEXPR* PARSER::ParseFromFile( const std::string &aFileName ) + std::unique_ptr PARSER::ParseFromFile( const std::string& aFileName ) { std::string str = GetFileContents( aFileName ); @@ -76,7 +78,8 @@ namespace SEXPR return str; } - SEXPR* PARSER::parseString( const std::string& aString, std::string::const_iterator& it ) + std::unique_ptr PARSER::parseString( + const std::string& aString, std::string::const_iterator& it ) { for( ; it != aString.end(); ++it ) { @@ -90,7 +93,8 @@ namespace SEXPR { std::advance( it, 1 ); - SEXPR_LIST* list = new SEXPR_LIST( m_lineNumber ); + auto list = std::make_unique( m_lineNumber ); + while( it != aString.end() && *it != ')' ) { //there may be newlines in between atoms of a list, so detect these here @@ -103,8 +107,8 @@ namespace SEXPR continue; } - SEXPR* item = parseString( aString, it ); - list->AddChild( item ); + std::unique_ptr item = parseString( aString, it ); + list->AddChild( item.release() ); } if( it != aString.end() ) @@ -131,8 +135,8 @@ namespace SEXPR if( closingPos != std::string::npos ) { - SEXPR_STRING* str = new SEXPR_STRING( - aString.substr( startPos, closingPos - startPos ),m_lineNumber ); + auto str = std::make_unique( + aString.substr( startPos, closingPos - startPos ), m_lineNumber ); std::advance( it, closingPos - startPos + 2 ); return str; @@ -156,17 +160,18 @@ namespace SEXPR ( tmp.size() > 1 && tmp[0] == '-' && tmp.find_first_not_of( "0123456789.", 1 ) == std::string::npos ) ) { - SEXPR* res; + std::unique_ptr res; if( tmp.find( '.' ) != std::string::npos ) { - res = new SEXPR_DOUBLE( strtod( tmp.c_str(), NULL ), m_lineNumber ); + res = std::make_unique( + strtod( tmp.c_str(), nullptr ), m_lineNumber ); //floating point type } else { - res = new SEXPR_INTEGER( - strtoll( tmp.c_str(), NULL, 0 ), m_lineNumber ); + res = std::make_unique( + strtoll( tmp.c_str(), nullptr, 0 ), m_lineNumber ); } std::advance( it, closingPos - startPos ); @@ -174,7 +179,7 @@ namespace SEXPR } else { - SEXPR_SYMBOL* str = new SEXPR_SYMBOL( tmp, m_lineNumber ); + auto str = std::make_unique( tmp, m_lineNumber ); std::advance( it, closingPos - startPos ); return str; @@ -187,6 +192,6 @@ namespace SEXPR } } - return NULL; + return nullptr; } }