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 31 of file sexpr_parser.h.

Constructor & Destructor Documentation

◆ PARSER()

PARSER::PARSER ( )

Definition at line 37 of file sexpr_parser.cpp.

37  : m_lineNumber( 1 )
38  {
39  }

◆ ~PARSER()

PARSER::~PARSER ( )

Definition at line 41 of file sexpr_parser.cpp.

42  {
43  }

Member Function Documentation

◆ GetFileContents()

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

Definition at line 59 of file sexpr_parser.cpp.

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

References FROM_UTF8().

Referenced by ParseFromFile().

◆ Parse()

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

Definition at line 45 of file sexpr_parser.cpp.

46  {
47  std::string::const_iterator it = aString.begin();
48  return parseString( aString, it );
49  }
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 > PARSER::ParseFromFile ( const std::string &  aFilename)

Definition at line 51 of file sexpr_parser.cpp.

52  {
53  std::string str = GetFileContents( aFileName );
54 
55  std::string::const_iterator it = str.begin();
56  return parseString( str, it );
57  }
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().

Referenced by SCH::IFACE::SaveFileAs().

◆ parseString()

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

Definition at line 81 of file sexpr_parser.cpp.

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

References distance(), m_lineNumber, and whitespaceCharacters.

Referenced by Parse(), and ParseFromFile().

Member Data Documentation

◆ m_lineNumber

int SEXPR::PARSER::m_lineNumber
private

Definition at line 44 of file sexpr_parser.h.

Referenced by parseString().

◆ whitespaceCharacters

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

Definition at line 43 of file sexpr_parser.h.

Referenced by parseString().


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