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