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-2021 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 #include <wx/ffile.h>
30 
31 #include <macros.h>
32 
33 namespace SEXPR
34 {
35  const std::string PARSER::whitespaceCharacters = " \t\n\r\b\f\v";
36 
37  PARSER::PARSER() : m_lineNumber( 1 )
38  {
39  }
40 
42  {
43  }
44 
45  std::unique_ptr<SEXPR> PARSER::Parse( const std::string& aString )
46  {
47  std::string::const_iterator it = aString.begin();
48  return parseString( aString, it );
49  }
50 
51  std::unique_ptr<SEXPR> PARSER::ParseFromFile( const std::string& aFileName )
52  {
53  std::string str = GetFileContents( aFileName );
54 
55  std::string::const_iterator it = str.begin();
56  return parseString( str, it );
57  }
58 
59  std::string PARSER::GetFileContents( const std::string &aFileName )
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  }
80 
81  std::unique_ptr<SEXPR> PARSER::parseString(
82  const std::string& aString, std::string::const_iterator& it )
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  }
197 }
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
std::unique_ptr< SEXPR > ParseFromFile(const std::string &aFilename)
std::unique_ptr< SEXPR > Parse(const std::string &aString)
std::unique_ptr< SEXPR > parseString(const std::string &aString, std::string::const_iterator &it)
This file contains miscellaneous commonly used macros and functions.
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
static std::string GetFileContents(const std::string &aFilename)
static const std::string whitespaceCharacters
Definition: sexpr_parser.h:43