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 wxString error;
841 error.Printf( _( "Invalid footprint ID in\nfile: '%s'\nline: %d\noffset: %d" ),
842 CurSource(), CurLineNumber(), CurOffset() );
843
844 THROW_IO_ERROR( error );
845 }
846
847 COMPONENT* component = new COMPONENT( fpid, ref, value, path, uuids );
848 component->SetName( name );
849 component->SetLibrary( library );
850 component->SetProperties( properties );
851 component->SetFields( fields );
852 component->SetHumanReadablePath( humanSheetPath );
853 component->SetComponentClassNames( componentClasses );
854 component->SetDuplicatePadNumbersAreJumpers( duplicatePinsAreJumpers );
855 std::ranges::copy( jumperPinGroups, std::inserter( component->JumperPadGroups(),
856 component->JumperPadGroups().end() ) );
857 component->SetUnitInfo( parsedUnits );
858
859 for( const COMPONENT_VARIANT& variant : parsedVariants )
860 component->AddVariant( variant );
861
862 m_netlist->AddComponent( component );
863}
864
865
867{
868 /* Parses a section like
869 * (groups
870 * (group (name "") (lib_id "DesignBlock:Block") (uuid "7b1488be-4c43-4004-94fc-e4a26dda8f5b")
871 * (members
872 * (member (uuid "dfef752d-e203-4feb-91de-483b44bc4062"))
873 */
874
875 wxString name;
876 KIID uuid;
877 wxString libId; // Design block library link
878 std::vector<KIID> 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 NeedSYMBOLorNUMBER();
898 uuid = From_UTF8( CurText() );
899 NeedRIGHT();
900 break;
901
902 case T_lib_id:
903 NeedSYMBOLorNUMBER();
904 libId = FromUTF8();
905 NeedRIGHT();
906 break;
907
908 case T_members:
909 while( ( token = NextTok() ) != T_RIGHT )
910 {
911 if( token == T_LEFT )
912 token = NextTok();
913
914 if( token == T_member )
915 {
916 wxString memberUuid;
917
918 while( ( token = NextTok() ) != T_RIGHT )
919 {
920 if( token == T_LEFT )
921 token = NextTok();
922
923 if( token == T_uuid )
924 {
925 NeedSYMBOLorNUMBER();
926 memberUuid = From_UTF8( CurText() );
927 NeedRIGHT();
928 }
929 else
930 {
931 Expecting( "uuid" );
932 }
933 }
934
935 members.emplace_back( memberUuid );
936 }
937 else
938 {
939 Expecting( "member" );
940 }
941
942 }
943 break;
944
945 default:
946 skipCurrent();
947 break;
948 }
949 }
950
951 LIB_ID groupLibId;
952
953 if( !libId.IsEmpty() && groupLibId.Parse( libId, true ) >= 0 )
954 {
955 wxString error;
956 error.Printf( _( "Invalid lib_id ID in\nfile: '%s'\nline: %d\noffset: %d" ), CurSource(), CurLineNumber(),
957 CurOffset() );
958
959 THROW_IO_ERROR( error );
960 }
961
962 NETLIST_GROUP* group = new NETLIST_GROUP{ std::move( name ), std::move( uuid ), std::move( groupLibId ),
963 std::move( members ) };
964 m_netlist->AddGroup( group );
965}
966
967
969{
970 // Parses a variant section like:
971 // (variant (name "Variant1") (description "First variant"))
972
973 wxString name;
974 wxString description;
975
976 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
977 {
978 if( token == T_LEFT )
979 token = NextTok();
980
981 switch( token )
982 {
983 case T_name:
984 NeedSYMBOLorNUMBER();
985 name = From_UTF8( CurText() );
986 NeedRIGHT();
987 break;
988
989 case T_description:
990 NeedSYMBOLorNUMBER();
991 description = From_UTF8( CurText() );
992 NeedRIGHT();
993 break;
994
995 default:
996 skipCurrent();
997 break;
998 }
999 }
1000
1001 if( !name.IsEmpty() )
1002 m_netlist->AddVariant( name, description );
1003}
1004
1005
1007{
1008 wxString name;
1009 wxString netClass;
1010 wxString chainClass;
1011 wxString colorStr;
1012 std::vector<wxString> members;
1013 std::vector<std::pair<wxString, wxString>> terminals;
1014
1015 while( ( token = NextTok() ) != T_EOF )
1016 {
1017 if( token == T_RIGHT )
1018 break;
1019 else if( token == T_LEFT )
1020 token = NextTok();
1021
1022 switch( token )
1023 {
1024 case T_name:
1025 NeedSYMBOLorNUMBER();
1026 name = From_UTF8( CurText() );
1027 NeedRIGHT();
1028 break;
1029
1030 case T_net_class:
1031 NeedSYMBOLorNUMBER();
1032 netClass = From_UTF8( CurText() );
1033 NeedRIGHT();
1034 break;
1035
1036 case T_net_chain_class:
1037 NeedSYMBOLorNUMBER();
1038 chainClass = From_UTF8( CurText() );
1039 NeedRIGHT();
1040 break;
1041
1042 case T_color:
1043 NeedSYMBOLorNUMBER();
1044 colorStr = From_UTF8( CurText() );
1045 NeedRIGHT();
1046 break;
1047
1048 case T_members:
1049 while( ( token = NextTok() ) != T_RIGHT )
1050 {
1051 if( token == T_LEFT )
1052 token = NextTok();
1053
1054 if( token == T_member )
1055 {
1056 while( ( token = NextTok() ) != T_RIGHT )
1057 {
1058 if( token == T_LEFT )
1059 token = NextTok();
1060
1061 if( token == T_net )
1062 {
1063 NeedSYMBOLorNUMBER();
1064 members.emplace_back( From_UTF8( CurText() ) );
1065 NeedRIGHT();
1066 }
1067 else
1068 skipCurrent();
1069 }
1070 }
1071 else
1072 skipCurrent();
1073 }
1074 break;
1075
1076 case T_terminal_pins:
1077 while( ( token = NextTok() ) != T_RIGHT )
1078 {
1079 if( token == T_LEFT )
1080 token = NextTok();
1081
1082 if( token == T_terminal_pin )
1083 {
1084 wxString ref;
1085 wxString pin;
1086
1087 while( ( token = NextTok() ) != T_RIGHT )
1088 {
1089 if( token == T_LEFT )
1090 token = NextTok();
1091
1092 if( token == T_ref )
1093 {
1094 NeedSYMBOLorNUMBER();
1095 ref = From_UTF8( CurText() );
1096 NeedRIGHT();
1097 }
1098 else if( token == T_pin )
1099 {
1100 NeedSYMBOLorNUMBER();
1101 pin = From_UTF8( CurText() );
1102 NeedRIGHT();
1103 }
1104 else
1105 skipCurrent();
1106 }
1107
1108 if( !ref.IsEmpty() && !pin.IsEmpty() )
1109 terminals.emplace_back( ref, pin );
1110 }
1111 else
1112 skipCurrent();
1113 }
1114 break;
1115
1116 default:
1117 skipCurrent();
1118 break;
1119 }
1120 }
1121
1122 for( const wxString& netName : members )
1123 m_netlist->SetNetChainFor( netName, name );
1124
1125 for( const auto& term : terminals )
1126 m_netlist->AddSignalTerminalPin( name, term.first, term.second );
1127
1128 if( !netClass.IsEmpty() )
1129 m_netlist->SetSignalNetClass( name, netClass );
1130
1131 if( !colorStr.IsEmpty() )
1132 m_netlist->SetSignalColor( name, colorStr );
1133
1134 if( !chainClass.IsEmpty() )
1135 m_netlist->SetSignalChainClass( name, chainClass );
1136}
1137
1138
1140{
1141 /* Parses a section like
1142 * (libpart (lib device) (part C)
1143 * (aliases
1144 * (alias Cxx)
1145 * (alias Cyy))
1146 * (description "Condensateur non polarise")
1147 * (footprints
1148 * (fp SM*)
1149 * (fp C?)
1150 * (fp C1-1))
1151 * (fields
1152 * (field (name Reference) C)
1153 * (field (name Value) C))
1154 * (pins
1155 * (pin (num 1) (name ~) (type passive))
1156 * (pin (num 2) (name ~) (type passive))))
1157 *
1158 * Currently footprints section/fp are read and data stored
1159 * other fields (unused) are skipped
1160 */
1161 COMPONENT* component = NULL;
1162 wxString libName;
1163 wxString libPartName;
1164 wxArrayString footprintFilters;
1165 wxArrayString aliases;
1166 int pinCount = 0;
1167
1168 // The last token read was libpart, so read the next token
1169 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseLibPartList: begin libpart" ) );
1170 while( (token = NextTok() ) != T_RIGHT )
1171 {
1172 if( token == T_LEFT )
1173 token = NextTok();
1174
1175 switch( token )
1176 {
1177 case T_lib:
1178 NeedSYMBOLorNUMBER();
1179 libName = From_UTF8( CurText() );
1180 NeedRIGHT();
1181 break;
1182
1183 case T_part:
1184 NeedSYMBOLorNUMBER();
1185 libPartName = From_UTF8( CurText() );
1186 NeedRIGHT();
1187 break;
1188
1189 case T_footprints:
1190 // Read all fp elements (footprint filter item)
1191 while( (token = NextTok() ) != T_RIGHT )
1192 {
1193 if( token == T_LEFT )
1194 token = NextTok();
1195
1196 if( token != T_fp )
1197 Expecting( T_fp );
1198
1199 token = NextTok();
1200
1201 // Accept an empty (fp) sexpr. We do write them out.
1202 if( token == T_RIGHT )
1203 continue;
1204
1205 if( !IsSymbol( token ) && !IsNumber( token ) )
1206 Expecting( "footprint ID" );
1207
1208 footprintFilters.Add( From_UTF8( CurText() ) );
1209 NeedRIGHT();
1210 }
1211 break;
1212
1213 case T_aliases:
1214 while( (token = NextTok() ) != T_RIGHT )
1215 {
1216 if( token == T_LEFT )
1217 token = NextTok();
1218
1219 if( token != T_alias )
1220 Expecting( T_alias );
1221
1222 NeedSYMBOLorNUMBER();
1223 aliases.Add( From_UTF8( CurText() ) );
1224 NeedRIGHT();
1225 }
1226 break;
1227
1228 case T_pins:
1229 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseLibPartList: entering pins for '%s:%s'" ),
1230 libName, libPartName );
1231 while( (token = NextTok() ) != T_RIGHT )
1232 {
1233 if( token == T_LEFT )
1234 token = NextTok();
1235
1236 if( token != T_pin )
1237 Expecting( T_pin );
1238
1239 pinCount++;
1240 wxLogTrace( "CVPCB_PINCOUNT", wxT( "parseLibPartList: pin #%d for '%s:%s'" ),
1241 pinCount, libName, libPartName );
1242
1243 skipCurrent();
1244 }
1245 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Parsed libpart '%s:%s' pins => pinCount=%d" ),
1246 libName, libPartName, pinCount );
1247 break;
1248
1249 default:
1250 // Skip not used data (i.e all other tokens)
1251 skipCurrent();
1252 break;
1253 }
1254 }
1255
1256 // Find all of the components that reference this component library part definition.
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++ )
1260 {
1261 component = m_netlist->GetComponent( i );
1262
1263 if( component->IsLibSource( libName, libPartName ) )
1264 {
1265 component->SetFootprintFilters( footprintFilters );
1266 component->SetPinCount( pinCount );
1267 wxLogTrace( "CVPCB_PINCOUNT", wxT( "Assign pinCount=%d to component ref='%s' part='%s:%s'" ),
1268 pinCount, component->GetReference(), libName, libPartName );
1269 }
1270
1271 for( unsigned jj = 0; jj < aliases.GetCount(); jj++ )
1272 {
1273 if( component->IsLibSource( libName, aliases[jj] ) )
1274 {
1275 component->SetFootprintFilters( footprintFilters );
1276 component->SetPinCount( pinCount );
1277 wxLogTrace( "CVPCB_PINCOUNT",
1278 wxT( "Assign pinCount=%d to component ref='%s' via alias='%s:%s'" ),
1279 pinCount, component->GetReference(), libName, aliases[jj] );
1280 }
1281 }
1282
1283 }
1284}
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.
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:48
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_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_hasExcludedFromPosFiles
nlohmann::ordered_map< wxString, wxString > m_fields
std::string path
KIBIS_PIN * pin