23#include <netlist_lexer.h>
46 for(
unsigned i = 0; i <
m_netlist->GetCount(); i++ )
54 NETLIST_LEXER( aReader )
56 m_lineReader = aReader;
66 while( (
token = NextTok() ) != T_EOF )
71 if(
token == T_RIGHT )
86 while( (
token = NextTok() ) != T_EOF )
105 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Parse: entering components section" ) );
107 while( (
token = NextTok() ) != T_EOF )
109 if(
token == T_RIGHT )
111 else if(
token == T_LEFT )
114 if(
token == T_comp )
121 while( (
token = NextTok() ) != T_EOF )
123 if(
token == T_RIGHT )
125 else if(
token == T_LEFT )
128 if(
token == T_group )
135 while( (
token = NextTok() ) != T_EOF )
137 if(
token == T_RIGHT )
139 else if(
token == T_LEFT )
142 if(
token == T_variant )
149 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Parse: entering nets section" ) );
151 while( (
token = NextTok() ) != T_EOF )
153 if(
token == T_RIGHT )
155 else if(
token == T_LEFT )
165 while( (
token = NextTok() ) != T_EOF )
167 if(
token == T_RIGHT )
169 else if(
token == T_LEFT )
172 if(
token == T_net_chain )
179 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Parse: entering libparts section" ) );
181 while( (
token = NextTok() ) != T_EOF )
183 if(
token == T_RIGHT )
185 else if(
token == T_LEFT )
188 if(
token == T_libpart )
220 wxFAIL_MSG( wxString::Format( wxT(
"KICAD_NETLIST_PARSER::Parse(): bad parenthesis "
221 "count (count = %d" ),
240 wxString pin_function;
244 while( (
token = NextTok() ) != T_EOF )
246 if(
token == T_RIGHT )
248 else if(
token == T_LEFT )
254 NeedSYMBOLorNUMBER();
260 NeedSYMBOLorNUMBER();
267 pin_function.Clear();
270 while( (
token = NextTok() ) != T_EOF )
272 if(
token == T_RIGHT )
274 else if(
token == T_LEFT )
280 NeedSYMBOLorNUMBER();
286 NeedSYMBOLorNUMBER();
292 NeedSYMBOLorNUMBER();
298 NeedSYMBOLorNUMBER();
312 if( strtol( code.c_str(),
nullptr, 10 ) >= 1 )
315 name = wxT(
"N-00000") + code;
317 component->AddNet( pin_number,
name, pin_function, pin_type );
353 wxString humanSheetPath;
356 std::vector<KIID> uuids;
357 std::map<wxString, wxString> properties;
358 nlohmann::ordered_map<wxString, wxString> fields;
359 std::unordered_set<wxString> componentClasses;
361 bool duplicatePinsAreJumpers =
false;
362 std::vector<std::set<wxString>> jumperPinGroups;
364 std::vector<COMPONENT::UNIT_INFO> parsedUnits;
365 std::vector<COMPONENT_VARIANT> parsedVariants;
368 while( (
token = NextTok() ) != T_RIGHT )
370 if(
token == T_LEFT )
376 NeedSYMBOLorNUMBER();
382 NeedSYMBOLorNUMBER();
388 NeedSYMBOLorNUMBER();
389 footprint = FromUTF8();
395 while( (
token = NextTok() ) != T_RIGHT )
397 if(
token == T_LEFT )
402 NeedSYMBOLorNUMBER();
406 else if(
token == T_part )
408 NeedSYMBOLorNUMBER();
412 else if(
token == T_description )
414 NeedSYMBOLorNUMBER();
419 Expecting(
"part, lib or description" );
422 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseComponent: ref='%s' libsource='%s:%s'" ),
431 while( (
token = NextTok() ) != T_RIGHT )
433 if(
token == T_LEFT )
436 if(
token == T_name )
438 NeedSYMBOLorNUMBER();
442 else if(
token == T_value )
444 NeedSYMBOLorNUMBER();
450 Expecting(
"name or value" );
454 if( !propName.IsEmpty() )
455 properties[propName] = std::move( propValue );
460 while( (
token = NextTok() ) != T_RIGHT )
462 if(
token == T_LEFT )
465 if(
token == T_field )
470 while( (
token = NextTok() ) != T_RIGHT )
472 if(
token == T_LEFT )
475 if(
token == T_name )
477 NeedSYMBOLorNUMBER();
481 else if(
token == T_STRING )
487 if( !fieldName.IsEmpty() )
488 fields[fieldName] = std::move( fieldValue );
492 Expecting(
"field" );
498 while( (
token = NextTok() ) != T_EOF )
500 if(
token == T_names )
502 NeedSYMBOLorNUMBER();
507 if(
token == T_tstamps )
509 NeedSYMBOLorNUMBER();
521 while( (
token = NextTok() ) != T_EOF )
523 if(
token == T_RIGHT )
526 uuids.emplace_back(
From_UTF8( CurText() ) );
535 while( (
token = NextTok() ) != T_RIGHT )
537 if(
token == T_LEFT )
540 if(
token == T_unit )
542 COMPONENT::UNIT_INFO
info;
544 while( (
token = NextTok() ) != T_RIGHT )
546 if(
token == T_LEFT )
552 NeedSYMBOLorNUMBER();
558 while( (
token = NextTok() ) != T_RIGHT )
560 if(
token == T_LEFT )
570 if(
token == T_LEFT )
575 NeedSYMBOLorNUMBER();
586 if( !pinNum.IsEmpty() )
587 info.m_pins.emplace_back( pinNum );
602 parsedUnits.push_back(
info );
613 case T_component_classes:
614 while( (
token = NextTok() ) != T_RIGHT )
616 if(
token != T_LEFT )
619 if( (
token = NextTok() ) != T_class )
620 Expecting( T_class );
622 NeedSYMBOLorNUMBER();
623 componentClasses.insert(
From_UTF8( CurText() ) );
629 case T_duplicate_pin_numbers_are_jumpers:
631 NeedSYMBOLorNUMBER();
632 duplicatePinsAreJumpers =
From_UTF8( CurText() ) == wxT(
"1" );
637 case T_jumper_pin_groups:
639 std::set<wxString>* currentGroup =
nullptr;
641 for(
token = NextTok(); currentGroup ||
token != T_RIGHT;
token = NextTok() )
643 if(
token == T_LEFT )
649 currentGroup = &jumperPinGroups.emplace_back();
654 NeedSYMBOLorNUMBER();
655 wxString padName =
From_UTF8( CurText() );
657 wxCHECK2( currentGroup,
continue );
658 currentGroup->insert( padName );
663 currentGroup =
nullptr;
667 Expecting(
"group or pin" );
676 while( (
token = NextTok() ) != T_RIGHT )
678 if(
token == T_LEFT )
681 if(
token != T_variant )
687 COMPONENT_VARIANT variant;
691 if(
token == T_LEFT )
697 NeedSYMBOLorNUMBER();
706 bool hasValue =
false;
708 while( (
token = NextTok() ) != T_RIGHT )
710 if(
token == T_LEFT )
713 if(
token == T_name )
715 NeedSYMBOLorNUMBER();
719 else if(
token == T_value )
721 NeedSYMBOLorNUMBER();
728 Expecting(
"name or value" );
732 if( propName.IsEmpty() )
735 bool propBool =
true;
739 wxString normalized = propValue;
740 normalized.MakeLower();
742 if( normalized == wxT(
"0" ) || normalized == wxT(
"false" ) )
744 else if( normalized == wxT(
"1" ) || normalized == wxT(
"true" ) )
747 propBool = !propValue.IsEmpty();
750 if( propName.CmpNoCase( wxT(
"dnp" ) ) == 0 )
752 variant.
m_dnp = propBool;
755 else if( propName.CmpNoCase( wxT(
"exclude_from_bom" ) ) == 0 )
760 else if( propName.CmpNoCase( wxT(
"exclude_from_sim" ) ) == 0 )
765 else if( propName.CmpNoCase( wxT(
"exclude_from_pos_files" ) ) == 0 )
775 while( (
token = NextTok() ) != T_RIGHT )
777 if(
token == T_LEFT )
780 if(
token == T_field )
785 while( (
token = NextTok() ) != T_RIGHT )
787 if(
token == T_LEFT )
790 if(
token == T_name )
792 NeedSYMBOLorNUMBER();
796 else if(
token == T_value )
798 NeedSYMBOLorNUMBER();
802 else if(
token == T_STRING )
808 if( !fieldName.IsEmpty() )
809 variant.
m_fields[fieldName] = std::move( fieldValue );
813 Expecting(
"field" );
824 if( !variant.
m_name.IsEmpty() )
825 parsedVariants.push_back( std::move( variant ) );
838 if( !footprint.IsEmpty() && fpid.
Parse( footprint,
true ) >= 0 )
840 THROW_IO_ERRORF(
_(
"Invalid footprint ID in\nfile: '%s'\nline: %d\noffset: %d" ),
841 CurSource(), CurLineNumber(), CurOffset() );
852 std::ranges::copy( jumperPinGroups, std::inserter( component->
JumperPadGroups(),
856 for(
const COMPONENT_VARIANT& variant : parsedVariants )
878 std::vector<KIID_PATH> members;
881 while( (
token = NextTok() ) != T_EOF )
883 if(
token == T_RIGHT )
885 else if(
token == T_LEFT )
891 NeedSYMBOLorNUMBER();
898 NeedSYMBOLorNUMBER();
899 wxString uuidStr =
From_UTF8( CurText() );
903 if( uuidStr.StartsWith( wxT(
"/" ) ) )
913 NeedSYMBOLorNUMBER();
919 while( (
token = NextTok() ) != T_RIGHT )
921 if(
token == T_LEFT )
924 if(
token == T_member )
928 while( (
token = NextTok() ) != T_RIGHT )
930 if(
token == T_LEFT )
933 if(
token == T_uuid )
935 NeedSYMBOLorNUMBER();
945 members.emplace_back( memberUuid );
949 Expecting(
"member" );
963 if( !libId.IsEmpty() && groupLibId.
Parse( libId,
true ) >= 0 )
965 THROW_IO_ERRORF(
_(
"Invalid lib_id ID in\nfile: '%s'\nline: %d\noffset: %d" ),
966 CurSource(), CurLineNumber(), CurOffset() );
969 NETLIST_GROUP*
group =
new NETLIST_GROUP{ std::move(
name ), std::move( uuid ), std::move( groupLibId ),
970 std::move( members ) };
981 wxString description;
985 if(
token == T_LEFT )
991 NeedSYMBOLorNUMBER();
997 NeedSYMBOLorNUMBER();
1008 if( !
name.IsEmpty() )
1017 wxString chainClass;
1019 std::vector<wxString> members;
1020 std::vector<std::pair<wxString, wxString>> terminals;
1022 while( (
token = NextTok() ) != T_EOF )
1024 if(
token == T_RIGHT )
1026 else if(
token == T_LEFT )
1032 NeedSYMBOLorNUMBER();
1038 NeedSYMBOLorNUMBER();
1043 case T_net_chain_class:
1044 NeedSYMBOLorNUMBER();
1050 NeedSYMBOLorNUMBER();
1056 while( (
token = NextTok() ) != T_RIGHT )
1058 if(
token == T_LEFT )
1061 if(
token == T_member )
1063 while( (
token = NextTok() ) != T_RIGHT )
1065 if(
token == T_LEFT )
1068 if(
token == T_net )
1070 NeedSYMBOLorNUMBER();
1071 members.emplace_back(
From_UTF8( CurText() ) );
1083 case T_terminal_pins:
1084 while( (
token = NextTok() ) != T_RIGHT )
1086 if(
token == T_LEFT )
1089 if(
token == T_terminal_pin )
1094 while( (
token = NextTok() ) != T_RIGHT )
1096 if(
token == T_LEFT )
1099 if(
token == T_ref )
1101 NeedSYMBOLorNUMBER();
1105 else if(
token == T_pin )
1107 NeedSYMBOLorNUMBER();
1115 if( !ref.IsEmpty() && !
pin.IsEmpty() )
1116 terminals.emplace_back( ref,
pin );
1129 for(
const wxString& netName : members )
1132 for(
const auto& term : terminals )
1133 m_netlist->AddSignalTerminalPin(
name, term.first, term.second );
1135 if( !netClass.IsEmpty() )
1138 if( !colorStr.IsEmpty() )
1141 if( !chainClass.IsEmpty() )
1170 wxString libPartName;
1171 wxArrayString footprintFilters;
1172 wxArrayString aliases;
1176 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: begin libpart" ) );
1177 while( (
token = NextTok() ) != T_RIGHT )
1179 if(
token == T_LEFT )
1185 NeedSYMBOLorNUMBER();
1191 NeedSYMBOLorNUMBER();
1198 while( (
token = NextTok() ) != T_RIGHT )
1200 if(
token == T_LEFT )
1209 if(
token == T_RIGHT )
1213 Expecting(
"footprint ID" );
1215 footprintFilters.Add(
From_UTF8( CurText() ) );
1221 while( (
token = NextTok() ) != T_RIGHT )
1223 if(
token == T_LEFT )
1226 if(
token != T_alias )
1227 Expecting( T_alias );
1229 NeedSYMBOLorNUMBER();
1236 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: entering pins for '%s:%s'" ),
1237 libName, libPartName );
1238 while( (
token = NextTok() ) != T_RIGHT )
1240 if(
token == T_LEFT )
1243 if(
token != T_pin )
1247 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: pin #%d for '%s:%s'" ),
1248 pinCount, libName, libPartName );
1252 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Parsed libpart '%s:%s' pins => pinCount=%d" ),
1253 libName, libPartName, pinCount );
1264 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: assigning pinCount=%d for libpart '%s:%s'" ),
1265 pinCount, libName, libPartName );
1266 for(
unsigned i = 0; i <
m_netlist->GetCount(); i++ )
1268 component =
m_netlist->GetComponent( i );
1270 if( component->
IsLibSource( libName, libPartName ) )
1274 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Assign pinCount=%d to component ref='%s' part='%s:%s'" ),
1275 pinCount, component->
GetReference(), libName, libPartName );
1278 for(
unsigned jj = 0; jj < aliases.GetCount(); jj++ )
1280 if( component->
IsLibSource( libName, aliases[jj] ) )
1284 wxLogTrace(
"CVPCB_PINCOUNT",
1285 wxT(
"Assign pinCount=%d to component ref='%s' via alias='%s:%s'" ),
1286 pinCount, component->
GetReference(), libName, aliases[jj] );
Store all of the related component information found in a netlist.
void SetLibrary(const wxString &aLibrary)
const wxString & GetReference() const
void AddVariant(const COMPONENT_VARIANT &aVariant)
void SetProperties(std::map< wxString, wxString > aProps)
void SetPinCount(int aPinCount)
void SetUnitInfo(const std::vector< UNIT_INFO > &aUnits)
bool IsLibSource(const wxString &aLibrary, const wxString &aName) const
void SetFootprintFilters(const wxArrayString &aFilters)
void SetFields(nlohmann::ordered_map< wxString, wxString > aFields)
void SetHumanReadablePath(const wxString &aPath)
void SetDuplicatePadNumbersAreJumpers(bool aEnabled)
void SetComponentClassNames(const std::unordered_set< wxString > &aClassNames)
std::vector< std::set< wxString > > & JumperPadGroups()
void SetName(const wxString &aName)
KICAD_NETLIST_PARSER(LINE_READER *aReader, NETLIST *aNetlist)
void parseComponent()
Parse a component description: (comp (ref P1) (value DB25FEMELLE) (footprint DB25FC) (libsource (lib ...
void Parse()
Function Parse parse the full netlist.
void parseVariant()
Parse a variant section (variant (name "VariantName") (description "Description"))
void parseNet()
Parse a net section (net (code 20) (name /PC-A0) (node (ref BUS1) (pin 62)) (node (ref U3) (pin 3)) (...
void parseLibPartList()
Read the section "libparts" in the netlist: (libparts (libpart (lib device) (part C) (description "Co...
void parseGroup()
Parse a group section (group (name "GroupName") (member (uuid "..."))))
void skipCurrent()
Skip the current token level, i.e search for the RIGHT parenthesis which closes the current descripti...
NETLIST * m_netlist
The netlist to parse into. Not owned.
virtual void LoadNetlist() override
Load the contents of the netlist file into aNetlist.
static KIID FromName(const std::string &aName)
Return a KIID derived from a name, the same name always gives the same KIID.
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
CMP_READER * m_footprintReader
The reader used to load the footprint links. If NULL, footprint links are not read.
LINE_READER * m_lineReader
The line reader of the netlist.
NETLIST * m_netlist
The net list to read the file(s) into.
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
#define THROW_IO_ERRORF(msg,...)
_OUT_STRING AsString(const std::string &aString)
static bool IsNumber(char x)
wxString From_UTF8(const char *cstring)
bool m_excludedFromPosFiles
bool m_hasExcludedFromBOM
bool m_hasExcludedFromSim
bool m_hasExcludedFromPosFiles
nlohmann::ordered_map< wxString, wxString > m_fields