KiCad PCB EDA Suite
Loading...
Searching...
No Matches
spice_library_parser.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Mikolaj Wielgus
5 * Copyright (C) 2022-2024 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * https://www.gnu.org/licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
27#include <sim/spice_grammar.h>
28#include <sim/sim_model_spice.h>
29#include <ki_exception.h>
30
31#include <pegtl.hpp>
32#include <pegtl/contrib/parse_tree.hpp>
33#include <utility>
34#include <core/thread_pool.h>
35
36
38{
39 using namespace SPICE_GRAMMAR;
40
41 // TODO: unknownLine is already handled in spiceUnit.
43
44
45 template <typename Rule> struct librarySelector : std::false_type {};
46
47 template <> struct librarySelector<modelUnit> : std::true_type {};
48 template <> struct librarySelector<modelName> : std::true_type {};
49
50 template <> struct librarySelector<dotInclude> : std::true_type {};
51 template <> struct librarySelector<dotIncludePathWithoutQuotes> : std::true_type {};
52 template <> struct librarySelector<dotIncludePathWithoutApostrophes> : std::true_type {};
53 template <> struct librarySelector<dotIncludePath> : std::true_type {};
54
55 // For debugging.
56 template <> struct librarySelector<unknownLine> : std::true_type {};
57};
58
59
60void SPICE_LIBRARY_PARSER::parseFile( const wxString &aFilePath, REPORTER& aReporter,
61 std::vector<std::pair<std::string, std::string>>* aQueue )
62{
63 try
64 {
65 tao::pegtl::string_input<> in( SafeReadFile( aFilePath, wxS( "r" ) ).ToStdString(),
66 aFilePath.ToStdString() );
67 auto root = tao::pegtl::parse_tree::parse<SIM_LIBRARY_SPICE_PARSER::libraryGrammar,
69 tao::pegtl::nothing,
71
72 for( const auto& node : root->children )
73 {
74 if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() )
75 {
76 aQueue->emplace_back( node->children.at( 0 )->string(), node->string() );
77 }
78 else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::dotInclude>() )
79 {
80 wxString lib = m_library.m_pathResolver( node->children.at( 0 )->string(), aFilePath );
81
82 try
83 {
84 parseFile( lib, aReporter, aQueue );
85 }
86 catch( const IO_ERROR& e )
87 {
88 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
89 }
90 }
91 else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::unknownLine>() )
92 {
93 // Do nothing.
94 }
95 else
96 {
97 wxFAIL_MSG( "Unhandled parse tree node" );
98 }
99 }
100 }
101 catch( const IO_ERROR& e )
102 {
103 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
104 }
105 catch( const tao::pegtl::parse_error& e )
106 {
107 aReporter.Report( e.what(), RPT_SEVERITY_ERROR );
108 }
109}
110
111
112void SPICE_LIBRARY_PARSER::ReadFile( const wxString& aFilePath, REPORTER& aReporter )
113{
114 m_library.m_models.clear();
115 m_library.m_modelNames.clear();
116
117 std::vector<std::pair<std::string, std::string>> modelQueue;
118
119 parseFile( aFilePath, aReporter, &modelQueue );
120
121 m_library.m_models.reserve( modelQueue.size() );
122 m_library.m_modelNames.reserve( modelQueue.size() );
123
124 for( const auto& [name, source] : modelQueue )
125 {
126 m_library.m_models.emplace_back( nullptr );
127 m_library.m_modelNames.emplace_back( name );
128 }
129
130 auto createModel =
131 [&]( int ii, bool firstPass )
132 {
133 m_library.m_models[ii] = SIM_MODEL_SPICE::Create( m_library, modelQueue[ii].second,
134 firstPass, aReporter );
135 };
136
137 // Read all self-contained models in parallel
139
140 tp.push_loop( modelQueue.size(),
141 [&]( const int a, const int b )
142 {
143 for( int ii = a; ii < b; ++ii )
144 createModel( ii, true );
145 } );
146 tp.wait_for_tasks();
147
148 // Now read all models that might refer to other models in order.
149 for( int ii = 0; ii < (int) modelQueue.size(); ++ii )
150 {
151 if( !m_library.m_models[ii] )
152 createModel( ii, false );
153 }
154}
const char * name
Definition: DXF_plotter.cpp:57
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
std::vector< std::string > m_modelNames
Definition: sim_library.h:77
std::vector< std::unique_ptr< SIM_MODEL > > m_models
Definition: sim_library.h:78
std::function< wxString(const wxString &, const wxString &)> m_pathResolver
Definition: sim_library.h:80
static std::unique_ptr< SIM_MODEL_SPICE > Create(const SIM_LIBRARY_SPICE &aLibrary, const std::string &aSpiceCode, bool aFirstPass, REPORTER &aReporter)
SIM_LIBRARY_SPICE & m_library
virtual void ReadFile(const wxString &aFilePath, REPORTER &firstPass)
void parseFile(const wxString &aFilePath, REPORTER &aReporter, std::vector< std::pair< std::string, std::string > > *aModelQueue)
must_if< error >::control< Rule > control
@ RPT_SEVERITY_ERROR
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition: richio.cpp:93
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32