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