KiCad PCB EDA Suite
SIM_VALUE_PARSER Namespace Reference

Classes

struct  numberSelector
 
struct  numberSelector< exponent >
 
struct  numberSelector< fracPart >
 
struct  numberSelector< intPart >
 
struct  numberSelector< metricSuffix< SIM_VALUE::TYPE_FLOAT, NOTATION::SI > >
 
struct  numberSelector< metricSuffix< SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE > >
 
struct  numberSelector< metricSuffix< SIM_VALUE::TYPE_INT, NOTATION::SI > >
 
struct  numberSelector< metricSuffix< SIM_VALUE::TYPE_INT, NOTATION::SPICE > >
 
struct  numberSelector< SIM_VALUE_GRAMMAR::significand< SIM_VALUE::TYPE_FLOAT > >
 
struct  numberSelector< SIM_VALUE_GRAMMAR::significand< SIM_VALUE::TYPE_INT > >
 
struct  PARSE_RESULT
 

Functions

PARSE_RESULT Parse (const std::string &aString, NOTATION aNotation=NOTATION::SI, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT)
 
int MetricSuffixToExponent (std::string aMetricSuffix, NOTATION aNotation=NOTATION::SI)
 
std::string ExponentToMetricSuffix (double aExponent, int &aReductionExponent, NOTATION aNotation=NOTATION::SI)
 

Function Documentation

◆ ExponentToMetricSuffix()

std::string SIM_VALUE_PARSER::ExponentToMetricSuffix ( double  aExponent,
int &  aReductionExponent,
NOTATION  aNotation = NOTATION::SI 
)

Definition at line 288 of file sim_value.cpp.

290{
291 if( aNotation == NOTATION::SI && aExponent >= -18 && aExponent <= -15 )
292 {
293 aReductionExponent = -18;
294 return "a";
295 }
296 else if( aExponent >= -15 && aExponent < -12 )
297 {
298 aReductionExponent = -15;
299 return "f";
300 }
301 else if( aExponent >= -12 && aExponent < -9 )
302 {
303 aReductionExponent = -12;
304 return "p";
305 }
306 else if( aExponent >= -9 && aExponent < -6 )
307 {
308 aReductionExponent = -9;
309 return "n";
310 }
311 else if( aExponent >= -6 && aExponent < -3 )
312 {
313 aReductionExponent = -6;
314 return "u";
315 }
316 else if( aExponent >= -3 && aExponent < 0 )
317 {
318 aReductionExponent = -3;
319 return "m";
320 }
321 else if( aExponent >= 0 && aExponent < 3 )
322 {
323 aReductionExponent = 0;
324 return "";
325 }
326 else if( aExponent >= 3 && aExponent < 6 )
327 {
328 aReductionExponent = 3;
329 return "k";
330 }
331 else if( aExponent >= 6 && aExponent < 9 )
332 {
333 aReductionExponent = 6;
334 return ( aNotation == NOTATION::SI ) ? "M" : "Meg";
335 }
336 else if( aExponent >= 9 && aExponent < 12 )
337 {
338 aReductionExponent = 9;
339 return "G";
340 }
341 else if( aExponent >= 12 && aExponent < 15 )
342 {
343 aReductionExponent = 12;
344 return "T";
345 }
346 else if( aNotation == NOTATION::SI && aExponent >= 15 && aExponent < 18 )
347 {
348 aReductionExponent = 15;
349 return "P";
350 }
351 else if( aNotation == NOTATION::SI && aExponent >= 18 && aExponent <= 21 )
352 {
353 aReductionExponent = 18;
354 return "E";
355 }
356
357 aReductionExponent = 0;
358 return "";
359}

◆ MetricSuffixToExponent()

int SIM_VALUE_PARSER::MetricSuffixToExponent ( std::string  aMetricSuffix,
NOTATION  aNotation = NOTATION::SI 
)

Definition at line 228 of file sim_value.cpp.

229{
230 switch( aNotation )
231 {
232 case NOTATION::SI:
233 if( aMetricSuffix.empty() )
234 return 0;
235
236 switch( aMetricSuffix[0] )
237 {
238 case 'a': return -18;
239 case 'f': return -15;
240 case 'p': return -12;
241 case 'n': return -9;
242 case 'u': return -6;
243 case 'm': return -3;
244 case 'k':
245 case 'K': return 3;
246 case 'M': return 6;
247 case 'G': return 9;
248 case 'T': return 12;
249 case 'P': return 15;
250 case 'E': return 18;
251 }
252
253 break;
254
255 case NOTATION::SPICE:
256 std::transform( aMetricSuffix.begin(), aMetricSuffix.end(), aMetricSuffix.begin(),
257 ::tolower );
258
259 if( aMetricSuffix == "f" )
260 return -15;
261 else if( aMetricSuffix == "p" )
262 return -12;
263 else if( aMetricSuffix == "n" )
264 return -9;
265 else if( aMetricSuffix == "u" )
266 return -6;
267 else if( aMetricSuffix == "m" )
268 return -3;
269 else if( aMetricSuffix == "" )
270 return 0;
271 else if( aMetricSuffix == "k" )
272 return 3;
273 else if( aMetricSuffix == "meg" )
274 return 6;
275 else if( aMetricSuffix == "g" )
276 return 9;
277 else if( aMetricSuffix == "t" )
278 return 12;
279
280 break;
281 }
282
283 wxFAIL_MSG( fmt::format( "Unknown simulator value suffix: '{:s}'", aMetricSuffix ) );
284 return 0;
285}

Referenced by handleNodeForParse().

◆ Parse()

SIM_VALUE_PARSER::PARSE_RESULT SIM_VALUE_PARSER::Parse ( const std::string &  aString,
NOTATION  aNotation = NOTATION::SI,
SIM_VALUE::TYPE  aValueType = SIM_VALUE::TYPE_FLOAT 
)

Definition at line 189 of file sim_value.cpp.

192{
193 LOCALE_IO toggle;
194
195 tao::pegtl::string_input<> in( aString, "from_content" );
196 std::unique_ptr<tao::pegtl::parse_tree::node> root;
197 PARSE_RESULT result;
198
199 try
200 {
201 CALL_INSTANCE( aValueType, aNotation, root = doParse, in );
202 }
203 catch( tao::pegtl::parse_error& )
204 {
205 result.isOk = false;
206 return result;
207 }
208
209 wxASSERT( root );
210
211 try
212 {
213 for( const auto& node : root->children )
214 {
215 CALL_INSTANCE( aValueType, aNotation, handleNodeForParse, *node, result );
216 }
217 }
218 catch( const std::invalid_argument& e )
219 {
220 wxFAIL_MSG( fmt::format( "Parsing simulator value failed: {:s}", e.what() ) );
221 result.isOk = false;
222 }
223
224 return result;
225}
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
static void handleNodeForParse(tao::pegtl::parse_tree::node &aNode, SIM_VALUE_PARSER::PARSE_RESULT &aParseResult)
Definition: sim_value.cpp:157
static std::unique_ptr< tao::pegtl::parse_tree::node > doParse(tao::pegtl::string_input<> &aIn)
Definition: sim_value.cpp:147
#define CALL_INSTANCE(ValueType, Notation, func,...)
Definition: sim_value.cpp:33

References CALL_INSTANCE, doParse(), handleNodeForParse(), and SIM_VALUE_PARSER::PARSE_RESULT::isOk.

Referenced by BOOST_AUTO_TEST_CASE(), LIBEVAL::COMPILER::Compile(), SIM_VALUE_INST< T >::FromString(), CADSTAR_ARCHIVE_PARSER::TEXT::Parse(), SCH_BASE_FRAME::PickSymbolFromLibBrowser(), and NUMERIC_EVALUATOR::Process().