Home | History | Annotate | Download | only in Support
      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.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_SUPPORT_REGEX_H
     15 #define LLVM_SUPPORT_REGEX_H
     16 
     17 #include <string>
     18 
     19 struct llvm_regex;
     20 
     21 namespace llvm {
     22   class StringRef;
     23   template<typename T> class SmallVectorImpl;
     24 
     25   class Regex {
     26   public:
     27     enum {
     28       NoFlags=0,
     29       /// Compile for matching that ignores upper/lower case distinctions.
     30       IgnoreCase=1,
     31       /// Compile for newline-sensitive matching. With this flag '[^' bracket
     32       /// expressions and '.' never match newline. A ^ anchor matches the
     33       /// null string after any newline in the string in addition to its normal
     34       /// function, and the $ anchor matches the null string before any
     35       /// newline in the string in addition to its normal function.
     36       Newline=2
     37     };
     38 
     39     /// Compiles the given POSIX Extended Regular Expression \arg Regex.
     40     /// This implementation supports regexes and matching strings with embedded
     41     /// NUL characters.
     42     Regex(StringRef Regex, unsigned Flags = NoFlags);
     43     ~Regex();
     44 
     45     /// isValid - returns the error encountered during regex compilation, or
     46     /// matching, if any.
     47     bool isValid(std::string &Error);
     48 
     49     /// getNumMatches - In a valid regex, return the number of parenthesized
     50     /// matches it contains.  The number filled in by match will include this
     51     /// many entries plus one for the whole regex (as element 0).
     52     unsigned getNumMatches() const;
     53 
     54     /// matches - Match the regex against a given \arg String.
     55     ///
     56     /// \param Matches - If given, on a successful match this will be filled in
     57     /// with references to the matched group expressions (inside \arg String),
     58     /// the first group is always the entire pattern.
     59     ///
     60     /// This returns true on a successful match.
     61     bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = 0);
     62 
     63     /// sub - Return the result of replacing the first match of the regex in
     64     /// \arg String with the \arg Repl string. Backreferences like "\0" in the
     65     /// replacement string are replaced with the appropriate match substring.
     66     ///
     67     /// Note that the replacement string has backslash escaping performed on
     68     /// it. Invalid backreferences are ignored (replaced by empty strings).
     69     ///
     70     /// \param Error If non-null, any errors in the substitution (invalid
     71     /// backreferences, trailing backslashes) will be recorded as a non-empty
     72     /// string.
     73     std::string sub(StringRef Repl, StringRef String, std::string *Error = 0);
     74 
     75   private:
     76     struct llvm_regex *preg;
     77     int error;
     78   };
     79 }
     80 
     81 #endif // LLVM_SUPPORT_REGEX_H
     82