Fix coding style of 'sexpr' code in kicad2step

This commit is contained in:
Cirilo Bernardo 2017-02-02 08:58:35 +01:00 committed by jean-pierre charras
parent 4a1e2644a4
commit 227ef64212
6 changed files with 442 additions and 381 deletions

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 QiEDA Developers
*
* 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
@ -31,7 +30,7 @@ namespace SEXPR
public:
virtual ~ISEXPRABLE() {}
virtual SEXPR* SerializeSEXPR() const { return nullptr; }
virtual void DeserializeSEXPR(SEXPR& sexp) {}
virtual void DeserializeSEXPR( SEXPR& sexp ) {}
};
}

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 QiEDA Developers
*
* 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
@ -25,94 +24,94 @@
namespace SEXPR
{
SEXPR::SEXPR(SEXPR_TYPE type, size_t lineNumber) :
m_type(type), m_lineNumber(lineNumber)
SEXPR::SEXPR( SEXPR_TYPE aType, size_t aLineNumber ) :
m_type( aType ), m_lineNumber( aLineNumber )
{
}
SEXPR::SEXPR(SEXPR_TYPE type) :
m_type(type), m_lineNumber(1)
SEXPR::SEXPR(SEXPR_TYPE aType) :
m_type( aType ), m_lineNumber( 1 )
{
}
SEXPR_VECTOR const * SEXPR::GetChildren() const
{
if (m_type != SEXPR_TYPE_LIST)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
}
return &static_cast<SEXPR_LIST const *>(this)->m_children;
return &static_cast< SEXPR_LIST const * >(this)->m_children;
}
SEXPR* SEXPR::GetChild(size_t idx) const
SEXPR* SEXPR::GetChild( size_t aIndex ) const
{
if (m_type != SEXPR_TYPE_LIST)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
}
return static_cast<SEXPR_LIST const *>(this)->m_children[idx];
return static_cast< SEXPR_LIST const * >(this)->m_children[aIndex];
}
void SEXPR::AddChild(SEXPR* child)
void SEXPR::AddChild( SEXPR* aChild )
{
if (m_type != SEXPR_TYPE_LIST)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
}
SEXPR_LIST* list = static_cast<SEXPR_LIST *>(this);
SEXPR_LIST* list = static_cast< SEXPR_LIST * >( this );
list->m_children.push_back(child);
list->m_children.push_back( aChild );
}
size_t SEXPR::GetNumberOfChildren() const
{
if (m_type != SEXPR_TYPE_LIST)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
}
return static_cast<SEXPR_LIST const *>(this)->m_children.size();
return static_cast< SEXPR_LIST const * >(this)->m_children.size();
}
std::string const & SEXPR::GetString() const
{
if (m_type != SEXPR_TYPE_ATOM_STRING)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_ATOM_STRING )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a string type!");
}
return static_cast<SEXPR_STRING const *>(this)->m_value;
return static_cast< SEXPR_STRING const * >(this)->m_value;
}
int32_t SEXPR::GetInteger() const
{
return static_cast<int>(GetLongInteger());
return static_cast< int >( GetLongInteger() );
}
int64_t SEXPR::GetLongInteger() const
{
if (m_type != SEXPR_TYPE_ATOM_INTEGER)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a integer type!");
}
return static_cast<SEXPR_INTEGER const *>(this)->m_value;
return static_cast< SEXPR_INTEGER const * >(this)->m_value;
}
double SEXPR::GetDouble() const
{
// we may end up parsing "intended" floats/doubles as ints
// so here we allow silent casting back to doubles
if (m_type == SEXPR_TYPE_ATOM_DOUBLE )
if( m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_DOUBLE )
{
return static_cast<SEXPR_DOUBLE const *>(this)->m_value;
return static_cast< SEXPR_DOUBLE const * >(this)->m_value;
}
else if( m_type == SEXPR_TYPE_ATOM_INTEGER )
else if( m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER )
{
return static_cast<SEXPR_INTEGER const *>(this)->m_value;
return static_cast< SEXPR_INTEGER const * >(this)->m_value;
}
else
{
@ -122,76 +121,78 @@ namespace SEXPR
float SEXPR::GetFloat() const
{
return static_cast<float>(GetDouble());
return static_cast< float >( GetDouble() );
}
std::string const & SEXPR::GetSymbol() const
{
if (m_type != SEXPR_TYPE_ATOM_SYMBOL)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_ATOM_SYMBOL )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a symbol type!");
}
return static_cast<SEXPR_SYMBOL const *>(this)->m_value;
return static_cast< SEXPR_SYMBOL const * >(this)->m_value;
}
SEXPR_LIST* SEXPR::GetList()
{
if (m_type != SEXPR_TYPE_LIST)
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
{
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
}
return static_cast<SEXPR_LIST*>(this);
return static_cast< SEXPR_LIST* >(this);
}
std::string SEXPR::AsString(size_t level)
std::string SEXPR::AsString( size_t aLevel )
{
std::string result;
if (IsList())
if( IsList() )
{
if (level != 0)
if( aLevel != 0 )
{
result = "\n";
}
result.append(level * 4, ' ');
level++;
result.append( aLevel* 4, ' ' );
aLevel++;
result += "(";
SEXPR_VECTOR const* list = GetChildren();
for (std::vector<SEXPR *>::const_iterator it = list->begin(); it != list->end(); ++it)
for( std::vector<SEXPR *>::const_iterator it = list->begin(); it != list->end(); ++it )
{
result += (*it)->AsString(level);
if (it != list->end()-1)
result += (*it)->AsString( aLevel );
if( it != list->end() - 1 )
{
result += " ";
}
}
result += ")";
level--;
aLevel--;
}
else if (IsString())
else if( IsString() )
{
result += "\"" + GetString() + "\"";
}
else if (IsSymbol())
else if( IsSymbol() )
{
result += GetSymbol();
}
else if (IsInteger())
else if( IsInteger() )
{
std::stringstream out;
out << GetInteger();
result += out.str();
}
else if (IsDouble())
else if( IsDouble() )
{
std::stringstream out;
out << std::setprecision(16) << GetDouble();
out << std::setprecision( 16 ) << GetDouble();
result += out.str();
}
@ -200,7 +201,7 @@ namespace SEXPR
SEXPR_LIST::~SEXPR_LIST()
{
for (auto child : m_children)
for( auto child : m_children )
{
delete child;
}
@ -208,109 +209,114 @@ namespace SEXPR
m_children.clear();
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, const ISEXPRABLE& obj)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, const ISEXPRABLE& obj )
{
SEXPR* sobj = obj.SerializeSEXPR();
list.AddChild(sobj);
list.AddChild( sobj );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, int64_t value)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, int64_t value )
{
list.AddChild(new SEXPR_INTEGER(value));
list.AddChild( new SEXPR_INTEGER( value ) );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, int32_t value)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, int32_t value )
{
list.AddChild(new SEXPR_INTEGER(value));
list.AddChild( new SEXPR_INTEGER( value ) );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, float value)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, float value )
{
list.AddChild(new SEXPR_DOUBLE(value));
list.AddChild( new SEXPR_DOUBLE( value ) );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, double value)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, double value )
{
list.AddChild(new SEXPR_DOUBLE(value));
list.AddChild( new SEXPR_DOUBLE( value ) );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, std::string value)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, std::string value )
{
list.AddChild(new SEXPR_STRING(value));
list.AddChild( new SEXPR_STRING( value ) );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, SEXPR* obj)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, SEXPR* obj )
{
list.AddChild(obj);
list.AddChild( obj );
return list;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, const _OUT_STRING setting)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, const _OUT_STRING setting )
{
SEXPR *res;
if (setting._Symbol)
if( setting._Symbol )
{
res = new SEXPR_SYMBOL(setting._String);
res = new SEXPR_SYMBOL( setting._String );
}
else
{
res = new SEXPR_STRING(setting._String);
res = new SEXPR_STRING( setting._String );
}
list.AddChild(res);
list.AddChild( res );
return list;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, ISEXPRABLE& obj)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, ISEXPRABLE& obj )
{
obj.DeserializeSEXPR(input);
obj.DeserializeSEXPR( input );
return input;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, int32_t& inte)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, int32_t& inte )
{
SEXPR* child = input.GetChild(input.m_inStreamChild);
if (child->IsInteger())
SEXPR* child = input.GetChild( input.m_inStreamChild );
if( child->IsInteger() )
{
inte = child->GetInteger();
input.m_inStreamChild++;
}
else
{
throw std::invalid_argument("SEXPR is not a integer type!");
throw std::invalid_argument( "SEXPR is not a integer type!" );
}
return input;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, std::string& str)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, std::string& str )
{
SEXPR* child = input.GetChild(input.m_inStreamChild);
if (child->IsString() || child->IsSymbol())
SEXPR* child = input.GetChild( input.m_inStreamChild );
if( child->IsString() || child->IsSymbol() )
{
str = child->GetString();
input.m_inStreamChild++;
}
else
{
throw std::invalid_argument("SEXPR is not a string type!");
throw std::invalid_argument( "SEXPR is not a string type!" );
}
return input;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, int64_t& lint)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, int64_t& lint )
{
SEXPR* child = input.GetChild(input.m_inStreamChild);
if (child->IsInteger())
SEXPR* child = input.GetChild( input.m_inStreamChild );
if( child->IsInteger() )
{
lint = child->GetLongInteger();
input.m_inStreamChild++;
@ -323,112 +329,115 @@ namespace SEXPR
return input;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, float& fl)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, float& fl )
{
SEXPR* child = input.GetChild(input.m_inStreamChild);
if (child->IsDouble())
SEXPR* child = input.GetChild( input.m_inStreamChild );
if( child->IsDouble() )
{
fl = child->GetFloat();
input.m_inStreamChild++;
}
else
{
throw std::invalid_argument("SEXPR is not a float type!");
throw std::invalid_argument( "SEXPR is not a float type!" );
}
return input;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, double& dbl)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, double& dbl )
{
SEXPR* child = input.GetChild(input.m_inStreamChild);
if (child->IsDouble())
SEXPR* child = input.GetChild( input.m_inStreamChild );
if( child->IsDouble() )
{
dbl = child->GetDouble();
input.m_inStreamChild++;
}
else
{
throw std::invalid_argument("SEXPR is not a double type!");
throw std::invalid_argument( "SEXPR is not a double type!" );
}
return input;
}
SEXPR_LIST& operator>> (SEXPR_LIST& input, const _IN_STRING is)
SEXPR_LIST& operator>> ( SEXPR_LIST& input, const _IN_STRING is )
{
SEXPR* child = input.GetChild(input.m_inStreamChild);
if (is._Symbol)
SEXPR* child = input.GetChild( input.m_inStreamChild );
if( is._Symbol )
{
if (child->IsSymbol())
if( child->IsSymbol() )
{
is._String = child->GetSymbol();
input.m_inStreamChild++;
}
else
{
throw std::invalid_argument("SEXPR is not a symbol type!");
throw std::invalid_argument( "SEXPR is not a symbol type!" );
}
}
else
{
if (child->IsString())
if( child->IsString() )
{
is._String = child->GetString();
input.m_inStreamChild++;
}
else
{
throw std::invalid_argument("SEXPR is not a string type!");
throw std::invalid_argument( "SEXPR is not a string type!" );
}
}
return input;
}
SEXPR_LIST& operator<< (SEXPR_LIST& list, SEXPR_LIST* list2)
SEXPR_LIST& operator<< ( SEXPR_LIST& list, SEXPR_LIST* list2 )
{
list.AddChild(list2);
list.AddChild( list2 );
return list;
}
size_t SEXPR_LIST::doScan(const SEXPR_SCAN_ARG *args, size_t num_args)
size_t SEXPR_LIST::doScan( const SEXPR_SCAN_ARG *args, size_t num_args )
{
size_t i = 0;
for (i = 0; i < num_args; i++)
for( i = 0; i < num_args; i++ )
{
SEXPR* child = GetChild(i);
SEXPR* child = GetChild( i );
const SEXPR_SCAN_ARG& arg = args[i];
try
{
if (arg.type == SEXPR_SCAN_ARG::Type::DOUBLE)
if( arg.type == SEXPR_SCAN_ARG::Type::DOUBLE )
{
*arg.u.dbl_value = child->GetDouble();
}
else if (arg.type == SEXPR_SCAN_ARG::Type::INT)
else if( arg.type == SEXPR_SCAN_ARG::Type::INT )
{
*arg.u.dbl_value = child->GetInteger();
}
else if (arg.type == SEXPR_SCAN_ARG::Type::STRING)
else if( arg.type == SEXPR_SCAN_ARG::Type::STRING )
{
if (child->IsSymbol())
if( child->IsSymbol() )
{
*arg.u.str_value = child->GetSymbol();
}
else if (child->IsString())
else if( child->IsString() )
{
*arg.u.str_value = child->GetString();
}
}
else if (arg.type == SEXPR_SCAN_ARG::Type::LONGINT)
else if( arg.type == SEXPR_SCAN_ARG::Type::LONGINT )
{
*arg.u.lint_value = child->GetLongInteger();
}
else if (arg.type == SEXPR_SCAN_ARG::Type::SEXPR_STRING)
else if( arg.type == SEXPR_SCAN_ARG::Type::SEXPR_STRING )
{
if (arg.u.sexpr_str->_Symbol)
if( arg.u.sexpr_str->_Symbol )
{
arg.u.sexpr_str->_String = child->GetSymbol();
}
@ -438,18 +447,18 @@ namespace SEXPR
}
}
else if (arg.type == SEXPR_SCAN_ARG::Type::STRING_COMP)
else if( arg.type == SEXPR_SCAN_ARG::Type::STRING_COMP )
{
if (child->IsSymbol())
if( child->IsSymbol() )
{
if (child->GetSymbol() != arg.str_value)
if( child->GetSymbol() != arg.str_value )
{
return i;
}
}
else if (child->IsString())
else if( child->IsString() )
{
if (child->GetString() != arg.str_value)
if( child->GetString() != arg.str_value )
{
return i;
}
@ -457,10 +466,10 @@ namespace SEXPR
}
else
{
throw std::invalid_argument("unsupported argument type, this shouldn't have happened");
throw std::invalid_argument( "unsupported argument type, this shouldn't have happened" );
}
}
catch (INVALID_TYPE_EXCEPTION)
catch( INVALID_TYPE_EXCEPTION )
{
return i;
}
@ -469,47 +478,48 @@ namespace SEXPR
return i;
}
void SEXPR_LIST::doAddChildren(const SEXPR_CHILDREN_ARG *args, size_t num_args)
void SEXPR_LIST::doAddChildren( const SEXPR_CHILDREN_ARG *args, size_t num_args )
{
size_t i = 0;
for (i = 0; i < num_args; i++)
for( i = 0; i < num_args; i++ )
{
const SEXPR_CHILDREN_ARG& arg = args[i];
if (arg.type == SEXPR_CHILDREN_ARG::Type::DOUBLE)
if( arg.type == SEXPR_CHILDREN_ARG::Type::DOUBLE )
{
AddChild(new SEXPR_DOUBLE(arg.u.dbl_value));
AddChild( new SEXPR_DOUBLE( arg.u.dbl_value ) );
}
else if (arg.type == SEXPR_CHILDREN_ARG::Type::INT)
else if( arg.type == SEXPR_CHILDREN_ARG::Type::INT )
{
AddChild(new SEXPR_INTEGER(arg.u.int_value));
AddChild( new SEXPR_INTEGER( arg.u.int_value ) );
}
else if (arg.type == SEXPR_CHILDREN_ARG::Type::LONGINT)
else if( arg.type == SEXPR_CHILDREN_ARG::Type::LONGINT )
{
AddChild(new SEXPR_INTEGER(arg.u.lint_value));
AddChild( new SEXPR_INTEGER( arg.u.lint_value ) );
}
else if (arg.type == SEXPR_CHILDREN_ARG::Type::STRING)
else if( arg.type == SEXPR_CHILDREN_ARG::Type::STRING )
{
AddChild(new SEXPR_STRING(arg.str_value));
AddChild( new SEXPR_STRING( arg.str_value ) );
}
else if (arg.type == SEXPR_CHILDREN_ARG::Type::SEXPR_ATOM)
else if( arg.type == SEXPR_CHILDREN_ARG::Type::SEXPR_ATOM )
{
AddChild(arg.u.sexpr_ptr);
AddChild( arg.u.sexpr_ptr );
}
else if (arg.type == SEXPR_CHILDREN_ARG::Type::SEXPR_STRING)
else if( arg.type == SEXPR_CHILDREN_ARG::Type::SEXPR_STRING )
{
if (arg.u.symbol)
if( arg.u.symbol )
{
AddChild(new SEXPR_SYMBOL(arg.str_value));
AddChild( new SEXPR_SYMBOL( arg.str_value ) );
}
else
{
AddChild(new SEXPR_STRING(arg.str_value));
AddChild( new SEXPR_STRING( arg.str_value ) );
}
}
else
{
throw std::invalid_argument("unexpected argument type, this shouldn't have happened");
throw std::invalid_argument( "unexpected argument type, this shouldn't have happened" );
}
}
}

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 QiEDA Developers
*
* 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
@ -28,216 +27,272 @@
namespace SEXPR
{
using std::int32_t;
using std::int64_t;
using std::int32_t;
using std::int64_t;
enum SEXPR_TYPE
{
SEXPR_TYPE_LIST,
SEXPR_TYPE_ATOM_INTEGER,
SEXPR_TYPE_ATOM_DOUBLE,
SEXPR_TYPE_ATOM_STRING,
SEXPR_TYPE_ATOM_SYMBOL,
};
enum class SEXPR_TYPE : char
{
SEXPR_TYPE_LIST,
SEXPR_TYPE_ATOM_INTEGER,
SEXPR_TYPE_ATOM_DOUBLE,
SEXPR_TYPE_ATOM_STRING,
SEXPR_TYPE_ATOM_SYMBOL,
};
typedef std::vector<class SEXPR *> SEXPR_VECTOR;
typedef std::vector< class SEXPR * > SEXPR_VECTOR;
class SEXPR
{
protected:
SEXPR_TYPE m_type;
SEXPR(SEXPR_TYPE type, size_t lineNumber);
SEXPR(SEXPR_TYPE type);
size_t m_lineNumber;
class SEXPR
{
protected:
SEXPR_TYPE m_type;
SEXPR( SEXPR_TYPE aType, size_t aLineNumber );
SEXPR( SEXPR_TYPE aType );
size_t m_lineNumber;
public:
virtual ~SEXPR() {};
bool IsList() const { return m_type == SEXPR_TYPE_LIST; }
bool IsSymbol() const { return m_type == SEXPR_TYPE_ATOM_SYMBOL; }
bool IsString() const { return m_type == SEXPR_TYPE_ATOM_STRING; }
bool IsDouble() const { return m_type == SEXPR_TYPE_ATOM_DOUBLE; }
bool IsInteger() const { return m_type == SEXPR_TYPE_ATOM_INTEGER; }
void AddChild(SEXPR* child);
SEXPR_VECTOR const * GetChildren() const;
SEXPR * GetChild(size_t idx) const;
size_t GetNumberOfChildren() const;
int64_t GetLongInteger() const;
int32_t GetInteger() const;
float GetFloat() const;
double GetDouble() const;
std::string const & GetString() const;
std::string const & GetSymbol() const;
SEXPR_LIST* GetList();
std::string AsString(size_t level = 0);
size_t GetLineNumber() { return m_lineNumber; }
};
public:
virtual ~SEXPR() {};
bool IsList() const { return m_type == SEXPR_TYPE::SEXPR_TYPE_LIST; }
bool IsSymbol() const { return m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_SYMBOL; }
bool IsString() const { return m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_STRING; }
bool IsDouble() const { return m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_DOUBLE; }
bool IsInteger() const { return m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER; }
void AddChild( SEXPR* aChild );
SEXPR_VECTOR const * GetChildren() const;
SEXPR * GetChild( size_t aIndex ) const;
size_t GetNumberOfChildren() const;
int64_t GetLongInteger() const;
int32_t GetInteger() const;
float GetFloat() const;
double GetDouble() const;
std::string const & GetString() const;
std::string const & GetSymbol() const;
SEXPR_LIST* GetList();
std::string AsString( size_t aLevel = 0);
size_t GetLineNumber() { return m_lineNumber; }
};
struct SEXPR_INTEGER : public SEXPR
{
int64_t m_value;
SEXPR_INTEGER(int64_t value) : SEXPR(SEXPR_TYPE_ATOM_INTEGER), m_value(value) {};
SEXPR_INTEGER(int64_t value, int lineNumber) : SEXPR(SEXPR_TYPE_ATOM_INTEGER, lineNumber), m_value(value) {};
};
struct SEXPR_INTEGER : public SEXPR
{
int64_t m_value;
struct SEXPR_DOUBLE : public SEXPR
{
double m_value;
SEXPR_DOUBLE(double value) : SEXPR(SEXPR_TYPE_ATOM_DOUBLE), m_value(value) {};
SEXPR_DOUBLE(double value, int lineNumber) : SEXPR(SEXPR_TYPE_ATOM_DOUBLE, lineNumber), m_value(value) {};
};
SEXPR_INTEGER( int64_t aValue ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER ), m_value( aValue ) {};
struct SEXPR_STRING : public SEXPR
{
std::string m_value;
SEXPR_STRING(std::string value) : SEXPR(SEXPR_TYPE_ATOM_STRING), m_value(value) {};
SEXPR_STRING(std::string value, int lineNumber) : SEXPR(SEXPR_TYPE_ATOM_STRING, lineNumber), m_value(value) {};
};
SEXPR_INTEGER( int64_t aValue, int aLineNumber ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER, aLineNumber ), m_value( aValue ) {};
};
struct SEXPR_SYMBOL : public SEXPR
{
std::string m_value;
SEXPR_SYMBOL(std::string value) : SEXPR(SEXPR_TYPE_ATOM_SYMBOL), m_value(value) {};
SEXPR_SYMBOL(std::string value, int lineNumber) : SEXPR(SEXPR_TYPE_ATOM_SYMBOL, lineNumber), m_value(value) {};
};
struct SEXPR_DOUBLE : public SEXPR
{
double m_value;
struct _OUT_STRING
{
bool _Symbol;
const std::string& _String;
};
SEXPR_DOUBLE( double aValue ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_DOUBLE ), m_value( aValue ) {};
inline _OUT_STRING AsSymbol(const std::string& str)
{
struct _OUT_STRING ret = { true, str };
return ret;
}
SEXPR_DOUBLE( double aValue, int aLineNumber ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_DOUBLE, aLineNumber ), m_value( aValue ) {};
};
inline _OUT_STRING AsString(const std::string& str)
{
struct _OUT_STRING ret = { false, str };
return ret;
}
struct SEXPR_STRING : public SEXPR
{
std::string m_value;
struct _IN_STRING
{
bool _Symbol;
std::string& _String;
SEXPR_STRING( std::string aValue ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_STRING ), m_value(aValue) {};
};
SEXPR_STRING( std::string aValue, int aLineNumber ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_STRING, aLineNumber ), m_value( aValue ) {};
};
inline _IN_STRING AsSymbol(std::string& str)
{
struct _IN_STRING ret = { true, str };
return ret;
}
struct SEXPR_SYMBOL : public SEXPR
{
std::string m_value;
inline _IN_STRING AsString(std::string& str)
{
struct _IN_STRING ret = { false, str };
return ret;
}
SEXPR_SYMBOL( std::string aValue ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_SYMBOL ), m_value( aValue ) {};
class SEXPR_SCAN_ARG {
friend class SEXPR_LIST;
public:
SEXPR_SCAN_ARG(int32_t* value) : type(INT) { u.int_value = value; }
SEXPR_SCAN_ARG(int64_t* value) : type(LONGINT) { u.lint_value = value; }
SEXPR_SCAN_ARG(double* value) : type(DOUBLE) { u.dbl_value = value; }
SEXPR_SCAN_ARG(std::string* value) : type(STRING) { u.str_value = value; }
SEXPR_SCAN_ARG(_IN_STRING& value) : type(SEXPR_STRING) { u.sexpr_str = &value; }
SEXPR_SCAN_ARG(const std::string* value) : type(STRING_COMP) { str_value = *value; }
SEXPR_SCAN_ARG(std::string value) : type(STRING_COMP) { str_value = value; }
SEXPR_SCAN_ARG(const char* value) : type(STRING_COMP) { str_value = value; }
SEXPR_SYMBOL( std::string aValue, int aLineNumber ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_ATOM_SYMBOL, aLineNumber ), m_value( aValue ) {};
};
private:
enum Type { INT, DOUBLE, STRING, LONGINT, STRING_COMP, SEXPR_STRING};
Type type;
union {
int64_t* lint_value;
int32_t* int_value;
double* dbl_value;
std::string* str_value;
_IN_STRING* sexpr_str;
} u;
struct _OUT_STRING
{
bool _Symbol;
const std::string& _String;
};
std::string str_value;
};
inline _OUT_STRING AsSymbol( const std::string& aString )
{
struct _OUT_STRING ret = { true, aString };
return ret;
}
class SEXPR_CHILDREN_ARG {
friend class SEXPR_LIST;
public:
SEXPR_CHILDREN_ARG(int32_t value) : type(INT) { u.int_value = value; }
SEXPR_CHILDREN_ARG(int64_t value) : type(LONGINT) { u.lint_value = value; }
SEXPR_CHILDREN_ARG(double value) : type(DOUBLE) { u.dbl_value = value; }
SEXPR_CHILDREN_ARG(std::string value) : type(STRING) { str_value = value; }
SEXPR_CHILDREN_ARG(const char* value) : type(STRING) { str_value = value; }
SEXPR_CHILDREN_ARG(const _OUT_STRING& value) : type(SEXPR_STRING) { str_value = value._String; u.symbol = value._Symbol; }
SEXPR_CHILDREN_ARG(SEXPR* ptr) : type(SEXPR_ATOM) { u.sexpr_ptr = ptr; }
inline _OUT_STRING AsString( const std::string& aString )
{
struct _OUT_STRING ret = { false, aString };
return ret;
}
private:
enum Type { INT, DOUBLE, STRING, LONGINT, SEXPR_STRING, SEXPR_ATOM };
Type type;
union {
int64_t lint_value;
int32_t int_value;
double dbl_value;
SEXPR* sexpr_ptr;
bool symbol;
} u;
std::string str_value;
};
struct _IN_STRING
{
bool _Symbol;
std::string& _String;
};
class SEXPR_LIST : public SEXPR
{
public:
SEXPR_LIST() : SEXPR(SEXPR_TYPE_LIST), m_inStreamChild(0) {};
SEXPR_LIST(int lineNumber) : SEXPR(SEXPR_TYPE_LIST, lineNumber), m_inStreamChild(0) {};
inline _IN_STRING AsSymbol( std::string& aString )
{
struct _IN_STRING ret = { true, aString };
return ret;
}
template <typename... Args>
SEXPR_LIST(const Args&... args) : SEXPR(SEXPR_TYPE_LIST), m_inStreamChild(0)
{
AddChildren(args...);
};
inline _IN_STRING AsString( std::string& aString )
{
struct _IN_STRING ret = { false, aString };
return ret;
}
SEXPR_VECTOR m_children;
class SEXPR_SCAN_ARG
{
friend class SEXPR_LIST;
template <typename... Args>
size_t Scan(const Args&... args)
{
SEXPR_SCAN_ARG arg_array[] = { args... };
return doScan(arg_array, sizeof...(Args));
}
public:
SEXPR_SCAN_ARG( int32_t* aValue ) :
type( Type::INT ) { u.int_value = aValue; }
template <typename... Args>
void AddChildren(const Args&... args)
{
SEXPR_CHILDREN_ARG arg_array[] = { args... };
doAddChildren(arg_array, sizeof...(Args));
}
SEXPR_SCAN_ARG( int64_t* aValue ) :
type( Type::LONGINT ) { u.lint_value = aValue; }
virtual ~SEXPR_LIST();
SEXPR_SCAN_ARG( double* aValue ) :
type( Type::DOUBLE ) { u.dbl_value = aValue; }
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, double value);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, float value);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, int64_t value);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, int32_t value);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, std::string value);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, const _OUT_STRING setting);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, const ISEXPRABLE& obj);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, SEXPR_LIST* list2);
friend SEXPR_LIST& operator<< (SEXPR_LIST& list, SEXPR* obj);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, ISEXPRABLE& obj);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, std::string& str);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, int32_t& inte);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, int64_t& inte);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, float& inte);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, double& inte);
friend SEXPR_LIST& operator>> (SEXPR_LIST& input, const _IN_STRING is);
private:
int m_inStreamChild;
size_t doScan(const SEXPR_SCAN_ARG *args, size_t num_args);
void doAddChildren(const SEXPR_CHILDREN_ARG *args, size_t num_args);
};
SEXPR_SCAN_ARG( std::string* aValue ) :
type( Type::STRING ) { u.str_value = aValue; }
SEXPR_SCAN_ARG( _IN_STRING& aValue ) :
type( Type::SEXPR_STRING ) { u.sexpr_str = &aValue; }
SEXPR_SCAN_ARG( const std::string* aValue ) :
type( Type::STRING_COMP ) { str_value = *aValue; }
SEXPR_SCAN_ARG( std::string aValue ) :
type( Type::STRING_COMP ) { str_value = aValue; }
SEXPR_SCAN_ARG( const char* aValue ) :
type( Type::STRING_COMP ) { str_value = aValue; }
private:
enum class Type : char { INT, DOUBLE, STRING, LONGINT, STRING_COMP, SEXPR_STRING };
Type type;
union
{
int64_t* lint_value;
int32_t* int_value;
double* dbl_value;
std::string* str_value;
_IN_STRING* sexpr_str;
} u;
std::string str_value;
};
class SEXPR_CHILDREN_ARG
{
friend class SEXPR_LIST;
public:
SEXPR_CHILDREN_ARG( int32_t aValue ) :
type( Type::INT ) { u.int_value = aValue; }
SEXPR_CHILDREN_ARG( int64_t aValue ) :
type( Type::LONGINT ) { u.lint_value = aValue; }
SEXPR_CHILDREN_ARG( double aValue ) :
type( Type::DOUBLE ) { u.dbl_value = aValue; }
SEXPR_CHILDREN_ARG( std::string aValue ) :
type( Type::STRING ) { str_value = aValue; }
SEXPR_CHILDREN_ARG( const char* aValue ) :
type( Type::STRING ) { str_value = aValue; }
SEXPR_CHILDREN_ARG( const _OUT_STRING& aValue ) :
type( Type::SEXPR_STRING ) { str_value = aValue._String; u.symbol = aValue._Symbol; }
SEXPR_CHILDREN_ARG( SEXPR* aPointer ) :
type( Type::SEXPR_ATOM ) { u.sexpr_ptr = aPointer; }
private:
enum class Type : char { INT, DOUBLE, STRING, LONGINT, SEXPR_STRING, SEXPR_ATOM };
Type type;
union
{
int64_t lint_value;
int32_t int_value;
double dbl_value;
SEXPR* sexpr_ptr;
bool symbol;
} u;
std::string str_value;
};
class SEXPR_LIST : public SEXPR
{
public:
SEXPR_LIST() : SEXPR( SEXPR_TYPE::SEXPR_TYPE_LIST ), m_inStreamChild( 0 ) {};
SEXPR_LIST( int aLineNumber ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_LIST, aLineNumber), m_inStreamChild( 0 ) {};
template <typename... Args>
SEXPR_LIST( const Args&... args ) :
SEXPR( SEXPR_TYPE::SEXPR_TYPE_LIST ), m_inStreamChild( 0 )
{
AddChildren(args...);
};
SEXPR_VECTOR m_children;
template <typename... Args>
size_t Scan( const Args&... args )
{
SEXPR_SCAN_ARG arg_array[] = { args... };
return doScan( arg_array, sizeof...( Args ) );
}
template <typename... Args>
void AddChildren( const Args&... args )
{
SEXPR_CHILDREN_ARG arg_array[] = { args... };
doAddChildren( arg_array, sizeof...( Args ) );
}
virtual ~SEXPR_LIST();
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, double value );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, float value );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, int64_t value );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, int32_t value );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, std::string value );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, const _OUT_STRING setting );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, const ISEXPRABLE& obj );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, SEXPR_LIST* list2 );
friend SEXPR_LIST& operator<< ( SEXPR_LIST& list, SEXPR* obj );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, ISEXPRABLE& obj );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, std::string& str );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, int32_t& inte );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, int64_t& inte );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, float& inte );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, double& inte );
friend SEXPR_LIST& operator>> ( SEXPR_LIST& input, const _IN_STRING is );
private:
int m_inStreamChild;
size_t doScan( const SEXPR_SCAN_ARG *args, size_t num_args );
void doAddChildren( const SEXPR_CHILDREN_ARG *args, size_t num_args );
};
}
#endif

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 QiEDA Developers
*
* 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
@ -27,7 +26,7 @@ namespace SEXPR
class PARSE_EXCEPTION : public std::exception
{
public:
PARSE_EXCEPTION(const std::string m) :msg(m) {}
PARSE_EXCEPTION( const std::string aMessage ) : msg( aMessage ) {}
const char* what() { return msg.c_str(); }
virtual ~PARSE_EXCEPTION() throw() {}
private:
@ -37,7 +36,7 @@ namespace SEXPR
class INVALID_TYPE_EXCEPTION : public std::exception
{
public:
INVALID_TYPE_EXCEPTION(const std::string m) :msg(m) {}
INVALID_TYPE_EXCEPTION( const std::string aMessage ) : msg( aMessage ) {}
const char* what() { return msg.c_str(); }
virtual ~INVALID_TYPE_EXCEPTION() throw() {}
private:

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 QiEDA Developers
*
* 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
@ -30,7 +29,7 @@ namespace SEXPR
{
const std::string PARSER::whitespaceCharacters = " \t\n\r\b\f\v";
PARSER::PARSER() : m_lineNumber(1)
PARSER::PARSER() : m_lineNumber( 1 )
{
}
@ -38,98 +37,94 @@ namespace SEXPR
{
}
SEXPR* PARSER::Parse(const std::string &aString)
SEXPR* PARSER::Parse( const std::string &aString )
{
std::string::const_iterator it = aString.begin();
return parseString(aString, it);
return parseString( aString, it );
}
SEXPR* PARSER::ParseFromFile(const std::string &aFileName)
SEXPR* PARSER::ParseFromFile( const std::string &aFileName )
{
std::string str = GetFileContents(aFileName);
std::string str = GetFileContents( aFileName );
std::string::const_iterator it = str.begin();
return parseString(str, it);
return parseString( str, it );
}
std::string PARSER::GetFileContents(const std::string &aFileName)
std::string PARSER::GetFileContents( const std::string &aFileName )
{
std::ifstream file(aFileName.c_str(), std::ios::binary);
std::ifstream file( aFileName.c_str(), std::ios::binary );
std::string str;
// Faster than automatic allocation
file.seekg(0, std::ios::end);
file.seekg( 0, std::ios::end );
auto length = file.tellg();
if (length < 0)
if( length < 0 )
{
throw PARSE_EXCEPTION("Error occurred attempting to read in file");
throw PARSE_EXCEPTION( "Error occurred attempting to read in file" );
}
str.resize(static_cast<size_t>(length));
file.seekg(0, std::ios::beg);
str.resize( static_cast<size_t>( length ) );
file.seekg( 0, std::ios::beg );
file.read(&str[0], str.length());
file.read( &str[0], str.length() );
return str;
}
SEXPR* PARSER::parseString(const std::string& aString, std::string::const_iterator& it)
SEXPR* PARSER::parseString( const std::string& aString, std::string::const_iterator& it )
{
std::string::const_iterator eit = aString.end();
for (; it != aString.end(); ++it)
{
if (*it == '\n')
{
m_lineNumber++;
}
if (whitespaceCharacters.find(*it) != std::string::npos)
for( ; it != aString.end(); ++it )
{
if( *it == '\n' )
m_lineNumber++;
if( whitespaceCharacters.find(*it) != std::string::npos )
continue;
if (*it == '(')
if( *it == '(' )
{
std::advance(it, 1);
std::advance( it, 1 );
SEXPR_LIST* list = new SEXPR_LIST(m_lineNumber);
while (it != aString.end() && *it != ')')
SEXPR_LIST* list = new SEXPR_LIST( m_lineNumber );
while( it != aString.end() && *it != ')' )
{
//there may be newlines in between atoms of a list, so detect these here
if (*it == '\n')
{
if( *it == '\n' )
m_lineNumber++;
}
if (whitespaceCharacters.find(*it) != std::string::npos)
if( whitespaceCharacters.find(*it) != std::string::npos )
{
std::advance(it, 1);
std::advance( it, 1 );
continue;
}
SEXPR* item = parseString(aString, it);
list->AddChild(item);
SEXPR* item = parseString( aString, it );
list->AddChild( item );
}
if (it != aString.end())
{
std::advance(it, 1);
}
if( it != aString.end() )
std::advance( it, 1 );
return list;
}
else if (*it == ')')
else if( *it == ')' )
{
return NULL;
}
else if (*it == '"')
else if( *it == '"' )
{
size_t startPos = std::distance(aString.begin(), it) + 1;
size_t closingPos = aString.find_first_of('"', startPos);
if (closingPos != std::string::npos)
if( closingPos != std::string::npos )
{
SEXPR_STRING* str = new SEXPR_STRING(aString.substr(startPos, closingPos - startPos),m_lineNumber);
std::advance(it, closingPos - startPos + 2);
SEXPR_STRING* str = new SEXPR_STRING(
aString.substr( startPos, closingPos - startPos ),m_lineNumber );
std::advance( it, closingPos - startPos + 2 );
return str;
}
@ -140,42 +135,45 @@ namespace SEXPR
}
else
{
size_t startPos = std::distance(aString.begin(), it);
size_t closingPos = aString.find_first_of(whitespaceCharacters+"()", startPos);
size_t startPos = std::distance( aString.begin(), it );
size_t closingPos = aString.find_first_of( whitespaceCharacters + "()", startPos );
std::string tmp = aString.substr(startPos, closingPos - startPos);
std::string tmp = aString.substr( startPos, closingPos - startPos );
if (closingPos != std::string::npos)
if( closingPos != std::string::npos )
{
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)
)
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 ) )
{
SEXPR* res;
if (tmp.find('.') != std::string::npos)
if( tmp.find( '.' ) != std::string::npos )
{
res = new SEXPR_DOUBLE(strtod(tmp.c_str(), NULL), m_lineNumber);
res = new SEXPR_DOUBLE( strtod( tmp.c_str(), NULL ), m_lineNumber );
//floating point type
}
else
{
res = new SEXPR_INTEGER(strtoll(tmp.c_str(), NULL, 0), m_lineNumber);
res = new SEXPR_INTEGER(
strtoll( tmp.c_str(), NULL, 0 ), m_lineNumber );
}
std::advance(it, closingPos - startPos);
std::advance( it, closingPos - startPos );
return res;
}
else
{
SEXPR_SYMBOL* str = new SEXPR_SYMBOL(tmp, m_lineNumber);
std::advance(it, closingPos - startPos);
SEXPR_SYMBOL* str = new SEXPR_SYMBOL( tmp, m_lineNumber );
std::advance( it, closingPos - startPos );
return str;
}
}
else
{
throw PARSE_EXCEPTION("format error");
throw PARSE_EXCEPTION( "format error" );
}
}
}

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 QiEDA Developers
*
* 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
@ -31,11 +30,12 @@ namespace SEXPR
public:
PARSER();
~PARSER();
SEXPR* Parse(const std::string &aString);
SEXPR* ParseFromFile(const std::string &filename);
static std::string GetFileContents(const std::string &filename);
SEXPR* Parse( const std::string &aString );
SEXPR* 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);
SEXPR* parseString( const std::string& aString, std::string::const_iterator& it );
static const std::string whitespaceCharacters;
int m_lineNumber;
};