28#include <drc_rules_lexer.h>
37wxString formatLayerClause(
const wxString& aLayerSource )
39 if( aLayerSource.IsEmpty() )
42 if( aLayerSource == DRC_RULES_LEXER::TokenName( DRCRULE_T::T_outer )
43 || aLayerSource == DRC_RULES_LEXER::TokenName( DRCRULE_T::T_inner ) )
44 return wxString::Format( wxS(
"(layer %s)" ), aLayerSource );
50std::pair<wxString, wxString> splitLeadingTrivia(
const wxString& aTrivia )
52 size_t blank = aTrivia.rfind( wxS(
"\n\n" ) );
54 if( blank == wxString::npos )
55 return { wxEmptyString, aTrivia };
57 return { aTrivia.Left( blank + 2 ), aTrivia.Mid( blank + 2 ) };
65 if( predicate.IsEmpty() )
71 return predicate + wxS(
" && " ) + aEntry.
condition;
82 const std::vector<DRC_RE_LOADED_PANEL_ENTRY>& aEntries,
87 std::string
utf8 = std::string( content.mb_str( wxConvUTF8 ) );
100 result = wxS(
"(version 2)" );
105 using GroupKey = std::tuple<int, wxString, wxString, wxString>;
107 std::vector<std::pair<GroupKey, std::vector<const DRC_RE_LOADED_PANEL_ENTRY*>>>
109 std::map<GroupKey, size_t> groupIndex;
113 auto key = std::make_tuple( entry.ruleGroupId, entry.ruleName, savedCondition( entry ), entry.layerSource );
114 auto it = groupIndex.find( key );
116 if( it == groupIndex.end() )
118 groupIndex[key] = groupedEntries.size();
119 groupedEntries.push_back( { key, { &entry } } );
123 groupedEntries[it->second].second.push_back( &entry );
128 std::vector<wxString> floating( triviaCount );
129 std::vector<wxString> attached( triviaCount );
130 std::vector<bool> present( triviaCount,
false );
132 for(
size_t i = 0; i < triviaCount; ++i )
133 std::tie( floating[i], attached[i] ) = splitLeadingTrivia( aTrivia.
leadingTrivia[i] );
137 std::vector<std::pair<int, wxString>> rules;
139 size_t nextTrivia = 0;
141 auto emitUpTo = [&](
size_t aLast )
143 while( nextTrivia <= aLast )
145 result += floating[nextTrivia];
147 if( !present[nextTrivia] )
148 result += attached[nextTrivia];
154 for(
const auto& [key, entries] : groupedEntries )
159 bool anyEdited =
false;
160 wxString originalText;
164 if( entry->wasEdited )
167 if( originalText.IsEmpty() )
168 originalText = entry->originalRuleText;
171 if( !anyEdited && !originalText.IsEmpty()
172 &&
static_cast<int>( entries.size() ) == entries[0]->originalEntryCount )
174 ruleText = originalText;
176 else if( entries.size() == 1 )
187 if( ruleText.IsEmpty() )
190 rules.emplace_back( entries[0]->sourceRule, ruleText );
193 for(
const auto& [
source, ruleText] : rules )
195 if(
source >= 0 &&
static_cast<size_t>(
source ) < triviaCount )
199 std::vector<bool> attachedWritten( triviaCount,
false );
201 for(
const auto& [
source, ruleText] : rules )
203 bool known =
source >= 0 &&
static_cast<size_t>(
source ) < triviaCount;
205 if( known &&
static_cast<size_t>(
source ) >= nextTrivia )
211 if( known && !attachedWritten[
source] )
213 if( !attached[
source].StartsWith( wxS(
"\n" ) ) && !
result.EndsWith( wxS(
"\n" ) ) )
217 attachedWritten[
source] =
true;
223 if( triviaCount > 0 )
224 emitUpTo( triviaCount - 1 );
226 if( aTrivia.
header.IsEmpty() )
236 const BOARD* aBoard )
239 return wxEmptyString;
257 silkCond = wxS(
"L == 'F.Mask'" );
259 silkCond = wxS(
"L == 'B.Mask'" );
261 silkCond = wxS(
"L == 'F.Mask' || L == 'B.Mask'" );
273 silkCond = wxS(
"L == 'F.SilkS'" );
275 silkCond = wxS(
"L == 'B.SilkS'" );
277 silkCond = wxS(
"L == 'F.SilkS' || L == 'B.SilkS'" );
301 if( !severityClause.IsEmpty() && !ruleText.Contains(
"(severity" ) )
304 size_t lastParen = ruleText.rfind(
')' );
306 if( lastParen != wxString::npos )
308 ruleText = ruleText.Left( lastParen ) +
"\n\t" + severityClause +
")";
321 return wxEmptyString;
324 return wxString::Format( wxS(
"(layer %s)" ), DRC_RULES_LEXER::TokenName( DRCRULE_T::T_outer ) );
327 return wxString::Format( wxS(
"(layer %s)" ), DRC_RULES_LEXER::TokenName( DRCRULE_T::T_inner ) );
332 return wxString::Format( wxS(
"(layer \"%s\")" ),
LSET::Name( layer ) );
334 return wxEmptyString;
346 default:
return wxEmptyString;
352 const std::vector<const DRC_RE_LOADED_PANEL_ENTRY*>& aEntries,
353 const BOARD* aBoard )
355 if( aEntries.empty() )
356 return wxEmptyString;
369 for(
const auto* entry : aEntries )
371 if( entry->layerCondition.any() && aBoard )
373 if( !entry->layerSource.IsEmpty() )
374 ctx.
layerClause = formatLayerClause( entry->layerSource );
383 std::vector<wxString> allClauses;
385 for(
const auto* entry : aEntries )
387 if( entry->constraintData )
390 entryCtx.
ruleName = entry->ruleName;
392 entryCtx.
constraintCode = entry->constraintData->GetConstraintCode();
394 auto clauses = entry->constraintData->GetConstraintClauses( entryCtx );
396 for(
const wxString& clause : clauses )
398 if( !clause.IsEmpty() )
399 allClauses.push_back( clause );
409 for(
const wxString& line : wxSplit( ctx.
comment,
'\n',
'\0' ) )
411 if( !line.IsEmpty() )
412 rule << wxS(
"\t# " ) << line << wxS(
"\n" );
416 rule << wxS(
"\t" ) << ctx.
layerClause << wxS(
"\n" );
418 for(
const wxString& clause : allClauses )
419 rule << wxS(
"\t" ) << clause << wxS(
"\n" );
423 rule << wxS(
"\t(condition \"" )
428 for(
const auto* entry : aEntries )
Information pertinent to a Pcbnew printed circuit board.
wxString GetConstraintCode() const
static wxString formatRuleName(const wxString &aRuleName)
Format a rule name for use in S-expression output while preserving the exact name the user chose.
virtual wxString GetConditionPredicate() const
Returns a predicate this panel holds in its own fields rather than in the rule condition,...
virtual wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext)
wxString GetGeneratedRule() const
wxString generateRuleText(const DRC_RE_LOADED_PANEL_ENTRY &aEntry, const BOARD *aBoard)
Generate the rule text for a single panel entry.
wxString generateMergedRuleText(const std::vector< const DRC_RE_LOADED_PANEL_ENTRY * > &aEntries, const BOARD *aBoard)
Generate a merged rule text from multiple panel entries with the same name/condition.
bool SaveFile(const wxString &aPath, const std::vector< DRC_RE_LOADED_PANEL_ENTRY > &aEntries, const BOARD *aBoard=nullptr, const DRC_RE_FILE_TRIVIA &aTrivia={})
Save all panel entries to a file.
wxString GenerateRulesText(const std::vector< DRC_RE_LOADED_PANEL_ENTRY > &aEntries, const BOARD *aBoard=nullptr, const DRC_RE_FILE_TRIVIA &aTrivia={})
Generate rule text from panel entries.
wxString generateLayerClause(const LSET &aLayers)
Generate a layer clause from an LSET.
wxString generateSeverityClause(SEVERITY aSeverity)
Generate a severity clause.
LSET is a set of PCB_LAYER_IDs.
static const LSET & ExternalCuMask()
Return a mask holding the Front and Bottom layers.
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
static LSET AllCuMask(int aCuLayerCount)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
static const LSET & AllLayersMask()
static const LSET & InternalCuMask()
Return a complete set of internal copper layers which is all Cu layers except F_Cu and B_Cu.
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
wxString GetComment()
Get the comment associated with the rule.
std::string utf8(const wxString &aText)
@ SILK_TO_SOLDERMASK_CLEARANCE
PCB_LAYER_ID
A quick note on layer IDs:
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
The text of a rules file that lies outside every rule, kept so a save can reproduce it.
wxString header
Everything through the version expression, empty for a new file.
std::vector< wxString > leadingTrivia
Comments and blank lines before each rule, by sourceRule.
wxString trailer
Everything after the last rule.
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString layerSource
Original layer text: "inner", "outer", or layer name.
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > constraintData
DRC_RULE_EDITOR_CONSTRAINT_NAME panelType
wxString conditionExpression
wxString result
Test unit parsing edge cases and error handling.