2022-04-01 04:30:50 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
2023-01-12 12:46:52 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-04-01 04:30:50 +00:00
|
|
|
*
|
|
|
|
* 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 SPICE_GRAMMAR_H
|
|
|
|
#define SPICE_GRAMMAR_H
|
|
|
|
|
|
|
|
#include <sim/sim_value.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace SPICE_GRAMMAR
|
|
|
|
{
|
|
|
|
using namespace SIM_VALUE_GRAMMAR;
|
|
|
|
|
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
struct garbage : plus<one<' ', '\t', '=', '(', ')', ','>> {};
|
|
|
|
struct leaders : plus<one<' ', '\t'>> {};
|
|
|
|
struct trailers : plus<one<' ', '\t', '\v', '\f'>> {};
|
|
|
|
|
2022-10-20 03:12:01 +00:00
|
|
|
struct garbageOrEolf : sor<garbage, eolf> {};
|
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
// NOTE: In Ngspice, a '$' opening a comment must be preceded by ' ', ',', or '\t'. We don't
|
|
|
|
// implement that here - this may cause problems in the future.
|
|
|
|
// Ngspice supports '//' for comments.
|
2022-09-12 01:59:42 +00:00
|
|
|
struct eolfCommentStart : sor<one<';', '$'>,
|
2022-05-31 02:55:48 +00:00
|
|
|
string<'/', '/'>> {};
|
|
|
|
|
2022-09-12 01:59:42 +00:00
|
|
|
struct eolfComment : seq<eolfCommentStart,
|
|
|
|
until<eolf>> {};
|
2022-05-31 02:55:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct commentLine : seq<opt<garbage>,
|
|
|
|
one<'*'>,
|
2022-09-12 01:59:42 +00:00
|
|
|
until<eolf>> {};
|
2022-04-01 04:30:50 +00:00
|
|
|
|
|
|
|
|
2022-09-12 01:59:42 +00:00
|
|
|
struct newline : seq<sor<eolf,
|
|
|
|
eolfComment>,
|
2022-04-01 04:30:50 +00:00
|
|
|
not_at<one<'+'>>> {};
|
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
struct backslashContinuation : seq<string<'\\', '\\'>,
|
|
|
|
opt<trailers>,
|
2022-09-12 01:59:42 +00:00
|
|
|
eolf> {};
|
2022-05-31 02:55:48 +00:00
|
|
|
|
2022-09-12 01:59:42 +00:00
|
|
|
struct commentBackslashContinuation : seq<eolfCommentStart,
|
|
|
|
seq<star<not_at<eolf>,
|
2022-05-31 02:55:48 +00:00
|
|
|
not_at<string<'\\', '\\'>,
|
|
|
|
opt<trailers>,
|
2022-09-12 01:59:42 +00:00
|
|
|
eolf>,
|
2022-05-31 02:55:48 +00:00
|
|
|
any>,
|
|
|
|
string<'\\', '\\'>,
|
|
|
|
opt<trailers>,
|
2022-09-12 01:59:42 +00:00
|
|
|
eolf>> {};
|
2022-05-31 02:55:48 +00:00
|
|
|
|
|
|
|
|
2022-09-12 01:59:42 +00:00
|
|
|
struct plusContinuation : seq<sor<eolf,
|
|
|
|
eolfComment>,
|
2022-05-31 02:55:48 +00:00
|
|
|
star<commentLine>,
|
|
|
|
opt<leaders>,
|
|
|
|
one<'+'>> {};
|
|
|
|
|
|
|
|
struct continuation : seq<opt<garbage>,
|
|
|
|
sor<backslashContinuation,
|
|
|
|
commentBackslashContinuation,
|
|
|
|
plusContinuation>,
|
|
|
|
opt<garbage>> {};
|
|
|
|
|
2022-10-04 20:41:19 +00:00
|
|
|
|
|
|
|
// Token separator.
|
2022-05-31 02:55:48 +00:00
|
|
|
struct sep : sor<plus<continuation>,
|
|
|
|
garbage> {};
|
|
|
|
|
2022-10-20 03:12:01 +00:00
|
|
|
struct modelName : plus<not_at<garbageOrEolf>, any> {};
|
2022-09-17 06:27:47 +00:00
|
|
|
|
2022-11-29 08:47:30 +00:00
|
|
|
struct dotModelType : sor<// VDMOS models have a special syntax.
|
|
|
|
seq<TAO_PEGTL_ISTRING( "vdmos" ),
|
|
|
|
sep,
|
|
|
|
sor<TAO_PEGTL_ISTRING( "nchan" ),
|
|
|
|
TAO_PEGTL_ISTRING( "pchan" )>>,
|
2023-02-03 23:49:19 +00:00
|
|
|
plus<not_at<garbageOrEolf>, any>> {};
|
2022-09-17 06:27:47 +00:00
|
|
|
|
2023-01-13 21:58:45 +00:00
|
|
|
struct vectorExpr : seq<one<'['>,
|
|
|
|
star<not_one<']'>>,
|
|
|
|
one<']'>> {};
|
|
|
|
|
|
|
|
struct bracedExpr : seq<one<'{'>,
|
2024-05-12 14:37:34 +00:00
|
|
|
star<not_one<'}'>>,
|
2023-01-13 21:58:45 +00:00
|
|
|
one<'}'>> {};
|
2022-11-27 05:16:15 +00:00
|
|
|
|
2022-08-30 15:24:13 +00:00
|
|
|
// Ngspice has some heuristic logic to allow + and - in tokens. We replicate that here.
|
2022-05-31 02:55:48 +00:00
|
|
|
struct tokenStart : seq<opt<one<'+', '-'>>,
|
|
|
|
opt<seq<star<sor<tao::pegtl::digit,
|
|
|
|
one<'.'>>>,
|
|
|
|
one<'e', 'E'>,
|
|
|
|
opt<one<'+', '-'>>>>> {};
|
|
|
|
|
|
|
|
struct token : seq<tokenStart,
|
2022-09-12 01:59:42 +00:00
|
|
|
star<not_at<eolf>,
|
2022-05-31 02:55:48 +00:00
|
|
|
not_at<backslashContinuation>,
|
2023-02-04 19:49:09 +00:00
|
|
|
not_one<' ', '\t', '=', '(', ')', ',', ';'>>> {};
|
2022-05-31 02:55:48 +00:00
|
|
|
|
2022-10-04 20:41:19 +00:00
|
|
|
// Param names cannot be `token` because LTspice models contain spurious values without
|
2023-02-04 19:49:09 +00:00
|
|
|
// parameter names, which we need to skip, and because tokens can include a very limited
|
|
|
|
// subset of un-braced expressions
|
2024-05-13 20:10:02 +00:00
|
|
|
// Note: we must support lists of both braced expressions and tokens for CPL models...
|
|
|
|
// ... but lists of tokens breaks cases where we have single-token name/values.
|
2022-10-04 20:41:19 +00:00
|
|
|
struct param : identifier {};
|
2024-05-13 20:19:23 +00:00
|
|
|
struct paramValue : sor<list<bracedExpr, opt<one<' '>>>,
|
2023-01-13 21:58:45 +00:00
|
|
|
vectorExpr,
|
2024-05-13 20:10:02 +00:00
|
|
|
token> {};
|
2022-04-01 04:30:50 +00:00
|
|
|
|
|
|
|
struct paramValuePair : seq<param,
|
2022-05-31 02:55:48 +00:00
|
|
|
sep,
|
|
|
|
paramValue> {};
|
2022-10-18 21:19:48 +00:00
|
|
|
struct paramValuePairs : list<paramValuePair, sep> {};
|
2022-10-04 15:17:41 +00:00
|
|
|
struct dotModelAko : seq<opt<sep>,
|
|
|
|
if_must<seq<TAO_PEGTL_ISTRING( ".model" ),
|
|
|
|
sep,
|
|
|
|
modelName,
|
|
|
|
sep,
|
|
|
|
TAO_PEGTL_ISTRING( "ako:" )>,
|
|
|
|
opt<sep>,
|
|
|
|
modelName,
|
|
|
|
opt<sep,
|
|
|
|
dotModelType>,
|
|
|
|
opt<sep,
|
2022-10-20 01:55:47 +00:00
|
|
|
paramValuePairs>,
|
|
|
|
opt<sep>,
|
|
|
|
newline>> {};
|
|
|
|
|
2022-04-01 04:30:50 +00:00
|
|
|
struct dotModel : seq<opt<sep>,
|
2022-09-17 06:27:47 +00:00
|
|
|
if_must<TAO_PEGTL_ISTRING( ".model" ),
|
|
|
|
sep,
|
|
|
|
modelName,
|
|
|
|
sep,
|
|
|
|
dotModelType,
|
|
|
|
opt<sep,
|
|
|
|
paramValuePairs>,
|
|
|
|
opt<sep>,
|
|
|
|
newline>> {};
|
2022-10-04 15:17:41 +00:00
|
|
|
|
2022-04-01 04:30:50 +00:00
|
|
|
|
|
|
|
|
2022-11-26 09:20:29 +00:00
|
|
|
struct dotSubcktParamValuePair : seq<param,
|
|
|
|
// TODO: Check if these `star<space>`s match Ngspice's
|
|
|
|
// behavior.
|
|
|
|
star<space>,
|
2022-12-23 20:04:43 +00:00
|
|
|
opt<plusContinuation>,
|
2022-11-26 09:20:29 +00:00
|
|
|
one<'='>,
|
2022-12-23 20:04:43 +00:00
|
|
|
opt<plusContinuation>,
|
2022-11-26 09:20:29 +00:00
|
|
|
star<space>,
|
|
|
|
paramValue> {};
|
|
|
|
struct dotSubcktParamValuePairs : list<dotSubcktParamValuePair, sep> {};
|
|
|
|
struct dotSubcktParams : seq<opt<TAO_PEGTL_ISTRING( "params:" ),
|
|
|
|
opt<sep>>,
|
|
|
|
dotSubcktParamValuePairs> {};
|
|
|
|
struct dotSubcktPinName : seq<not_at<dotSubcktParams>,
|
2022-09-17 06:27:47 +00:00
|
|
|
plus<not_at<space>, any>> {};
|
2022-09-17 00:17:54 +00:00
|
|
|
struct dotSubcktPinSequence : list<dotSubcktPinName, sep> {};
|
2024-05-13 12:35:54 +00:00
|
|
|
struct dotSubcktEnd : seq<opt<sep>,
|
|
|
|
TAO_PEGTL_ISTRING( ".ends" ),
|
2022-06-12 03:39:13 +00:00
|
|
|
until<newline>> {};
|
|
|
|
struct spiceUnit;
|
2022-04-01 04:30:50 +00:00
|
|
|
struct dotSubckt : seq<opt<sep>,
|
2022-09-17 06:27:47 +00:00
|
|
|
if_must<TAO_PEGTL_ISTRING( ".subckt" ),
|
|
|
|
sep,
|
|
|
|
modelName,
|
|
|
|
opt<sep,
|
|
|
|
dotSubcktPinSequence>,
|
|
|
|
opt<sep,
|
|
|
|
dotSubcktParams>,
|
|
|
|
opt<sep>,
|
|
|
|
newline,
|
|
|
|
until<dotSubcktEnd,
|
|
|
|
spiceUnit>>> {};
|
2022-04-01 04:30:50 +00:00
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2023-09-15 18:55:36 +00:00
|
|
|
struct modelUnit : seq<star<commentLine>,
|
|
|
|
sor<dotModelAko,
|
|
|
|
dotModel,
|
|
|
|
dotSubckt>> {};
|
2022-04-01 04:30:50 +00:00
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2022-09-17 06:27:47 +00:00
|
|
|
// Intentionally no if_must<>.
|
2022-08-30 15:24:13 +00:00
|
|
|
struct dotControl : seq<opt<sep>,
|
|
|
|
TAO_PEGTL_ISTRING( ".control" ),
|
|
|
|
until<TAO_PEGTL_ISTRING( ".endc" )>,
|
|
|
|
until<newline>> {};
|
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
struct dotTitleTitle : star<not_at<newline>, any> {};
|
2022-09-17 06:27:47 +00:00
|
|
|
// Intentionally no if_must<>.
|
2022-04-12 14:37:06 +00:00
|
|
|
struct dotTitle : seq<opt<sep>,
|
|
|
|
TAO_PEGTL_ISTRING( ".title" ),
|
|
|
|
sep,
|
|
|
|
dotTitleTitle,
|
|
|
|
newline> {};
|
|
|
|
|
|
|
|
|
|
|
|
struct dotIncludePathWithoutQuotes : star<not_one<'"'>> {};
|
|
|
|
struct dotIncludePathWithoutApostrophes : star<not_one<'\''>> {};
|
|
|
|
struct dotIncludePath : star<not_at<newline>, any> {};
|
2022-09-17 06:27:47 +00:00
|
|
|
// Intentionally no if_must<>.
|
2022-04-12 14:37:06 +00:00
|
|
|
struct dotInclude : seq<opt<sep>,
|
2022-10-20 03:12:01 +00:00
|
|
|
TAO_PEGTL_ISTRING( ".inc" ),
|
|
|
|
star<not_at<garbageOrEolf>, any>,
|
2022-04-12 14:37:06 +00:00
|
|
|
sep,
|
|
|
|
sor<seq<one<'\"'>,
|
|
|
|
dotIncludePathWithoutQuotes,
|
|
|
|
one<'\"'>>,
|
|
|
|
seq<one<'\''>,
|
|
|
|
dotIncludePathWithoutApostrophes,
|
|
|
|
one<'\''>>,
|
|
|
|
dotIncludePath>,
|
|
|
|
opt<sep>,
|
|
|
|
newline> {};
|
|
|
|
|
|
|
|
|
2022-09-17 06:27:47 +00:00
|
|
|
// Intentionally no if_must<>.
|
|
|
|
struct dotLine : seq<opt<sep>,
|
|
|
|
one<'.'>,
|
|
|
|
until<newline>> {};
|
|
|
|
|
|
|
|
|
|
|
|
// Intentionally no if_must<>.
|
2022-08-30 15:24:13 +00:00
|
|
|
struct kLine : seq<opt<sep>,
|
|
|
|
one<'K'>,
|
|
|
|
until<sep>,
|
|
|
|
one<'L'>,
|
|
|
|
until<sep>,
|
|
|
|
one<'L'>,
|
|
|
|
until<sep>,
|
|
|
|
until<newline>> {};
|
|
|
|
|
2022-09-17 06:27:47 +00:00
|
|
|
struct unknownLine : seq<plus<not_at<newline>, any>,
|
|
|
|
until<newline>> {};
|
2022-04-01 04:30:50 +00:00
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2022-04-01 04:30:50 +00:00
|
|
|
struct spiceUnit : sor<modelUnit,
|
2022-08-30 15:24:13 +00:00
|
|
|
dotControl,
|
2022-04-12 14:37:06 +00:00
|
|
|
dotTitle,
|
|
|
|
dotInclude,
|
2022-04-01 04:30:50 +00:00
|
|
|
dotLine,
|
2022-08-30 15:24:13 +00:00
|
|
|
kLine,
|
2022-09-12 01:59:42 +00:00
|
|
|
eol, // Empty line. This is necessary to terminate on EOF.
|
2022-04-01 04:30:50 +00:00
|
|
|
unknownLine> {};
|
2022-10-15 17:31:15 +00:00
|
|
|
struct spiceUnitGrammar : must<spiceUnit> {};
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct spiceSource : star<spiceUnit> {};
|
2022-10-15 17:31:15 +00:00
|
|
|
struct spiceSourceGrammar : must<spiceSource> {};
|
2022-09-17 06:27:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename> inline constexpr const char* errorMessage = nullptr;
|
|
|
|
template <> inline constexpr auto errorMessage<newline> =
|
2022-12-04 17:56:28 +00:00
|
|
|
"expected newline";
|
2022-09-17 06:27:47 +00:00
|
|
|
template <> inline constexpr auto errorMessage<sep> =
|
2022-12-04 17:56:28 +00:00
|
|
|
"expected token separator (one or more whitespace, parenthesis, '=', ',', or line continuation)";
|
|
|
|
template <> inline constexpr auto errorMessage<opt<sep>> =
|
|
|
|
"";
|
|
|
|
template <> inline constexpr auto errorMessage<modelName> =
|
|
|
|
"expected model name";
|
|
|
|
template <> inline constexpr auto errorMessage<dotModelType> =
|
|
|
|
"expected model type";
|
|
|
|
template <> inline constexpr auto errorMessage<opt<sep, dotModelType>> =
|
|
|
|
"";
|
|
|
|
template <> inline constexpr auto errorMessage<opt<sep, paramValuePairs>> =
|
|
|
|
"";
|
|
|
|
template <> inline constexpr auto errorMessage<opt<sep, dotSubcktPinSequence>> =
|
|
|
|
"";
|
|
|
|
template <> inline constexpr auto errorMessage<opt<sep, dotSubcktParams>> =
|
|
|
|
"";
|
|
|
|
template <> inline constexpr auto errorMessage<until<dotSubcktEnd, spiceUnit>> =
|
|
|
|
"expected (possibly empty) sequence of Spice lines followed by an .ends line";
|
2022-10-15 17:31:15 +00:00
|
|
|
template <> inline constexpr auto errorMessage<spiceUnit> =
|
2022-12-04 17:56:28 +00:00
|
|
|
"expected Spice directive, item, subcircuit definitions, or empty or commented-out line";
|
2022-10-15 17:31:15 +00:00
|
|
|
template <> inline constexpr auto errorMessage<spiceSource> =
|
2022-12-04 17:56:28 +00:00
|
|
|
"expected zero or more Spice directives, items, subcircuit definitions, or empty or commented-out lines";
|
2022-09-17 06:27:47 +00:00
|
|
|
|
|
|
|
// We create a custom PEGTL control to modify the parser error messages.
|
|
|
|
struct error
|
|
|
|
{
|
|
|
|
template <typename Rule> static constexpr bool raise_on_failure = false;
|
|
|
|
template <typename Rule> static constexpr auto message = errorMessage<Rule>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Rule> using control = must_if<error>::control<Rule>;
|
2022-04-01 04:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SPICE_GRAMMAR_H
|