xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/Regex.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- Regex.h - Regular Expression matcher implementation -*- C++ -*-----===//
2 //
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
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 
19 #include "llvm/ADT/BitmaskEnum.h"
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 RegexFlags : unsigned {
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       LLVM_MARK_AS_BITMASK_ENUM(BasicRegex)
47     };
48 
49     LLVM_ABI Regex();
50     /// Compiles the given regular expression \p Regex.
51     ///
52     /// \param Regex - referenced string is no longer needed after this
53     /// constructor does finish.  Only its compiled form is kept stored.
54     LLVM_ABI Regex(StringRef Regex, RegexFlags Flags = NoFlags);
55     LLVM_ABI Regex(StringRef Regex, unsigned Flags);
56     Regex(const Regex &) = delete;
57     Regex &operator=(Regex regex) {
58       std::swap(preg, regex.preg);
59       std::swap(error, regex.error);
60       return *this;
61     }
62     LLVM_ABI Regex(Regex &&regex);
63     LLVM_ABI ~Regex();
64 
65     /// isValid - returns the error encountered during regex compilation, if
66     /// any.
67     LLVM_ABI bool isValid(std::string &Error) const;
isValid()68     bool isValid() const { return !error; }
69 
70     /// getNumMatches - In a valid regex, return the number of parenthesized
71     /// matches it contains.  The number filled in by match will include this
72     /// many entries plus one for the whole regex (as element 0).
73     LLVM_ABI unsigned getNumMatches() const;
74 
75     /// matches - Match the regex against a given \p String.
76     ///
77     /// \param Matches - If given, on a successful match this will be filled in
78     /// with references to the matched group expressions (inside \p String),
79     /// the first group is always the entire pattern.
80     ///
81     /// \param Error - If non-null, any errors in the matching will be recorded
82     /// as a non-empty string. If there is no error, it will be an empty string.
83     ///
84     /// This returns true on a successful match.
85     LLVM_ABI bool match(StringRef String,
86                         SmallVectorImpl<StringRef> *Matches = nullptr,
87                         std::string *Error = nullptr) const;
88 
89     /// sub - Return the result of replacing the first match of the regex in
90     /// \p String with the \p Repl string. Backreferences like "\0" and "\g<1>"
91     /// in the replacement string are replaced with the appropriate match
92     /// substring.
93     ///
94     /// Note that the replacement string has backslash escaping performed on
95     /// it. Invalid backreferences are ignored (replaced by empty strings).
96     ///
97     /// \param Error If non-null, any errors in the substitution (invalid
98     /// backreferences, trailing backslashes) will be recorded as a non-empty
99     /// string. If there is no error, it will be an empty string.
100     LLVM_ABI std::string sub(StringRef Repl, StringRef String,
101                              std::string *Error = nullptr) const;
102 
103     /// If this function returns true, ^Str$ is an extended regular
104     /// expression that matches Str and only Str.
105     LLVM_ABI static bool isLiteralERE(StringRef Str);
106 
107     /// Turn String into a regex by escaping its special characters.
108     LLVM_ABI static std::string escape(StringRef String);
109 
110   private:
111     struct llvm_regex *preg;
112     int error;
113   };
114 }
115 
116 #endif // LLVM_SUPPORT_REGEX_H
117