KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_custom_rule_panel.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) 2024 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
21
22#include <stack>
23
24#include <drc/drc_rule.h>
25#include <drc/drc_rule_parser.h>
26#include <ki_exception.h>
27#include <reporter.h>
28#include <scintilla_tricks.h>
29#include <wx/button.h>
30#include <wx/sizer.h>
31#include <wx/stc/stc.h>
32#include <wx/tipwin.h>
33
35 wxWindow* aParent, std::shared_ptr<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA> aConstraintData ) :
36 wxPanel( aParent ),
37 m_constraintData( aConstraintData ),
38 m_textCtrl( nullptr ),
39 m_checkSyntaxBtn( nullptr )
40{
41 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
42
43 m_textCtrl = new wxStyledTextCtrl( this, wxID_ANY );
44 m_textCtrl->SetToolTip( _( "Complete rule text in the DRC rule language" ) );
45 sizer->Add( m_textCtrl, 1, wxEXPAND | wxALL, 5 );
46
47 m_checkSyntaxBtn = new wxButton( this, wxID_ANY, _( "Check Syntax" ) );
48 sizer->Add( m_checkSyntaxBtn, 0, wxALIGN_RIGHT | wxRIGHT | wxBOTTOM, 5 );
49
50 SetSizer( sizer );
51
52 m_scintillaTricks = std::make_unique<SCINTILLA_TRICKS>(
53 m_textCtrl, wxT( "()" ), false,
54 // onAcceptFn
55 []( wxKeyEvent& aEvent )
56 {
57 aEvent.Skip();
58 },
59 // onCharFn
60 [this]( wxStyledTextEvent& aEvent )
61 {
62 onScintillaCharAdded( aEvent );
63 } );
64
65 m_textCtrl->AutoCompSetSeparator( '|' );
66
68}
69
70
74
75
77{
79 {
80 wxString text = m_constraintData->GetRuleText();
81
82 if( text.IsEmpty() )
83 text = wxS( "(constraint clearance (min 0.2mm))" );
84
85 m_textCtrl->SetValue( text );
86 }
87
88 return true;
89}
90
91
93{
95 m_constraintData->SetRuleText( m_textCtrl->GetValue() );
96
97 return true;
98}
99
100
101bool DRC_RE_CUSTOM_RULE_PANEL::ValidateInputs( int* aErrorCount, wxString* aValidationMessage )
102{
103 (void) aErrorCount;
104 (void) aValidationMessage;
105 return true;
106}
107
108
110{
111 wxString body;
112
113 if( m_textCtrl )
114 body = m_textCtrl->GetValue();
115 else if( m_constraintData )
116 body = m_constraintData->GetRuleText();
117
118 if( body.Trim().IsEmpty() )
119 return wxEmptyString;
120
121 return DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA::FormatRule( aContext, body );
122}
123
124
125void DRC_RE_CUSTOM_RULE_PANEL::UpdateRuleName( const wxString& aName )
126{
127 if( m_constraintData )
128 m_constraintData->SetRuleName( aName );
129}
130
131
132void DRC_RE_CUSTOM_RULE_PANEL::onScintillaCharAdded( wxStyledTextEvent& aEvent )
133{
134 if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKey() == ' ' )
135 {
136 // Short-cut for do-auto-complete
137 }
138
139 m_textCtrl->SearchAnchor();
140
141 wxString rules = m_textCtrl->GetText();
142 int currentPos = m_textCtrl->GetCurrentPos();
143 int startPos = 0;
144
145 enum class EXPR_CONTEXT_T : int
146 {
147 NONE,
148 STRING,
149 SEXPR_OPEN,
150 SEXPR_TOKEN,
151 };
152
153 std::stack<wxString> sexprs;
154 wxString partial;
155 EXPR_CONTEXT_T context = EXPR_CONTEXT_T::NONE;
156
157 for( int i = startPos; i < currentPos; ++i )
158 {
159 wxChar c = m_textCtrl->GetCharAt( i );
160
161 if( c == '\\' )
162 {
163 i++; // skip escaped char
164 }
165 else if( context == EXPR_CONTEXT_T::STRING )
166 {
167 if( c == '"' )
168 context = EXPR_CONTEXT_T::NONE;
169 }
170 else if( c == '"' )
171 {
172 partial = wxEmptyString;
173 context = EXPR_CONTEXT_T::STRING;
174 }
175 else if( c == '(' )
176 {
177 if( context == EXPR_CONTEXT_T::SEXPR_OPEN && !partial.IsEmpty() )
178 {
179 m_textCtrl->AutoCompCancel();
180 sexprs.push( partial );
181 }
182
183 partial = wxEmptyString;
184 context = EXPR_CONTEXT_T::SEXPR_OPEN;
185 }
186 else if( c == ')' )
187 {
188 if( !sexprs.empty() )
189 sexprs.pop();
190
191 context = EXPR_CONTEXT_T::NONE;
192 }
193 else if( c == ' ' )
194 {
195 if( context == EXPR_CONTEXT_T::SEXPR_OPEN && !partial.IsEmpty() )
196 {
197 m_textCtrl->AutoCompCancel();
198 sexprs.push( partial );
199 context = EXPR_CONTEXT_T::SEXPR_TOKEN;
200 partial = wxEmptyString;
201 continue;
202 }
203
204 context = EXPR_CONTEXT_T::NONE;
205 }
206 else
207 {
208 partial += c;
209 }
210 }
211
212 wxString tokens;
213
214 if( context == EXPR_CONTEXT_T::SEXPR_OPEN )
215 {
216 if( sexprs.empty() )
217 {
218 tokens = wxT( "condition|constraint|layer|severity" );
219 }
220 else if( sexprs.top() == wxT( "rule" ) )
221 {
222 // Inside (rule ...) - suggest sub-expressions
223 tokens = wxT( "condition|constraint|layer|severity" );
224 }
225 else if( sexprs.top() == wxT( "constraint" ) )
226 {
227 // Inside (constraint ...) - suggest constraint parameters
228 tokens = wxT( "min|max|opt" );
229 }
230 }
231 else if( context == EXPR_CONTEXT_T::SEXPR_TOKEN )
232 {
233 if( !sexprs.empty() && sexprs.top() == wxT( "constraint" ) )
234 {
235 // After (constraint - suggest constraint types
236 tokens = wxT( "annular_width|"
237 "assertion|"
238 "clearance|"
239 "connection_width|"
240 "courtyard_clearance|"
241 "creepage|"
242 "diff_pair_gap|"
243 "diff_pair_uncoupled|"
244 "disallow|"
245 "edge_clearance|"
246 "hole_clearance|"
247 "hole_size|"
248 "hole_to_hole|"
249 "length|"
250 "line_width|"
251 "max_error|"
252 "min_resolved_spokes|"
253 "physical_clearance|"
254 "physical_hole_clearance|"
255 "silk_clearance|"
256 "skew|"
257 "text_height|"
258 "thermal_relief_gap|"
259 "thermal_spoke_width|"
260 "track_angle|"
261 "track_segment_length|"
262 "track_width|"
263 "via_count|"
264 "via_diameter|"
265 "zone_connection" );
266 }
267 else if( !sexprs.empty() && sexprs.top() == wxT( "layer" ) )
268 {
269 // After (layer - suggest layer keywords
270 tokens = wxT( "inner|outer" );
271 }
272 else if( !sexprs.empty() && sexprs.top() == wxT( "severity" ) )
273 {
274 // After (severity - suggest severity levels
275 tokens = wxT( "error|warning|ignore|exclusion" );
276 }
277 }
278
279 if( !tokens.IsEmpty() )
280 m_scintillaTricks->DoAutocomplete( partial, wxSplit( tokens, '|' ) );
281}
282
283
284void DRC_RE_CUSTOM_RULE_PANEL::onCheckSyntax( wxCommandEvent& aEvent )
285{
286 // Close any existing tip window
287 if( m_tipWindow )
288 {
289 m_tipWindow->Close();
290 m_tipWindow = nullptr;
291 }
292
293 wxString body = m_textCtrl->GetText();
294 wxString ruleName = m_constraintData ? m_constraintData->GetRuleName() : wxString( wxS( "test" ) );
295 ruleName.Replace( wxS( "\"" ), wxS( "\\\"" ) );
296 wxString rulesText = wxString::Format( wxS( "(version 2)\n(rule \"%s\"\n%s)" ), ruleName, body );
297
298 if( body.Trim().IsEmpty() )
299 {
300#if wxCHECK_VERSION( 3, 3, 2 )
301 m_tipWindow = wxTipWindow::New( this, _( "No rule text to check." ) );
302#else
303 m_tipWindow = new wxTipWindow( this, _( "No rule text to check." ) );
304 m_tipWindow->SetTipWindowPtr( &m_tipWindow );
305#endif
306 return;
307 }
308
310
311 try
312 {
313 std::vector<std::shared_ptr<DRC_RULE>> dummyRules;
314 DRC_RULES_PARSER parser( rulesText, _( "Custom rule" ) );
315
316 parser.Parse( dummyRules, &reporter );
317 }
318 catch( PARSE_ERROR& pe )
319 {
320 reporter.Report( wxString::Format( _( "ERROR at line %d, column %d: %s" ),
321 pe.lineNumber,
322 pe.byteIndex,
323 pe.ParseProblem() ),
325 }
326
327 wxString message;
328
329 if( reporter.HasMessageOfSeverity( RPT_SEVERITY_ERROR ) )
330 {
331 message = reporter.GetMessages();
332 }
333 else if( reporter.HasMessage() )
334 {
335 message = _( "Syntax check passed with warnings:\n" ) + reporter.GetMessages();
336 }
337 else
338 {
339 message = _( "Syntax OK" );
340 }
341
342#if wxCHECK_VERSION( 3, 3, 2 )
343 m_tipWindow = wxTipWindow::New( this, message );
344#else
345 m_tipWindow = new wxTipWindow( this, message );
346 m_tipWindow->SetTipWindowPtr( &m_tipWindow );
347#endif
348}
static wxString FormatRule(const RULE_GENERATION_CONTEXT &aContext, const wxString &aBody)
Wraps a rule body as it is edited, without the indent the file gives it, in a rule.
void UpdateRuleName(const wxString &aName)
void onScintillaCharAdded(wxStyledTextEvent &aEvent)
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
std::shared_ptr< DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA > m_constraintData
std::unique_ptr< SCINTILLA_TRICKS > m_scintillaTricks
DRC_RE_CUSTOM_RULE_PANEL(wxWindow *aParent, std::shared_ptr< DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA > aConstraintData)
void onCheckSyntax(wxCommandEvent &aEvent)
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
A wrapper for reporting to a wxString object.
Definition reporter.h:242
#define _(s)
@ NONE
Definition eda_fill.h:42
std::string message
@ RPT_SEVERITY_ERROR
A filename or source description, a problem input line, a line number, a byte offset,...
int lineNumber
at which line number, 1 based index.
const wxString ParseProblem()
int byteIndex
at which byte offset within the line, 1 based index
IbisParser parser & reporter