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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <stdexcept>
24#include <utility>
25
26#include <thread_pool.h>
27#include <ki_exception.h>
29#include <sim/spice_grammar.h>
30#include <sim/sim_model_spice.h>
31#include <richio.h>
32#include <wx/strconv.h>
33
34#include <pegtl.hpp>
35#include <pegtl/contrib/parse_tree.hpp>
36
37
39{
40 using namespace SPICE_GRAMMAR;
41
42 // TODO: unknownLine is already handled in spiceUnit.
44
45
46 template <typename Rule> struct librarySelector : std::false_type {};
47
48 template <> struct librarySelector<modelUnit> : std::true_type {};
49 template <> struct librarySelector<modelName> : std::true_type {};
50
51 template <> struct librarySelector<dotInclude> : std::true_type {};
52 template <> struct librarySelector<dotIncludePathWithoutQuotes> : std::true_type {};
53 template <> struct librarySelector<dotIncludePathWithoutApostrophes> : std::true_type {};
54 template <> struct librarySelector<dotIncludePath> : std::true_type {};
55
56 // For debugging.
57 template <> struct librarySelector<unknownLine> : std::true_type {};
58};
59
60
61void SPICE_LIBRARY_PARSER::parseFile( const wxString &aFilePath, REPORTER& aReporter,
62 std::vector<std::pair<std::string, std::string>>* aQueue )
63{
64 try
65 {
66 std::string fileContents = SafeReadFile( aFilePath, wxS( "r" ) ).ToStdString( wxConvUTF8 );
67 std::string filePath = aFilePath.ToStdString( wxConvUTF8 );
68
69 tao::pegtl::string_input<> in( fileContents, filePath );
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 catch( const std::out_of_range& e )
113 {
114 aReporter.Report( wxString::Format( _( "Error parsing SPICE library '%s': %s" ),
115 aFilePath, e.what() ),
117 }
118}
119
120
121void SPICE_LIBRARY_PARSER::ReadFile( const wxString& aFilePath, REPORTER& aReporter )
122{
123 m_library.m_models.clear();
124 m_library.m_modelNames.clear();
125
126 std::vector<std::pair<std::string, std::string>> modelQueue;
127
128 parseFile( aFilePath, aReporter, &modelQueue );
129
130 m_library.m_models.reserve( modelQueue.size() );
131 m_library.m_modelNames.reserve( modelQueue.size() );
132
133 for( const auto& [name, source] : modelQueue )
134 {
135 m_library.m_models.emplace_back( nullptr );
136 m_library.m_modelNames.emplace_back( name );
137 }
138
139 auto createModel =
140 [&]( int ii, bool firstPass )
141 {
142 m_library.m_models[ii] = SIM_MODEL_SPICE::Create( m_library, modelQueue[ii].second,
143 firstPass, aReporter );
144 };
145
146 // Read all self-contained models in parallel
148
149 auto results = tp.submit_loop( 0, modelQueue.size(),
150 [&]( const int ii )
151 {
152 createModel( ii, true );
153 } );
154 results.wait();
155
156 // Now read all models that might refer to other models in order.
157 for( int ii = 0; ii < (int) modelQueue.size(); ++ii )
158 {
159 if( !m_library.m_models[ii] )
160 createModel( ii, false );
161 }
162}
const char * name
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual const char * what() const override
std::exception interface, returned as UTF-8
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:100
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)
#define _(s)
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:53
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27