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 17 #ifndef LLVM_SUPPORT_REGEX_H 18 #define LLVM_SUPPORT_REGEX_H 19 20 #include "llvm/Support/Compiler.h" 21 #include <string> 22 23 struct llvm_regex; 24 25 namespace llvm { 26 class StringRef; 27 template<typename T> class SmallVectorImpl; 28 29 class Regex { 30 public: 31 enum { 32 NoFlags=0, 33 /// Compile for matching that ignores upper/lower case distinctions. 34 IgnoreCase=1, 35 /// Compile for newline-sensitive matching. With this flag '[^' bracket 36 /// expressions and '.' never match newline. A ^ anchor matches the 37 /// null string after any newline in the string in addition to its normal 38 /// function, and the $ anchor matches the null string before any 39 /// newline in the string in addition to its normal function. 40 Newline=2, 41 /// By default, the POSIX extended regular expression (ERE) syntax is 42 /// assumed. Pass this flag to turn on basic regular expressions (BRE) 43 /// instead. 44 BasicRegex=4 45 }; 46 47 /// Compiles the given regular expression \p Regex. 48 Regex(StringRef Regex, unsigned Flags = NoFlags); 49 Regex(const Regex &) LLVM_DELETED_FUNCTION; 50 Regex &operator=(Regex regex) { 51 std::swap(preg, regex.preg); 52 std::swap(error, regex.error); 53 return *this; 54 } Regex(Regex && regex)55 Regex(Regex &®ex) { 56 preg = regex.preg; 57 error = regex.error; 58 regex.preg = nullptr; 59 } 60 ~Regex(); 61 62 /// isValid - returns the error encountered during regex compilation, or 63 /// matching, if any. 64 bool isValid(std::string &Error); 65 66 /// getNumMatches - In a valid regex, return the number of parenthesized 67 /// matches it contains. The number filled in by match will include this 68 /// many entries plus one for the whole regex (as element 0). 69 unsigned getNumMatches() const; 70 71 /// matches - Match the regex against a given \p String. 72 /// 73 /// \param Matches - If given, on a successful match this will be filled in 74 /// with references to the matched group expressions (inside \p String), 75 /// the first group is always the entire pattern. 76 /// 77 /// This returns true on a successful match. 78 bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = nullptr); 79 80 /// sub - Return the result of replacing the first match of the regex in 81 /// \p String with the \p Repl string. Backreferences like "\0" in the 82 /// replacement string are replaced with the appropriate match substring. 83 /// 84 /// Note that the replacement string has backslash escaping performed on 85 /// it. Invalid backreferences are ignored (replaced by empty strings). 86 /// 87 /// \param Error If non-null, any errors in the substitution (invalid 88 /// backreferences, trailing backslashes) will be recorded as a non-empty 89 /// string. 90 std::string sub(StringRef Repl, StringRef String, 91 std::string *Error = nullptr); 92 93 /// \brief 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 97 /// \brief Turn String into a regex by escaping its special characters. 98 static std::string escape(StringRef String); 99 100 private: 101 struct llvm_regex *preg; 102 int error; 103 }; 104 } 105 106 #endif // LLVM_SUPPORT_REGEX_H 107