2016-01-12 16:33:33 +00:00
|
|
|
/*
|
2015-12-20 20:52:39 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2023-01-29 16:01:03 +00:00
|
|
|
* Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-20 20:52:39 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file eda_pattern_match.h
|
|
|
|
* @brief Abstract pattern-matching tool and implementations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EDA_PATTERN_MATCH_H
|
|
|
|
#define EDA_PATTERN_MATCH_H
|
|
|
|
|
2023-12-28 03:07:21 +00:00
|
|
|
#include <kicommon.h>
|
2015-12-20 20:52:39 +00:00
|
|
|
#include <vector>
|
2017-03-27 20:25:20 +00:00
|
|
|
#include <map>
|
2017-03-06 14:50:48 +00:00
|
|
|
#include <memory>
|
2015-12-20 20:52:39 +00:00
|
|
|
#include <wx/string.h>
|
|
|
|
#include <wx/regex.h>
|
|
|
|
|
|
|
|
static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
|
|
|
|
|
2023-05-01 20:26:29 +00:00
|
|
|
/*
|
|
|
|
* A structure for storing weighted search terms.
|
|
|
|
*
|
|
|
|
* NOTE: an exact match is scored at 8 * Score while a match at the start of the text is scored
|
|
|
|
* at 2 * Score.
|
|
|
|
*/
|
2023-12-28 03:07:21 +00:00
|
|
|
struct KICOMMON_API SEARCH_TERM
|
2023-05-01 20:26:29 +00:00
|
|
|
{
|
|
|
|
SEARCH_TERM( const wxString& aText, int aScore ) :
|
|
|
|
Text( aText ),
|
|
|
|
Score( aScore ),
|
|
|
|
Normalized( false )
|
|
|
|
{}
|
|
|
|
|
|
|
|
wxString Text;
|
|
|
|
int Score;
|
|
|
|
bool Normalized;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-12-20 20:52:39 +00:00
|
|
|
/*
|
|
|
|
* Interface for a pattern matcher, for which there are several implementations
|
|
|
|
*/
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH
|
2015-12-20 20:52:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-11-04 02:49:19 +00:00
|
|
|
struct FIND_RESULT
|
|
|
|
{
|
|
|
|
int start = EDA_PATTERN_NOT_FOUND;
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
bool valid() const
|
|
|
|
{
|
|
|
|
return start != EDA_PATTERN_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const
|
|
|
|
{
|
|
|
|
return valid();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-20 20:52:39 +00:00
|
|
|
virtual ~EDA_PATTERN_MATCH() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the pattern against which candidates will be matched. If the pattern
|
|
|
|
* can not be processed, returns false.
|
|
|
|
*/
|
|
|
|
virtual bool SetPattern( const wxString& aPattern ) = 0;
|
|
|
|
|
2017-03-23 00:59:25 +00:00
|
|
|
/**
|
|
|
|
* Return the pattern passed to SetPattern().
|
|
|
|
*/
|
|
|
|
virtual wxString const& GetPattern() const = 0;
|
|
|
|
|
2015-12-20 20:52:39 +00:00
|
|
|
/**
|
2020-11-04 02:49:19 +00:00
|
|
|
* Return the location and possibly length of a match iff a given candidate
|
|
|
|
* string matches the set pattern.
|
|
|
|
* Otherwise, return an invalid FIND_RESULT.
|
2015-12-20 20:52:39 +00:00
|
|
|
*/
|
2020-11-04 02:49:19 +00:00
|
|
|
virtual FIND_RESULT Find( const wxString& aCandidate ) const = 0;
|
2015-12-20 20:52:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match simple substring
|
|
|
|
*/
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH_SUBSTR : public EDA_PATTERN_MATCH
|
2015-12-20 20:52:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-12-28 03:07:21 +00:00
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
virtual bool SetPattern( const wxString& aPattern ) override;
|
2017-03-23 00:59:25 +00:00
|
|
|
virtual wxString const& GetPattern() const override;
|
2020-11-04 02:49:19 +00:00
|
|
|
virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
|
2015-12-20 20:52:39 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
wxString m_pattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match regular expression
|
|
|
|
*/
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH_REGEX : public EDA_PATTERN_MATCH
|
2015-12-20 20:52:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-12-28 03:07:21 +00:00
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
virtual bool SetPattern( const wxString& aPattern ) override;
|
2017-03-23 00:59:25 +00:00
|
|
|
virtual wxString const& GetPattern() const override;
|
2020-11-04 02:49:19 +00:00
|
|
|
virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
|
2015-12-20 20:52:39 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
wxString m_pattern;
|
|
|
|
wxRegEx m_regex;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH_REGEX_ANCHORED : public EDA_PATTERN_MATCH_REGEX
|
2022-08-12 23:51:19 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool SetPattern( const wxString& aPattern ) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH_WILDCARD : public EDA_PATTERN_MATCH_REGEX
|
2015-12-20 20:52:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-12-28 03:07:21 +00:00
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
virtual bool SetPattern( const wxString& aPattern ) override;
|
2017-03-23 00:59:25 +00:00
|
|
|
virtual wxString const& GetPattern() const override;
|
2020-11-04 02:49:19 +00:00
|
|
|
virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
|
2017-03-23 00:59:25 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
wxString m_wildcard_pattern;
|
2015-12-20 20:52:39 +00:00
|
|
|
};
|
|
|
|
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH_WILDCARD_ANCHORED : public EDA_PATTERN_MATCH_WILDCARD
|
2018-04-19 20:42:25 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool SetPattern( const wxString& aPattern ) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-27 20:25:20 +00:00
|
|
|
/**
|
|
|
|
* Relational match.
|
|
|
|
*
|
|
|
|
* Matches tokens of the format:
|
|
|
|
*
|
|
|
|
* key:value or key=value
|
|
|
|
*
|
|
|
|
* with search patterns of the format:
|
|
|
|
*
|
|
|
|
* key<value, key<=value, key=value, key>=value, key>value
|
|
|
|
*
|
|
|
|
* by parsing the value numerically and comparing.
|
|
|
|
*/
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_PATTERN_MATCH_RELATIONAL : public EDA_PATTERN_MATCH
|
2017-03-27 20:25:20 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool SetPattern( const wxString& aPattern ) override;
|
|
|
|
virtual wxString const& GetPattern() const override;
|
2020-11-04 02:49:19 +00:00
|
|
|
virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
|
2017-03-27 20:25:20 +00:00
|
|
|
int FindOne( const wxString& aCandidate ) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2021-12-02 11:40:31 +00:00
|
|
|
enum RELATION { LT, LE, EQ, GE, GT, ANY };
|
2017-03-27 20:25:20 +00:00
|
|
|
|
|
|
|
wxString m_pattern;
|
|
|
|
wxString m_key;
|
|
|
|
RELATION m_relation;
|
|
|
|
double m_value;
|
|
|
|
|
|
|
|
static wxRegEx m_regex_description;
|
|
|
|
static wxRegEx m_regex_search;
|
|
|
|
static const std::map<wxString, double> m_units;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-08-12 23:51:19 +00:00
|
|
|
enum COMBINED_MATCHER_CONTEXT
|
|
|
|
{
|
|
|
|
CTX_LIBITEM,
|
2023-01-29 16:01:03 +00:00
|
|
|
CTX_NETCLASS,
|
2023-06-10 13:49:37 +00:00
|
|
|
CTX_SIGNAL,
|
|
|
|
CTX_SEARCH
|
2022-08-12 23:51:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-12-28 03:07:21 +00:00
|
|
|
class KICOMMON_API EDA_COMBINED_MATCHER
|
2017-03-06 14:50:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-08-12 23:51:19 +00:00
|
|
|
EDA_COMBINED_MATCHER( const wxString& aPattern, COMBINED_MATCHER_CONTEXT aContext );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2023-12-28 03:07:21 +00:00
|
|
|
/*
|
|
|
|
* Deleted copy or else we have to implement copy constructors for all EDA_PATTERN_MATCH classes
|
|
|
|
* due to this class' m_matchers member being copied
|
|
|
|
*/
|
|
|
|
EDA_COMBINED_MATCHER( EDA_COMBINED_MATCHER const& ) = delete;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deleted copy or else we have to implement copy constructors for all EDA_PATTERN_MATCH classes
|
|
|
|
* due to this class' m_matchers member being copied
|
|
|
|
*/
|
|
|
|
EDA_COMBINED_MATCHER& operator=( EDA_COMBINED_MATCHER const& ) = delete;
|
|
|
|
|
2017-03-06 14:50:48 +00:00
|
|
|
/*
|
|
|
|
* Look in all existing matchers, return the earliest match of any of
|
|
|
|
* the existing.
|
|
|
|
*
|
|
|
|
* @param aTerm term to look for
|
|
|
|
* @param aMatchersTriggered out: number of matcher that found the term
|
|
|
|
* @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
|
|
|
|
*
|
|
|
|
* @return true if any matchers found the term
|
|
|
|
*/
|
2023-04-30 11:32:19 +00:00
|
|
|
bool Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition );
|
|
|
|
|
2023-04-14 22:34:14 +00:00
|
|
|
bool Find( const wxString& aTerm );
|
|
|
|
|
|
|
|
bool StartsWith( const wxString& aTerm );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
const wxString& GetPattern() const;
|
2017-03-06 14:50:48 +00:00
|
|
|
|
2023-05-01 20:26:29 +00:00
|
|
|
int ScoreTerms( std::vector<SEARCH_TERM>& aWeightedTerms );
|
|
|
|
|
2017-03-06 14:50:48 +00:00
|
|
|
private:
|
|
|
|
// Add matcher if it can compile the pattern.
|
2021-07-27 12:22:27 +00:00
|
|
|
void AddMatcher( const wxString& aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
|
2017-03-06 14:50:48 +00:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
|
|
|
|
wxString m_pattern;
|
|
|
|
};
|
|
|
|
|
2015-12-20 20:52:39 +00:00
|
|
|
#endif // EDA_PATTERN_MATCH_H
|