KiCad PCB EDA Suite
SEXPR::PARSER Class Reference

#include <sexpr_parser.h>

Public Member Functions

 PARSER ()
 
 ~PARSER ()
 
std::unique_ptr< SEXPRParse (const std::string &aString)
 
std::unique_ptr< SEXPRParseFromFile (const std::string &aFilename)
 

Static Public Member Functions

static std::string GetFileContents (const std::string &aFilename)
 

Private Member Functions

std::unique_ptr< SEXPRparseString (const std::string &aString, std::string::const_iterator &it)
 

Private Attributes

int m_lineNumber
 

Static Private Attributes

static const std::string whitespaceCharacters = " \t\n\r\b\f\v"
 

Detailed Description

Definition at line 30 of file sexpr_parser.h.

Constructor & Destructor Documentation

◆ PARSER()

SEXPR::PARSER::PARSER ( )

Definition at line 36 of file sexpr_parser.cpp.

36  : m_lineNumber( 1 )
37  {
38  }

◆ ~PARSER()

SEXPR::PARSER::~PARSER ( )

Definition at line 40 of file sexpr_parser.cpp.

41  {
42  }

Member Function Documentation

◆ GetFileContents()

std::string SEXPR::PARSER::GetFileContents ( const std::string &  aFilename)
static

Definition at line 58 of file sexpr_parser.cpp.

59  {
60  std::string str;
61 
62  // the filename is not always a UTF7 string, so do not use ifstream
63  // that do not work with unicode chars.
64  wxString fname( FROM_UTF8( aFileName.c_str() ) );
65  wxFile file( fname );
66  size_t length = file.Length();
67 
68  if( length <= 0 )
69  {
70  throw PARSE_EXCEPTION( "Error occurred attempting to read in file or empty file" );
71  }
72 
73 
74  str.resize( length );
75  file.Read( &str[0], str.length() );
76 
77  return str;
78  }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:62

References FROM_UTF8().

Referenced by ParseFromFile().

◆ Parse()

std::unique_ptr< SEXPR > SEXPR::PARSER::Parse ( const std::string &  aString)

Definition at line 44 of file sexpr_parser.cpp.

45  {
46  std::string::const_iterator it = aString.begin();
47  return parseString( aString, it );
48  }
std::unique_ptr< SEXPR > parseString(const std::string &aString, std::string::const_iterator &it)

References parseString().

Referenced by TEST_SEXPR_PARSER_FIXTURE::Parse(), and QA_SEXPR_PARSER::Parse().

◆ ParseFromFile()

std::unique_ptr< SEXPR > SEXPR::PARSER::ParseFromFile ( const std::string &  aFilename)

Definition at line 50 of file sexpr_parser.cpp.

51  {
52  std::string str = GetFileContents( aFileName );
53 
54  std::string::const_iterator it = str.begin();
55  return parseString( str, it );
56  }
std::unique_ptr< SEXPR > parseString(const std::string &aString, std::string::const_iterator &it)
static std::string GetFileContents(const std::string &aFilename)

References GetFileContents(), and parseString().

◆ parseString()

std::unique_ptr< SEXPR > SEXPR::PARSER::parseString ( const std::string &  aString,
std::string::const_iterator &  it 
)
private

Definition at line 80 of file sexpr_parser.cpp.

82  {
83  for( ; it != aString.end(); ++it )
84  {
85  if( *it == '\n' )
86  m_lineNumber++;
87 
88  if( whitespaceCharacters.find(*it) != std::string::npos )
89  continue;
90 
91  if( *it == '(' )
92  {
93  std::advance( it, 1 );
94 
95  auto list = std::make_unique<SEXPR_LIST>( m_lineNumber );
96 
97  while( it != aString.end() && *it != ')' )
98  {
99  //there may be newlines in between atoms of a list, so detect these here
100  if( *it == '\n' )
101  m_lineNumber++;
102 
103  if( whitespaceCharacters.find(*it) != std::string::npos )
104  {
105  std::advance( it, 1 );
106  continue;
107  }
108 
109  std::unique_ptr<SEXPR> item = parseString( aString, it );
110  list->AddChild( item.release() );
111  }
112 
113  if( it != aString.end() )
114  std::advance( it, 1 );
115 
116  return list;
117  }
118  else if( *it == ')' )
119  {
120  return NULL;
121  }
122  else if( *it == '"' )
123  {
124  size_t startPos = std::distance(aString.begin(), it) + 1;
125  size_t closingPos = startPos > 0 ? startPos - 1 : startPos;
126 
127  // find the closing quote character, be sure it is not escaped
128  do
129  {
130  closingPos = aString.find_first_of( '"', closingPos + 1 );
131  }
132  while( closingPos != std::string::npos
133  && ( closingPos > 0 && aString[closingPos - 1] == '\\' ) );
134 
135  if( closingPos != std::string::npos )
136  {
137  auto str = std::make_unique<SEXPR_STRING>(
138  aString.substr( startPos, closingPos - startPos ), m_lineNumber );
139  std::advance( it, closingPos - startPos + 2 );
140 
141  return str;
142  }
143  else
144  {
145  throw PARSE_EXCEPTION("missing closing quote");
146  }
147  }
148  else
149  {
150  size_t startPos = std::distance( aString.begin(), it );
151  size_t closingPos = aString.find_first_of( whitespaceCharacters + "()", startPos );
152 
153  std::string tmp = aString.substr( startPos, closingPos - startPos );
154 
155 
156  if( closingPos != std::string::npos )
157  {
158  if( tmp.find_first_not_of( "0123456789." ) == std::string::npos ||
159  ( tmp.size() > 1 && tmp[0] == '-'
160  && tmp.find_first_not_of( "0123456789.", 1 ) == std::string::npos ) )
161  {
162  std::unique_ptr<SEXPR> res;
163 
164  if( tmp.find( '.' ) != std::string::npos )
165  {
166  res = std::make_unique<SEXPR_DOUBLE>(
167  strtod( tmp.c_str(), nullptr ), m_lineNumber );
168  //floating point type
169  }
170  else
171  {
172  res = std::make_unique<SEXPR_INTEGER>(
173  strtoll( tmp.c_str(), nullptr, 0 ), m_lineNumber );
174  }
175 
176  std::advance( it, closingPos - startPos );
177  return res;
178  }
179  else
180  {
181  auto str = std::make_unique<SEXPR_SYMBOL>( tmp, m_lineNumber );
182  std::advance( it, closingPos - startPos );
183 
184  return str;
185  }
186  }
187  else
188  {
189  throw PARSE_EXCEPTION( "format error" );
190  }
191  }
192  }
193 
194  return nullptr;
195  }
#define NULL
std::unique_ptr< SEXPR > parseString(const std::string &aString, std::string::const_iterator &it)
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
static const std::string whitespaceCharacters
Definition: sexpr_parser.h:42

References distance(), m_lineNumber, NULL, and whitespaceCharacters.

Referenced by Parse(), and ParseFromFile().

Member Data Documentation

◆ m_lineNumber

int SEXPR::PARSER::m_lineNumber
private

Definition at line 43 of file sexpr_parser.h.

Referenced by parseString().

◆ whitespaceCharacters

const std::string SEXPR::PARSER::whitespaceCharacters = " \t\n\r\b\f\v"
staticprivate

Definition at line 42 of file sexpr_parser.h.

Referenced by parseString().


The documentation for this class was generated from the following files: