KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sim_value.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-2023 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_VALUE_H
26#define SIM_VALUE_H
27
28#include <wx/string.h>
29#include <optional>
30#include <complex>
31#include <memory>
32#include <pegtl.hpp>
33
34// Undef some annoying defines in windows headers added by pegtl.hpp
35// that can create issues in .cpp files, mainly on msys2
36#if defined (__MINGW32__)
37#if defined ( LoadLibrary )
38 #undef LoadLibrary
39#endif
40#if defined ( GetClassInfo )
41 #undef GetClassInfo
42#endif
43#endif
44
45
47{
48 using namespace tao::pegtl;
49
50 enum class NOTATION
51 {
52 SI,
53 SPICE
54 };
55}
56
57
59{
60public:
62
63 // Names like BOOL, INT, FLOAT need to be prefixed or MS Windows compilers will complain (enum
64 // class doesn't help).
65 enum TYPE
66 {
72
77 };
78
79 static std::string ConvertNotation( const std::string& aString, NOTATION aFromNotation,
80 NOTATION aToNotation );
81
82 static std::string Normalize( double aValue );
83
84 static std::string ToSpice( const std::string& aString )
85 {
86 return ConvertNotation( aString, NOTATION::SI, NOTATION::SPICE );
87 }
88
89 static double ToDouble( const std::string& aString, double aDefault = NAN );
90
91 static int ToInt( const std::string& aString, int aDefault = -1 );
92
93 static bool Equal( double aLH, const std::string& aRH );
94};
95
96
97namespace SIM_VALUE_GRAMMAR
98{
99 template <NOTATION Notation>
100 std::string allowedIntChars;
101
102
103 struct digits : plus<tao::pegtl::digit> {}; // For some reason it fails on just "digit".
104
105 struct sign : one<'+', '-'> {};
106
107 struct intPart : seq<opt<sign>, digits> {};
108
109 //struct fracPartPrefix : one<'.'> {};
110 struct fracPart : digits {};
111 //struct fracPartWithPrefix : seq<fracPartPrefix, fracPart> {};
112
113
114 template <SIM_VALUE::TYPE ValueType>
116
117 template <> struct significand<SIM_VALUE::TYPE_FLOAT> :
118 sor<seq<intPart, one<'.'>, fracPart>,
119 seq<intPart, one<'.'>>,
120 intPart,
121 seq<one<'.'>, fracPart>,
122 one<'.'>,
123 one<'-'>> {};
124
125 template <> struct significand<SIM_VALUE::TYPE_INT> : intPart {};
126
127
128 struct exponentPrefix : one<'e', 'E'> {};
129 struct exponent : seq<opt<sign>, opt<digits>> {};
130 struct exponentWithPrefix : seq<exponentPrefix, exponent> {};
131
132
133 template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
135
136 template <> struct unitPrefix<SIM_VALUE::TYPE_INT, NOTATION::SI>
137 : one<'k', 'K', 'M', 'G', 'T', 'P', 'E'> {};
138 template <> struct unitPrefix<SIM_VALUE::TYPE_INT, NOTATION::SPICE>
139 : sor<TAO_PEGTL_ISTRING( "k" ),
140 TAO_PEGTL_ISTRING( "Meg" ),
141 TAO_PEGTL_ISTRING( "G" ),
142 TAO_PEGTL_ISTRING( "T" )> {};
143
144 template <> struct unitPrefix<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>
145 : one<'a', 'f', 'p', 'n', 'u', 'm', 'k', 'K', 'M', 'G', 'T', 'P', 'E'> {};
146 template <> struct unitPrefix<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>
147 : sor<TAO_PEGTL_ISTRING( "f" ),
148 TAO_PEGTL_ISTRING( "p" ),
149 TAO_PEGTL_ISTRING( "n" ),
150 TAO_PEGTL_ISTRING( "u" ),
151 TAO_PEGTL_ISTRING( "Meg" ), // "Meg" must be before "m".
152 TAO_PEGTL_ISTRING( "m" ),
153 //TAO_PEGTL_ISTRING( "mil" ),
154 TAO_PEGTL_ISTRING( "k" ),
155 TAO_PEGTL_ISTRING( "G" ),
156 TAO_PEGTL_ISTRING( "T" )> {};
157
158
159 template <NOTATION Notation>
161
162 template <> struct garbageSuffix<NOTATION::SI> : seq<> {};
163 template <> struct garbageSuffix<NOTATION::SPICE> : star<alpha> {};
164
165
166 template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
167 struct number : seq<opt<significand<ValueType>>,
168 opt<exponentWithPrefix>,
169 opt<unitPrefix<ValueType, Notation>>,
170 garbageSuffix<Notation>> {};
171
172 template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
173 struct numberGrammar : must<number<ValueType, Notation>, tao::pegtl::eof> {};
174
175
176 bool IsValid( const std::string& aString,
178 NOTATION aNotation = NOTATION::SI );
179}
180
181#endif // SIM_VALUE_H
static std::string Normalize(double aValue)
Definition: sim_value.cpp:405
static bool Equal(double aLH, const std::string &aRH)
Definition: sim_value.cpp:463
static std::string ConvertNotation(const std::string &aString, NOTATION aFromNotation, NOTATION aToNotation)
Definition: sim_value.cpp:369
static std::string ToSpice(const std::string &aString)
Definition: sim_value.h:84
@ TYPE_BOOL
Definition: sim_value.h:67
@ TYPE_FLOAT_VECTOR
Definition: sim_value.h:75
@ TYPE_BOOL_VECTOR
Definition: sim_value.h:73
@ TYPE_INT
Definition: sim_value.h:68
@ TYPE_FLOAT
Definition: sim_value.h:69
@ TYPE_INT_VECTOR
Definition: sim_value.h:74
@ TYPE_COMPLEX_VECTOR
Definition: sim_value.h:76
@ TYPE_STRING
Definition: sim_value.h:71
@ TYPE_COMPLEX
Definition: sim_value.h:70
static double ToDouble(const std::string &aString, double aDefault=NAN)
Definition: sim_value.cpp:418
static int ToInt(const std::string &aString, int aDefault=-1)
Definition: sim_value.cpp:443
SIM_VALUE_GRAMMAR::NOTATION NOTATION
Definition: sim_value.h:61
bool IsValid(const std::string &aString, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT, NOTATION aNotation=NOTATION::SI)
Definition: sim_value.cpp:128
std::string allowedIntChars
Definition: sim_value.h:100