KiCad PCB EDA Suite
sexpr_parser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
3  * Copyright (C) 2018-2019 KiCad Developers, see AUTHORS.txt for contributors.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "sexpr/sexpr_parser.h"
20 #include "sexpr/sexpr_exception.h"
21 #include <cctype>
22 #include <cstdlib> /* strtod */
23 #include <iterator>
24 #include <stdexcept>
25 
26 #include <fstream>
27 #include <streambuf>
28 #include <wx/file.h>
29 
30 #include <macros.h>
31 
32 namespace SEXPR
33 {
34  const std::string PARSER::whitespaceCharacters = " \t\n\r\b\f\v";
35 
36  PARSER::PARSER() : m_lineNumber( 1 )
37  {
38  }
39 
41  {
42  }
43 
44  std::unique_ptr<SEXPR> PARSER::Parse( const std::string& aString )
45  {
46  std::string::const_iterator it = aString.begin();
47  return parseString( aString, it );
48  }
49 
50  std::unique_ptr<SEXPR> PARSER::ParseFromFile( const std::string& aFileName )
51  {
52  std::string str = GetFileContents( aFileName );
53 
54  std::string::const_iterator it = str.begin();
55  return parseString( str, it );
56  }
57 
58  std::string PARSER::GetFileContents( const std::string &aFileName )
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  }
79 
80  std::unique_ptr<SEXPR> PARSER::parseString(
81  const std::string& aString, std::string::const_iterator& it )
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  }
196 }
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
This file contains miscellaneous commonly used macros and functions.
#define NULL
std::unique_ptr< SEXPR > Parse(const std::string &aString)
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
static std::string GetFileContents(const std::string &aFilename)
std::unique_ptr< SEXPR > ParseFromFile(const std::string &aFilename)