Regex.h 4.5 KB

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