KiCad PCB EDA Suite
altium_rule_transformer.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2021 Thomas Pointhuber <[email protected]>
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
26 
27 #include <iostream>
28 #include <wx/wxcrt.h> // for wxIsspace
29 
31 {
33 
34  // skip whitespaces
35  for( ; m_it != m_expr.end() && wxIsspace( curChar() ); nextChar() )
36  ;
37 
38  // check for end of string
39  if( m_it == m_expr.end() )
40  {
42  return m_currentToken;
43  }
44 
45  const size_t startPos = m_pos;
46  const wxUniChar curCh = curChar();
47  wxUniChar nextCh = nextChar();
48 
49  if( curCh == '(' )
50  {
52  return m_currentToken;
53  }
54  else if( curCh == ')' )
55  {
57  return m_currentToken;
58  }
59  else if( curCh == '*' )
60  {
62  return m_currentToken;
63  }
64  else if( curCh == '/' )
65  {
67  return m_currentToken;
68  }
69  else if( curCh == '=' )
70  {
72  return m_currentToken;
73  }
74  else if( curCh == '<' )
75  {
76  if( nextCh == '=' )
77  {
78  nextChar();
80  }
81  else if( nextCh == '>' )
82  {
83  nextChar();
85  }
86  else
87  {
89  }
90  return m_currentToken;
91  }
92  else if( curCh == '>' )
93  {
94  if( nextCh == '=' )
95  {
96  nextChar();
98  }
99  else
100  {
102  }
103  return m_currentToken;
104  }
105  else if( curCh == '&' && nextCh == '&' )
106  {
107  nextChar();
109  return m_currentToken;
110  }
111  else if( curCh == '|' && nextCh == '|' )
112  {
113  nextChar();
115  return m_currentToken;
116  }
117  else if( curCh == '\'' )
118  {
119  std::cout << "start const string" << std::endl;
120  wxString constString;
121  while( m_it != m_expr.end() && nextCh != '\'' )
122  {
123  constString += nextCh; // TODO: escaping?
124  nextCh = nextChar();
125  }
126  std::cout << "end const string: " << constString << std::endl;
127 
128  if( m_it != m_expr.end() )
129  {
130  nextChar(); // TODO: exception if EOF reached?
131  }
132 
133  m_nextToken = { ALTIUM_RULE_TOKEN_KIND::CONST_STRING, startPos, constString };
134  return m_currentToken;
135  }
136  else if( curCh == '+' && !wxIsdigit( nextCh ) )
137  {
139  return m_currentToken;
140  }
141  else if( curCh == '-' && !wxIsdigit( nextCh ) )
142  {
144  return m_currentToken;
145  }
146  else if( curCh == '+' || curCh == '-' || wxIsdigit( curCh ) )
147  {
148  wxString digitString = curCh;
149  while( wxIsdigit( nextCh ) )
150  {
151  digitString += nextCh;
152  nextCh = nextChar();
153  }
154 
155  long value;
156  digitString.ToLong( &value ); // TODO: check return value
157 
158  m_nextToken = { ALTIUM_RULE_TOKEN_KIND::CONST_INT, startPos, value };
159  return m_currentToken;
160  }
161  else
162  {
163  wxString identString = curCh;
164  while( wxIsalnum( nextCh ) )
165  {
166  identString += nextCh;
167  nextCh = nextChar();
168  }
169 
170  if( identString.IsSameAs( "True", false ) )
171  {
173  }
174  else if( identString.IsSameAs( "False", false ) )
175  {
177  }
178  else if( identString.IsSameAs( "Div", false ) )
179  {
181  }
182  else if( identString.IsSameAs( "Mod", false ) )
183  {
185  }
186  else if( identString.IsSameAs( "And", false ) )
187  {
189  }
190  else if( identString.IsSameAs( "Or", false ) )
191  {
192  m_nextToken = { ALTIUM_RULE_TOKEN_KIND::OR, startPos };
193  }
194  else if( identString.IsSameAs( "Xor", false ) )
195  {
197  }
198  else if( identString.IsSameAs( "Not", false ) )
199  {
201  }
202  else if( identString.IsSameAs( "Between", false ) )
203  {
205  }
206  else if( identString.IsSameAs( "Like", false ) )
207  {
209  }
210  else
211  {
212  m_nextToken = { ALTIUM_RULE_TOKEN_KIND::IDENT, startPos, identString };
213  }
214 
215  return m_currentToken;
216  }
217 }
218 
220 {
221  return m_nextToken;
222 }
const ALTIUM_RULE_TOKEN & Peek() const
wxString::const_iterator m_it
const ALTIUM_RULE_TOKEN & Next()
ALTIUM_RULE_TOKEN m_currentToken