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