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
26
27#include <utility>
28
29#include <core/thread_pool.h>
30#include <ki_exception.h>
32#include <sim/spice_grammar.h>
33#include <sim/sim_model_spice.h>
34#include <richio.h>
35
36#include <pegtl.hpp>
37#include <pegtl/contrib/parse_tree.hpp>
38
39
41{
42 using namespace SPICE_GRAMMAR;
43
44 // TODO: unknownLine is already handled in spiceUnit.
46
47
48 template <typename Rule> struct librarySelector : std::false_type {};
49
50 template <> struct librarySelector<modelUnit> : std::true_type {};
51 template <> struct librarySelector<modelName> : std::true_type {};
52
53 template <> struct librarySelector<dotInclude> : std::true_type {};
54 template <> struct librarySelector<dotIncludePathWithoutQuotes> : std::true_type {};
55 template <> struct librarySelector<dotIncludePathWithoutApostrophes> : std::true_type {};
56 template <> struct librarySelector<dotIncludePath> : std::true_type {};
57
58 // For debugging.
59 template <> struct librarySelector<unknownLine> : std::true_type {};
60};
61
62
63void SPICE_LIBRARY_PARSER::parseFile( const wxString &aFilePath, REPORTER& aReporter,
64 std::vector<std::pair<std::string, std::string>>* aQueue )
65{
66 try
67 {
68 tao::pegtl::string_input<> in( SafeReadFile( aFilePath, wxS( "r" ) ).ToStdString(),
69 aFilePath.ToStdString() );
70 auto root = tao::pegtl::parse_tree::parse<SIM_LIBRARY_SPICE_PARSER::libraryGrammar,
72 tao::pegtl::nothing,
74
75 for( const auto& node : root->children )
76 {
77 if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() )
78 {
79 aQueue->emplace_back( node->children.at( 0 )->string(), node->string() );
80 }
81 else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::dotInclude>() )
82 {
83 wxString lib = m_library.m_pathResolver( node->children.at( 0 )->string(), aFilePath );
84
85 try
86 {
87 parseFile( lib, aReporter, aQueue );
88 }
89 catch( const IO_ERROR& e )
90 {
91 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
92 }
93 }
94 else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::unknownLine>() )
95 {
96 // Do nothing.
97 }
98 else
99 {
100 wxFAIL_MSG( "Unhandled parse tree node" );
101 }
102 }
103 }
104 catch( const IO_ERROR& e )
105 {
106 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
107 }
108 catch( const tao::pegtl::parse_error& e )
109 {
110 aReporter.Report( e.what(), RPT_SEVERITY_ERROR );
111 }
112}
113
114
115void SPICE_LIBRARY_PARSER::ReadFile( const wxString& aFilePath, REPORTER& aReporter )
116{
117 m_library.m_models.clear();
118 m_library.m_modelNames.clear();
119
120 std::vector<std::pair<std::string, std::string>> modelQueue;
121
122 parseFile( aFilePath, aReporter, &modelQueue );
123
124 m_library.m_models.reserve( modelQueue.size() );
125 m_library.m_modelNames.reserve( modelQueue.size() );
126
127 for( const auto& [name, source] : modelQueue )
128 {
129 m_library.m_models.emplace_back( nullptr );
130 m_library.m_modelNames.emplace_back( name );
131 }
132
133 auto createModel =
134 [&]( int ii, bool firstPass )
135 {
136 m_library.m_models[ii] = SIM_MODEL_SPICE::Create( m_library, modelQueue[ii].second,
137 firstPass, aReporter );
138 };
139
140 // Read all self-contained models in parallel
142
143 tp.push_loop( modelQueue.size(),
144 [&]( const int a, const int b )
145 {
146 for( int ii = a; ii < b; ++ii )
147 createModel( ii, true );
148 } );
149 tp.wait_for_tasks();
150
151 // Now read all models that might refer to other models in order.
152 for( int ii = 0; ii < (int) modelQueue.size(); ++ii )
153 {
154 if( !m_library.m_models[ii] )
155 createModel( ii, false );
156 }
157}
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