KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_model_serializer.h
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
21#ifndef SIM_MODEL_SERIALIZER_H
22#define SIM_MODEL_SERIALIZER_H
23
24#include <sim/sim_model.h>
25
26
28{
29 using namespace SIM_VALUE_GRAMMAR;
30
31 struct sep : plus<space> {};
32
33
35 struct legacyPinSequence : list<legacyPinNumber, sep> {};
36
37 struct legacyPinSequenceGrammar : must<legacyPinSequence,
38 tao::pegtl::eof> {};
39
40
41 struct pinSymbolPinNumber : plus<not_at<sep>, not_one<'='>> {};
42 struct pinName : plus<not_at<sep>, any> {};
43 struct pinAssignment : seq<pinSymbolPinNumber,
44 one<'='>,
45 pinName> {};
46 struct pinSequence : list<pinAssignment,
47 sep> {};
48 struct pinSequenceGrammar : must<opt<sep>,
49 opt<pinSequence>,
50 opt<sep>,
51 tao::pegtl::eof> {};
52
53 struct param : identifier {};
54
55 struct unquotedString : plus<not_at<sep>, any> {};
56 // Allow \" and \\ inside quoted values so nested quotes (e.g. model="… infile=\"file\"") work.
57 struct escapedChar : seq<one<'\\'>, one<'"', '\\'>> {};
58 struct quotedChar : sor<escapedChar, not_one<'"'>> {};
59 struct quotedStringContent : star<quotedChar> {};
60 struct quotedString : seq<one<'"'>,
61 quotedStringContent,
62 one<'"'>> {};
63
64 struct flagParam : sor<seq<TAO_PEGTL_ISTRING( "off" ), not_at<alnum>>, // VDMOS
65 seq<TAO_PEGTL_ISTRING( "thermal" ), not_at<alnum>>, // VDMOS
66 seq<TAO_PEGTL_ISTRING( "xpart" ), not_at<alnum>>> {}; // BSIM1
67
68 struct fieldParamValuePair : sor<flagParam,
69 if_must<param,
70 opt<sep>,
71 one<'='>,
72 opt<sep>,
73 sor<quotedString,
74 unquotedString>>> {};
75 struct fieldParamValuePairs : list<fieldParamValuePair, sep> {};
76 struct fieldParamValuePairsGrammar : must<opt<sep>,
77 opt<fieldParamValuePairs>,
78 opt<sep>,
79 tao::pegtl::eof> {};
80
81 struct fieldInferValue : sor<one<'R', 'C', 'L', 'V', 'I'>,
82 number<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>> {};
83 struct fieldInferValueGrammar : must<opt<sep>,
84 fieldInferValue,
85 opt<sep>,
86 tao::pegtl::eof> {};
87
88
89 template <typename> inline constexpr const char* errorMessage = nullptr;
90 template <> inline constexpr auto errorMessage<opt<sep>> = "";
91 template <> inline constexpr auto errorMessage<opt<pinSequence>> = "";
92 template <> inline constexpr auto errorMessage<one<'='>> =
93 "expected '='";
94 template <> inline constexpr auto errorMessage<sor<quotedString,
96 "expected quoted or unquoted string";
97 template <> inline constexpr auto errorMessage<fieldParamValuePairs> =
98 "expected parameter=value pairs";
99 template <> inline constexpr auto errorMessage<opt<fieldParamValuePairs>> = "";
100 template <> inline constexpr auto errorMessage<fieldInferValue> =
101 "expected 'R', 'C', 'L', 'V', 'I' or a number";
102 template <> inline constexpr auto errorMessage<tao::pegtl::eof> =
103 "expected end of string";
104
105 struct error
106 {
107 template <typename Rule> static constexpr bool raise_on_failure = false;
108 template <typename Rule> static constexpr auto message = errorMessage<Rule>;
109 };
110
111 template <typename Rule> using control = must_if<error>::control<Rule>;
112}
113
114
119{
120public:
121 virtual ~SIM_MODEL_SERIALIZER() = default;
122 SIM_MODEL_SERIALIZER( SIM_MODEL& aModel ) : m_model( aModel ) {}
123
124 std::string GenerateDevice() const;
125 std::string GenerateDeviceSubtype() const;
126 std::string GenerateValue() const;
127 std::string GenerateParams() const;
128 std::string GeneratePins() const;
129 std::string GenerateEnable() const;
130
131 void ParseValue( const std::string& aValue );
132 bool ParseParams( const std::string& aParams );
133 void ParsePins( const std::string& aPins );
134 void ParseEnable( const std::string& aEnable );
135
136protected:
137 std::string generateParamValuePair( const SIM_MODEL::PARAM& aParam ) const;
138
139private:
141};
142
143#endif // SIM_MODEL_SERIALIZER_H
std::string GeneratePins() const
SIM_MODEL_SERIALIZER(SIM_MODEL &aModel)
void ParsePins(const std::string &aPins)
std::string generateParamValuePair(const SIM_MODEL::PARAM &aParam) const
std::string GenerateParams() const
std::string GenerateValue() const
void ParseValue(const std::string &aValue)
void ParseEnable(const std::string &aEnable)
std::string GenerateEnable() const
std::string GenerateDevice() const
virtual ~SIM_MODEL_SERIALIZER()=default
bool ParseParams(const std::string &aParams)
std::string GenerateDeviceSubtype() const
constexpr const char * errorMessage
must_if< error >::control< Rule > control