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 static double ToDouble( const std::string& aString, double aDefault = NAN );
87
88 static int ToInt( const std::string& aString, int aDefault = -1 );
89
90 static bool Equal( double aLH, const std::string& aRH );
91};
92
93
94namespace SIM_VALUE_GRAMMAR
95{
96 template <NOTATION Notation>
97 std::string allowedIntChars;
98
99
100 struct digits : plus<tao::pegtl::digit> {}; // For some reason it fails on just "digit".
101
102 struct sign : one<'+', '-'> {};
103
104 struct intPart : seq<opt<sign>, digits> {};
105
106 //struct fracPartPrefix : one<'.'> {};
107 struct fracPart : digits {};
108 //struct fracPartWithPrefix : seq<fracPartPrefix, fracPart> {};
109
110
111 template <SIM_VALUE::TYPE ValueType>
113
114 template <> struct significand<SIM_VALUE::TYPE_FLOAT> :
115 sor<seq<intPart, one<'.'>, fracPart>,
116 seq<intPart, one<'.'>>,
117 intPart,
118 seq<one<'.'>, fracPart>,
119 one<'.'>,
120 one<'-'>> {};
121
122 template <> struct significand<SIM_VALUE::TYPE_INT> : intPart {};
123
124
125 struct exponentPrefix : one<'e', 'E'> {};
126 struct exponent : seq<opt<sign>, opt<digits>> {};
127 struct exponentWithPrefix : seq<exponentPrefix, exponent> {};
128
129
130 template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
132
133 template <> struct unitPrefix<SIM_VALUE::TYPE_INT, NOTATION::SI>
134 : one<'k', 'K', 'M', 'G', 'T', 'P', 'E'> {};
135 template <> struct unitPrefix<SIM_VALUE::TYPE_INT, NOTATION::SPICE>
136 : sor<TAO_PEGTL_ISTRING( "k" ),
137 TAO_PEGTL_ISTRING( "Meg" ),
138 TAO_PEGTL_ISTRING( "G" ),
139 TAO_PEGTL_ISTRING( "T" )> {};
140
141 template <> struct unitPrefix<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>
142 : one<'a', 'f', 'p', 'n', 'u', 'm', 'k', 'K', 'M', 'G', 'T', 'P', 'E'> {};
143 template <> struct unitPrefix<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>
144 : sor<TAO_PEGTL_ISTRING( "f" ),
145 TAO_PEGTL_ISTRING( "p" ),
146 TAO_PEGTL_ISTRING( "n" ),
147 TAO_PEGTL_ISTRING( "u" ),
148 TAO_PEGTL_ISTRING( "Meg" ), // "Meg" must be before "m".
149 TAO_PEGTL_ISTRING( "m" ),
150 //TAO_PEGTL_ISTRING( "mil" ),
151 TAO_PEGTL_ISTRING( "k" ),
152 TAO_PEGTL_ISTRING( "G" ),
153 TAO_PEGTL_ISTRING( "T" )> {};
154
155
156 template <NOTATION Notation>
158
159 template <> struct garbageSuffix<NOTATION::SI> : seq<> {};
160 template <> struct garbageSuffix<NOTATION::SPICE> : star<alpha> {};
161
162
163 template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
164 struct number : seq<opt<significand<ValueType>>,
165 opt<exponentWithPrefix>,
166 opt<unitPrefix<ValueType, Notation>>,
167 garbageSuffix<Notation>> {};
168
169 template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
170 struct numberGrammar : must<number<ValueType, Notation>, tao::pegtl::eof> {};
171
172
173 bool IsValid( const std::string& aString,
175 NOTATION aNotation = NOTATION::SI );
176}
177
178#endif // SIM_VALUE_H
static std::string Normalize(double aValue)
Definition: sim_value.cpp:406
static bool Equal(double aLH, const std::string &aRH)
Definition: sim_value.cpp:487
static std::string ConvertNotation(const std::string &aString, NOTATION aFromNotation, NOTATION aToNotation)
Definition: sim_value.cpp:370
@ 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:442
static int ToInt(const std::string &aString, int aDefault=-1)
Definition: sim_value.cpp:467
static std::string ToSpice(const std::string &aString)
Definition: sim_value.cpp:419
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:129
std::string allowedIntChars
Definition: sim_value.h:97