/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2022 Mikolaj Wielgus * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, you may find one here: * https://www.gnu.org/licenses/gpl-3.0.html * or you may search the http://www.gnu.org website for the version 3 license, * or you may write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #ifndef SIM_MODEL_SERIALIZER_H #define SIM_MODEL_SERIALIZER_H #include namespace SIM_MODEL_SERIALIZER_GRAMMAR { using namespace SIM_VALUE_GRAMMAR; struct sep : plus {}; struct legacyPinNumber : digits {}; struct legacyPinSequence : list {}; struct legacyPinSequenceGrammar : must {}; struct pinSymbolPinNumber : plus, not_one<'='>> {}; struct pinName : plus, any> {}; struct pinAssignment : seq>, any> {}; // TODO: Allow escaping '"'. struct quotedString : seq, quotedStringContent, one<'"'>> {}; struct flagParam : sor {}; // BSIM1 struct fieldParamValuePair : sor, one<'='>, opt, sor>> {}; struct fieldParamValuePairs : list {}; struct fieldParamValuePairsGrammar : must, opt, opt, tao::pegtl::eof> {}; struct fieldInferValue : sor, number> {}; struct fieldInferValueGrammar : must, fieldInferValue, opt, tao::pegtl::eof> {}; template inline constexpr const char* errorMessage = nullptr; template <> inline constexpr auto errorMessage> = ""; template <> inline constexpr auto errorMessage> = ""; template <> inline constexpr auto errorMessage> = "expected '='"; template <> inline constexpr auto errorMessage> = "expected quoted or unquoted string"; template <> inline constexpr auto errorMessage = "expected parameter=value pairs"; template <> inline constexpr auto errorMessage> = ""; template <> inline constexpr auto errorMessage = "expected 'R', 'C', 'L', 'V', 'I' or a number"; template <> inline constexpr auto errorMessage = "expected end of string"; struct error { template static constexpr bool raise_on_failure = false; template static constexpr auto message = errorMessage; }; template using control = must_if::control; } /** * Serializes/deserializes a SIM_MODEL for storage in LIB_FIELDs/SCH_FIELDs. */ class SIM_MODEL_SERIALIZER { public: static constexpr auto REFERENCE_FIELD = "Reference"; static constexpr auto VALUE_FIELD = "Value"; virtual ~SIM_MODEL_SERIALIZER() = default; SIM_MODEL_SERIALIZER( SIM_MODEL& aModel ) : m_model( aModel ) {} std::string GenerateDevice() const; std::string GenerateDeviceSubtype() const; std::string GenerateValue() const; std::string GenerateParams() const; std::string GeneratePins() const; std::string GenerateEnable() const; void ParseValue( const std::string& aValue ); bool ParseParams( const std::string& aParams ); void ParsePins( const std::string& aPins ); void ParseEnable( const std::string& aEnable ); protected: std::string generateParamValuePair( const SIM_MODEL::PARAM& aParam ) const; private: SIM_MODEL& m_model; }; #endif // SIM_MODEL_SERIALIZER_H