31bool isIdentChar( wxUniChar aChar )
33 return wxIsalnum( aChar ) || aChar ==
'_';
37bool isGround(
const wxString& aNet )
39 return aNet.IsSameAs( wxS(
"GND" ),
false ) || aNet == wxS(
"0" );
50 if( aExpression.Length() < 4 || !aExpression.Upper().StartsWith( wxS(
"V(" ) ) )
55 for(
size_t ii = 1; ii < aExpression.Length(); ++ii )
57 if( aExpression[ii] ==
'(' )
61 else if( aExpression[ii] ==
')' && --depth == 0 )
63 return ii == aExpression.Length() - 1;
73 static const std::set<wxString> refs = { wxS(
"V" ), wxS(
"VDB" ), wxS(
"VM" ), wxS(
"VP" ),
74 wxS(
"VR" ), wxS(
"VI" ), wxS(
"I" ) };
76 int open = aSignal.Find(
'(' );
78 if( open == wxNOT_FOUND )
79 return !aSignal.IsEmpty() && aSignal.find_first_of( wxS(
" +-*/^,<>=" ) ) == wxString::npos;
81 if( !refs.count( aSignal.Left( open ).Upper() ) )
87 for(
size_t ii = open; ii < aSignal.Length(); ++ii )
89 if( aSignal[ii] ==
'"' )
93 else if( aSignal[ii] ==
'(' )
95 else if( aSignal[ii] ==
')' && --depth == 0 )
96 return ii == aSignal.Length() - 1;
112 aResult = wxS(
"vdb" ) + aBase.Mid( 1 );
114 aResult = wxS(
"db(" ) + aBase + wxS(
")" );
119 aResult = wxS(
"mag(" ) + aBase + wxS(
")" );
131void SplitMeasurement(
const wxString& aCmd, wxString& aFunc, wxString& aSignal, wxString& aParams )
134 cmd.Trim(
false ).Trim(
true );
140 size_t space = cmd.find_first_of( wxS(
" \t" ) );
142 if( space == wxString::npos )
145 aFunc = cmd.Left( space );
147 wxString rest = cmd.Mid( space );
150 size_t len = rest.Length();
151 size_t paramStart = wxString::npos;
155 for(
size_t ii = 0; ii < len && paramStart == wxString::npos; ++ii )
157 wxUniChar ch = rest[ii];
162 if( quoted || ch ==
'"' )
173 else if( depth == 0 && ii > 0 && wxIsspace( rest[ii - 1] ) && wxIsalpha( ch ) )
177 while( jj < len && isIdentChar( rest[jj] ) )
180 while( jj < len && wxIsspace( rest[jj] ) )
184 if( jj < len && rest[jj] ==
'=' && ( jj + 1 == len || rest[jj + 1] !=
'=' ) )
189 aSignal = rest.Left( paramStart );
190 aSignal.Trim(
true );
192 if( paramStart != wxString::npos )
194 aParams = rest.Mid( paramStart );
195 aParams.Trim(
true );
202 if( aDeviceExprs.empty() )
206 static const std::pair<wxString, wxString> forms[] = {
207 { wxS(
"VDB(" ), wxS(
"db" ) }, { wxS(
"VM(" ), wxS(
"mag" ) }, { wxS(
"VP(" ), wxS(
"ph" ) },
208 { wxS(
"VR(" ), wxS(
"real" ) }, { wxS(
"VI(" ), wxS(
"imag" ) }, { wxS(
"V(" ), wxS(
"" ) }
211 std::map<wxString, wxString> byUpper;
213 for(
const auto& [ref, expr] : aDeviceExprs )
214 byUpper[ref.Upper()] = expr;
216 wxString upper = aExpression.Upper();
218 size_t len = aExpression.Length();
221 for(
size_t ii = 0; ii < len; )
223 if( aExpression[ii] ==
'"' )
226 bool matched =
false;
228 if( !quoted && ( ii == 0 || !isIdentChar( aExpression[ii - 1] ) ) )
230 for(
const auto& [prefix, function] : forms )
232 if( upper.compare( ii, prefix.Length(), prefix ) != 0 )
235 size_t open = ii + prefix.Length();
236 size_t close = aExpression.find(
')', open );
238 if( close == wxString::npos )
241 wxString ref = upper.Mid( open, close - open );
242 ref.Trim(
true ).Trim(
false );
244 auto it = byUpper.find( ref );
246 if( it == byUpper.end() )
249 result << function << wxS(
"(" ) << it->second << wxS(
")" );
257 result << aExpression[ii++];
264wxString
QuoteNetNames(
const wxString& aExpression,
const std::vector<wxString>& aNetnames )
266 size_t len = aExpression.length();
267 std::vector<bool> mask( len,
false );
268 std::vector<bool> userQuoted( len,
false );
269 wxString lower = aExpression.Lower();
271 auto isNetnameChar = []( wxUniChar aChar ) ->
bool
273 wxUint32 value = aChar.GetValue();
275 if( ( value >=
'0' && value <=
'9' ) || ( value >=
'A' && value <=
'Z' ) || ( value >=
'a' && value <=
'z' ) )
293 bool inQuote =
false;
295 for(
size_t ii = 0; ii < len; ++ii )
297 if( aExpression[ii] ==
'"' )
300 userQuoted[ii] = inQuote || aExpression[ii] ==
'"';
303 for(
const wxString& netname : aNetnames )
305 if( netname.IsEmpty() )
308 wxString needle = netname.Lower();
309 size_t pos = lower.find( needle );
311 while( pos != wxString::npos )
313 size_t end = pos + needle.length();
316 bool whole = ( pos == 0 || !isNetnameChar( aExpression[pos - 1] ) )
317 && (
end == len || !isNetnameChar( aExpression[
end] ) );
319 if( whole && !userQuoted[pos] )
321 for(
size_t ii = pos; ii <
end; ++ii )
325 pos = lower.find( needle, pos + 1 );
332 for(
size_t ii = 0; ii < len; ++ii )
334 if( mask[ii] != open )
340 quoted << aExpression[ii];
353 bool inQuote =
false;
355 for( wxUniChar ch : aExpression )
384 std::vector<wxString> refs;
385 size_t len = aExpression.length();
387 for(
size_t ii = 0; ii < len; ++ii )
390 if( ii > 0 && isIdentChar( aExpression[ii - 1] ) )
393 wxUniChar prefix = wxToupper( aExpression[ii] );
395 if( prefix !=
'V' && prefix !=
'I' && prefix !=
'P' )
398 size_t open = ii + 1;
403 static const wxString variants[] = { wxS(
"DB" ), wxS(
"M" ), wxS(
"P" ), wxS(
"R" ), wxS(
"I" ) };
405 for(
const wxString& variant : variants )
407 size_t after = open + variant.length();
409 if( after < len && aExpression.Mid( open, variant.length() ).Upper() == variant
410 && aExpression[after] ==
'(' )
418 if( open >= len || aExpression[open] !=
'(' )
421 std::vector<wxString> args;
424 bool inQuote =
false;
427 for( ; jj < len; ++jj )
429 wxUniChar ch = aExpression[jj];
435 if( inQuote || ch ==
'"' )
450 args.push_back( arg );
454 else if( ch ==
',' && depth == 1 )
456 args.push_back( arg );
464 for( wxString& each : args )
466 each.Replace( wxS(
"\"" ), wxEmptyString );
467 each.Trim(
true ).Trim(
false );
469 if( each.IsEmpty() || isGround( each ) )
472 refs.push_back( wxString( prefix ) + wxS(
"(" ) + each + wxS(
")" ) );
483 const std::vector<std::string>& aPinNumbers,
484 const std::vector<std::string>& aPinNetNames )
486 if( aModelPins.size() != 2 )
487 return wxEmptyString;
491 for(
size_t ii = 0; ii < 2; ++ii )
493 auto it = std::find( aPinNumbers.begin(), aPinNumbers.end(), aModelPins[ii] );
495 if( it == aPinNumbers.end() )
496 return wxEmptyString;
498 size_t index = std::distance( aPinNumbers.begin(), it );
500 if(
index >= aPinNetNames.size() )
501 return wxEmptyString;
503 nets[ii] = wxString( aPinNetNames[
index] );
506 const wxString& netA = nets[0];
507 const wxString& netB = nets[1];
509 if( netA.IsEmpty() || netB.IsEmpty() || netA == netB )
510 return wxEmptyString;
512 if( isGround( netA ) && isGround( netB ) )
513 return wxEmptyString;
515 if( isGround( netB ) )
516 return wxString::Format( wxS(
"V(%s)" ), netA );
518 if( isGround( netA ) )
519 return wxString::Format( wxS(
"-V(%s)" ), netB );
521 return wxString::Format( wxS(
"V(%s) - V(%s)" ), netA, netB );
527 wxString lower = aVectorName.Lower();
530 if( !lower.StartsWith( wxS(
"user" ), &digits ) && !lower.StartsWith(
DEVICE_VECTOR_PREFIX, &digits ) )
533 return !digits.IsEmpty() && digits.find_first_not_of( wxS(
"0123456789" ) ) == wxString::npos;
Text handling for simulator signal expressions and measurement commands.
bool ParensBalanced(const wxString &aExpression)
bool MeasurableSignal(const wxString &aBase, PART aPart, wxString &aResult)
Build the signal a measurement should read so it matches what is plotted.
wxString QuoteNetNames(const wxString &aExpression, const std::vector< wxString > &aNetnames)
Quote every whole occurrence of a net name that ngspice would otherwise parse as an expression.
void SplitMeasurement(const wxString &aCmd, wxString &aFunc, wxString &aSignal, wxString &aParams)
Split "MAX V(out) from=1m to=2m" into its function, signal and trailing parameters.
wxString DeviceVoltageExpression(const std::vector< std::string > &aModelPins, const std::vector< std::string > &aPinNumbers, const std::vector< std::string > &aPinNetNames)
Build the voltage across a two-terminal part in its model's pin order, matching the sign of I() and P...
const wxString DEVICE_VECTOR_PREFIX
Vector name prefix for the voltage across a component, "kicad_vdev3".
bool IsDerivedSignalVector(const wxString &aVectorName)
std::vector< wxString > ExtractSignalRefs(const wxString &aExpression)
Extract the V() and I() and P() node or device references from an expression, unquoted.
PART
Which part of a complex signal a plotted row shows.
@ GAIN
AC magnitude, plotted in dB.
@ AMPLITUDE
S-parameter magnitude, plotted linear.
bool IsSingleVoltageRef(const wxString &aExpression)
bool IsMeasVector(const wxString &aSignal)
wxString ExpandDeviceVoltages(const wxString &aExpression, const std::map< wxString, wxString > &aDeviceExprs)
Replace V(R1), VDB(R1), VM(R1), ... with the terminal voltage expression of the part.
wxString result
Test unit parsing edge cases and error handling.