2016-09-02 10:04:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
|
2021-07-18 14:06:48 +00:00
|
|
|
* Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-09-02 10:04:42 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sexpr/sexpr_parser.h"
|
|
|
|
#include "sexpr/sexpr_exception.h"
|
|
|
|
#include <cctype>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdlib> /* strtod */
|
2016-09-02 10:04:42 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
2018-09-18 08:23:08 +00:00
|
|
|
#include <wx/file.h>
|
2020-12-12 18:11:51 +00:00
|
|
|
#include <wx/ffile.h>
|
2019-05-23 12:09:38 +00:00
|
|
|
|
2023-09-09 04:10:57 +00:00
|
|
|
#include <string_utils.h>
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
namespace SEXPR
|
|
|
|
{
|
|
|
|
const std::string PARSER::whitespaceCharacters = " \t\n\r\b\f\v";
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
PARSER::PARSER() : m_lineNumber( 1 )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PARSER::~PARSER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:38 +00:00
|
|
|
std::unique_ptr<SEXPR> PARSER::Parse( const std::string& aString )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
std::string::const_iterator it = aString.begin();
|
2017-02-02 07:58:35 +00:00
|
|
|
return parseString( aString, it );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:38 +00:00
|
|
|
std::unique_ptr<SEXPR> PARSER::ParseFromFile( const std::string& aFileName )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
std::string str = GetFileContents( aFileName );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
std::string::const_iterator it = str.begin();
|
2017-02-02 07:58:35 +00:00
|
|
|
return parseString( str, it );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
std::string PARSER::GetFileContents( const std::string &aFileName )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
|
2018-09-18 08:23:08 +00:00
|
|
|
// the filename is not always a UTF7 string, so do not use ifstream
|
|
|
|
// that do not work with unicode chars.
|
2023-09-09 04:10:57 +00:00
|
|
|
wxString fname( From_UTF8( aFileName.c_str() ) );
|
2020-12-12 18:11:51 +00:00
|
|
|
wxFFile file( fname, "rb" );
|
2018-09-18 08:23:08 +00:00
|
|
|
size_t length = file.Length();
|
2017-02-02 07:58:35 +00:00
|
|
|
|
2018-09-18 08:23:08 +00:00
|
|
|
if( length <= 0 )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2018-09-18 08:23:08 +00:00
|
|
|
throw PARSE_EXCEPTION( "Error occurred attempting to read in file or empty file" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-18 08:23:08 +00:00
|
|
|
str.resize( length );
|
|
|
|
file.Read( &str[0], str.length() );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:38 +00:00
|
|
|
std::unique_ptr<SEXPR> PARSER::parseString(
|
|
|
|
const std::string& aString, std::string::const_iterator& it )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
for( ; it != aString.end(); ++it )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( *it == '\n' )
|
2016-09-02 10:04:42 +00:00
|
|
|
m_lineNumber++;
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( whitespaceCharacters.find(*it) != std::string::npos )
|
2016-09-02 10:04:42 +00:00
|
|
|
continue;
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( *it == '(' )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
std::advance( it, 1 );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
2019-05-23 12:09:38 +00:00
|
|
|
auto list = std::make_unique<SEXPR_LIST>( m_lineNumber );
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
while( it != aString.end() && *it != ')' )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-01 04:53:16 +00:00
|
|
|
//there may be newlines in between atoms of a list, so detect these here
|
2017-02-02 07:58:35 +00:00
|
|
|
if( *it == '\n' )
|
2017-02-01 04:53:16 +00:00
|
|
|
m_lineNumber++;
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( whitespaceCharacters.find(*it) != std::string::npos )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
std::advance( it, 1 );
|
2016-09-02 10:04:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:38 +00:00
|
|
|
std::unique_ptr<SEXPR> item = parseString( aString, it );
|
|
|
|
list->AddChild( item.release() );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( it != aString.end() )
|
|
|
|
std::advance( it, 1 );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( *it == ')' )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( *it == '"' )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2021-12-08 23:50:33 +00:00
|
|
|
++it;
|
2018-05-14 11:28:12 +00:00
|
|
|
|
2021-12-08 23:50:33 +00:00
|
|
|
auto starting_it = it;
|
2016-09-02 10:04:42 +00:00
|
|
|
|
2021-12-08 23:50:33 +00:00
|
|
|
for( ; it != aString.end(); ++it )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2021-12-08 23:50:33 +00:00
|
|
|
auto ch = *it;
|
|
|
|
|
|
|
|
if( ch == '\\' )
|
|
|
|
{
|
|
|
|
// Skip the next escaped character
|
|
|
|
if( ++it == aString.end() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-02 10:04:42 +00:00
|
|
|
|
2021-12-08 23:50:33 +00:00
|
|
|
if( ch == '"' )
|
|
|
|
break;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2021-12-08 23:50:33 +00:00
|
|
|
|
|
|
|
if( it == aString.end() )
|
2016-09-02 10:04:42 +00:00
|
|
|
throw PARSE_EXCEPTION("missing closing quote");
|
2021-12-08 23:50:33 +00:00
|
|
|
|
|
|
|
auto str = std::make_unique<SEXPR_STRING>( std::string( starting_it, it ),
|
|
|
|
m_lineNumber );
|
|
|
|
|
|
|
|
++it;
|
|
|
|
return str;
|
|
|
|
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
size_t startPos = std::distance( aString.begin(), it );
|
|
|
|
size_t closingPos = aString.find_first_of( whitespaceCharacters + "()", startPos );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
std::string tmp = aString.substr( startPos, closingPos - startPos );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( closingPos != std::string::npos )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( tmp.find_first_not_of( "0123456789." ) == std::string::npos ||
|
|
|
|
( tmp.size() > 1 && tmp[0] == '-'
|
|
|
|
&& tmp.find_first_not_of( "0123456789.", 1 ) == std::string::npos ) )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2019-05-23 12:09:38 +00:00
|
|
|
std::unique_ptr<SEXPR> res;
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
if( tmp.find( '.' ) != std::string::npos )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2019-05-23 12:09:38 +00:00
|
|
|
res = std::make_unique<SEXPR_DOUBLE>(
|
|
|
|
strtod( tmp.c_str(), nullptr ), m_lineNumber );
|
2016-09-02 10:04:42 +00:00
|
|
|
//floating point type
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-23 12:09:38 +00:00
|
|
|
res = std::make_unique<SEXPR_INTEGER>(
|
|
|
|
strtoll( tmp.c_str(), nullptr, 0 ), m_lineNumber );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
std::advance( it, closingPos - startPos );
|
2016-09-02 10:04:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-23 12:09:38 +00:00
|
|
|
auto str = std::make_unique<SEXPR_SYMBOL>( tmp, m_lineNumber );
|
2017-02-02 07:58:35 +00:00
|
|
|
std::advance( it, closingPos - startPos );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw PARSE_EXCEPTION( "format error" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:38 +00:00
|
|
|
return nullptr;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|