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, 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 <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#include <wx/strconv.h>
36
37#include <pegtl.hpp>
38#include <pegtl/contrib/parse_tree.hpp>
39
40
42{
43 using namespace SPICE_GRAMMAR;
44
45 // TODO: unknownLine is already handled in spiceUnit.
47
48
49 template <typename Rule> struct librarySelector : std::false_type {};
50
51 template <> struct librarySelector<modelUnit> : std::true_type {};
52 template <> struct librarySelector<modelName> : std::true_type {};
53
54 template <> struct librarySelector<dotInclude> : std::true_type {};
55 template <> struct librarySelector<dotIncludePathWithoutQuotes> : std::true_type {};
56 template <> struct librarySelector<dotIncludePathWithoutApostrophes> : std::true_type {};
57 template <> struct librarySelector<dotIncludePath> : std::true_type {};
58
59 // For debugging.
60 template <> struct librarySelector<unknownLine> : std::true_type {};
61};
62
63
64void SPICE_LIBRARY_PARSER::parseFile( const wxString &aFilePath, REPORTER& aReporter,
65 std::vector<std::pair<std::string, std::string>>* aQueue )
66{
67 try
68 {
69 std::string fileContents = SafeReadFile( aFilePath, wxS( "r" ) ).ToStdString( wxConvUTF8 );
70 std::string filePath = aFilePath.ToStdString( wxConvUTF8 );
71
72 tao::pegtl::string_input<> in( fileContents, filePath );
73 auto root = tao::pegtl::parse_tree::parse<SIM_LIBRARY_SPICE_PARSER::libraryGrammar,
75 tao::pegtl::nothing,
77
78 for( const auto& node : root->children )
79 {
80 if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() )
81 {
82 aQueue->emplace_back( node->children.at( 0 )->string(), node->string() );
83 }
84 else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::dotInclude>() )
85 {
86 wxString lib = m_library.m_pathResolver( node->children.at( 0 )->string(), aFilePath );
87
88 try
89 {
90 parseFile( lib, aReporter, aQueue );
91 }
92 catch( const IO_ERROR& e )
93 {
94 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
95 }
96 }
97 else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::unknownLine>() )
98 {
99 // Do nothing.
100 }
101 else
102 {
103 wxFAIL_MSG( "Unhandled parse tree node" );
104 }
105 }
106 }
107 catch( const IO_ERROR& e )
108 {
109 aReporter.Report( e.What(), RPT_SEVERITY_ERROR );
110 }
111 catch( const tao::pegtl::parse_error& e )
112 {
113 aReporter.Report( e.what(), RPT_SEVERITY_ERROR );
114 }
115}
116
117
118void SPICE_LIBRARY_PARSER::ReadFile( const wxString& aFilePath, REPORTER& aReporter )
119{
120 m_library.m_models.clear();
121 m_library.m_modelNames.clear();
122
123 std::vector<std::pair<std::string, std::string>> modelQueue;
124
125 parseFile( aFilePath, aReporter, &modelQueue );
126
127 m_library.m_models.reserve( modelQueue.size() );
128 m_library.m_modelNames.reserve( modelQueue.size() );
129
130 for( const auto& [name, source] : modelQueue )
131 {
132 m_library.m_models.emplace_back( nullptr );
133 m_library.m_modelNames.emplace_back( name );
134 }
135
136 auto createModel =
137 [&]( int ii, bool firstPass )
138 {
139 m_library.m_models[ii] = SIM_MODEL_SPICE::Create( m_library, modelQueue[ii].second,
140 firstPass, aReporter );
141 };
142
143 // Read all self-contained models in parallel
145
146 auto results = tp.submit_loop( 0, modelQueue.size(),
147 [&]( const int ii )
148 {
149 createModel( ii, true );
150 } );
151 results.wait();
152
153 // Now read all models that might refer to other models in order.
154 for( int ii = 0; ii < (int) modelQueue.size(); ++ii )
155 {
156 if( !m_library.m_models[ii] )
157 createModel( ii, false );
158 }
159}
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()
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
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:96
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::thread_pool< 0 > thread_pool
Definition thread_pool.h:31