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 struct quotedStringContent : star<not_at<one<'"'>>, any> {}; // TODO: Allow escaping '"'.
57 struct quotedString : seq<one<'"'>,
58 quotedStringContent,
59 one<'"'>> {};
60
61 struct flagParam : sor<seq<TAO_PEGTL_ISTRING( "off" ), not_at<alnum>>, // VDMOS
62 seq<TAO_PEGTL_ISTRING( "thermal" ), not_at<alnum>>, // VDMOS
63 seq<TAO_PEGTL_ISTRING( "xpart" ), not_at<alnum>>> {}; // BSIM1
64
65 struct fieldParamValuePair : sor<flagParam,
66 if_must<param,
67 opt<sep>,
68 one<'='>,
69 opt<sep>,
70 sor<quotedString,
71 unquotedString>>> {};
72 struct fieldParamValuePairs : list<fieldParamValuePair, sep> {};
73 struct fieldParamValuePairsGrammar : must<opt<sep>,
74 opt<fieldParamValuePairs>,
75 opt<sep>,
76 tao::pegtl::eof> {};
77
78 struct fieldInferValue : sor<one<'R', 'C', 'L', 'V', 'I'>,
79 number<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>> {};
80 struct fieldInferValueGrammar : must<opt<sep>,
81 fieldInferValue,
82 opt<sep>,
83 tao::pegtl::eof> {};
84
85
86 template <typename> inline constexpr const char* errorMessage = nullptr;
87 template <> inline constexpr auto errorMessage<opt<sep>> = "";
88 template <> inline constexpr auto errorMessage<opt<pinSequence>> = "";
89 template <> inline constexpr auto errorMessage<one<'='>> =
90 "expected '='";
91 template <> inline constexpr auto errorMessage<sor<quotedString,
93 "expected quoted or unquoted string";
94 template <> inline constexpr auto errorMessage<fieldParamValuePairs> =
95 "expected parameter=value pairs";
96 template <> inline constexpr auto errorMessage<opt<fieldParamValuePairs>> = "";
97 template <> inline constexpr auto errorMessage<fieldInferValue> =
98 "expected 'R', 'C', 'L', 'V', 'I' or a number";
99 template <> inline constexpr auto errorMessage<tao::pegtl::eof> =
100 "expected end of string";
101
102 struct error
103 {
104 template <typename Rule> static constexpr bool raise_on_failure = false;
105 template <typename Rule> static constexpr auto message = errorMessage<Rule>;
106 };
107
108 template <typename Rule> using control = must_if<error>::control<Rule>;
109}
110
111
116{
117public:
118 virtual ~SIM_MODEL_SERIALIZER() = default;
119 SIM_MODEL_SERIALIZER( SIM_MODEL& aModel ) : m_model( aModel ) {}
120
121 std::string GenerateDevice() const;
122 std::string GenerateDeviceSubtype() const;
123 std::string GenerateValue() const;
124 std::string GenerateParams() const;
125 std::string GeneratePins() const;
126 std::string GenerateEnable() const;
127
128 void ParseValue( const std::string& aValue );
129 bool ParseParams( const std::string& aParams );
130 void ParsePins( const std::string& aPins );
131 void ParseEnable( const std::string& aEnable );
132
133protected:
134 std::string generateParamValuePair( const SIM_MODEL::PARAM& aParam ) const;
135
136private:
138};
139
140#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