You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.3 KiB

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