Use the PEGTL grammar for detecting .control and .subckt text directives

This commit is contained in:
Mikolaj Wielgus 2022-08-30 17:24:13 +02:00
parent ae637296cd
commit 39ac58ea33
2 changed files with 28 additions and 31 deletions

View File

@ -49,6 +49,8 @@ namespace NETLIST_EXPORTER_SPICE_PARSER
template <typename Rule> struct textSelector : std::false_type {};
template <> struct textSelector<modelUnit> : std::true_type {};
template <> struct textSelector<dotControl> : std::true_type {};
template <> struct textSelector<dotTitle> : std::true_type {};
template <> struct textSelector<dotTitleTitle> : std::true_type {};
@ -57,6 +59,8 @@ namespace NETLIST_EXPORTER_SPICE_PARSER
template <> struct textSelector<dotIncludePathWithoutApostrophes> : std::true_type {};
template <> struct textSelector<dotIncludePath> : std::true_type {};
template <> struct textSelector<kLine> : std::true_type {};
template <> struct textSelector<dotLine> : std::true_type {};
}
@ -184,31 +188,6 @@ void NETLIST_EXPORTER_SPICE::ReadDirectives()
else
continue;
// Some directives have a plain text inside them:
// .control (that ends with .endc
// .subckt (that ends with .ends
// So we insert the full text in netlist without any test
// (it is not the right place here to verify the directive)
if( text.StartsWith( ".control" ) || text.StartsWith( ".subckt") )
{
m_directives.emplace_back( text );
continue;
}
// A specific "directive" allows entering coupling parameter between 2 inductors
// Kxx Lyy Lzz nn (xx, yy, zz are digits, nn is the coupling value
if( text.StartsWith( "K" ) )
{
wxRegEx couplingK( "^[kK][[:digit:]]*[[:space:]]+[[:alnum:]]+[[:space:]]+[[:alnum:]]+",
wxRE_ADVANCED );
if( couplingK.Matches( text ) ) // K## L## L## coupling constant
{
m_directives.emplace_back( text );
continue;
}
}
tao::pegtl::string_input<> in( ( text + "\n" ).ToUTF8(), "from_content" );
std::unique_ptr<tao::pegtl::parse_tree::node> root;

View File

@ -88,7 +88,7 @@ namespace SPICE_GRAMMAR
struct sep : sor<plus<continuation>,
garbage> {};
// Ngspice has some heuristic logic to allow + and - in tokens. We mimic that here.
// Ngspice has some heuristic logic to allow + and - in tokens. We replicate that here.
struct tokenStart : seq<opt<one<'+', '-'>>,
opt<seq<star<sor<tao::pegtl::digit,
one<'.'>>>,
@ -156,9 +156,9 @@ namespace SPICE_GRAMMAR
struct dotSubcktPinName : seq<not_at<TAO_PEGTL_ISTRING( "params:" )>,
plus<not_at<space>,
any>> {};
struct dotSubcktPinSequence : seq<opt<dotSubcktPinName,
star<sep,
dotSubcktPinName>>> {};
struct dotSubcktPinSequence : seq<opt<dotSubcktPinName>,
star<sep,
dotSubcktPinName>> {};
struct dotSubcktParams : seq<TAO_PEGTL_ISTRING( "params:" ),
sep,
paramValuePairs> {};
@ -169,8 +169,8 @@ namespace SPICE_GRAMMAR
TAO_PEGTL_ISTRING( ".subckt" ),
sep,
modelName,
sep,
dotSubcktPinSequence,
opt<sep,
dotSubcktPinSequence>,
opt<sep,
dotSubcktParams>,
opt<sep>,
@ -182,6 +182,12 @@ namespace SPICE_GRAMMAR
dotSubckt> {};
struct dotControl : seq<opt<sep>,
TAO_PEGTL_ISTRING( ".control" ),
until<TAO_PEGTL_ISTRING( ".endc" )>,
until<newline>> {};
struct dotTitleTitle : star<not_at<newline>, any> {};
struct dotTitle : seq<opt<sep>,
TAO_PEGTL_ISTRING( ".title" ),
@ -207,6 +213,16 @@ namespace SPICE_GRAMMAR
newline> {};
struct kLine : seq<opt<sep>,
one<'K'>,
until<sep>,
one<'L'>,
until<sep>,
one<'L'>,
until<sep>,
until<newline>> {};
struct dotLine : seq<opt<sep>,
one<'.'>,
until<newline>> {};
@ -215,9 +231,11 @@ namespace SPICE_GRAMMAR
struct spiceUnit : sor<modelUnit,
dotControl,
dotTitle,
dotInclude,
dotLine,
kLine,
unknownLine> {};
struct spiceUnitGrammar : must<spiceUnit, tao::pegtl::eof> {};