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 int currentPos = m_textCtrl->GetCurrentPos();
142 int startPos = 0;
143
144 enum class EXPR_CONTEXT_T : int
145 {
146 NONE,
147 STRING,
148 SEXPR_OPEN,
149 SEXPR_TOKEN,
150 };
151
152 std::stack<wxString> sexprs;
153 wxString partial;
154 EXPR_CONTEXT_T context = EXPR_CONTEXT_T::NONE;
155
156 for( int i = startPos; i < currentPos; ++i )
157 {
158 wxChar c = m_textCtrl->GetCharAt( i );
159
160 if( c == '\\' )
161 {
162 i++; // skip escaped char
163 }
164 else if( context == EXPR_CONTEXT_T::STRING )
165 {
166 if( c == '"' )
167 context = EXPR_CONTEXT_T::NONE;
168 }
169 else if( c == '"' )
170 {
171 partial = wxEmptyString;
172 context = EXPR_CONTEXT_T::STRING;
173 }
174 else if( c == '(' )
175 {
176 if( context == EXPR_CONTEXT_T::SEXPR_OPEN && !partial.IsEmpty() )
177 {
178 m_textCtrl->AutoCompCancel();
179 sexprs.push( partial );
180 }
181
182 partial = wxEmptyString;
183 context = EXPR_CONTEXT_T::SEXPR_OPEN;
184 }
185 else if( c == ')' )
186 {
187 if( !sexprs.empty() )
188 sexprs.pop();
189
190 context = EXPR_CONTEXT_T::NONE;
191 }
192 else if( c == ' ' )
193 {
194 if( context == EXPR_CONTEXT_T::SEXPR_OPEN && !partial.IsEmpty() )
195 {
196 m_textCtrl->AutoCompCancel();
197 sexprs.push( partial );
198 context = EXPR_CONTEXT_T::SEXPR_TOKEN;
199 partial = wxEmptyString;
200 continue;
201 }
202
203 context = EXPR_CONTEXT_T::NONE;
204 }
205 else
206 {
207 partial += c;
208 }
209 }
210
211 wxString tokens;
212
213 if( context == EXPR_CONTEXT_T::SEXPR_OPEN )
214 {
215 if( sexprs.empty() )
216 {
217 tokens = wxT( "condition|constraint|layer|severity" );
218 }
219 else if( sexprs.top() == wxT( "rule" ) )
220 {
221 // Inside (rule ...) - suggest sub-expressions
222 tokens = wxT( "condition|constraint|layer|severity" );
223 }
224 else if( sexprs.top() == wxT( "constraint" ) )
225 {
226 // Inside (constraint ...) - suggest constraint parameters
227 tokens = wxT( "min|max|opt" );
228 }
229 }
230 else if( context == EXPR_CONTEXT_T::SEXPR_TOKEN )
231 {
232 if( !sexprs.empty() && sexprs.top() == wxT( "constraint" ) )
233 {
234 // After (constraint - suggest constraint types
235 tokens = wxT( "annular_width|"
236 "assertion|"
237 "clearance|"
238 "connection_width|"
239 "courtyard_clearance|"
240 "creepage|"
241 "diff_pair_gap|"
242 "diff_pair_uncoupled|"
243 "disallow|"
244 "edge_clearance|"
245 "hole_clearance|"
246 "hole_size|"
247 "hole_to_hole|"
248 "length|"
249 "line_width|"
250 "max_error|"
251 "min_resolved_spokes|"
252 "physical_clearance|"
253 "physical_hole_clearance|"
254 "silk_clearance|"
255 "skew|"
256 "text_height|"
257 "thermal_relief_gap|"
258 "thermal_spoke_width|"
259 "track_angle|"
260 "track_segment_length|"
261 "track_width|"
262 "via_count|"
263 "via_diameter|"
264 "zone_connection" );
265 }
266 else if( !sexprs.empty() && sexprs.top() == wxT( "layer" ) )
267 {
268 // After (layer - suggest layer keywords
269 tokens = wxT( "inner|outer" );
270 }
271 else if( !sexprs.empty() && sexprs.top() == wxT( "severity" ) )
272 {
273 // After (severity - suggest severity levels
274 tokens = wxT( "error|warning|ignore|exclusion" );
275 }
276 }
277
278 if( !tokens.IsEmpty() )
279 m_scintillaTricks->DoAutocomplete( partial, wxSplit( tokens, '|' ) );
280}
281
282
283void DRC_RE_CUSTOM_RULE_PANEL::onCheckSyntax( wxCommandEvent& aEvent )
284{
285 // Close any existing tip window
286 if( m_tipWindow )
287 {
288 m_tipWindow->Close();
289 m_tipWindow = nullptr;
290 }
291
292 wxString body = m_textCtrl->GetText();
293 wxString ruleName = m_constraintData ? m_constraintData->GetRuleName() : wxString( wxS( "test" ) );
294 ruleName.Replace( wxS( "\"" ), wxS( "\\\"" ) );
295 wxString rulesText = wxString::Format( wxS( "(version 2)\n(rule \"%s\"\n%s)" ), ruleName, body );
296
297 if( body.Trim().IsEmpty() )
298 {
299#if wxCHECK_VERSION( 3, 3, 2 )
300 m_tipWindow = wxTipWindow::New( this, _( "No rule text to check." ) );
301#else
302 m_tipWindow = new wxTipWindow( this, _( "No rule text to check." ) );
303 m_tipWindow->SetTipWindowPtr( &m_tipWindow );
304#endif
305 return;
306 }
307
309
310 try
311 {
312 std::vector<std::shared_ptr<DRC_RULE>> dummyRules;
313 DRC_RULES_PARSER parser( rulesText, _( "Custom rule" ) );
314
315 parser.Parse( dummyRules, &reporter );
316 }
317 catch( PARSE_ERROR& pe )
318 {
319 reporter.Report( wxString::Format( _( "ERROR at line %d, column %d: %s" ),
320 pe.lineNumber,
321 pe.byteIndex,
322 pe.ParseProblem() ),
324 }
325
326 wxString message;
327
328 if( reporter.HasMessageOfSeverity( RPT_SEVERITY_ERROR ) )
329 {
330 message = reporter.GetMessages();
331 }
332 else if( reporter.HasMessage() )
333 {
334 message = _( "Syntax check passed with warnings:\n" ) + reporter.GetMessages();
335 }
336 else
337 {
338 message = _( "Syntax OK" );
339 }
340
341#if wxCHECK_VERSION( 3, 3, 2 )
342 m_tipWindow = wxTipWindow::New( this, message );
343#else
344 m_tipWindow = new wxTipWindow( this, message );
345 m_tipWindow->SetTipWindowPtr( &m_tipWindow );
346#endif
347}
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