82df2c67bd027eb22d8f37de943766c0920970c9
[oota-llvm.git] / include / llvm / Support / Regex.h
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 <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:
30     enum {
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     };
45
46     /// Compiles the given regular expression \p Regex.
47     Regex(StringRef Regex, unsigned Flags = NoFlags);
48     ~Regex();
49
50     /// isValid - returns the error encountered during regex compilation, or
51     /// matching, if any.
52     bool isValid(std::string &Error);
53
54     /// getNumMatches - In a valid regex, return the number of parenthesized
55     /// matches it contains.  The number filled in by match will include this
56     /// many entries plus one for the whole regex (as element 0).
57     unsigned getNumMatches() const;
58
59     /// matches - Match the regex against a given \p String.
60     ///
61     /// \param Matches - If given, on a successful match this will be filled in
62     /// with references to the matched group expressions (inside \p String),
63     /// the first group is always the entire pattern.
64     ///
65     /// This returns true on a successful match.
66     bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = 0);
67
68     /// sub - Return the result of replacing the first match of the regex in
69     /// \p String with the \p Repl string. Backreferences like "\0" in the
70     /// replacement string are replaced with the appropriate match substring.
71     ///
72     /// Note that the replacement string has backslash escaping performed on
73     /// it. Invalid backreferences are ignored (replaced by empty strings).
74     ///
75     /// \param Error If non-null, any errors in the substitution (invalid
76     /// backreferences, trailing backslashes) will be recorded as a non-empty
77     /// string.
78     std::string sub(StringRef Repl, StringRef String, std::string *Error = 0);
79
80   private:
81     struct llvm_regex *preg;
82     int error;
83   };
84 }
85
86 #endif // LLVM_SUPPORT_REGEX_H