From 9819fa43dd4b4718aebb04289ad9d400427b73aa Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 21 Oct 2022 08:14:13 +0200 Subject: [PATCH] Sim: Allow principal value in Value field to be followed by regular params --- eeschema/sim/sim_model.cpp | 57 ++++++++++++++++++++------------------ eeschema/sim/sim_model.h | 53 +++++++++++++++-------------------- 2 files changed, 53 insertions(+), 57 deletions(-) diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp index fdab171fba..7c773a611f 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -56,24 +56,18 @@ namespace SIM_MODEL_PARSER using namespace SIM_MODEL_GRAMMAR; template struct fieldParamValuePairsSelector : std::false_type {}; - - - template struct pinSequenceSelector : std::false_type {}; - template <> struct pinSequenceSelector : std::true_type {}; - - - template struct fieldFloatValueSelector : std::false_type {}; - template <> struct fieldFloatValueSelector> - : std::true_type {}; template <> struct fieldParamValuePairsSelector : std::true_type {}; template <> struct fieldParamValuePairsSelector : std::true_type {}; template <> struct fieldParamValuePairsSelector : std::true_type {}; + template struct pinSequenceSelector : std::false_type {}; + template <> struct pinSequenceSelector : std::true_type {}; + template struct fieldInferValueSelector : std::false_type {}; - template <> struct fieldInferValueSelector : std::true_type {}; + template <> struct fieldInferValueSelector : std::true_type {}; + template <> struct fieldInferValueSelector : std::true_type {}; template <> struct fieldInferValueSelector> : std::true_type {}; - template <> struct fieldInferValueSelector : std::true_type {}; template <> struct fieldInferValueSelector : std::true_type {}; } @@ -454,7 +448,7 @@ DEVICE_TYPE SIM_MODEL::InferDeviceTypeFromRef( const std::string& aRef ) TYPE SIM_MODEL::InferTypeFromRefAndValue( const std::string& aRef, const std::string& aValue ) { - std::string prefix; + std::string typeString; try { @@ -466,8 +460,8 @@ TYPE SIM_MODEL::InferTypeFromRefAndValue( const std::string& aRef, const std::st for( const auto& node : root->children ) { - if( node->is_type() ) - prefix = node->string(); + if( node->is_type() ) + typeString = node->string(); } } catch( const tao::pegtl::parse_error& ) @@ -478,7 +472,7 @@ TYPE SIM_MODEL::InferTypeFromRefAndValue( const std::string& aRef, const std::st for( TYPE type : TYPE_ITERATOR() ) { - if( TypeInfo( type ).deviceType == deviceType && TypeInfo( type ).fieldValue == prefix ) + if( TypeInfo( type ).deviceType == deviceType && TypeInfo( type ).fieldValue == typeString ) return type; } @@ -812,8 +806,9 @@ void SIM_MODEL::SetParamValue( const std::string& aParamName, const SIM_VALUE& a if( it == params.end() ) { - THROW_IO_ERROR( wxString::Format( _( "Could not find a simulation model parameter named '%s'" ), - aParamName ) ); + THROW_IO_ERROR( wxString::Format( _( "Could not find a parameter named '%s' in simulation model of type '%s'" ), + aParamName, + GetTypeInfo().fieldValue ) ); } SetParamValue( static_cast( it - params.begin() ), aValue ); @@ -827,8 +822,9 @@ void SIM_MODEL::SetParamValue( const std::string& aParamName, const std::string& if( !param ) { - THROW_IO_ERROR( wxString::Format( _( "Could not find a simulation model parameter named '%s'" ), - aParamName ) ); + THROW_IO_ERROR( wxString::Format( _( "Could not find a parameter named '%s' in simulation model of type '%s'" ), + aParamName, + GetTypeInfo().fieldValue ) ); } const SIM_VALUE& value = *FindParam( aParamName )->value; @@ -1059,7 +1055,7 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) try { // Using parse tree instead of actions because we don't care about performance that much, - // and having a tree greatly simplifies some things. + // and having a tree greatly simplifies things. root = tao::pegtl::parse_tree::parse< SIM_MODEL_PARSER::fieldParamValuePairsGrammar, SIM_MODEL_PARSER::fieldParamValuePairsSelector, @@ -1172,7 +1168,7 @@ void SIM_MODEL::doReadDataFields( unsigned aSymbolPinCount, const std::vector template void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, const std::vector* aFields, - bool aAllowOnlyFirstValue ) + bool aAllowPrincipalValueWithoutName ) { // TODO: Make a subclass SIM_MODEL_NONE. if( GetType() == SIM_MODEL::TYPE::NONE ) @@ -1197,13 +1193,11 @@ void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, const std::vec for( const auto& node : root->children ) { - if( node->is_type() ) - ParseParamsField( node->string() ); - else if( node->is_type() ) + if( node->is_type() ) { - if( aAllowOnlyFirstValue ) + if( aAllowPrincipalValueWithoutName ) { - for( const auto& subnode : node->children ) + for( const auto& subnode : node->children ) { if( subnode->is_type>() ) @@ -1212,7 +1206,16 @@ void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, const std::vec } } } + else + { + THROW_IO_ERROR( + wxString::Format( _( "Model of type '%s' cannot have a principal value (which is '%s') in Value field" ), + GetTypeInfo().fieldValue, + node->string() ) ); + } } + else if( node->is_type() ) + ParseParamsField( node->string() ); } } catch( const tao::pegtl::parse_error& e ) @@ -1220,7 +1223,7 @@ void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, const std::vec THROW_IO_ERROR( e.what() ); } - m_isInferred = true; + SetIsInferred( true ); } template void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, diff --git a/eeschema/sim/sim_model.h b/eeschema/sim/sim_model.h index 993acc14cb..1c5b61501e 100644 --- a/eeschema/sim/sim_model.h +++ b/eeschema/sim/sim_model.h @@ -55,22 +55,13 @@ namespace SIM_MODEL_GRAMMAR tao::pegtl::eof> {}; - struct pinNumber : plus, - any> {}; + struct pinNumber : plus, any> {}; struct pinSequence : list {}; struct pinSequenceGrammar : must, opt, opt, tao::pegtl::eof> {}; - - struct fieldFloatValue : seq, - star, not_at>, any>> {}; // Garbage suffix. - - struct fieldFloatValueGrammar : must {}; - - struct param : plus {}; struct unquotedString : plus, any> {}; @@ -91,24 +82,31 @@ namespace SIM_MODEL_GRAMMAR opt, tao::pegtl::eof> {}; - struct fieldInferValuePrefix : plus {}; - struct fieldInferValue : seq, - fieldParamValuePairs> {}; + struct fieldInferValueType : plus {}; + struct fieldInferValuePrincipalValue : seq>>, + // END HACK. + number, + star, not_at>, any>> {}; + struct fieldInferValue : sor>, + seq, + opt, + opt>> {}; struct fieldInferValueGrammar : must, - sor, - tao::pegtl::eof>, - seq, - tao::pegtl::eof>>> {}; + fieldInferValue, + opt, + tao::pegtl::eof> {}; template inline constexpr const char* errorMessage = nullptr; template <> inline constexpr auto errorMessage> = ""; template <> inline constexpr auto errorMessage> = ""; - template <> inline constexpr auto errorMessage = - "expected number"; + template <> inline constexpr auto errorMessage>> = ""; template <> inline constexpr auto errorMessage> = "expected '='"; template <> inline constexpr auto errorMessage inline constexpr auto errorMessage = "expected parameter=value pairs"; template <> inline constexpr auto errorMessage> = ""; - template <> inline constexpr auto errorMessage, - tao::pegtl::eof>, - seq, - tao::pegtl::eof>>> = - "expected parameter=value pairs, together optionally preceded by type, or a single number"; + template <> inline constexpr auto errorMessage = + "expected parameter=value pairs, together possibly preceded by a type or principal value"; template <> inline constexpr auto errorMessage = "expected end of string"; @@ -629,7 +622,7 @@ protected: template void InferredReadDataFields( unsigned aSymbolPinCount, const std::vector* aFields, - bool aAllowOnlyFirstValue = false ); + bool aAllowPrincipalValueWithoutName = false ); std::vector m_params; const SIM_MODEL* m_baseModel;