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 )
841 error.Printf(
_(
"Invalid footprint ID in\nfile: '%s'\nline: %d\noffset: %d" ),
842 CurSource(), CurLineNumber(), CurOffset() );
855 std::ranges::copy( jumperPinGroups, std::inserter( component->
JumperPadGroups(),
859 for(
const COMPONENT_VARIANT& variant : parsedVariants )
878 std::vector<KIID> members;
881 while( (
token = NextTok() ) != T_EOF )
883 if(
token == T_RIGHT )
885 else if(
token == T_LEFT )
891 NeedSYMBOLorNUMBER();
897 NeedSYMBOLorNUMBER();
903 NeedSYMBOLorNUMBER();
909 while( (
token = NextTok() ) != T_RIGHT )
911 if(
token == T_LEFT )
914 if(
token == T_member )
918 while( (
token = NextTok() ) != T_RIGHT )
920 if(
token == T_LEFT )
923 if(
token == T_uuid )
925 NeedSYMBOLorNUMBER();
935 members.emplace_back( memberUuid );
939 Expecting(
"member" );
953 if( !libId.IsEmpty() && groupLibId.
Parse( libId,
true ) >= 0 )
956 error.Printf(
_(
"Invalid lib_id ID in\nfile: '%s'\nline: %d\noffset: %d" ), CurSource(), CurLineNumber(),
962 NETLIST_GROUP*
group =
new NETLIST_GROUP{ std::move(
name ), std::move( uuid ), std::move( groupLibId ),
963 std::move( members ) };
974 wxString description;
978 if(
token == T_LEFT )
984 NeedSYMBOLorNUMBER();
990 NeedSYMBOLorNUMBER();
1001 if( !
name.IsEmpty() )
1010 wxString chainClass;
1012 std::vector<wxString> members;
1013 std::vector<std::pair<wxString, wxString>> terminals;
1015 while( (
token = NextTok() ) != T_EOF )
1017 if(
token == T_RIGHT )
1019 else if(
token == T_LEFT )
1025 NeedSYMBOLorNUMBER();
1031 NeedSYMBOLorNUMBER();
1036 case T_net_chain_class:
1037 NeedSYMBOLorNUMBER();
1043 NeedSYMBOLorNUMBER();
1049 while( (
token = NextTok() ) != T_RIGHT )
1051 if(
token == T_LEFT )
1054 if(
token == T_member )
1056 while( (
token = NextTok() ) != T_RIGHT )
1058 if(
token == T_LEFT )
1061 if(
token == T_net )
1063 NeedSYMBOLorNUMBER();
1064 members.emplace_back(
From_UTF8( CurText() ) );
1076 case T_terminal_pins:
1077 while( (
token = NextTok() ) != T_RIGHT )
1079 if(
token == T_LEFT )
1082 if(
token == T_terminal_pin )
1087 while( (
token = NextTok() ) != T_RIGHT )
1089 if(
token == T_LEFT )
1092 if(
token == T_ref )
1094 NeedSYMBOLorNUMBER();
1098 else if(
token == T_pin )
1100 NeedSYMBOLorNUMBER();
1108 if( !ref.IsEmpty() && !
pin.IsEmpty() )
1109 terminals.emplace_back( ref,
pin );
1122 for(
const wxString& netName : members )
1125 for(
const auto& term : terminals )
1126 m_netlist->AddSignalTerminalPin(
name, term.first, term.second );
1128 if( !netClass.IsEmpty() )
1131 if( !colorStr.IsEmpty() )
1134 if( !chainClass.IsEmpty() )
1163 wxString libPartName;
1164 wxArrayString footprintFilters;
1165 wxArrayString aliases;
1169 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: begin libpart" ) );
1170 while( (
token = NextTok() ) != T_RIGHT )
1172 if(
token == T_LEFT )
1178 NeedSYMBOLorNUMBER();
1184 NeedSYMBOLorNUMBER();
1191 while( (
token = NextTok() ) != T_RIGHT )
1193 if(
token == T_LEFT )
1202 if(
token == T_RIGHT )
1206 Expecting(
"footprint ID" );
1208 footprintFilters.Add(
From_UTF8( CurText() ) );
1214 while( (
token = NextTok() ) != T_RIGHT )
1216 if(
token == T_LEFT )
1219 if(
token != T_alias )
1220 Expecting( T_alias );
1222 NeedSYMBOLorNUMBER();
1229 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: entering pins for '%s:%s'" ),
1230 libName, libPartName );
1231 while( (
token = NextTok() ) != T_RIGHT )
1233 if(
token == T_LEFT )
1236 if(
token != T_pin )
1240 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: pin #%d for '%s:%s'" ),
1241 pinCount, libName, libPartName );
1245 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Parsed libpart '%s:%s' pins => pinCount=%d" ),
1246 libName, libPartName, pinCount );
1257 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"parseLibPartList: assigning pinCount=%d for libpart '%s:%s'" ),
1258 pinCount, libName, libPartName );
1259 for(
unsigned i = 0; i <
m_netlist->GetCount(); i++ )
1261 component =
m_netlist->GetComponent( i );
1263 if( component->
IsLibSource( libName, libPartName ) )
1267 wxLogTrace(
"CVPCB_PINCOUNT", wxT(
"Assign pinCount=%d to component ref='%s' part='%s:%s'" ),
1268 pinCount, component->
GetReference(), libName, libPartName );
1271 for(
unsigned jj = 0; jj < aliases.GetCount(); jj++ )
1273 if( component->
IsLibSource( libName, aliases[jj] ) )
1277 wxLogTrace(
"CVPCB_PINCOUNT",
1278 wxT(
"Assign pinCount=%d to component ref='%s' via alias='%s:%s'" ),
1279 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.
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_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
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