2016-09-02 10:04:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
|
2018-09-18 08:23:08 +00:00
|
|
|
* Copyright (C) 2018 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>
|
|
|
|
#include <iterator>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <stdlib.h> /* strtod */
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
2018-09-18 08:23:08 +00:00
|
|
|
#include <wx/file.h>
|
|
|
|
#include <macros.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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
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.
|
|
|
|
wxString fname( FROM_UTF8( aFileName.c_str() ) );
|
|
|
|
wxFile file( fname );
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
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
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST* list = new SEXPR_LIST( m_lineNumber );
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* item = parseString( aString, it );
|
|
|
|
list->AddChild( item );
|
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
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( *it == '"' )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
size_t startPos = std::distance(aString.begin(), it) + 1;
|
2018-05-14 11:28:12 +00:00
|
|
|
size_t closingPos = startPos > 0 ? startPos - 1 : startPos;
|
|
|
|
|
|
|
|
// find the closing quote character, be sure it is not escaped
|
|
|
|
do
|
|
|
|
{
|
|
|
|
closingPos = aString.find_first_of( '"', closingPos + 1 );
|
|
|
|
}
|
|
|
|
while( closingPos != std::string::npos
|
|
|
|
&& ( closingPos > 0 && aString[closingPos - 1] == '\\' ) );
|
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
|
|
|
SEXPR_STRING* str = new SEXPR_STRING(
|
|
|
|
aString.substr( startPos, closingPos - startPos ),m_lineNumber );
|
|
|
|
std::advance( it, closingPos - startPos + 2 );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw PARSE_EXCEPTION("missing closing quote");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
SEXPR* res;
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
if( tmp.find( '.' ) != std::string::npos )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
res = new SEXPR_DOUBLE( strtod( tmp.c_str(), NULL ), m_lineNumber );
|
2016-09-02 10:04:42 +00:00
|
|
|
//floating point type
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
res = new SEXPR_INTEGER(
|
|
|
|
strtoll( tmp.c_str(), NULL, 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
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_SYMBOL* str = new SEXPR_SYMBOL( tmp, m_lineNumber );
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|