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()

SEXPR::PARSER::PARSER ( )

Definition at line 37 of file sexpr_parser.cpp.

37 : m_lineNumber( 1 )
38 {
39 }

◆ ~PARSER()

SEXPR::PARSER::~PARSER ( )

Definition at line 41 of file sexpr_parser.cpp.

42 {
43 }

Member Function Documentation

◆ GetFileContents()

std::string SEXPR::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 > 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 > 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 }
static std::string GetFileContents(const std::string &aFilename)

References GetFileContents(), and parseString().

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

◆ parseString()

std::unique_ptr< SEXPR > 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' )
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 ++it;
126
127 auto starting_it = it;
128
129 for( ; it != aString.end(); ++it )
130 {
131 auto ch = *it;
132
133 if( ch == '\\' )
134 {
135 // Skip the next escaped character
136 if( ++it == aString.end() )
137 break;
138
139 continue;
140 }
141
142 if( ch == '"' )
143 break;
144 }
145
146 if( it == aString.end() )
147 throw PARSE_EXCEPTION("missing closing quote");
148
149 auto str = std::make_unique<SEXPR_STRING>( std::string( starting_it, it ),
150 m_lineNumber );
151
152 ++it;
153 return str;
154
155 }
156 else
157 {
158 size_t startPos = std::distance( aString.begin(), it );
159 size_t closingPos = aString.find_first_of( whitespaceCharacters + "()", startPos );
160
161 std::string tmp = aString.substr( startPos, closingPos - startPos );
162
163
164 if( closingPos != std::string::npos )
165 {
166 if( tmp.find_first_not_of( "0123456789." ) == std::string::npos ||
167 ( tmp.size() > 1 && tmp[0] == '-'
168 && tmp.find_first_not_of( "0123456789.", 1 ) == std::string::npos ) )
169 {
170 std::unique_ptr<SEXPR> res;
171
172 if( tmp.find( '.' ) != std::string::npos )
173 {
174 res = std::make_unique<SEXPR_DOUBLE>(
175 strtod( tmp.c_str(), nullptr ), m_lineNumber );
176 //floating point type
177 }
178 else
179 {
180 res = std::make_unique<SEXPR_INTEGER>(
181 strtoll( tmp.c_str(), nullptr, 0 ), m_lineNumber );
182 }
183
184 std::advance( it, closingPos - startPos );
185 return res;
186 }
187 else
188 {
189 auto str = std::make_unique<SEXPR_SYMBOL>( tmp, m_lineNumber );
190 std::advance( it, closingPos - startPos );
191
192 return str;
193 }
194 }
195 else
196 {
197 throw PARSE_EXCEPTION( "format error" );
198 }
199 }
200 }
201
202 return nullptr;
203 }
static const std::string whitespaceCharacters
Definition: sexpr_parser.h:43
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
VECTOR3I res

References distance(), m_lineNumber, parseString(), res, and whitespaceCharacters.

Referenced by Parse(), ParseFromFile(), and parseString().

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 SEXPR::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: