blob: 4c4229eb57b10b508355ce77bc3ce816f2ba6cf3 [file] [log] [blame]
Torok Edwince0c81e2009-08-30 08:24:09 +00001//===-- 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#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16
17struct llvm_regex;
18namespace llvm {
19 class Regex {
20 public:
21 enum {
22 /// Compile with support for subgroup matches, this is just to make
23 /// constructs like Regex("...", 0) more readable as Regex("...", Sub).
24 Sub=0,
25 /// Compile for matching that ignores upper/lower case distinctions.
26 IgnoreCase=1,
27 /// Compile for matching that need only report success or failure,
28 /// not what was matched.
29 NoSub=2,
30 /// Compile for newline-sensitive matching. With this flag '[^' bracket
31 /// expressions and '.' never match newline. A ^ anchor matches the
32 /// null string after any newline in the string in addition to its normal
33 /// function, and the $ anchor matches the null string before any
34 /// newline in the string in addition to its normal function.
35 Newline=4
36 };
37
38 /// Compiles the given POSIX Extended Regular Expression \arg Regex.
39 /// This implementation supports regexes and matching strings with embedded
40 /// NUL characters.
41 Regex(const StringRef &Regex, unsigned Flags=NoSub);
42 ~Regex();
43
44 /// isValid - returns the error encountered during regex compilation, or
45 /// matching, if any.
46 bool isValid(std::string &Error);
47
48 /// matches - Match the regex against a given \arg String.
49 ///
50 /// \param Matches - If given, on a succesful match this will be filled in
51 /// with references to the matched group expressions (inside \arg String),
52 /// the first group is always the entire pattern.
53 /// By default the regex is compiled with NoSub, which disables support for
54 /// Matches.
55 /// For this feature to be enabled you must construct the regex using
56 /// Regex("...", Regex::Sub) constructor.
Torok Edwince0c81e2009-08-30 08:24:09 +000057 bool match(const StringRef &String, SmallVectorImpl<StringRef> *Matches=0);
58 private:
59 struct llvm_regex *preg;
60 int error;
61 bool sub;
62 };
63}