You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2017 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. /**
  24. * @file eda_pattern_match.h
  25. * @brief Abstract pattern-matching tool and implementations.
  26. */
  27. #ifndef EDA_PATTERN_MATCH_H
  28. #define EDA_PATTERN_MATCH_H
  29. #include <vector>
  30. #include <map>
  31. #include <memory>
  32. #include <wx/wx.h>
  33. #include <wx/string.h>
  34. #include <wx/regex.h>
  35. static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
  36. /*
  37. * Interface for a pattern matcher, for which there are several implementations
  38. */
  39. class EDA_PATTERN_MATCH
  40. {
  41. public:
  42. virtual ~EDA_PATTERN_MATCH() {}
  43. /**
  44. * Set the pattern against which candidates will be matched. If the pattern
  45. * can not be processed, returns false.
  46. */
  47. virtual bool SetPattern( const wxString& aPattern ) = 0;
  48. /**
  49. * Return the pattern passed to SetPattern().
  50. */
  51. virtual wxString const& GetPattern() const = 0;
  52. /**
  53. * Return the location of a match iff a given candidate string matches the set pattern.
  54. * Otherwise, return EDA_PATTERN_NOT_FOUND.
  55. */
  56. virtual int Find( const wxString& aCandidate ) const = 0;
  57. };
  58. /*
  59. * Match simple substring
  60. */
  61. class EDA_PATTERN_MATCH_SUBSTR : public EDA_PATTERN_MATCH
  62. {
  63. public:
  64. virtual bool SetPattern( const wxString& aPattern ) override;
  65. virtual wxString const& GetPattern() const override;
  66. virtual int Find( const wxString& aCandidate ) const override;
  67. protected:
  68. wxString m_pattern;
  69. };
  70. /*
  71. * Match regular expression
  72. */
  73. class EDA_PATTERN_MATCH_REGEX : public EDA_PATTERN_MATCH
  74. {
  75. public:
  76. virtual bool SetPattern( const wxString& aPattern ) override;
  77. virtual wxString const& GetPattern() const override;
  78. virtual int Find( const wxString& aCandidate ) const override;
  79. protected:
  80. wxString m_pattern;
  81. wxRegEx m_regex;
  82. };
  83. class EDA_PATTERN_MATCH_WILDCARD : public EDA_PATTERN_MATCH_REGEX
  84. {
  85. public:
  86. virtual bool SetPattern( const wxString& aPattern ) override;
  87. virtual wxString const& GetPattern() const override;
  88. virtual int Find( const wxString& aCandidate ) const override;
  89. protected:
  90. wxString m_wildcard_pattern;
  91. };
  92. class EDA_PATTERN_MATCH_WILDCARD_EXPLICIT : public EDA_PATTERN_MATCH_WILDCARD
  93. {
  94. public:
  95. virtual bool SetPattern( const wxString& aPattern ) override;
  96. };
  97. /**
  98. * Relational match.
  99. *
  100. * Matches tokens of the format:
  101. *
  102. * key:value or key=value
  103. *
  104. * with search patterns of the format:
  105. *
  106. * key<value, key<=value, key=value, key>=value, key>value
  107. *
  108. * by parsing the value numerically and comparing.
  109. */
  110. class EDA_PATTERN_MATCH_RELATIONAL : public EDA_PATTERN_MATCH
  111. {
  112. public:
  113. virtual bool SetPattern( const wxString& aPattern ) override;
  114. virtual wxString const& GetPattern() const override;
  115. virtual int Find( const wxString& aCandidate ) const override;
  116. int FindOne( const wxString& aCandidate ) const;
  117. protected:
  118. enum RELATION { LT, LE, EQ, GE, GT, NONE };
  119. wxString m_pattern;
  120. wxString m_key;
  121. RELATION m_relation;
  122. double m_value;
  123. static wxRegEx m_regex_description;
  124. static wxRegEx m_regex_search;
  125. static const std::map<wxString, double> m_units;
  126. };
  127. class EDA_COMBINED_MATCHER
  128. {
  129. public:
  130. EDA_COMBINED_MATCHER( const wxString &aPattern );
  131. /*
  132. * Look in all existing matchers, return the earliest match of any of
  133. * the existing.
  134. *
  135. * @param aTerm term to look for
  136. * @param aMatchersTriggered out: number of matcher that found the term
  137. * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
  138. *
  139. * @return true if any matchers found the term
  140. */
  141. bool Find( const wxString &aTerm, int& aMatchersTriggered, int& aPosition );
  142. wxString const& GetPattern() const;
  143. private:
  144. // Add matcher if it can compile the pattern.
  145. void AddMatcher( const wxString &aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
  146. std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
  147. wxString m_pattern;
  148. };
  149. #endif // EDA_PATTERN_MATCH_H