KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 {
41 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::END_OF_EXPR, m_pos };
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 {
51 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::LPAR, startPos };
52 return m_currentToken;
53 }
54 else if( curCh == ')' )
55 {
56 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::RPAR, startPos };
57 return m_currentToken;
58 }
59 else if( curCh == '*' )
60 {
61 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::MUL, startPos };
62 return m_currentToken;
63 }
64 else if( curCh == '/' )
65 {
66 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::DIV, startPos };
67 return m_currentToken;
68 }
69 else if( curCh == '=' )
70 {
71 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::EQUAL, startPos };
72 return m_currentToken;
73 }
74 else if( curCh == '<' )
75 {
76 if( nextCh == '=' )
77 {
78 nextChar();
79 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::LESS_EQUAL, startPos };
80 }
81 else if( nextCh == '>' )
82 {
83 nextChar();
84 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::NOT_EQUAL, startPos };
85 }
86 else
87 {
88 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::LESS, startPos };
89 }
90 return m_currentToken;
91 }
92 else if( curCh == '>' )
93 {
94 if( nextCh == '=' )
95 {
96 nextChar();
97 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::GREATER_EQUAL, startPos };
98 }
99 else
100 {
101 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::GREATER, startPos };
102 }
103 return m_currentToken;
104 }
105 else if( curCh == '&' && nextCh == '&' )
106 {
107 nextChar();
108 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::LOW_AND, startPos };
109 return m_currentToken;
110 }
111 else if( curCh == '|' && nextCh == '|' )
112 {
113 nextChar();
114 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::LOW_OR, startPos };
115 return m_currentToken;
116 }
117 else if( curCh == '\'' )
118 {
119 wxString constString;
120 while( m_it != m_expr.end() && nextCh != '\'' )
121 {
122 constString += nextCh; // TODO: escaping?
123 nextCh = nextChar();
124 }
125
126 if( m_it != m_expr.end() )
127 {
128 nextChar(); // TODO: exception if EOF reached?
129 }
130
131 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::CONST_STRING, startPos, constString };
132 return m_currentToken;
133 }
134 else if( curCh == '+' && !wxIsdigit( nextCh ) )
135 {
136 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::ADD, startPos };
137 return m_currentToken;
138 }
139 else if( curCh == '-' && !wxIsdigit( nextCh ) )
140 {
141 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::SUB, startPos };
142 return m_currentToken;
143 }
144 else if( curCh == '+' || curCh == '-' || wxIsdigit( curCh ) )
145 {
146 wxString digitString = curCh;
147 while( wxIsdigit( nextCh ) )
148 {
149 digitString += nextCh;
150 nextCh = nextChar();
151 }
152
153 long value;
154 digitString.ToLong( &value ); // TODO: check return value
155
156 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::CONST_INT, startPos, value };
157 return m_currentToken;
158 }
159 else
160 {
161 wxString identString = curCh;
162 while( wxIsalnum( nextCh ) )
163 {
164 identString += nextCh;
165 nextCh = nextChar();
166 }
167
168 if( identString.IsSameAs( wxT( "True" ), false ) )
169 {
170 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::CONST_TRUE, startPos };
171 }
172 else if( identString.IsSameAs( wxT( "False" ), false ) )
173 {
174 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::CONST_FALSE, startPos };
175 }
176 else if( identString.IsSameAs( wxT( "Div" ), false ) )
177 {
178 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::INTEGRAL_DIV, startPos };
179 }
180 else if( identString.IsSameAs( wxT( "Mod" ), false ) )
181 {
182 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::MOD, startPos };
183 }
184 else if( identString.IsSameAs( wxT( "And" ), false ) )
185 {
186 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::AND, startPos };
187 }
188 else if( identString.IsSameAs( wxT( "Or" ), false ) )
189 {
190 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::OR, startPos };
191 }
192 else if( identString.IsSameAs( wxT( "Xor" ), false ) )
193 {
194 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::XOR, startPos };
195 }
196 else if( identString.IsSameAs( wxT( "Not" ), false ) )
197 {
198 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::NOT, startPos };
199 }
200 else if( identString.IsSameAs( wxT( "Between" ), false ) )
201 {
202 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::BETWEEN, startPos };
203 }
204 else if( identString.IsSameAs( wxT( "Like" ), false ) )
205 {
206 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::LIKE, startPos };
207 }
208 else
209 {
210 m_nextToken = { ALTIUM_RULE_TOKEN_KIND::IDENT, startPos, identString };
211 }
212
213 return m_currentToken;
214 }
215}
216
218{
219 return m_nextToken;
220}
const ALTIUM_RULE_TOKEN & Peek() const
ALTIUM_RULE_TOKEN m_currentToken
const ALTIUM_RULE_TOKEN & Next()
wxString::const_iterator m_it