20#ifndef DRC_RULES_DOWNGRADE_H
21#define DRC_RULES_DOWNGRADE_H
46 if( aTarget.
m_id == vocabulary.m_targetId )
62inline bool DrcVocabularyHas(
const char*
const* aWords,
size_t aCount,
const std::string& aWord )
64 return std::binary_search( aWords, aWords + aCount, aWord );
78 {
"line_width",
"text_thickness" },
90 if( aKind == rename.m_current )
91 return rename.m_legacy;
128inline bool TokenizeDrcRules(
const std::string& aText, std::vector<DRC_RULE_TOKEN>& aTokens, wxString& aError )
130 std::vector<size_t> lineStarts{ 0 };
132 for(
size_t i = 0; i < aText.size(); i++ )
134 if( aText[i] ==
'\n' )
135 lineStarts.push_back( i + 1 );
138 DSNLEXER lexer( aText, wxT(
"drc_rules_downgrade" ) );
151 aTokens.push_back( { type, lexer.
CurText(), offset } );
166inline bool SplitDrcRuleBlocks(
const std::vector<DRC_RULE_TOKEN>& aTokens, std::vector<DRC_RULE_BLOCK>& aBlocks,
171 for(
size_t i = 0; i < aTokens.size(); i++ )
177 aError =
_(
"Design rule comments must start on a separate line." );
188 aError =
_(
"Design rules contain text outside a rule." );
196 auto nextCode = [&aTokens](
size_t aFrom )
198 while( aFrom < aTokens.size() && aTokens[aFrom].m_type ==
DSN_COMMENT )
204 size_t head = nextCode( i + 1 );
206 if( head < aTokens.size() && aTokens[head].m_type ==
DSN_SYMBOL )
207 block.
m_head = aTokens[head].m_text;
209 size_t name = nextCode( head + 1 );
214 block.
m_name = wxString::FromUTF8( aTokens[
name].m_text.c_str() );
217 aBlocks.push_back( block );
230 aBlocks.back().m_end = token.
m_offset + 1;
231 aBlocks.back().m_last = i;
238 aError =
_(
"Unbalanced parentheses in design rules." );
250 std::vector<size_t>* aRenamed =
nullptr )
252 std::vector<std::string> nodes;
253 std::vector<std::string> words;
254 std::string expressions;
256 bool headPending =
false;
257 bool kindPending =
false;
258 bool namePending =
false;
261 auto addExpression = [&expressions](
const std::string& aExpression )
265 for(
size_t j = 0; j < aExpression.size(); j++ )
267 char c = aExpression[j];
271 if( c ==
'\\' && j + 1 < aExpression.size() )
273 else if( c == quote )
276 else if( c ==
'\'' || c ==
'"' )
299 nodes.emplace_back();
314 if( token.
m_text.find(
"${" ) != std::string::npos )
320 std::string folded = token.
m_text;
322 for(
char& c : folded )
323 c =
static_cast<char>( std::tolower(
static_cast<unsigned char>( c ) ) );
326 if( folded.find(
"${class:" ) != std::string::npos )
337 nodes.back() = token.
m_text;
338 words.push_back( token.
m_text );
339 kindPending = token.
m_text ==
"constraint";
340 namePending = token.
m_text ==
"rule";
344 const std::string& node = nodes.back();
350 else if( kindPending )
357 if( aRenamed && kind != token.
m_text )
358 aRenamed->push_back( i );
360 words.push_back( kind );
362 if( token.
m_text ==
"assertion" || token.
m_text ==
"disallow" || token.
m_text ==
"zone_connection" )
363 nodes.back() = token.
m_text;
365 else if( node ==
"condition" || node ==
"assertion" )
367 addExpression( token.
m_text );
369 else if( node ==
"min" || node ==
"max" || node ==
"opt" )
371 values += token.
m_text +
' ';
373 else if( node ==
"layer" )
377 else if( node ==
"disallow" || node ==
"zone_connection" )
380 words.push_back( token.
m_text );
384 words.push_back( token.
m_text );
388 for(
const std::string& word : words )
391 if( word.find(
'.' ) != std::string::npos )
398 auto isWord = [](
char c )
400 return std::isalnum(
static_cast<unsigned char>( c ) ) || c ==
'_';
403 std::string folded = expressions;
405 for(
char& c : folded )
406 c =
static_cast<char>( std::tolower(
static_cast<unsigned char>( c ) ) );
410 for(
size_t i = 0; i < folded.size(); i++ )
412 if( !std::isalpha(
static_cast<unsigned char>( folded[i] ) ) && folded[i] !=
'_' )
415 if( i > 0 && isWord( folded[i - 1] ) )
420 while(
end < folded.size() && isWord( folded[
end] ) )
423 const std::string word = folded.substr( i,
end - i );
427 while(
next < folded.size() && std::isspace(
static_cast<unsigned char>( folded[
next] ) ) )
432 while( prev > 0 && std::isspace(
static_cast<unsigned char>( folded[prev - 1] ) ) )
435 if(
next < folded.size() && folded[
next] ==
'(' )
446 else if( prev > 0 && folded[prev - 1] ==
'.' )
449 if( word ==
"parent" &&
next < folded.size() && folded[
next] ==
'.' )
461 const std::string numeric = values +
'\n' + expressions;
463 for(
size_t i = 0; i < numeric.size(); i++ )
465 if( !std::isalpha(
static_cast<unsigned char>( numeric[i] ) ) )
468 if( i > 0 && ( std::isalpha(
static_cast<unsigned char>( numeric[i - 1] ) ) || numeric[i - 1] ==
'_' ) )
473 while( before > 0 && std::isspace(
static_cast<unsigned char>( numeric[before - 1] ) ) )
478 while(
end < numeric.size() && isWord( numeric[
end] ) )
481 if( before > 0 && std::isdigit(
static_cast<unsigned char>( numeric[before - 1] ) )
497 std::string out =
"# ";
499 for(
char c : aBlock )
516 std::string
text = aRulesText.ToStdString( wxConvUTF8 );
524 result.m_error = wxString::Format(
_(
"No design rule vocabulary is recorded for %s." ), aTarget.
m_name );
528 std::vector<DRC_RULE_TOKEN> tokens;
529 std::vector<DRC_RULE_BLOCK> blocks;
534 const wxString header = wxT(
"(version 1)" );
535 const wxString eol =
text.find(
"\r\n" ) != std::string::npos ? wxT(
"\r\n" ) : wxT(
"\n" );
538 bool headerWritten =
false;
539 bool commentedOut =
false;
541 auto endsOnBlankLine = [](
const wxString& aText )
543 int lineStart = aText.Find(
'\n',
true );
544 return aText.Mid( lineStart == wxNOT_FOUND ? 0 : lineStart + 1 ).Trim().IsEmpty();
550 wxString prefix = wxString::FromUTF8(
text.substr( consumed, block.m_start - consumed ).c_str() );
551 std::string body =
text.substr( block.m_start, block.m_end - block.m_start );
552 consumed = block.m_end;
555 if( block.m_head ==
"version" )
562 headerWritten =
true;
568 result.m_text += header + eol;
569 headerWritten =
true;
572 std::vector<size_t> renamed;
578 for(
auto it = renamed.rbegin(); it != renamed.rend(); ++it )
581 size_t at = body.find( token.
m_text, token.
m_offset - block.m_start );
591 if( ( unsupported || commentedOut ) && !endsOnBlankLine(
result.m_text ) )
596 result.m_dropped.push_back( block.m_name );
597 result.m_text += wxT(
"# Not supported by " ) + aTarget.
m_name + wxT(
":" ) + eol;
602 result.m_text += wxString::FromUTF8( body.c_str() );
605 commentedOut = unsupported;
609 result.m_text += header + eol;
612 result.m_text += wxString::FromUTF8(
text.substr( consumed ).c_str() );
614 if( !
result.m_text.EndsWith( wxT(
"\n" ) ) )
Implement a lexical analyzer for the SPECCTRA DSN file format.
const char * CurText() const
Return a pointer to the current token's text.
int NextTok()
Return the next token found in the input file or DSN_EOF when reaching the end of file.
bool SetCommentsAreTokens(bool val)
Change the handling of comments.
int CurOffset() const
Return the byte offset within the current line, using a 1 based index.
int CurLineNumber() const
Return the current line number within my LINE_READER.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString Problem() const
what was the problem?
std::string CommentOutDrcBlock(const std::string &aBlock)
Prefix every line, so the target reads the whole block as comments.
bool DrcVocabularyHas(const char *const *aWords, size_t aCount, const std::string &aWord)
The generated arrays are sorted, so membership is a binary search.
constexpr const char * DRC_RULE_SEMANTIC_BREAKS[]
A function a release parses but answers differently, which the vocabulary cannot say.
constexpr DRC_CONSTRAINT_RENAME DRC_CONSTRAINT_RENAMES[]
bool SplitDrcRuleBlocks(const std::vector< DRC_RULE_TOKEN > &aTokens, std::vector< DRC_RULE_BLOCK > &aBlocks, wxString &aError)
Group the tokens into top-level blocks.
std::string DrcConstraintKindForTarget(const std::string &aKind, const DRC_RULE_VOCABULARY &aVocabulary)
The name the target reads a constraint kind by.
const DRC_RULE_VOCABULARY * FindDrcRuleVocabulary(const DOWNGRADE_TARGET &aTarget)
The vocabulary a target release could parse, generated from its release tags by tools/generate_drc_ru...
bool DrcRuleUnsupportedByTarget(const std::vector< DRC_RULE_TOKEN > &aTokens, const DRC_RULE_BLOCK &aBlock, const DRC_RULE_VOCABULARY &aVocabulary, std::vector< size_t > *aRenamed=nullptr)
True if the rule uses something the target's parser or evaluator cannot handle.
bool TokenizeDrcRules(const std::string &aText, std::vector< DRC_RULE_TOKEN > &aTokens, wxString &aError)
Tokenize with the lexer the rule readers use, so comments, strings and symbols end exactly where they...
DRC_RULES_FILTER_RESULT FilterDrcRulesForTarget(const wxString &aRulesText, const DOWNGRADE_TARGET &aTarget)
Filter a .kicad_dru file for the target.
constexpr DRC_RULE_VOCABULARY DRC_RULE_VOCABULARIES[]
An older KiCad release we can export a project back to.
wxString m_id
Stable machine key for CLI and jobsets, e.g. "10.0".
wxString m_name
User-facing name, e.g. "KiCad 10.0".
A constraint kind renamed after a release. The release reads the same check by its old name.
wxString m_text
The filtered rules file text.
wxString m_error
Invalid source syntax, do not write m_text.
std::vector< wxString > m_dropped
Names of the rules the target cannot parse.
A top-level block of the rules file, as byte span and token range.
wxString m_name
The name after a "rule" head, for the report.
size_t m_start
Byte offset of the opening parenthesis.
size_t m_last
Index of the closing parenthesis token.
size_t m_first
Index of the opening parenthesis token.
size_t m_end
Byte offset just past the closing parenthesis.
std::string m_head
The word after the opening parenthesis, "rule" or "version".
std::string m_text
The token as the lexer reports it, strings without their quotes.
size_t m_offset
Byte offset of the token's first character in the source.
int m_type
A DSNLEXER token type.
Everything a rules file could name in one release.
const char *const * m_tokens
const char *const * m_properties
const char *const * m_units
const char *const * m_functions
wxString result
Test unit parsing edge cases and error handling.