/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2022 Mikolaj Wielgus * Copyright (C) 2022-2023 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 #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 {}; template <> struct librarySelector : std::true_type {}; template <> struct librarySelector : std::true_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::readElement( const std::string &aFilePath, REPORTER& aReporter ) { try { tao::pegtl::file_input in( aFilePath ); std::unique_ptr root = tao::pegtl::parse_tree::parse ( in ); for( const auto& node : root->children ) { if( node->is_type() ) { std::string model = node->string(); std::string modelName = node->children.at( 0 )->string(); try { m_library.m_models.push_back( SIM_MODEL_SPICE::Create( m_library, model ) ); m_library.m_modelNames.emplace_back( modelName ); } catch( const IO_ERROR& e ) { aReporter.Report( e.What(), RPT_SEVERITY_ERROR ); } } else if( node->is_type() ) { std::string lib = node->children.at( 0 )->string(); try { if( m_library.m_pathResolver ) lib = ( *m_library.m_pathResolver )( lib, aFilePath ); readElement( lib, aReporter ); } catch( const IO_ERROR& e ) { aReporter.Report( e.What(), RPT_SEVERITY_ERROR ); } } else if( node->is_type() ) { // Do nothing. } else { wxFAIL_MSG( "Unhandled parse tree node" ); } } } catch( const std::filesystem::filesystem_error& e ) { aReporter.Report( e.what(), RPT_SEVERITY_ERROR ); } catch( const tao::pegtl::parse_error& e ) { aReporter.Report( e.what(), RPT_SEVERITY_ERROR ); } } void SPICE_LIBRARY_PARSER::ReadFile( const std::string& aFilePath, REPORTER* aReporter ) { m_library.m_models.clear(); m_library.m_modelNames.clear(); if( aReporter ) { readElement( aFilePath, *aReporter ); } else { wxString msg; WX_STRING_REPORTER reporter( &msg ); readElement( aFilePath, reporter ); if( reporter.HasMessage() ) THROW_IO_ERROR( msg ); } }