KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_rule_parser.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#include <board.h>
22#include <zones.h>
23#include <drc/drc_rule_parser.h>
25#include <drc_rules_lexer.h>
26#include <pcbexpr_evaluator.h>
27#include <reporter.h>
29#include <properties/property.h>
31
32using namespace DRCRULE_T;
33
34
35DRC_RULES_PARSER::DRC_RULES_PARSER( const wxString& aSource, const wxString& aSourceDescr ) :
36 DRC_RULES_LEXER( aSource.ToStdString(), aSourceDescr ),
38 m_tooRecent( false ),
39 m_reporter( nullptr )
40{
41}
42
43
44void DRC_RULES_PARSER::reportError( const wxString& aMessage, int aOffset )
45{
46 reportErrorAt( aMessage, CurLineNumber(), CurOffset() + aOffset, CurLine() );
47}
48
49
50void DRC_RULES_PARSER::reportErrorAt( const wxString& aMessage, int aLine, int aOffset, const char* aSourceLine )
51{
52 wxString rest;
53 wxString first = aMessage.BeforeFirst( '|', &rest );
54
55 if( m_reporter )
56 {
57 wxString msg = wxString::Format( _( "ERROR: <a href='%d:%d'>%s</a>%s" ),
58 aLine,
59 aOffset,
60 first,
61 rest );
62
63 m_reporter->Report( msg, RPT_SEVERITY_ERROR );
64 }
65 else
66 {
67 wxString msg = wxString::Format( _( "ERROR: %s%s" ), first, rest );
68
69 THROW_PARSE_ERROR( msg, CurSource(), aSourceLine, aLine, aOffset );
70 }
71}
72
73
74void DRC_RULES_PARSER::reportDeprecation( const wxString& oldToken, const wxString& newToken )
75{
76 if( m_reporter )
77 {
78 wxString msg = wxString::Format( _( "The '%s' keyword has been deprecated. Please use '%s' instead." ),
79 oldToken,
80 newToken);
81
82 m_reporter->Report( msg, RPT_SEVERITY_WARNING );
83 }
84}
85
86
88{
89 for( size_t pos = curText.find( "${" ); pos != std::string::npos;
90 pos = curText.find( "${", pos + 2 ) )
91 {
92 size_t end = curText.find( '}', pos + 2 );
93
94 if( end != std::string::npos )
95 {
96 wxString token = wxString::FromUTF8( curText.substr( pos + 2, end - ( pos + 2 ) ) );
97
98 // ${Class:X} is a DRC expression directive handled by testFootprintSelector(), so it is
99 // left unexpanded on purpose and must not be reported as unresolved.
100 if( IsComponentClassSelector( token ) )
101 continue;
102 }
103
104 reportError( _( "Unresolved text variable" ), (int) pos );
105 return true;
106 }
107
108 return false;
109}
110
111
113{
114 int depth = 1;
115
116 for( T token = NextTok(); token != T_EOF; token = NextTok() )
117 {
118 if( token == T_LEFT )
119 depth++;
120
121 if( token == T_RIGHT )
122 {
123 if( --depth == 0 )
124 break;
125 }
126 }
127}
128
129
130void DRC_RULES_PARSER::expected( const wxString& expectedTokens )
131{
132 wxString msg;
133
134 if( curText.starts_with( "${" ) )
135 msg.Printf( _( "Unresolved text variable." ) );
136 else
137 msg.Printf( _( "Unrecognized item '%s'.| Expected %s." ), FromUTF8(), expectedTokens );
138
139 reportError( msg );
140 parseUnknown();
141}
142
143
145{
146 wxString expr;
147 int depth = 1;
148
149 for( T token = NextTok(); token != T_EOF; token = NextTok() )
150 {
151 if( token == T_LEFT )
152 depth++;
153
154 if( token == T_RIGHT )
155 {
156 if( --depth == 0 )
157 break;
158 }
159
160 if( !expr.IsEmpty() )
161 expr += CurSeparator();
162
164 expr += FromUTF8();
165 }
166
167 return expr;
168}
169
170
171void DRC_RULES_PARSER::Parse( std::vector<std::shared_ptr<DRC_RULE>>& aRules, REPORTER* aReporter )
172{
173 bool haveVersion = false;
174
175 m_reporter = aReporter;
176
177 for( T token = NextTok(); token != T_EOF; token = NextTok() )
178 {
180 continue;
181
182 if( token != T_LEFT )
183 reportError( _( "Missing '('." ) );
184
185 token = NextTok();
186
187 if( !haveVersion && token != T_version )
188 {
189 reportError( _( "Missing version statement." ) );
190 haveVersion = true; // don't keep on reporting it
191 }
192
193 switch( token )
194 {
195 case T_version:
196 haveVersion = true;
197 token = NextTok();
198
199 if( (int) token == DSN_RIGHT )
200 {
201 reportError( _( "Missing version number." ) );
202 }
203 else if( (int) token == DSN_NUMBER )
204 {
205 m_requiredVersion = (int)strtol( CurText(), nullptr, 10 );
207
208 if( (int) NextTok() != DSN_RIGHT )
209 reportError( _( "Missing ')'." ) );
210 }
211 else
212 {
213 expected( _( "version number" ) ); // translate "version number"; it is not a token
214 }
215
216 break;
217
218 case T_rule:
219 aRules.emplace_back( parseDRC_RULE() );
220 break;
221
222 case T_EOF:
223 reportError( _( "Incomplete statement." ) );
224 break;
225
226 default:
227 expected( wxT( "rule or version" ) );
228 }
229 }
230
231 if( m_reporter && !m_reporter->HasMessage() )
232 m_reporter->Report( _( "No errors found." ), RPT_SEVERITY_INFO );
233
234 m_reporter = nullptr;
235}
236
237
239 std::vector<std::shared_ptr<COMPONENT_CLASS_ASSIGNMENT_RULE>>& aRules, REPORTER* aReporter )
240{
241 bool haveVersion = false;
242
243 m_reporter = aReporter;
244
245 for( T token = NextTok(); token != T_EOF; token = NextTok() )
246 {
247 if( token != T_LEFT )
248 reportError( _( "Missing '('." ) );
249
250 token = NextTok();
251
252 if( !haveVersion && token != T_version )
253 {
254 reportError( _( "Missing version statement." ) );
255 haveVersion = true; // don't keep on reporting it
256 }
257
258 switch( token )
259 {
260 case T_version:
261 haveVersion = true;
262 token = NextTok();
263
264 if( (int) token == DSN_RIGHT )
265 {
266 reportError( _( "Missing version number." ) );
267 }
268 else if( (int) token == DSN_NUMBER )
269 {
270 m_requiredVersion = (int) strtol( CurText(), nullptr, 10 );
272
273 if( (int) NextTok() != DSN_RIGHT )
274 reportError( _( "Missing ')'." ) );
275 }
276 else
277 {
278 expected( _( "version number" ) ); // translate "version number"; it is not a token
279 }
280
281 break;
282
283 case T_assign_component_class:
284 aRules.emplace_back( parseComponentClassAssignment() );
285 break;
286
287 case T_EOF:
288 reportError( _( "Incomplete statement." ) );
289 break;
290
291 default:
292 expected( wxT( "assign_component_class or version" ) );
293 }
294 }
295
296 if( m_reporter && !m_reporter->HasMessage() )
297 m_reporter->Report( _( "No errors found." ), RPT_SEVERITY_INFO );
298
299 m_reporter = nullptr;
300}
301
302
303std::shared_ptr<DRC_RULE> DRC_RULES_PARSER::parseDRC_RULE()
304{
305 std::shared_ptr<DRC_RULE> rule = std::make_shared<DRC_RULE>();
306 int conditionLine = 0;
307 int conditionOffset = 0;
308 std::string conditionSource;
309
310 T token = NextTok();
311
312 if( !IsSymbol( token ) )
313 reportError( _( "Missing rule name." ) );
314
316 rule->m_Name = FromUTF8();
317
318 for( token = NextTok(); token != T_RIGHT && token != T_EOF; token = NextTok() )
319 {
321 continue;
322
323 if( token != T_LEFT )
324 reportError( _( "Missing '('." ) );
325
326 token = NextTok();
327
328 switch( token )
329 {
330 case T_constraint:
331 parseConstraint( rule.get() );
332 break;
333
334 case T_condition:
335 token = NextTok();
336
337 if( (int) token == DSN_RIGHT )
338 {
339 reportError( _( "Missing condition expression." ) );
340 }
341 else if( IsSymbol( token ) )
342 {
344 rule->m_Condition = new DRC_RULE_CONDITION( FromUTF8() );
345 conditionLine = CurLineNumber();
346 conditionOffset = CurOffset();
347 conditionSource = CurLine();
348
349 if( !rule->m_Condition->Compile( m_reporter, CurLineNumber(), CurOffset() ) )
350 reportError( wxString::Format( _( "Could not parse expression '%s'." ), FromUTF8() ) );
351
352 if( (int) NextTok() != DSN_RIGHT )
353 reportError( _( "Missing ')'." ) );
354 }
355 else
356 {
357 expected( _( "quoted expression" ) ); // translate "quoted expression"; it is not a token
358 }
359
360 break;
361
362 case T_layer:
363 if( rule->m_LayerCondition != LSET::AllLayersMask() )
364 reportError( _( "'layer' keyword already present." ) );
365
366 rule->m_LayerCondition = parseLayer( &rule->m_LayerSource );
367 break;
368
369 case T_severity:
370 rule->m_Severity = parseSeverity();
371 break;
372
373 case T_EOF:
374 reportError( _( "Incomplete statement." ) );
375 return rule;
376
377 default:
378 expected( wxT( "constraint, condition, or disallow" ) );
379 }
380 }
381
382 if( (int) CurTok() != DSN_RIGHT )
383 reportError( _( "Missing ')'." ) );
384
385 if( rule->m_Condition && rule->m_Condition->RequiresPairItems() )
386 {
387 for( const DRC_CONSTRAINT& constraint : rule->m_Constraints )
388 {
389 // isCoupledDiffPair() can identify the pair from A's net for these constraints
390 if( !rule->m_Condition->ReferencesItemB()
391 && ( constraint.m_Type == LENGTH_CONSTRAINT
392 || constraint.m_Type == NET_CHAIN_LENGTH_CONSTRAINT
393 || constraint.m_Type == SKEW_CONSTRAINT ) )
394 {
395 continue;
396 }
397
398 if( constraint.IsUnary() )
399 {
400 reportErrorAt( wxString::Format(
401 _( "Item 'B' is not available for a single-item constraint in rule '%s'." ), rule->m_Name ),
402 conditionLine, conditionOffset, conditionSource.c_str() );
403 break;
404 }
405 }
406 }
407
408 return rule;
409}
410
411
412std::shared_ptr<COMPONENT_CLASS_ASSIGNMENT_RULE> DRC_RULES_PARSER::parseComponentClassAssignment()
413{
414 std::shared_ptr<DRC_RULE_CONDITION> condition;
415
416 T token = NextTok();
417
418 if( !IsSymbol( token ) )
419 reportError( _( "Missing component class name." ) );
420
422 wxString componentClass = FromUTF8();
423
424 for( token = NextTok(); token != T_RIGHT && token != T_EOF; token = NextTok() )
425 {
426 if( token != T_LEFT )
427 reportError( _( "Missing '('." ) );
428
429 token = NextTok();
430
431 switch( token )
432 {
433 case T_condition:
434 token = NextTok();
435
436 if( (int) token == DSN_RIGHT )
437 {
438 reportError( _( "Missing condition expression." ) );
439 }
440 else if( IsSymbol( token ) )
441 {
443 condition = std::make_shared<DRC_RULE_CONDITION>( FromUTF8() );
444
445 if( !condition->Compile( m_reporter, CurLineNumber(), CurOffset() ) )
446 reportError( wxString::Format( _( "Could not parse expression '%s'." ), FromUTF8() ) );
447
448 if( (int) NextTok() != DSN_RIGHT )
449 reportError( _( "Missing ')'." ) );
450 }
451 else
452 {
453 expected( _( "quoted expression" ) ); // translate "quoted expression"; it is not a token
454 }
455
456 break;
457
458 case T_EOF:
459 reportError( _( "Incomplete statement." ) );
460 return nullptr;
461
462 default:
463 expected( wxT( "condition" ) );
464 }
465 }
466
467 if( (int) CurTok() != DSN_RIGHT )
468 reportError( _( "Missing ')'." ) );
469
470 return std::make_shared<COMPONENT_CLASS_ASSIGNMENT_RULE>( componentClass, std::move( condition ) );
471}
472
473
475{
477 int value;
480 wxString msg;
481 bool allowsTimeDomain = false;
482
483 auto validateAndSetValueWithUnits =
484 [this, &allowsTimeDomain, &unitsType, &c]( int aValue, const EDA_UNITS aUnits, auto aSetter )
485 {
486 const EDA_DATA_TYPE unitsTypeTmp = UNITS_PROVIDER::GetTypeFromUnits( aUnits );
487
488 if( !allowsTimeDomain && unitsTypeTmp == EDA_DATA_TYPE::TIME )
489 reportError( _( "Time based units not allowed for constraint type." ) );
490
491 if( ( c.m_Value.HasMin() || c.m_Value.HasMax() || c.m_Value.HasOpt() )
492 && unitsType != unitsTypeTmp )
493 {
494 reportError( _( "Mixed units for constraint values." ) );
495 }
496
497 unitsType = unitsTypeTmp;
498 aSetter( aValue );
499
500 if( allowsTimeDomain )
501 {
503 {
506 }
507 else
508 {
511 }
512 }
513 };
514
515 T token = NextTok();
516
518 return;
519
520 if( token == T_mechanical_clearance )
521 {
522 reportDeprecation( wxT( "mechanical_clearance" ), wxT( "physical_clearance" ) );
523 token = T_physical_clearance;
524 }
525 else if( token == T_mechanical_hole_clearance )
526 {
527 reportDeprecation( wxT( "mechanical_hole_clearance" ), wxT( "physical_hole_clearance" ) );
528 token = T_physical_hole_clearance;
529 }
530 else if( token == T_hole )
531 {
532 reportDeprecation( wxT( "hole" ), wxT( "hole_size" ) );
533 token = T_hole_size;
534 }
535 else if( (int) token == DSN_RIGHT || token == T_EOF )
536 {
537 msg.Printf( _( "Missing constraint type.| Expected %s." ),
538 wxT( "assertion, clearance, hole_clearance, edge_clearance, physical_clearance, "
539 "physical_hole_clearance, courtyard_clearance, silk_clearance, hole_size, "
540 "hole_to_hole, track_width, track_angle, track_segment_length, annular_width, "
541 "disallow, zone_connection, thermal_relief_gap, thermal_spoke_width, "
542 "min_resolved_spokes, microvia_stack_depth, hole_aspect_ratio, solder_mask_expansion, "
543 "solder_paste_abs_margin, "
544 "solder_paste_rel_margin, length, net_chain_length, skew, via_count, "
545 "via_dangling, via_diameter, diff_pair_gap or diff_pair_uncoupled" ) );
546
547 reportError( msg );
548 return;
549 }
550
551 switch( token )
552 {
553 case T_assertion: c.m_Type = ASSERTION_CONSTRAINT; break;
554 case T_clearance: c.m_Type = CLEARANCE_CONSTRAINT; break;
555 case T_creepage: c.m_Type = CREEPAGE_CONSTRAINT; break;
556 case T_hole_clearance: c.m_Type = HOLE_CLEARANCE_CONSTRAINT; break;
557 case T_edge_clearance: c.m_Type = EDGE_CLEARANCE_CONSTRAINT; break;
558 case T_hole_size: c.m_Type = HOLE_SIZE_CONSTRAINT; break;
559 case T_hole_to_hole: c.m_Type = HOLE_TO_HOLE_CONSTRAINT; break;
560 case T_courtyard_clearance: c.m_Type = COURTYARD_CLEARANCE_CONSTRAINT; break;
561 case T_silk_clearance: c.m_Type = SILK_CLEARANCE_CONSTRAINT; break;
562 case T_line_width: c.m_Type = LINE_WIDTH_CONSTRAINT; break;
563 case T_text_height: c.m_Type = TEXT_HEIGHT_CONSTRAINT; break;
564 case T_text_thickness: c.m_Type = LINE_WIDTH_CONSTRAINT; break;
565 case T_track_width: c.m_Type = TRACK_WIDTH_CONSTRAINT; break;
566 case T_track_angle: c.m_Type = TRACK_ANGLE_CONSTRAINT; break;
567 case T_track_segment_length: c.m_Type = TRACK_SEGMENT_LENGTH_CONSTRAINT; break;
568 case T_connection_width: c.m_Type = CONNECTION_WIDTH_CONSTRAINT; break;
569 case T_annular_width: c.m_Type = ANNULAR_WIDTH_CONSTRAINT; break;
570 case T_via_diameter: c.m_Type = VIA_DIAMETER_CONSTRAINT; break;
571 case T_via_dangling: c.m_Type = VIA_DANGLING_CONSTRAINT; break;
572 case T_zone_connection: c.m_Type = ZONE_CONNECTION_CONSTRAINT; break;
573 case T_thermal_relief_gap: c.m_Type = THERMAL_RELIEF_GAP_CONSTRAINT; break;
574 case T_thermal_spoke_width: c.m_Type = THERMAL_SPOKE_WIDTH_CONSTRAINT; break;
575 case T_microvia_stack_depth: c.m_Type = MICROVIA_STACK_DEPTH_CONSTRAINT; break;
576 case T_hole_aspect_ratio: c.m_Type = HOLE_ASPECT_RATIO_CONSTRAINT; break;
577 case T_min_resolved_spokes: c.m_Type = MIN_RESOLVED_SPOKES_CONSTRAINT; break;
578 case T_solder_mask_expansion: c.m_Type = SOLDER_MASK_EXPANSION_CONSTRAINT; break;
579 case T_solder_mask_sliver: c.m_Type = SOLDER_MASK_SLIVER_CONSTRAINT; break;
580 case T_solder_paste_abs_margin: c.m_Type = SOLDER_PASTE_ABS_MARGIN_CONSTRAINT; break;
581 case T_solder_paste_rel_margin: c.m_Type = SOLDER_PASTE_REL_MARGIN_CONSTRAINT; break;
582 case T_disallow: c.m_Type = DISALLOW_CONSTRAINT; break;
583 case T_length: c.m_Type = LENGTH_CONSTRAINT; break;
584 case T_net_chain_length: c.m_Type = NET_CHAIN_LENGTH_CONSTRAINT; break;
585 case T_stub_length: c.m_Type = NET_CHAIN_STUB_LENGTH_CONSTRAINT; break;
586 case T_return_path: c.m_Type = NET_CHAIN_RETURN_PATH_CONSTRAINT; break;
587 case T_skew: c.m_Type = SKEW_CONSTRAINT; break;
588 case T_via_count: c.m_Type = VIA_COUNT_CONSTRAINT; break;
589 case T_diff_pair_gap: c.m_Type = DIFF_PAIR_GAP_CONSTRAINT; break;
590 case T_diff_pair_uncoupled: c.m_Type = MAX_UNCOUPLED_CONSTRAINT; break;
591 case T_physical_clearance: c.m_Type = PHYSICAL_CLEARANCE_CONSTRAINT; break;
592 case T_physical_hole_clearance: c.m_Type = PHYSICAL_HOLE_CLEARANCE_CONSTRAINT; break;
593 case T_bridged_mask: c.m_Type = BRIDGED_MASK_CONSTRAINT; break;
594 default:
595 expected( wxT( "assertion, clearance, hole_clearance, edge_clearance, physical_clearance, "
596 "physical_hole_clearance, courtyard_clearance, silk_clearance, hole_size, "
597 "hole_to_hole, track_width, track_angle, track_segment_length, annular_width, "
598 "disallow, zone_connection, thermal_relief_gap, thermal_spoke_width, "
599 "min_resolved_spokes, microvia_stack_depth, hole_aspect_ratio, solder_mask_expansion, "
600 "solder_mask_sliver, "
601 "solder_paste_abs_margin, solder_paste_rel_margin, length, line_width, net_chain_length, "
602 "skew, via_count, via_dangling, via_diameter, diff_pair_gap, "
603 "diff_pair_uncoupled or bridged_mask" ) );
604 return;
605 }
606
607 if( aRule->FindConstraint( c.m_Type ) )
608 {
609 msg.Printf( _( "Rule already has a '%s' constraint." ), FromUTF8() );
610 reportError( msg );
611 }
612
613 bool ratio = c.m_Type == HOLE_ASPECT_RATIO_CONSTRAINT;
614
615 bool unitless = ratio
621
622 // Because previous versions allowed non-united values (in fact, required them), we have to interpret
623 // a track angle constraint value with no units as degrees.
624 bool impliedDegrees = c.m_Type == TRACK_ANGLE_CONSTRAINT;
625
626 allowsTimeDomain = c.m_Type == LENGTH_CONSTRAINT
629 || c.m_Type == SKEW_CONSTRAINT;
630
631 if( c.m_Type == DISALLOW_CONSTRAINT )
632 {
633 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
634 {
635 if( (int) token == DSN_STRING )
636 token = GetCurStrAsToken();
637
638 switch( token )
639 {
640 case T_track: c.m_DisallowFlags |= DRC_DISALLOW_TRACKS; break;
645 case T_through_via: c.m_DisallowFlags |= DRC_DISALLOW_THROUGH_VIAS; break;
646 case T_blind_via: c.m_DisallowFlags |= DRC_DISALLOW_BLIND_VIAS; break;
647 case T_buried_via: c.m_DisallowFlags |= DRC_DISALLOW_BURIED_VIAS; break;
648 case T_micro_via: c.m_DisallowFlags |= DRC_DISALLOW_MICRO_VIAS; break;
649 case T_pad: c.m_DisallowFlags |= DRC_DISALLOW_PADS; break;
650 case T_zone: c.m_DisallowFlags |= DRC_DISALLOW_ZONES; break;
651 case T_text: c.m_DisallowFlags |= DRC_DISALLOW_TEXTS; break;
652 case T_graphic: c.m_DisallowFlags |= DRC_DISALLOW_GRAPHICS; break;
653 case T_hole: c.m_DisallowFlags |= DRC_DISALLOW_HOLES; break;
654 case T_footprint: c.m_DisallowFlags |= DRC_DISALLOW_FOOTPRINTS; break;
655
656 case T_EOF:
657 reportError( _( "Missing ')'." ) );
658 return;
659
660 default:
661 expected( wxT( "track, via, through_via, blind_via, micro_via, buried_via, pad, zone, text, "
662 "graphic, hole, or footprint." ) );
663 return;
664 }
665 }
666
667 if( (int) CurTok() != DSN_RIGHT )
668 reportError( _( "Missing ')'." ) );
669
670 aRule->AddConstraint( c );
671 return;
672 }
674 {
675 // (constraint return_path (layer "B.Cu") (net "GND"))
676 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
677 {
678 if( token == T_LEFT )
679 token = NextTok();
680
681 switch( token )
682 {
683 case T_layer:
684 NeedSYMBOLorNUMBER();
685 c.m_ReferenceLayer = FromUTF8();
686 NeedRIGHT();
687 break;
688
689 case T_net:
690 NeedSYMBOLorNUMBER();
691 c.m_ReferenceNet = FromUTF8();
692 NeedRIGHT();
693 break;
694
695 case T_EOF:
696 reportError( _( "Missing ')'." ) );
697 return;
698
699 default:
700 // Unknown sub-option; skip its subtree.
701 while( NextTok() != T_RIGHT )
702 ;
703 break;
704 }
705 }
706
707 aRule->AddConstraint( c );
708 return;
709 }
710 else if( c.m_Type == ZONE_CONNECTION_CONSTRAINT )
711 {
712 token = NextTok();
713
714 if( (int) token == DSN_STRING )
715 token = GetCurStrAsToken();
716
717 switch( token )
718 {
719 case T_solid: c.m_ZoneConnection = ZONE_CONNECTION::FULL; break;
720 case T_thermal_reliefs: c.m_ZoneConnection = ZONE_CONNECTION::THERMAL; break;
721 case T_none: c.m_ZoneConnection = ZONE_CONNECTION::NONE; break;
722
723 case T_EOF:
724 reportError( _( "Missing ')'." ) );
725 return;
726
727 default:
728 expected( wxT( "solid, thermal_reliefs or none." ) );
729 return;
730 }
731
732 if( (int) NextTok() != DSN_RIGHT )
733 reportError( _( "Missing ')'." ) );
734
735 aRule->AddConstraint( c );
736 return;
737 }
739 {
740 // We don't use a min/max/opt structure here because it would give a strong implication
741 // that you could specify the optimal number of spokes. We don't want to open that door
742 // because the spoke generator is highly optimized around being able to "cheat" off of a
743 // cartesian coordinate system.
744
745 token = NextTok();
746
747 if( (int) token == DSN_NUMBER )
748 {
749 value = (int) strtol( CurText(), nullptr, 10 );
750 c.m_Value.SetMin( value );
751
752 if( (int) NextTok() != DSN_RIGHT )
753 reportError( _( "Missing ')'." ) );
754 }
755 else
756 {
757 expected( _( "number" ) ); // translate "number"; it is not a token
758 }
759
760 aRule->AddConstraint( c );
761 return;
762 }
763 else if( c.m_Type == ASSERTION_CONSTRAINT )
764 {
765 token = NextTok();
766
767 if( (int) token == DSN_RIGHT )
768 reportError( _( "Missing assertion expression." ) );
769
770 if( IsSymbol( token ) )
771 {
772 c.m_Test = new DRC_RULE_CONDITION( FromUTF8() );
773 c.m_Test->Compile( m_reporter, CurLineNumber(), CurOffset() );
774
775 if( c.m_Test->RequiresPairItems() )
776 reportError( _( "Item 'B' is not available in assertion expressions." ) );
777
778 if( (int) NextTok() != DSN_RIGHT )
779 reportError( _( "Missing ')'." ) );
780 }
781 else
782 {
783 expected( _( "quoted expression" ) ); // translate "quoted expression"; it is not a token
784 }
785
786 aRule->AddConstraint( c );
787 return;
788 }
789
790 for( token = NextTok(); token != T_RIGHT && token != T_EOF; token = NextTok() )
791 {
792 if( token != T_LEFT )
793 reportError( _( "Missing '('." ) );
794
795 token = NextTok();
796
797 switch( token )
798 {
799 case T_within_diff_pairs:
800 if( c.m_Type == SKEW_CONSTRAINT )
802 else
803 reportError( _( "within_diff_pairs option invalid for constraint type." ) );
804
805 if( (int) NextTok() != DSN_RIGHT )
806 reportError( _( "Missing ')'." ) );
807
808 break;
809
810 case T_min:
811 {
812 size_t offset = CurOffset() + GetTokenString( token ).length();
813 wxString expr = parseExpression();
814
815 if( expr.IsEmpty() )
816 {
817 reportError( _( "Missing min value." ) );
818 break;
819 }
820
821 parseValueWithUnits( (int) offset, expr, value, units, unitless, ratio, impliedDegrees );
822 validateAndSetValueWithUnits( value, units,
823 [&c]( const int aValue )
824 {
825 c.m_Value.SetMin( aValue );
826 } );
827
828 break;
829 }
830
831 case T_max:
832 {
833 size_t offset = CurOffset() + GetTokenString( token ).length();
834 wxString expr = parseExpression();
835
836 if( expr.IsEmpty() )
837 {
838 reportError( _( "Missing max value." ) );
839 break;
840 }
841
842 parseValueWithUnits( (int) offset, expr, value, units, unitless, ratio, impliedDegrees );
843 validateAndSetValueWithUnits( value, units,
844 [&c]( const int aValue )
845 {
846 c.m_Value.SetMax( aValue );
847 } );
848
849 break;
850 }
851
852 case T_opt:
853 {
854 size_t offset = CurOffset() + GetTokenString( token ).length();
855 wxString expr = parseExpression();
856
857 if( expr.IsEmpty() )
858 {
859 reportError( _( "Missing opt value." ) );
860 break;
861 }
862
863 parseValueWithUnits( (int) offset, expr, value, units, unitless, ratio, impliedDegrees );
864 validateAndSetValueWithUnits( value, units,
865 [&c]( const int aValue )
866 {
867 c.m_Value.SetOpt( aValue );
868 } );
869
870 break;
871 }
872
873 case T_EOF:
874 reportError( _( "Incomplete statement." ) );
875 return;
876
877 default:
878 expected( wxT( "min, max, opt, or within_diff_pairs" ) );
879 }
880 }
881
882 aRule->AddConstraint( c );
883}
884
885
886void DRC_RULES_PARSER::parseValueWithUnits( int aOffset, const wxString& aExpr, int& aResult, EDA_UNITS& aUnits,
887 bool aUnitless, bool aRatio, bool aImpliedDegrees )
888{
889 aResult = 0.0;
890 aUnits = EDA_UNITS::UNSCALED;
891
892 auto errorHandler =
893 [&]( const wxString& message, int offset )
894 {
895 wxString rest;
896 wxString first = message.BeforeFirst( '|', &rest );
897
898 if( m_reporter )
899 {
900 wxString msg = wxString::Format( _( "ERROR: <a href='%d:%d'>%s</a>%s" ),
901 CurLineNumber(),
902 aOffset + offset,
903 first,
904 rest );
905
906 m_reporter->Report( msg, RPT_SEVERITY_ERROR );
907 }
908 else
909 {
910 wxString msg = wxString::Format( _( "ERROR: %s%s" ), first, rest );
911
912 THROW_PARSE_ERROR( msg, CurSource(), CurLine(), CurLineNumber(),
913 CurOffset() + aOffset );
914 }
915 };
916
918 : (LIBEVAL::UNIT_RESOLVER*) new PCBEXPR_UNIT_RESOLVER( aImpliedDegrees ) );
919 evaluator.SetErrorCallback( errorHandler );
920
921 if( evaluator.Evaluate( aExpr ) )
922 {
923 // A ratio is fractional, so keep three decimals rather than rounding to a whole number.
924 aResult = aRatio ? KiROUND( evaluator.ResultAsDouble() * 1000.0 ) : evaluator.Result();
925 aUnits = evaluator.Units();
926 }
927}
928
929
931{
932 LSET retVal;
933 int token = NextTok();
934
935 if( (int) token == DSN_RIGHT )
936 {
937 reportError( _( "Missing layer name or type." ) );
938 return LSET::AllCuMask();
939 }
940 else if( token == T_outer )
941 {
942 *aSource = GetTokenString( token );
943 retVal = LSET::ExternalCuMask();
944 }
945 else if( token == T_inner )
946 {
947 *aSource = GetTokenString( token );
948 retVal = LSET::InternalCuMask();
949 }
950 else
951 {
952 wxString layerName = FromUTF8();
953 wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
954
955 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
956 {
957 wxPGChoiceEntry& entry = layerMap[ii];
958
959 if( entry.GetText().Matches( layerName ) )
960 {
961 *aSource = layerName;
962 retVal.set( ToLAYER_ID( entry.GetValue() ) );
963 }
964 }
965
966 if( !retVal.any() )
967 {
969 reportError( wxString::Format( _( "Unrecognized layer '%s'." ), layerName ) );
970
971 retVal.set( Rescue );
972 }
973 }
974
975 if( (int) NextTok() != DSN_RIGHT )
976 reportError( _( "Missing ')'." ) );
977
978 return retVal;
979}
980
981
983{
985
986 T token = NextTok();
987
988 if( (int) token == DSN_RIGHT || token == T_EOF )
989 {
990 reportError( _( "Missing severity name." ) );
992 }
993
994 switch( token )
995 {
996 case T_ignore: retVal = RPT_SEVERITY_IGNORE; break;
997 case T_warning: retVal = RPT_SEVERITY_WARNING; break;
998 case T_error: retVal = RPT_SEVERITY_ERROR; break;
999 case T_exclusion: retVal = RPT_SEVERITY_EXCLUSION; break;
1000
1001 default:
1002 expected( wxT( "ignore, warning, error, or exclusion" ) );
1003 }
1004
1005 if( (int) NextTok() != DSN_RIGHT )
1006 reportError( _( "Missing ')'." ) );
1007
1008 return retVal;
1009}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
BASE_SET & set(size_t pos)
Definition base_set.h:126
DRC_RULE_CONDITION * m_Test
Definition drc_rule.h:209
int m_DisallowFlags
Definition drc_rule.h:207
void SetOption(OPTIONS option)
Definition drc_rule.h:189
ZONE_CONNECTION m_ZoneConnection
Definition drc_rule.h:208
MINOPTMAX< int > m_Value
Definition drc_rule.h:206
wxString m_ReferenceLayer
Definition drc_rule.h:215
DRC_CONSTRAINT_T m_Type
Definition drc_rule.h:205
bool IsUnary() const
Definition drc_rule.cpp:88
void ClearOption(OPTIONS option)
Definition drc_rule.h:191
wxString m_ReferenceNet
Definition drc_rule.h:220
void parseValueWithUnits(int aOffset, const wxString &aExpr, int &aResult, EDA_UNITS &aUnits, bool aUnitless=false, bool aRatio=false, bool aImpliedDegrees=false)
std::shared_ptr< COMPONENT_CLASS_ASSIGNMENT_RULE > parseComponentClassAssignment()
void reportDeprecation(const wxString &oldToken, const wxString &newToken)
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
REPORTER * m_reporter
void reportError(const wxString &aMessage, int aOffset=0)
void ParseComponentClassAssignmentRules(std::vector< std::shared_ptr< COMPONENT_CLASS_ASSIGNMENT_RULE > > &aRules, REPORTER *aReporter)
void expected(const wxString &expectedTokens)
bool checkUnresolvedTextVariable()
std::shared_ptr< DRC_RULE > parseDRC_RULE()
LSET parseLayer(wxString *aSource)
void parseConstraint(DRC_RULE *aRule)
void reportErrorAt(const wxString &aMessage, int aLine, int aOffset, const char *aSourceLine)
DRC_RULES_PARSER(const wxString &aSource, const wxString &aSourceDescr)
bool Compile(REPORTER *aReporter, int aSourceLine=0, int aSourceOffset=0)
void AddConstraint(DRC_CONSTRAINT &aConstraint)
Definition drc_rule.cpp:69
std::optional< DRC_CONSTRAINT > FindConstraint(DRC_CONSTRAINT_T aType)
Definition drc_rule.cpp:76
static ENUM_MAP< T > & Instance()
Definition property.h:811
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
static const LSET & ExternalCuMask()
Return a mask holding the Front and Bottom layers.
Definition lset.cpp:630
static const LSET & AllLayersMask()
Definition lset.cpp:637
static const LSET & InternalCuMask()
Return a complete set of internal copper layers which is all Cu layers except F_Cu and B_Cu.
Definition lset.cpp:573
void SetMin(T v)
Definition minoptmax.h:41
bool HasMax() const
Definition minoptmax.h:38
void SetOpt(T v)
Definition minoptmax.h:43
bool HasMin() const
Definition minoptmax.h:37
void SetMax(T v)
Definition minoptmax.h:42
bool HasOpt() const
Definition minoptmax.h:39
double ResultAsDouble() const
Unrounded result, for constraints whose value is a ratio rather than a count or a length.
bool Evaluate(const wxString &aExpr)
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
EDA_UNITS Units() const
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
static EDA_DATA_TYPE GetTypeFromUnits(const EDA_UNITS aUnits)
Gets the inferred type from the given units.
@ DRC_DISALLOW_PADS
@ DRC_DISALLOW_BURIED_VIAS
@ DRC_DISALLOW_BLIND_VIAS
@ DRC_DISALLOW_TEXTS
@ DRC_DISALLOW_ZONES
@ DRC_DISALLOW_HOLES
@ DRC_DISALLOW_GRAPHICS
@ DRC_DISALLOW_THROUGH_VIAS
@ DRC_DISALLOW_FOOTPRINTS
@ DRC_DISALLOW_TRACKS
@ DRC_DISALLOW_MICRO_VIAS
@ ANNULAR_WIDTH_CONSTRAINT
@ BRIDGED_MASK_CONSTRAINT
@ COURTYARD_CLEARANCE_CONSTRAINT
@ VIA_DIAMETER_CONSTRAINT
@ ZONE_CONNECTION_CONSTRAINT
@ DIFF_PAIR_GAP_CONSTRAINT
@ NET_CHAIN_LENGTH_CONSTRAINT
@ VIA_DANGLING_CONSTRAINT
@ SOLDER_MASK_SLIVER_CONSTRAINT
@ NET_CHAIN_STUB_LENGTH_CONSTRAINT
@ DISALLOW_CONSTRAINT
@ TRACK_WIDTH_CONSTRAINT
@ SILK_CLEARANCE_CONSTRAINT
@ EDGE_CLEARANCE_CONSTRAINT
@ MIN_RESOLVED_SPOKES_CONSTRAINT
@ TRACK_SEGMENT_LENGTH_CONSTRAINT
@ HOLE_ASPECT_RATIO_CONSTRAINT
@ LENGTH_CONSTRAINT
@ VIA_COUNT_CONSTRAINT
@ NET_CHAIN_RETURN_PATH_CONSTRAINT
@ PHYSICAL_HOLE_CLEARANCE_CONSTRAINT
@ CLEARANCE_CONSTRAINT
@ THERMAL_SPOKE_WIDTH_CONSTRAINT
@ CONNECTION_WIDTH_CONSTRAINT
@ THERMAL_RELIEF_GAP_CONSTRAINT
@ MAX_UNCOUPLED_CONSTRAINT
@ ASSERTION_CONSTRAINT
@ SKEW_CONSTRAINT
@ LINE_WIDTH_CONSTRAINT
@ MICROVIA_STACK_DEPTH_CONSTRAINT
@ HOLE_CLEARANCE_CONSTRAINT
@ SOLDER_PASTE_ABS_MARGIN_CONSTRAINT
@ SOLDER_MASK_EXPANSION_CONSTRAINT
@ TRACK_ANGLE_CONSTRAINT
@ HOLE_SIZE_CONSTRAINT
@ TEXT_HEIGHT_CONSTRAINT
@ CREEPAGE_CONSTRAINT
@ PHYSICAL_CLEARANCE_CONSTRAINT
@ SOLDER_PASTE_REL_MARGIN_CONSTRAINT
@ HOLE_TO_HOLE_CONSTRAINT
bool IsComponentClassSelector(const wxString &aToken)
${Class:X} inside a DRC rule is a component-class selector consumed by testFootprintSelector(),...
Definition drc_rule.cpp:32
#define DRC_RULE_FILE_VERSION
@ DSN_RIGHT
Definition dsnlexer.h:62
@ DSN_NUMBER
Definition dsnlexer.h:61
@ DSN_STRING
Definition dsnlexer.h:64
static std::string ToStdString(const wxString &aStr)
#define _(s)
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:45
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
std::string message
unitsType
@ Rescue
Definition layer_ids.h:117
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
@ RPT_SEVERITY_INFO
VECTOR3I expected(15, 30, 45)
VECTOR2I end
@ THERMAL
Use thermal relief for pads.
Definition zones.h:46
@ NONE
Pads are not covered.
Definition zones.h:45
@ FULL
pads are covered by copper
Definition zones.h:47