2022-11-11 02:07:34 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
2023-01-12 12:46:52 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-11-11 02:07:34 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
#include <sim/sim_model_serializer.h>
|
2022-11-11 02:07:34 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <pegtl.hpp>
|
|
|
|
#include <pegtl/contrib/parse_tree.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2022-11-26 00:28:57 +00:00
|
|
|
#include <string_utils.h>
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
namespace SIM_MODEL_SERIALIZER_PARSER
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
2022-12-21 00:38:52 +00:00
|
|
|
using namespace SIM_MODEL_SERIALIZER_GRAMMAR;
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
template <typename Rule> struct fieldParamValuePairsSelector : std::false_type {};
|
|
|
|
template <> struct fieldParamValuePairsSelector<param> : std::true_type {};
|
2023-03-22 01:37:55 +00:00
|
|
|
template <> struct fieldParamValuePairsSelector<flagParam> : std::true_type {};
|
2022-11-11 02:07:34 +00:00
|
|
|
template <> struct fieldParamValuePairsSelector<quotedStringContent> : std::true_type {};
|
|
|
|
template <> struct fieldParamValuePairsSelector<unquotedString> : std::true_type {};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Rule> struct pinSequenceSelector : std::false_type {};
|
2022-11-25 04:31:03 +00:00
|
|
|
template <> struct pinSequenceSelector<pinAssignment> : std::true_type {};
|
|
|
|
template <> struct pinSequenceSelector<pinSymbolPinNumber> : std::true_type {};
|
|
|
|
template <> struct pinSequenceSelector<pinName> : std::true_type {};
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
template <typename Rule> struct fieldInferValueSelector : std::false_type {};
|
|
|
|
template <> struct fieldInferValueSelector<number<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>> : std::true_type {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::GenerateDevice() const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
2022-11-20 21:06:21 +00:00
|
|
|
return m_model.GetDeviceInfo().fieldValue;
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::GenerateType() const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
return m_model.GetTypeInfo().fieldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::GenerateValue() const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
2022-12-14 13:00:25 +00:00
|
|
|
const SIM_MODEL::PARAM& param = m_model.GetParamOverride( 0 );
|
2023-02-22 09:10:06 +00:00
|
|
|
std::string result = param.value;
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
if( result == "" )
|
2022-11-20 21:06:21 +00:00
|
|
|
result = m_model.GetDeviceInfo().fieldValue;
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::GenerateParams() const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
2022-11-11 04:16:34 +00:00
|
|
|
bool isFirst = true;
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < m_model.GetParamCount(); ++i )
|
|
|
|
{
|
2022-11-22 06:06:53 +00:00
|
|
|
if( i == 0 && m_model.IsStoredInValue() )
|
|
|
|
continue;
|
|
|
|
|
2022-12-14 13:00:25 +00:00
|
|
|
const SIM_MODEL::PARAM& param = m_model.GetParamOverride( i );
|
2022-11-11 02:07:34 +00:00
|
|
|
|
2023-02-22 09:10:06 +00:00
|
|
|
if( param.value == ""
|
2022-11-30 09:38:50 +00:00
|
|
|
&& !( i == 0 && m_model.HasPrimaryValue() && !m_model.IsStoredInValue() ) )
|
|
|
|
{
|
2022-11-11 02:07:34 +00:00
|
|
|
continue;
|
2022-11-30 09:38:50 +00:00
|
|
|
}
|
2022-11-11 02:07:34 +00:00
|
|
|
|
2023-03-22 01:37:03 +00:00
|
|
|
if( m_model.GetBaseModel() && m_model.GetBaseModel()->GetParam( i ).value == param.value )
|
|
|
|
continue;
|
|
|
|
|
2022-11-30 11:40:50 +00:00
|
|
|
// If the parameter is an enum and the value is default, don't write anything.
|
2023-02-22 09:10:06 +00:00
|
|
|
if( param.info.enumValues.size() >= 1 && param.value == param.info.defaultValue )
|
2022-11-30 11:40:50 +00:00
|
|
|
continue;
|
|
|
|
|
2023-01-06 13:44:45 +00:00
|
|
|
std::string paramValuePair = generateParamValuePair( param );
|
2022-11-11 04:16:34 +00:00
|
|
|
|
|
|
|
if( paramValuePair == "" )
|
|
|
|
continue; // Prevent adding empty spaces.
|
|
|
|
|
|
|
|
if( isFirst ) // Don't add a space at the beginning.
|
|
|
|
isFirst = false;
|
|
|
|
else
|
2022-11-11 02:07:34 +00:00
|
|
|
result.append( " " );
|
|
|
|
|
2022-11-11 04:16:34 +00:00
|
|
|
result.append( paramValuePair );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::GeneratePins() const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2022-11-26 00:28:57 +00:00
|
|
|
std::vector<std::reference_wrapper<const SIM_MODEL::PIN>> pins = m_model.GetPins();
|
|
|
|
|
|
|
|
// m_model.GetPins() returns pins in the order they appear in the model, but the keys in the
|
|
|
|
// key=value pairs we create here are symbol pin numbers, so we sort the pins so that they are
|
|
|
|
// ordered by the latter instead.
|
|
|
|
std::sort( pins.begin(), pins.end(),
|
|
|
|
[]( const SIM_MODEL::PIN& lhs, const SIM_MODEL::PIN& rhs )
|
|
|
|
{
|
|
|
|
return StrNumCmp( lhs.symbolPinNumber, rhs.symbolPinNumber, true ) < 0;
|
|
|
|
} );
|
2022-11-11 02:07:34 +00:00
|
|
|
|
2022-11-26 00:28:57 +00:00
|
|
|
bool isFirst = true;
|
|
|
|
|
|
|
|
for( const SIM_MODEL::PIN& pin : pins )
|
|
|
|
{
|
2022-11-25 04:31:03 +00:00
|
|
|
if( pin.symbolPinNumber != "" )
|
|
|
|
{
|
2022-11-26 00:28:57 +00:00
|
|
|
if( !isFirst )
|
2022-11-25 04:31:03 +00:00
|
|
|
result.append( " " );
|
2022-11-26 00:28:57 +00:00
|
|
|
else
|
|
|
|
isFirst = false;
|
2022-11-11 02:07:34 +00:00
|
|
|
|
2022-11-25 04:31:03 +00:00
|
|
|
result.append( fmt::format( "{}={}", pin.symbolPinNumber, pin.name ) );
|
|
|
|
}
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::GenerateEnable() const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
return m_model.IsEnabled() ? "" : "0";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
void SIM_MODEL_SERIALIZER::ParseValue( const std::string& aValue )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-01-12 12:46:52 +00:00
|
|
|
tao::pegtl::string_input<> in( aValue, "Value field" );
|
2022-12-21 00:38:52 +00:00
|
|
|
auto root =
|
|
|
|
tao::pegtl::parse_tree::parse<SIM_MODEL_SERIALIZER_PARSER::fieldInferValueGrammar,
|
|
|
|
SIM_MODEL_SERIALIZER_PARSER::fieldInferValueSelector,
|
|
|
|
tao::pegtl::nothing,
|
|
|
|
SIM_MODEL_SERIALIZER_PARSER::control>
|
|
|
|
( in );
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
for( const auto& node : root->children )
|
|
|
|
{
|
2022-12-21 00:38:52 +00:00
|
|
|
if( node->is_type<SIM_MODEL_SERIALIZER_PARSER::number<SIM_VALUE::TYPE_FLOAT,
|
|
|
|
SIM_VALUE::NOTATION::SI>>()
|
2022-11-22 06:06:53 +00:00
|
|
|
&& node->string() != "" )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
2022-11-22 06:06:53 +00:00
|
|
|
m_model.SetParamValue( 0, node->string() );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( const tao::pegtl::parse_error& e )
|
|
|
|
{
|
2023-01-12 12:46:52 +00:00
|
|
|
THROW_IO_ERROR( e.what() );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
2022-11-18 07:37:58 +00:00
|
|
|
|
|
|
|
m_model.SetIsStoredInValue( true );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
bool SIM_MODEL_SERIALIZER::ParseParams( const std::string& aParams )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
2023-01-12 12:46:52 +00:00
|
|
|
tao::pegtl::string_input<> in( aParams, "Sim.Params field" );
|
2022-11-11 02:07:34 +00:00
|
|
|
std::unique_ptr<tao::pegtl::parse_tree::node> root;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Using parse tree instead of actions because we don't care about performance that much,
|
|
|
|
// and having a tree greatly simplifies things.
|
2022-12-21 00:38:52 +00:00
|
|
|
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SERIALIZER_PARSER::fieldParamValuePairsGrammar,
|
|
|
|
SIM_MODEL_SERIALIZER_PARSER::fieldParamValuePairsSelector,
|
|
|
|
tao::pegtl::nothing,
|
|
|
|
SIM_MODEL_SERIALIZER_PARSER::control>
|
2022-11-11 02:07:34 +00:00
|
|
|
( in );
|
|
|
|
}
|
|
|
|
catch( const tao::pegtl::parse_error& e )
|
|
|
|
{
|
2023-01-12 12:46:52 +00:00
|
|
|
THROW_IO_ERROR( e.what() );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string paramName;
|
2023-05-22 10:45:59 +00:00
|
|
|
bool isPrimaryValueSet = false;
|
2022-11-11 02:07:34 +00:00
|
|
|
|
|
|
|
for( const auto& node : root->children )
|
|
|
|
{
|
2022-12-21 00:38:52 +00:00
|
|
|
if( node->is_type<SIM_MODEL_SERIALIZER_PARSER::param>() )
|
2023-01-21 13:24:47 +00:00
|
|
|
{
|
2022-11-11 02:07:34 +00:00
|
|
|
paramName = node->string();
|
2023-01-21 13:24:47 +00:00
|
|
|
}
|
2022-11-11 02:07:34 +00:00
|
|
|
// TODO: Do something with number<SIM_VALUE::TYPE_INT, ...>.
|
|
|
|
// It doesn't seem too useful?
|
2022-12-21 00:38:52 +00:00
|
|
|
else if( node->is_type<SIM_MODEL_SERIALIZER_PARSER::quotedStringContent>()
|
|
|
|
|| node->is_type<SIM_MODEL_SERIALIZER_PARSER::unquotedString>() )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
wxASSERT( paramName != "" );
|
|
|
|
|
|
|
|
m_model.SetParamValue( paramName, node->string(), SIM_VALUE_GRAMMAR::NOTATION::SI );
|
2022-11-22 06:06:53 +00:00
|
|
|
|
2023-05-22 10:45:59 +00:00
|
|
|
if( m_model.GetParam( 0 ).Matches( paramName ) )
|
2022-11-22 06:06:53 +00:00
|
|
|
isPrimaryValueSet = true;
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
2022-12-21 00:38:52 +00:00
|
|
|
else if( node->is_type<SIM_MODEL_SERIALIZER_PARSER::quotedString>() )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
std::string str = node->string();
|
|
|
|
|
|
|
|
// Unescape quotes.
|
|
|
|
boost::replace_all( str, "\\\"", "\"" );
|
|
|
|
|
|
|
|
m_model.SetParamValue( paramName, str, SIM_VALUE_GRAMMAR::NOTATION::SI );
|
|
|
|
}
|
2023-03-22 01:37:55 +00:00
|
|
|
else if( node->is_type<SIM_MODEL_SERIALIZER_PARSER::flagParam>() )
|
|
|
|
{
|
|
|
|
std::string token = node->string();
|
|
|
|
|
|
|
|
m_model.SetParamValue( token, "1", SIM_VALUE_GRAMMAR::NOTATION::SI );
|
|
|
|
}
|
2022-11-11 02:07:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
wxFAIL;
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 06:06:53 +00:00
|
|
|
|
|
|
|
return !m_model.HasPrimaryValue() || isPrimaryValueSet;
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
void SIM_MODEL_SERIALIZER::ParsePins( const std::string& aPins )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
if( aPins == "" )
|
|
|
|
return;
|
|
|
|
|
2023-01-12 12:46:52 +00:00
|
|
|
tao::pegtl::string_input<> in( aPins, "Sim.Pins field" );
|
2022-11-11 02:07:34 +00:00
|
|
|
std::unique_ptr<tao::pegtl::parse_tree::node> root;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-12-21 00:38:52 +00:00
|
|
|
root = tao::pegtl::parse_tree::parse<SIM_MODEL_SERIALIZER_PARSER::pinSequenceGrammar,
|
|
|
|
SIM_MODEL_SERIALIZER_PARSER::pinSequenceSelector,
|
2022-11-11 02:07:34 +00:00
|
|
|
tao::pegtl::nothing,
|
2022-12-21 00:38:52 +00:00
|
|
|
SIM_MODEL_SERIALIZER_PARSER::control>
|
|
|
|
( in );
|
2022-11-25 04:31:03 +00:00
|
|
|
|
|
|
|
for( const auto& node : root->children )
|
|
|
|
{
|
|
|
|
std::string symbolPinNumber = node->children.at( 0 )->string();
|
|
|
|
std::string pinName = node->children.at( 1 )->string();
|
|
|
|
|
|
|
|
m_model.SetPinSymbolPinNumber( pinName, symbolPinNumber );
|
|
|
|
}
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
catch( const tao::pegtl::parse_error& e )
|
|
|
|
{
|
2023-01-12 12:46:52 +00:00
|
|
|
THROW_IO_ERROR( e.what() );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
void SIM_MODEL_SERIALIZER::ParseEnable( const std::string& aEnable )
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
if( aEnable == "" )
|
|
|
|
return;
|
|
|
|
|
|
|
|
char c = boost::to_lower_copy( aEnable )[0];
|
|
|
|
|
|
|
|
if( c == 'n' || c == 'f' || c == '0' )
|
|
|
|
m_model.SetIsEnabled( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-06 13:44:45 +00:00
|
|
|
std::string SIM_MODEL_SERIALIZER::generateParamValuePair( const SIM_MODEL::PARAM& aParam ) const
|
2022-11-11 02:07:34 +00:00
|
|
|
{
|
|
|
|
std::string name = aParam.info.name;
|
|
|
|
|
|
|
|
// Because of collisions with instance parameters, we append some model parameters with "_".
|
2023-05-22 10:45:59 +00:00
|
|
|
if( boost::ends_with( name, "_" ) )
|
|
|
|
name = name.substr( 0, aParam.info.name.length() - 1 );
|
2022-11-11 02:07:34 +00:00
|
|
|
|
2023-02-22 09:10:06 +00:00
|
|
|
std::string value = aParam.value;
|
2023-03-22 01:37:55 +00:00
|
|
|
|
|
|
|
if( aParam.info.category == SIM_MODEL::PARAM::CATEGORY::FLAGS )
|
2023-05-22 10:45:59 +00:00
|
|
|
return value == "1" ? name : "";
|
2023-03-22 01:37:55 +00:00
|
|
|
|
2022-12-21 00:38:52 +00:00
|
|
|
if( value == "" || value.find( ' ' ) != std::string::npos )
|
2022-11-11 02:07:34 +00:00
|
|
|
value = fmt::format( "\"{}\"", value );
|
|
|
|
|
2022-12-12 14:42:24 +00:00
|
|
|
return fmt::format( "{}={}", name, value );
|
2022-11-11 02:07:34 +00:00
|
|
|
}
|