KiCad PCB EDA Suite
eda_pattern_match.h
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) 2015-2022 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
29#ifndef EDA_PATTERN_MATCH_H
30#define EDA_PATTERN_MATCH_H
31
32#include <vector>
33#include <map>
34#include <memory>
35#include <wx/string.h>
36#include <wx/regex.h>
37
38static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
39
40/*
41 * Interface for a pattern matcher, for which there are several implementations
42 */
44{
45public:
47 {
49 int length = 0;
50
51 bool valid() const
52 {
54 }
55
56 explicit operator bool() const
57 {
58 return valid();
59 }
60 };
61
62 virtual ~EDA_PATTERN_MATCH() {}
63
68 virtual bool SetPattern( const wxString& aPattern ) = 0;
69
73 virtual wxString const& GetPattern() const = 0;
74
80 virtual FIND_RESULT Find( const wxString& aCandidate ) const = 0;
81};
82
83
84/*
85 * Match simple substring
86 */
88{
89public:
90 virtual bool SetPattern( const wxString& aPattern ) override;
91 virtual wxString const& GetPattern() const override;
92 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
93
94protected:
95 wxString m_pattern;
96};
97
98
99/*
100 * Match regular expression
101 */
103{
104public:
105 virtual bool SetPattern( const wxString& aPattern ) override;
106 virtual wxString const& GetPattern() const override;
107 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
108
109protected:
110 wxString m_pattern;
111 wxRegEx m_regex;
112};
113
114
116{
117public:
118 virtual bool SetPattern( const wxString& aPattern ) override;
119};
120
121
123{
124public:
125 virtual bool SetPattern( const wxString& aPattern ) override;
126 virtual wxString const& GetPattern() const override;
127 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
128
129protected:
131};
132
133
135{
136public:
137 virtual bool SetPattern( const wxString& aPattern ) override;
138};
139
140
155{
156public:
157 virtual bool SetPattern( const wxString& aPattern ) override;
158 virtual wxString const& GetPattern() const override;
159 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
160 int FindOne( const wxString& aCandidate ) const;
161
162protected:
163
164 enum RELATION { LT, LE, EQ, GE, GT, ANY };
165
166 wxString m_pattern;
167 wxString m_key;
169 double m_value;
170
171 static wxRegEx m_regex_description;
172 static wxRegEx m_regex_search;
173 static const std::map<wxString, double> m_units;
174};
175
176
178{
182
183
185{
186public:
187 EDA_COMBINED_MATCHER( const wxString& aPattern, COMBINED_MATCHER_CONTEXT aContext );
188
189 /*
190 * Look in all existing matchers, return the earliest match of any of
191 * the existing.
192 *
193 * @param aTerm term to look for
194 * @param aMatchersTriggered out: number of matcher that found the term
195 * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
196 *
197 * @return true if any matchers found the term
198 */
199 bool Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition );
200
201 const wxString& GetPattern() const;
202
203private:
204 // Add matcher if it can compile the pattern.
205 void AddMatcher( const wxString& aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
206
207 std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
208 wxString m_pattern;
209};
210
211#endif // EDA_PATTERN_MATCH_H
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
void AddMatcher(const wxString &aPattern, std::unique_ptr< EDA_PATTERN_MATCH > aMatcher)
EDA_COMBINED_MATCHER(const wxString &aPattern, COMBINED_MATCHER_CONTEXT aContext)
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
const wxString & GetPattern() const
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
int FindOne(const wxString &aCandidate) const
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
static const std::map< wxString, double > m_units
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual bool SetPattern(const wxString &aPattern)=0
Set the pattern against which candidates will be matched.
virtual wxString const & GetPattern() const =0
Return the pattern passed to SetPattern().
virtual FIND_RESULT Find(const wxString &aCandidate) const =0
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual ~EDA_PATTERN_MATCH()
static const int EDA_PATTERN_NOT_FOUND
COMBINED_MATCHER_CONTEXT
@ CTX_NETCLASS
@ CTX_LIBITEM