From 2bf67913529ab4117d88beba62cdb3e71a60ee24 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 20 Sep 2022 08:16:40 +0200 Subject: [PATCH] Sim: Move SIM_LIBRARY_SPICE parsing facilities to a new class This leaves SIM_LIBRARY_SPICE very small, but it will grow larger later. --- eeschema/CMakeLists.txt | 1 + eeschema/sim/sim_library_spice.cpp | 74 ++------------------ eeschema/sim/sim_library_spice.h | 10 ++- eeschema/sim/spice_library_parser.cpp | 99 +++++++++++++++++++++++++++ eeschema/sim/spice_library_parser.h | 44 ++++++++++++ 5 files changed, 158 insertions(+), 70 deletions(-) create mode 100644 eeschema/sim/spice_library_parser.cpp create mode 100644 eeschema/sim/spice_library_parser.h diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt index c4ac1ae4a8..660bee48b6 100644 --- a/eeschema/CMakeLists.txt +++ b/eeschema/CMakeLists.txt @@ -340,6 +340,7 @@ if( KICAD_SPICE ) sim/sim_plot_panel.cpp sim/sim_property.cpp sim/sim_workbook.cpp + sim/spice_library_parser.cpp sim/spice_model_parser.cpp sim/spice_simulator.cpp sim/spice_value.cpp diff --git a/eeschema/sim/sim_library_spice.cpp b/eeschema/sim/sim_library_spice.cpp index 2f1065fa3b..4242421918 100644 --- a/eeschema/sim/sim_library_spice.cpp +++ b/eeschema/sim/sim_library_spice.cpp @@ -24,81 +24,19 @@ #include #include -#include -#include -#include -#include -#include -namespace SIM_LIBRARY_SPICE_PARSER +SIM_LIBRARY_SPICE::SIM_LIBRARY_SPICE() : + SIM_LIBRARY(), + m_spiceLibraryParser( std::make_unique( *this ) ) { - using namespace SPICE_GRAMMAR; - - // TODO: unknownLine is already handled in spiceUnit. - struct libraryGrammar : spiceSourceGrammar {}; - - - template struct librarySelector : std::false_type {}; - - template <> struct librarySelector : std::true_type {}; - template <> struct librarySelector : std::true_type {}; - - // For debugging. - template <> struct librarySelector : std::true_type {}; -}; +} void SIM_LIBRARY_SPICE::ReadFile( const wxString& aFilePath ) { - LOCALE_IO toggle; - - try - { - tao::pegtl::file_input in( aFilePath.ToStdString() ); - auto root = tao::pegtl::parse_tree::parse - ( in ); - - SIM_LIBRARY::ReadFile( aFilePath ); - - m_models.clear(); - m_modelNames.clear(); - - for( const auto& node : root->children ) - { - if( node->is_type() ) - { - m_models.push_back( SIM_MODEL_SPICE::Create( node->string() ) ); - - if( node->children.size() < 1 - || !node->children.at( 0 )->is_type() ) - { - THROW_IO_ERROR( wxString::Format( "Model name token not found" ) ); - } - - m_modelNames.emplace_back( node->children.at( 0 )->string() ); - } - else if( node->is_type() ) - { - // Do nothing. - } - else - { - wxFAIL_MSG( "Unhandled parse tree node" ); - } - } - } - catch( const std::filesystem::filesystem_error& e ) - { - THROW_IO_ERROR( e.what() ); - } - catch( const tao::pegtl::parse_error& e ) - { - THROW_IO_ERROR( e.what() ); - } + SIM_LIBRARY::ReadFile( aFilePath ); + m_spiceLibraryParser->ReadFile( aFilePath ); } diff --git a/eeschema/sim/sim_library_spice.h b/eeschema/sim/sim_library_spice.h index a708a8dd05..29540eb663 100644 --- a/eeschema/sim/sim_library_spice.h +++ b/eeschema/sim/sim_library_spice.h @@ -26,18 +26,24 @@ #define SIM_LIBRARY_SPICE_H #include +#include class SIM_LIBRARY_SPICE : public SIM_LIBRARY { - // We'll make SIM_LIBRARY have no subclasses probably. - public: + friend class SPICE_LIBRARY_PARSER; + + SIM_LIBRARY_SPICE(); + // @copydoc SIM_LIBRARY::ReadFile() void ReadFile( const wxString& aFilePath ) override; // @copydoc SIM_LIBRARY::WriteFile() void WriteFile( const wxString& aFilePath ) override; + +private: + std::unique_ptr m_spiceLibraryParser; }; #endif // SIM_LIBRARY_SPICE_H diff --git a/eeschema/sim/spice_library_parser.cpp b/eeschema/sim/spice_library_parser.cpp new file mode 100644 index 0000000000..bd7ce29d67 --- /dev/null +++ b/eeschema/sim/spice_library_parser.cpp @@ -0,0 +1,99 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 Mikolaj Wielgus + * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors. + * + * 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 + */ + +#include +#include +#include +#include +#include + +#include +#include + + +namespace SIM_LIBRARY_SPICE_PARSER +{ + using namespace SPICE_GRAMMAR; + + // TODO: unknownLine is already handled in spiceUnit. + struct libraryGrammar : spiceSourceGrammar {}; + + + template struct librarySelector : std::false_type {}; + + template <> struct librarySelector : std::true_type {}; + template <> struct librarySelector : std::true_type {}; + + // For debugging. + template <> struct librarySelector : std::true_type {}; +}; + + +void SPICE_LIBRARY_PARSER::ReadFile( const wxString& aFilePath ) +{ + try + { + tao::pegtl::file_input in( aFilePath.ToStdString() ); + auto root = tao::pegtl::parse_tree::parse + ( in ); + + m_library.m_models.clear(); + m_library.m_modelNames.clear(); + + for( const auto& node : root->children ) + { + if( node->is_type() ) + { + m_library.m_models.push_back( SIM_MODEL_SPICE::Create( node->string() ) ); + + if( node->children.size() < 1 + || !node->children.at( 0 )->is_type() ) + { + THROW_IO_ERROR( wxString::Format( "Model name token not found" ) ); + } + + m_library.m_modelNames.emplace_back( node->children.at( 0 )->string() ); + } + else if( node->is_type() ) + { + // Do nothing. + } + else + { + wxFAIL_MSG( "Unhandled parse tree node" ); + } + } + } + catch( const std::filesystem::filesystem_error& e ) + { + THROW_IO_ERROR( e.what() ); + } + catch( const tao::pegtl::parse_error& e ) + { + THROW_IO_ERROR( e.what() ); + } +} diff --git a/eeschema/sim/spice_library_parser.h b/eeschema/sim/spice_library_parser.h new file mode 100644 index 0000000000..bc7b111a00 --- /dev/null +++ b/eeschema/sim/spice_library_parser.h @@ -0,0 +1,44 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2022 Mikolaj Wielgus + * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors. + * + * 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 + */ + +#ifndef SPICE_LIBRARY_PARSER_H +#define SPICE_LIBRARY_PARSER_H + +#include + +class SIM_LIBRARY_SPICE; + + +class SPICE_LIBRARY_PARSER +{ +public: + SPICE_LIBRARY_PARSER( SIM_LIBRARY_SPICE& aLibrary ) : m_library( aLibrary ) {} + + virtual void ReadFile( const wxString& aFilePath ); + +private: + SIM_LIBRARY_SPICE& m_library; +}; + +#endif // SPICE_LIBRARY_PARSER_H