KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew/netlist_reader/kicad_netlist_reader.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 1992-2011 Jean-Pierre Charras.
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/log.h>
22
23#include <netlist_lexer.h> // netlist_lexer is common to Eeschema and Pcbnew
24#include <string_utils.h>
25#include <json_common.h>
26
27#include "pcb_netlist.h"
28#include "netlist_reader.h"
30
31using namespace NL_T;
32
33
35{
36 KICAD_NETLIST_PARSER parser( m_lineReader, m_netlist );
37
38 parser.Parse();
39
41 {
43
44 // Sort the component pins so they are in the same order as the legacy format. This
45 // is useful for comparing legacy and s-expression netlist dumps.
46 for( unsigned i = 0; i < m_netlist->GetCount(); i++ )
47 m_netlist->GetComponent( i )->SortPins();
48 }
49}
50
51
52// KICAD_NETLIST_PARSER
54 NETLIST_LEXER( aReader )
55{
56 m_lineReader = aReader;
57 m_netlist = aNetlist;
58 token = T_NONE;
59}
60
61
63{
64 int curr_level = 0;
65
66 while( ( token = NextTok() ) != T_EOF )
67 {
68 if( token == T_LEFT )
69 curr_level--;
70
71 if( token == T_RIGHT )
72 {
73 curr_level++;
74
75 if( curr_level > 0 )
76 return;
77 }
78 }
79}
80
81
83{
84 int plevel = 0; // the count of ')' to read at end of file after parsing all sections
85
86 while( ( token = NextTok() ) != T_EOF )
87 {
88 if( token == T_LEFT )
89 token = NextTok();
90
91 switch( token )
92 {
93 case T_export: // The netlist starts here.
94 // nothing to do here, just increment the count of ')' to read at end of file
95 plevel++;
96 break;
97
98 case T_version: // The netlist starts here.
99 // version id not yet used: read it but does not use it
100 NextTok();
101 NeedRIGHT();
102 break;
103
104 case T_components: // The section comp starts here.
105 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Parse: entering components section" ) );
106
107 while( ( token = NextTok() ) != T_EOF )
108 {
109 if( token == T_RIGHT )
110 break;
111 else if( token == T_LEFT )
112 token = NextTok();
113
114 if( token == T_comp ) // A component section found. Read it
116 }
117
118 break;
119
120 case T_groups: // The section groups starts here.
121 while( ( token = NextTok() ) != T_EOF )
122 {
123 if( token == T_RIGHT )
124 break;
125 else if( token == T_LEFT )
126 token = NextTok();
127
128 if( token == T_group ) // A group section found. Read it
129 parseGroup();
130 }
131
132 break;
133
134 case T_variants: // The section variants starts here.
135 while( ( token = NextTok() ) != T_EOF )
136 {
137 if( token == T_RIGHT )
138 break;
139 else if( token == T_LEFT )
140 token = NextTok();
141
142 if( token == T_variant ) // A variant section found. Read it
143 parseVariant();
144 }
145
146 break;
147
148 case T_nets: // The section nets starts here.
149 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Parse: entering nets section" ) );
150
151 while( ( token = NextTok() ) != T_EOF )
152 {
153 if( token == T_RIGHT )
154 break;
155 else if( token == T_LEFT )
156 token = NextTok();
157
158 if( token == T_net ) // A net section if found. Read it
159 parseNet();
160 }
161
162 break;
163
164 case T_net_chains:
165 while( ( token = NextTok() ) != T_EOF )
166 {
167 if( token == T_RIGHT )
168 break;
169 else if( token == T_LEFT )
170 token = NextTok();
171
172 if( token == T_net_chain )
174 }
175
176 break;
177
178 case T_libparts: // The section libparts starts here.
179 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Parse: entering libparts section" ) );
180
181 while( ( token = NextTok() ) != T_EOF )
182 {
183 if( token == T_RIGHT )
184 break;
185 else if( token == T_LEFT )
186 token = NextTok();
187
188 if( token == T_libpart ) // A libpart section if found. Read it
190 }
191
192 break;
193
194 case T_libraries: // The section libraries starts here.
195 // List of libraries in use.
196 // Not used here, just skip it
197 skipCurrent();
198 break;
199
200 case T_design: // The section design starts here.
201 // Not used (mainly they are comments), just skip it
202 skipCurrent();
203 break;
204
205 case T_RIGHT: // The closing parenthesis of the file.
206 plevel--;
207 break;
208
209 default:
210 skipCurrent();
211 break;
212 }
213 }
214
215 // Go back and apply group information to the components
216 m_netlist->ApplyGroupMembership();
217
218 if( plevel != 0 )
219 {
220 wxFAIL_MSG( wxString::Format( wxT( "KICAD_NETLIST_PARSER::Parse(): bad parenthesis "
221 "count (count = %d" ),
222 plevel ) );
223 }
224}
225
226
228{
229 /* Parses a section like
230 * (net (code 20) (name /PC-A0)
231 * (node (ref "BUS1") (pin "62)")
232 * (node (ref "U3") ("pin 3") (pin_function "clock"))
233 * (node (ref "U9") (pin "M6") (pin_function "reset")))
234 */
235
236 wxString code;
237 wxString name;
238 wxString reference;
239 wxString pin_number;
240 wxString pin_function;
241 wxString pin_type;
242
243 // The token net was read, so the next data is (code <number>)
244 while( (token = NextTok() ) != T_EOF )
245 {
246 if( token == T_RIGHT )
247 break;
248 else if( token == T_LEFT )
249 token = NextTok();
250
251 switch( token )
252 {
253 case T_code:
254 NeedSYMBOLorNUMBER();
255 code = From_UTF8( CurText() );
256 NeedRIGHT();
257 break;
258
259 case T_name:
260 NeedSYMBOLorNUMBER();
261 name = From_UTF8( CurText() );
262 NeedRIGHT();
263 break;
264
265 case T_node:
266 // By default: no pin function or type.
267 pin_function.Clear();
268 pin_type.Clear();
269
270 while( (token = NextTok() ) != T_EOF )
271 {
272 if( token == T_RIGHT )
273 break;
274 else if( token == T_LEFT )
275 token = NextTok();
276
277 switch( token )
278 {
279 case T_ref:
280 NeedSYMBOLorNUMBER();
281 reference = From_UTF8( CurText() );
282 NeedRIGHT();
283 break;
284
285 case T_pin:
286 NeedSYMBOLorNUMBER();
287 pin_number = From_UTF8( CurText() );
288 NeedRIGHT();
289 break;
290
291 case T_pinfunction:
292 NeedSYMBOLorNUMBER();
293 pin_function = From_UTF8( CurText() );
294 NeedRIGHT();
295 break;
296
297 case T_pintype:
298 NeedSYMBOLorNUMBER();
299 pin_type = From_UTF8( CurText() );
300 NeedRIGHT();
301 break;
302
303 default:
304 skipCurrent();
305 break;
306 }
307 }
308
309 // Don't assume component will be found; it might be "DNP" or "Exclude from board".
310 if( COMPONENT* component = m_netlist->GetComponentByReference( reference ) )
311 {
312 if( strtol( code.c_str(), nullptr, 10 ) >= 1 )
313 {
314 if( name.IsEmpty() ) // Give a dummy net name like N-000009
315 name = wxT("N-00000") + code;
316
317 component->AddNet( pin_number, name, pin_function, pin_type );
318 }
319 }
320
321 break;
322
323 default:
324 skipCurrent();
325 break;
326 }
327 }
328}
329
330
332{
333 /* Parses a section like
334 * (comp (ref P1)
335 * (value DB25FEMALE)
336 * (footprint DB25FC)
337 * (libsource (lib conn) (part DB25))
338 * (property (name PINCOUNT) (value 25))
339 * (sheetpath (names /) (tstamps /))
340 * (component_classes (class (name "CLASS")))
341 * (tstamp 68183921-93a5-49ac-91b0-49d05a0e1647))
342 *
343 * other fields (unused) are skipped
344 * A component need a reference, value, footprint name and a full time stamp
345 * The full time stamp is the sheetpath time stamp + the component time stamp
346 */
347 LIB_ID fpid;
348 wxString footprint;
349 wxString ref;
350 wxString value;
351 wxString library;
352 wxString name;
353 wxString humanSheetPath;
354 KIID_PATH path;
355
356 std::vector<KIID> uuids;
357 std::map<wxString, wxString> properties;
358 nlohmann::ordered_map<wxString, wxString> fields;
359 std::unordered_set<wxString> componentClasses;
360
361 bool duplicatePinsAreJumpers = false;
362 std::vector<std::set<wxString>> jumperPinGroups;
363
364 std::vector<COMPONENT::UNIT_INFO> parsedUnits;
365 std::vector<COMPONENT_VARIANT> parsedVariants;
366
367 // The token comp was read, so the next data is (ref P1)
368 while( (token = NextTok() ) != T_RIGHT )
369 {
370 if( token == T_LEFT )
371 token = NextTok();
372
373 switch( token )
374 {
375 case T_ref:
376 NeedSYMBOLorNUMBER();
377 ref = From_UTF8( CurText() );
378 NeedRIGHT();
379 break;
380
381 case T_value:
382 NeedSYMBOLorNUMBER();
383 value = From_UTF8( CurText() );
384 NeedRIGHT();
385 break;
386
387 case T_footprint:
388 NeedSYMBOLorNUMBER();
389 footprint = FromUTF8();
390 NeedRIGHT();
391 break;
392
393 case T_libsource:
394 // Read libsource
395 while( ( token = NextTok() ) != T_RIGHT )
396 {
397 if( token == T_LEFT )
398 token = NextTok();
399
400 if( token == T_lib )
401 {
402 NeedSYMBOLorNUMBER();
403 library = From_UTF8( CurText() );
404 NeedRIGHT();
405 }
406 else if( token == T_part )
407 {
408 NeedSYMBOLorNUMBER();
409 name = From_UTF8( CurText() );
410 NeedRIGHT();
411 }
412 else if( token == T_description )
413 {
414 NeedSYMBOLorNUMBER();
415 NeedRIGHT();
416 }
417 else
418 {
419 Expecting( "part, lib or description" );
420 }
421 }
422 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseComponent: ref='%s' libsource='%s:%s'" ),
423 ref, library, name );
424 break;
425
426 case T_property:
427 {
428 wxString propName;
429 wxString propValue;
430
431 while( (token = NextTok() ) != T_RIGHT )
432 {
433 if( token == T_LEFT )
434 token = NextTok();
435
436 if( token == T_name )
437 {
438 NeedSYMBOLorNUMBER();
439 propName = From_UTF8( CurText() );
440 NeedRIGHT();
441 }
442 else if( token == T_value )
443 {
444 NeedSYMBOLorNUMBER();
445 propValue = From_UTF8( CurText() );
446 NeedRIGHT();
447 }
448 else
449 {
450 Expecting( "name or value" );
451 }
452 }
453
454 if( !propName.IsEmpty() )
455 properties[propName] = std::move( propValue );
456 break;
457 }
458
459 case T_fields:
460 while( ( token = NextTok() ) != T_RIGHT )
461 {
462 if( token == T_LEFT )
463 token = NextTok();
464
465 if( token == T_field )
466 {
467 wxString fieldName;
468 wxString fieldValue;
469
470 while( ( token = NextTok() ) != T_RIGHT )
471 {
472 if( token == T_LEFT )
473 token = NextTok();
474
475 if( token == T_name )
476 {
477 NeedSYMBOLorNUMBER();
478 fieldName = From_UTF8( CurText() );
479 NeedRIGHT();
480 }
481 else if( token == T_STRING )
482 {
483 fieldValue = From_UTF8( CurText() );
484 }
485 }
486
487 if( !fieldName.IsEmpty() )
488 fields[fieldName] = std::move( fieldValue );
489 }
490 else
491 {
492 Expecting( "field" );
493 }
494 }
495 break;
496
497 case T_sheetpath:
498 while( ( token = NextTok() ) != T_EOF )
499 {
500 if( token == T_names )
501 {
502 NeedSYMBOLorNUMBER();
503 humanSheetPath = From_UTF8( CurText() );
504 NeedRIGHT();
505 }
506
507 if( token == T_tstamps )
508 {
509 NeedSYMBOLorNUMBER();
510 path = KIID_PATH( From_UTF8( CurText() ) );
511 NeedRIGHT();
512 break;
513 }
514 }
515
516 NeedRIGHT();
517
518 break;
519
520 case T_tstamps:
521 while( ( token = NextTok() ) != T_EOF )
522 {
523 if( token == T_RIGHT )
524 break;
525
526 uuids.emplace_back( From_UTF8( CurText() ) );
527 }
528
529 break;
530
531 case T_units:
532 {
533 // Parse a section like:
534 // (units (unit (ref "U1A") (name "A") (pins (pin "1") (pin "2"))))
535 while( ( token = NextTok() ) != T_RIGHT )
536 {
537 if( token == T_LEFT )
538 token = NextTok();
539
540 if( token == T_unit )
541 {
542 COMPONENT::UNIT_INFO info;
543
544 while( ( token = NextTok() ) != T_RIGHT )
545 {
546 if( token == T_LEFT )
547 token = NextTok();
548
549 switch( token )
550 {
551 case T_name:
552 NeedSYMBOLorNUMBER();
553 info.m_unitName = From_UTF8( CurText() );
554 NeedRIGHT();
555 break;
556
557 case T_pins:
558 while( ( token = NextTok() ) != T_RIGHT )
559 {
560 if( token == T_LEFT )
561 token = NextTok();
562
563 if( token == T_pin )
564 {
565 wxString pinNum;
566
567 // Parse pins in attribute style: (pin (num "1"))
568 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
569 {
570 if( token == T_LEFT )
571 token = NextTok();
572
573 if( token == T_num )
574 {
575 NeedSYMBOLorNUMBER();
576 pinNum = From_UTF8( CurText() );
577 NeedRIGHT();
578 }
579 else
580 {
581 // ignore other subfields of pin
582 // leave bare tokens untouched; they are not supported in this context
583 }
584 }
585
586 if( !pinNum.IsEmpty() )
587 info.m_pins.emplace_back( pinNum );
588 }
589 else
590 {
591 skipCurrent();
592 }
593 }
594 break;
595
596 default:
597 skipCurrent();
598 break;
599 }
600 }
601
602 parsedUnits.push_back( info );
603 }
604 else
605 {
606 skipCurrent();
607 }
608 }
609
610 break;
611 }
612
613 case T_component_classes:
614 while( ( token = NextTok() ) != T_RIGHT )
615 {
616 if( token != T_LEFT )
617 Expecting( T_LEFT );
618
619 if( ( token = NextTok() ) != T_class )
620 Expecting( T_class );
621
622 NeedSYMBOLorNUMBER();
623 componentClasses.insert( From_UTF8( CurText() ) );
624 NeedRIGHT();
625 }
626
627 break;
628
629 case T_duplicate_pin_numbers_are_jumpers:
630 {
631 NeedSYMBOLorNUMBER();
632 duplicatePinsAreJumpers = From_UTF8( CurText() ) == wxT( "1" );
633 NeedRIGHT();
634 break;
635 }
636
637 case T_jumper_pin_groups:
638 {
639 std::set<wxString>* currentGroup = nullptr;
640
641 for( token = NextTok(); currentGroup || token != T_RIGHT; token = NextTok() )
642 {
643 if( token == T_LEFT )
644 token = NextTok();
645
646 switch( token )
647 {
648 case T_group:
649 currentGroup = &jumperPinGroups.emplace_back();
650 break;
651
652 case T_pin:
653 {
654 NeedSYMBOLorNUMBER();
655 wxString padName = From_UTF8( CurText() );
656 NeedRIGHT();
657 wxCHECK2( currentGroup, continue );
658 currentGroup->insert( padName );
659 break;
660 }
661
662 case T_RIGHT:
663 currentGroup = nullptr;
664 break;
665
666 default:
667 Expecting( "group or pin" );
668 }
669 }
670
671 break;
672 }
673
674 case T_variants:
675 {
676 while( ( token = NextTok() ) != T_RIGHT )
677 {
678 if( token == T_LEFT )
679 token = NextTok();
680
681 if( token != T_variant )
682 {
683 skipCurrent();
684 continue;
685 }
686
687 COMPONENT_VARIANT variant;
688
689 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
690 {
691 if( token == T_LEFT )
692 token = NextTok();
693
694 switch( token )
695 {
696 case T_name:
697 NeedSYMBOLorNUMBER();
698 variant.m_name = From_UTF8( CurText() );
699 NeedRIGHT();
700 break;
701
702 case T_property:
703 {
704 wxString propName;
705 wxString propValue;
706 bool hasValue = false;
707
708 while( ( token = NextTok() ) != T_RIGHT )
709 {
710 if( token == T_LEFT )
711 token = NextTok();
712
713 if( token == T_name )
714 {
715 NeedSYMBOLorNUMBER();
716 propName = From_UTF8( CurText() );
717 NeedRIGHT();
718 }
719 else if( token == T_value )
720 {
721 NeedSYMBOLorNUMBER();
722 propValue = From_UTF8( CurText() );
723 hasValue = true;
724 NeedRIGHT();
725 }
726 else
727 {
728 Expecting( "name or value" );
729 }
730 }
731
732 if( propName.IsEmpty() )
733 break;
734
735 bool propBool = true;
736
737 if( hasValue )
738 {
739 wxString normalized = propValue;
740 normalized.MakeLower();
741
742 if( normalized == wxT( "0" ) || normalized == wxT( "false" ) )
743 propBool = false;
744 else if( normalized == wxT( "1" ) || normalized == wxT( "true" ) )
745 propBool = true;
746 else
747 propBool = !propValue.IsEmpty();
748 }
749
750 if( propName.CmpNoCase( wxT( "dnp" ) ) == 0 )
751 {
752 variant.m_dnp = propBool;
753 variant.m_hasDnp = true;
754 }
755 else if( propName.CmpNoCase( wxT( "exclude_from_bom" ) ) == 0 )
756 {
757 variant.m_excludedFromBOM = propBool;
758 variant.m_hasExcludedFromBOM = true;
759 }
760 else if( propName.CmpNoCase( wxT( "exclude_from_sim" ) ) == 0 )
761 {
762 variant.m_excludedFromSim = propBool;
763 variant.m_hasExcludedFromSim = true;
764 }
765 else if( propName.CmpNoCase( wxT( "exclude_from_pos_files" ) ) == 0 )
766 {
767 variant.m_excludedFromPosFiles = propBool;
768 variant.m_hasExcludedFromPosFiles = true;
769 }
770
771 break;
772 }
773
774 case T_fields:
775 while( ( token = NextTok() ) != T_RIGHT )
776 {
777 if( token == T_LEFT )
778 token = NextTok();
779
780 if( token == T_field )
781 {
782 wxString fieldName;
783 wxString fieldValue;
784
785 while( ( token = NextTok() ) != T_RIGHT )
786 {
787 if( token == T_LEFT )
788 token = NextTok();
789
790 if( token == T_name )
791 {
792 NeedSYMBOLorNUMBER();
793 fieldName = From_UTF8( CurText() );
794 NeedRIGHT();
795 }
796 else if( token == T_value )
797 {
798 NeedSYMBOLorNUMBER();
799 fieldValue = From_UTF8( CurText() );
800 NeedRIGHT();
801 }
802 else if( token == T_STRING )
803 {
804 fieldValue = From_UTF8( CurText() );
805 }
806 }
807
808 if( !fieldName.IsEmpty() )
809 variant.m_fields[fieldName] = std::move( fieldValue );
810 }
811 else
812 {
813 Expecting( "field" );
814 }
815 }
816 break;
817
818 default:
819 skipCurrent();
820 break;
821 }
822 }
823
824 if( !variant.m_name.IsEmpty() )
825 parsedVariants.push_back( std::move( variant ) );
826 }
827
828 break;
829 }
830
831 default:
832 // Skip not used data (i.e all other tokens)
833 skipCurrent();
834 break;
835 }
836 }
837
838 if( !footprint.IsEmpty() && fpid.Parse( footprint, true ) >= 0 )
839 {
840 THROW_IO_ERRORF( _( "Invalid footprint ID in\nfile: '%s'\nline: %d\noffset: %d" ),
841 CurSource(), CurLineNumber(), CurOffset() );
842 }
843
844 COMPONENT* component = new COMPONENT( fpid, ref, value, path, uuids );
845 component->SetName( name );
846 component->SetLibrary( library );
847 component->SetProperties( properties );
848 component->SetFields( fields );
849 component->SetHumanReadablePath( humanSheetPath );
850 component->SetComponentClassNames( componentClasses );
851 component->SetDuplicatePadNumbersAreJumpers( duplicatePinsAreJumpers );
852 std::ranges::copy( jumperPinGroups, std::inserter( component->JumperPadGroups(),
853 component->JumperPadGroups().end() ) );
854 component->SetUnitInfo( parsedUnits );
855
856 for( const COMPONENT_VARIANT& variant : parsedVariants )
857 component->AddVariant( variant );
858
859 m_netlist->AddComponent( component );
860}
861
862
864{
865 /* Parses a section like
866 * (groups
867 * (group (name "") (lib_id "DesignBlock:Block") (uuid "/f1b49c40-.../7b1488be-...")
868 * (members
869 * (member (uuid "/f1b49c40-.../dfef752d-..."))
870 *
871 * Group and member uuids are full instance paths (sheet path + item uuid), so a group on
872 * a shared sheet resolves to one group per sheet instance. Bare uuids are also accepted.
873 */
874
875 wxString name;
876 KIID uuid;
877 wxString libId; // Design block library link
878 std::vector<KIID_PATH> members;
879
880 // The token net was read, so the next data is (code <number>)
881 while( (token = NextTok() ) != T_EOF )
882 {
883 if( token == T_RIGHT )
884 break;
885 else if( token == T_LEFT )
886 token = NextTok();
887
888 switch( token )
889 {
890 case T_name:
891 NeedSYMBOLorNUMBER();
892 name = From_UTF8( CurText() );
893 NeedRIGHT();
894 break;
895
896 case T_uuid:
897 {
898 NeedSYMBOLorNUMBER();
899 wxString uuidStr = From_UTF8( CurText() );
900
901 // An instance path collapses to a deterministic uuid so re-reading the same
902 // netlist finds the same board group.
903 if( uuidStr.StartsWith( wxT( "/" ) ) )
904 uuid = KIID::FromName( std::string( KIID_PATH( uuidStr ).AsString().ToUTF8() ) );
905 else
906 uuid = uuidStr;
907
908 NeedRIGHT();
909 break;
910 }
911
912 case T_lib_id:
913 NeedSYMBOLorNUMBER();
914 libId = FromUTF8();
915 NeedRIGHT();
916 break;
917
918 case T_members:
919 while( ( token = NextTok() ) != T_RIGHT )
920 {
921 if( token == T_LEFT )
922 token = NextTok();
923
924 if( token == T_member )
925 {
926 wxString memberUuid;
927
928 while( ( token = NextTok() ) != T_RIGHT )
929 {
930 if( token == T_LEFT )
931 token = NextTok();
932
933 if( token == T_uuid )
934 {
935 NeedSYMBOLorNUMBER();
936 memberUuid = From_UTF8( CurText() );
937 NeedRIGHT();
938 }
939 else
940 {
941 Expecting( "uuid" );
942 }
943 }
944
945 members.emplace_back( memberUuid );
946 }
947 else
948 {
949 Expecting( "member" );
950 }
951
952 }
953 break;
954
955 default:
956 skipCurrent();
957 break;
958 }
959 }
960
961 LIB_ID groupLibId;
962
963 if( !libId.IsEmpty() && groupLibId.Parse( libId, true ) >= 0 )
964 {
965 THROW_IO_ERRORF( _( "Invalid lib_id ID in\nfile: '%s'\nline: %d\noffset: %d" ),
966 CurSource(), CurLineNumber(), CurOffset() );
967 }
968
969 NETLIST_GROUP* group = new NETLIST_GROUP{ std::move( name ), std::move( uuid ), std::move( groupLibId ),
970 std::move( members ) };
971 m_netlist->AddGroup( group );
972}
973
974
976{
977 // Parses a variant section like:
978 // (variant (name "Variant1") (description "First variant"))
979
980 wxString name;
981 wxString description;
982
983 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
984 {
985 if( token == T_LEFT )
986 token = NextTok();
987
988 switch( token )
989 {
990 case T_name:
991 NeedSYMBOLorNUMBER();
992 name = From_UTF8( CurText() );
993 NeedRIGHT();
994 break;
995
996 case T_description:
997 NeedSYMBOLorNUMBER();
998 description = From_UTF8( CurText() );
999 NeedRIGHT();
1000 break;
1001
1002 default:
1003 skipCurrent();
1004 break;
1005 }
1006 }
1007
1008 if( !name.IsEmpty() )
1009 m_netlist->AddVariant( name, description );
1010}
1011
1012
1014{
1015 wxString name;
1016 wxString netClass;
1017 wxString chainClass;
1018 wxString colorStr;
1019 std::vector<wxString> members;
1020 std::vector<std::pair<wxString, wxString>> terminals;
1021
1022 while( ( token = NextTok() ) != T_EOF )
1023 {
1024 if( token == T_RIGHT )
1025 break;
1026 else if( token == T_LEFT )
1027 token = NextTok();
1028
1029 switch( token )
1030 {
1031 case T_name:
1032 NeedSYMBOLorNUMBER();
1033 name = From_UTF8( CurText() );
1034 NeedRIGHT();
1035 break;
1036
1037 case T_net_class:
1038 NeedSYMBOLorNUMBER();
1039 netClass = From_UTF8( CurText() );
1040 NeedRIGHT();
1041 break;
1042
1043 case T_net_chain_class:
1044 NeedSYMBOLorNUMBER();
1045 chainClass = From_UTF8( CurText() );
1046 NeedRIGHT();
1047 break;
1048
1049 case T_color:
1050 NeedSYMBOLorNUMBER();
1051 colorStr = From_UTF8( CurText() );
1052 NeedRIGHT();
1053 break;
1054
1055 case T_members:
1056 while( ( token = NextTok() ) != T_RIGHT )
1057 {
1058 if( token == T_LEFT )
1059 token = NextTok();
1060
1061 if( token == T_member )
1062 {
1063 while( ( token = NextTok() ) != T_RIGHT )
1064 {
1065 if( token == T_LEFT )
1066 token = NextTok();
1067
1068 if( token == T_net )
1069 {
1070 NeedSYMBOLorNUMBER();
1071 members.emplace_back( From_UTF8( CurText() ) );
1072 NeedRIGHT();
1073 }
1074 else
1075 skipCurrent();
1076 }
1077 }
1078 else
1079 skipCurrent();
1080 }
1081 break;
1082
1083 case T_terminal_pins:
1084 while( ( token = NextTok() ) != T_RIGHT )
1085 {
1086 if( token == T_LEFT )
1087 token = NextTok();
1088
1089 if( token == T_terminal_pin )
1090 {
1091 wxString ref;
1092 wxString pin;
1093
1094 while( ( token = NextTok() ) != T_RIGHT )
1095 {
1096 if( token == T_LEFT )
1097 token = NextTok();
1098
1099 if( token == T_ref )
1100 {
1101 NeedSYMBOLorNUMBER();
1102 ref = From_UTF8( CurText() );
1103 NeedRIGHT();
1104 }
1105 else if( token == T_pin )
1106 {
1107 NeedSYMBOLorNUMBER();
1108 pin = From_UTF8( CurText() );
1109 NeedRIGHT();
1110 }
1111 else
1112 skipCurrent();
1113 }
1114
1115 if( !ref.IsEmpty() && !pin.IsEmpty() )
1116 terminals.emplace_back( ref, pin );
1117 }
1118 else
1119 skipCurrent();
1120 }
1121 break;
1122
1123 default:
1124 skipCurrent();
1125 break;
1126 }
1127 }
1128
1129 for( const wxString& netName : members )
1130 m_netlist->SetNetChainFor( netName, name );
1131
1132 for( const auto& term : terminals )
1133 m_netlist->AddSignalTerminalPin( name, term.first, term.second );
1134
1135 if( !netClass.IsEmpty() )
1136 m_netlist->SetSignalNetClass( name, netClass );
1137
1138 if( !colorStr.IsEmpty() )
1139 m_netlist->SetSignalColor( name, colorStr );
1140
1141 if( !chainClass.IsEmpty() )
1142 m_netlist->SetSignalChainClass( name, chainClass );
1143}
1144
1145
1147{
1148 /* Parses a section like
1149 * (libpart (lib device) (part C)
1150 * (aliases
1151 * (alias Cxx)
1152 * (alias Cyy))
1153 * (description "Condensateur non polarise")
1154 * (footprints
1155 * (fp SM*)
1156 * (fp C?)
1157 * (fp C1-1))
1158 * (fields
1159 * (field (name Reference) C)
1160 * (field (name Value) C))
1161 * (pins
1162 * (pin (num 1) (name ~) (type passive))
1163 * (pin (num 2) (name ~) (type passive))))
1164 *
1165 * Currently footprints section/fp are read and data stored
1166 * other fields (unused) are skipped
1167 */
1168 COMPONENT* component = NULL;
1169 wxString libName;
1170 wxString libPartName;
1171 wxArrayString footprintFilters;
1172 wxArrayString aliases;
1173 int pinCount = 0;
1174
1175 // The last token read was libpart, so read the next token
1176 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseLibPartList: begin libpart" ) );
1177 while( (token = NextTok() ) != T_RIGHT )
1178 {
1179 if( token == T_LEFT )
1180 token = NextTok();
1181
1182 switch( token )
1183 {
1184 case T_lib:
1185 NeedSYMBOLorNUMBER();
1186 libName = From_UTF8( CurText() );
1187 NeedRIGHT();
1188 break;
1189
1190 case T_part:
1191 NeedSYMBOLorNUMBER();
1192 libPartName = From_UTF8( CurText() );
1193 NeedRIGHT();
1194 break;
1195
1196 case T_footprints:
1197 // Read all fp elements (footprint filter item)
1198 while( (token = NextTok() ) != T_RIGHT )
1199 {
1200 if( token == T_LEFT )
1201 token = NextTok();
1202
1203 if( token != T_fp )
1204 Expecting( T_fp );
1205
1206 token = NextTok();
1207
1208 // Accept an empty (fp) sexpr. We do write them out.
1209 if( token == T_RIGHT )
1210 continue;
1211
1212 if( !IsSymbol( token ) && !IsNumber( token ) )
1213 Expecting( "footprint ID" );
1214
1215 footprintFilters.Add( From_UTF8( CurText() ) );
1216 NeedRIGHT();
1217 }
1218 break;
1219
1220 case T_aliases:
1221 while( (token = NextTok() ) != T_RIGHT )
1222 {
1223 if( token == T_LEFT )
1224 token = NextTok();
1225
1226 if( token != T_alias )
1227 Expecting( T_alias );
1228
1229 NeedSYMBOLorNUMBER();
1230 aliases.Add( From_UTF8( CurText() ) );
1231 NeedRIGHT();
1232 }
1233 break;
1234
1235 case T_pins:
1236 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseLibPartList: entering pins for '%s:%s'" ),
1237 libName, libPartName );
1238 while( (token = NextTok() ) != T_RIGHT )
1239 {
1240 if( token == T_LEFT )
1241 token = NextTok();
1242
1243 if( token != T_pin )
1244 Expecting( T_pin );
1245
1246 pinCount++;
1247 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseLibPartList: pin #%d for '%s:%s'" ),
1248 pinCount, libName, libPartName );
1249
1250 skipCurrent();
1251 }
1252 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Parsed libpart '%s:%s' pins => pinCount=%d" ),
1253 libName, libPartName, pinCount );
1254 break;
1255
1256 default:
1257 // Skip not used data (i.e all other tokens)
1258 skipCurrent();
1259 break;
1260 }
1261 }
1262
1263 // Find all of the components that reference this component library part definition.
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++ )
1267 {
1268 component = m_netlist->GetComponent( i );
1269
1270 if( component->IsLibSource( libName, libPartName ) )
1271 {
1272 component->SetFootprintFilters( footprintFilters );
1273 component->SetPinCount( pinCount );
1274 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Assign pinCount=%d to component ref='%s' part='%s:%s'" ),
1275 pinCount, component->GetReference(), libName, libPartName );
1276 }
1277
1278 for( unsigned jj = 0; jj < aliases.GetCount(); jj++ )
1279 {
1280 if( component->IsLibSource( libName, aliases[jj] ) )
1281 {
1282 component->SetFootprintFilters( footprintFilters );
1283 component->SetPinCount( pinCount );
1284 wxLogTrace( "CVPCB_PINCOUNT",
1285 wxT( "Assign pinCount=%d to component ref='%s' via alias='%s:%s'" ),
1286 pinCount, component->GetReference(), libName, aliases[jj] );
1287 }
1288 }
1289
1290 }
1291}
const char * name
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.
Definition kiid.cpp:237
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:62
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 _(s)
#define THROW_IO_ERRORF(msg,...)
_OUT_STRING AsString(const std::string &aString)
Definition sexpr.h:131
static bool IsNumber(char x)
wxString From_UTF8(const char *cstring)
bool m_hasExcludedFromPosFiles
nlohmann::ordered_map< wxString, wxString > m_fields
std::string path
KIBIS_PIN * pin