Regex.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===-- Regex.h - Regular Expression matcher implementation -*- C++ -*-----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a POSIX regular expression matcher. Both Basic and
  11. // Extended POSIX regular expressions (ERE) are supported. EREs were extended
  12. // to support backreferences in matches.
  13. // This implementation also supports matching strings with embedded NUL chars.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_SUPPORT_REGEX_H
  17. #define LLVM_SUPPORT_REGEX_H
  18. #include <string>
  19. struct llvm_regex;
  20. namespace llvm {
  21. class StringRef;
  22. template<typename T> class SmallVectorImpl;
  23. class Regex {
  24. public:
  25. enum {
  26. NoFlags=0,
  27. /// Compile for matching that ignores upper/lower case distinctions.
  28. IgnoreCase=1,
  29. /// Compile for newline-sensitive matching. With this flag '[^' bracket
  30. /// expressions and '.' never match newline. A ^ anchor matches the
  31. /// null string after any newline in the string in addition to its normal
  32. /// function, and the $ anchor matches the null string before any
  33. /// newline in the string in addition to its normal function.
  34. Newline=2,
  35. /// By default, the POSIX extended regular expression (ERE) syntax is
  36. /// assumed. Pass this flag to turn on basic regular expressions (BRE)
  37. /// instead.
  38. BasicRegex=4
  39. };
  40. /// Compiles the given regular expression \p Regex.
  41. Regex(StringRef Regex, unsigned Flags = NoFlags);
  42. Regex(const Regex &) = delete;
  43. Regex &operator=(Regex regex) {
  44. std::swap(preg, regex.preg);
  45. std::swap(error, regex.error);
  46. return *this;
  47. }
  48. Regex(Regex &&regex) {
  49. preg = regex.preg;
  50. error = regex.error;
  51. regex.preg = nullptr;
  52. }
  53. ~Regex();
  54. /// isValid - returns the error encountered during regex compilation, or
  55. /// matching, if any.
  56. bool isValid(std::string &Error);
  57. /// getNumMatches - In a valid regex, return the number of parenthesized
  58. /// matches it contains. The number filled in by match will include this
  59. /// many entries plus one for the whole regex (as element 0).
  60. unsigned getNumMatches() const;
  61. /// matches - Match the regex against a given \p String.
  62. ///
  63. /// \param Matches - If given, on a successful match this will be filled in
  64. /// with references to the matched group expressions (inside \p String),
  65. /// the first group is always the entire pattern.
  66. ///
  67. /// This returns true on a successful match.
  68. bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = nullptr);
  69. /// sub - Return the result of replacing the first match of the regex in
  70. /// \p String with the \p Repl string. Backreferences like "\0" in the
  71. /// replacement string are replaced with the appropriate match substring.
  72. ///
  73. /// Note that the replacement string has backslash escaping performed on
  74. /// it. Invalid backreferences are ignored (replaced by empty strings).
  75. ///
  76. /// \param Error If non-null, any errors in the substitution (invalid
  77. /// backreferences, trailing backslashes) will be recorded as a non-empty
  78. /// string.
  79. std::string sub(StringRef Repl, StringRef String,
  80. std::string *Error = nullptr);
  81. /// \brief If this function returns true, ^Str$ is an extended regular
  82. /// expression that matches Str and only Str.
  83. static bool isLiteralERE(StringRef Str);
  84. /// \brief Turn String into a regex by escaping its special characters.
  85. static std::string escape(StringRef String);
  86. private:
  87. struct llvm_regex *preg;
  88. int error;
  89. };
  90. }
  91. #endif // LLVM_SUPPORT_REGEX_H