]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/RegularExpression.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / include / lldb / Core / RegularExpression.h
1 //===-- RegularExpression.h -------------------------------------*- 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 #ifndef liblldb_DBRegex_h_
11 #define liblldb_DBRegex_h_
12 #if defined(__cplusplus)
13
14 #ifdef _WIN32
15 #include "../lib/Support/regex_impl.h"
16
17 typedef llvm_regmatch_t regmatch_t;
18 typedef llvm_regex_t regex_t;
19
20 inline int regcomp(llvm_regex_t * a, const char *b, int c)
21 {
22     return llvm_regcomp(a, b, c);
23 }
24
25 inline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d)
26 {
27     return llvm_regerror(a, b, c, d);
28 }
29
30 inline int regexec(const llvm_regex_t * a, const char * b, size_t c,
31     llvm_regmatch_t d [], int e)
32 {
33     return llvm_regexec(a, b, c, d, e);
34 }
35
36 inline void regfree(llvm_regex_t * a)
37 {
38     llvm_regfree(a);
39 }
40
41 #else
42 #include <regex.h>
43 #endif
44 #include <stdint.h>
45
46 #include <string>
47 #include <vector>
48
49 namespace llvm
50 {
51     class StringRef;
52 }
53
54 namespace lldb_private {
55
56 //----------------------------------------------------------------------
57 /// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h"
58 /// @brief A C++ wrapper class for regex.
59 ///
60 /// This regular expression class wraps the posix regex functions
61 /// \c regcomp(), \c regerror(), \c regexec(), and \c regfree() from
62 /// the header file in \c /usr/include/regex\.h.
63 //----------------------------------------------------------------------
64 class RegularExpression
65 {
66 public:
67     class Match
68     {
69     public:
70         Match (uint32_t max_matches) :
71             m_matches ()
72         {
73             if (max_matches > 0)
74                 m_matches.resize(max_matches + 1);
75         }
76
77         void
78         Clear()
79         {
80             const size_t num_matches = m_matches.size();
81             regmatch_t invalid_match = { -1, -1 };
82             for (size_t i=0; i<num_matches; ++i)
83                 m_matches[i] = invalid_match;
84         }
85
86         size_t
87         GetSize () const
88         {
89             return m_matches.size();
90         }
91         
92         regmatch_t *
93         GetData ()
94         {
95             if (m_matches.empty())
96                 return NULL;
97             return m_matches.data();
98         }
99         
100         bool
101         GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const;
102         
103         bool
104         GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const;
105         
106         bool
107         GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const;
108
109     protected:
110         
111         std::vector<regmatch_t> m_matches; ///< Where parenthesized subexpressions results are stored
112     };
113     //------------------------------------------------------------------
114     /// Default constructor.
115     ///
116     /// The default constructor that initializes the object state such
117     /// that it contains no compiled regular expression.
118     //------------------------------------------------------------------
119     RegularExpression ();
120
121     //------------------------------------------------------------------
122     /// Constructor that takes a regulare expression with flags.
123     ///
124     /// Constructor that compiles \a re using \a flags and stores the
125     /// resulting compiled regular expression into this object.
126     ///
127     /// @param[in] re
128     ///     A c string that represents the regular expression to
129     ///     compile.
130     ///
131     /// @param[in] flags
132     ///     Flags that are passed the the \c regcomp() function.
133     //------------------------------------------------------------------
134     explicit
135     RegularExpression (const char* re, int flags);
136
137     // This one uses flags = REG_EXTENDED.
138     explicit
139     RegularExpression (const char* re);
140
141     //------------------------------------------------------------------
142     /// Destructor.
143     ///
144     /// Any previosuly compiled regular expression contained in this
145     /// object will be freed.
146     //------------------------------------------------------------------
147     ~RegularExpression ();
148     
149     RegularExpression (const RegularExpression &rhs);
150     
151     const RegularExpression & operator=(const RegularExpression &rhs);
152
153     //------------------------------------------------------------------
154     /// Compile a regular expression.
155     ///
156     /// Compile a regular expression using the supplied regular
157     /// expression text and flags. The compied regular expression lives
158     /// in this object so that it can be readily used for regular
159     /// expression matches. Execute() can be called after the regular
160     /// expression is compiled. Any previosuly compiled regular
161     /// expression contained in this object will be freed.
162     ///
163     /// @param[in] re
164     ///     A NULL terminated C string that represents the regular
165     ///     expression to compile.
166     ///
167     /// @param[in] flags
168     ///     Flags that are passed the the \c regcomp() function.
169     ///
170     /// @return
171     ///     \b true if the regular expression compiles successfully,
172     ///     \b false otherwise.
173     //------------------------------------------------------------------
174     bool
175     Compile (const char* re);
176
177     bool
178     Compile (const char* re, int flags);
179
180     //------------------------------------------------------------------
181     /// Executes a regular expression.
182     ///
183     /// Execute a regular expression match using the compiled regular
184     /// expression that is already in this object against the match
185     /// string \a s. If any parens are used for regular expression
186     /// matches \a match_count should indicate the number of regmatch_t
187     /// values that are present in \a match_ptr. The regular expression
188     /// will be executed using the \a execute_flags
189     ///
190     /// @param[in] string
191     ///     The string to match against the compile regular expression.
192     ///
193     /// @param[in] match
194     ///     A pointer to a RegularExpression::Match structure that was
195     ///     properly initialized with the desired number of maximum
196     ///     matches, or NULL if no parenthesized matching is needed.
197     ///
198     /// @param[in] execute_flags
199     ///     Flags to pass to the \c regexec() function.
200     ///
201     /// @return
202     ///     \b true if \a string matches the compiled regular
203     ///     expression, \b false otherwise.
204     //------------------------------------------------------------------
205     bool
206     Execute (const char* string, Match *match = NULL, int execute_flags = 0) const;
207
208     size_t
209     GetErrorAsCString (char *err_str, size_t err_str_max_len) const;
210
211     //------------------------------------------------------------------
212     /// Free the compiled regular expression.
213     ///
214     /// If this object contains a valid compiled regular expression,
215     /// this function will free any resources it was consuming.
216     //------------------------------------------------------------------
217     void
218     Free ();
219
220     //------------------------------------------------------------------
221     /// Access the regular expression text.
222     ///
223     /// Returns the text that was used to compile the current regular
224     /// expression.
225     ///
226     /// @return
227     ///     The NULL terminated C string that was used to compile the
228     ///     current regular expression
229     //------------------------------------------------------------------
230     const char*
231     GetText () const;
232     
233     int
234     GetCompileFlags () const
235     {
236         return m_compile_flags;
237     }
238
239     //------------------------------------------------------------------
240     /// Test if valid.
241     ///
242     /// Test if this object contains a valid regular expression.
243     ///
244     /// @return
245     ///     \b true if the regular expression compiled and is ready
246     ///     for execution, \b false otherwise.
247     //------------------------------------------------------------------
248     bool
249     IsValid () const;
250     
251     void
252     Clear ()
253     {
254         Free();
255         m_re.clear();
256         m_compile_flags = 0;
257         m_comp_err = 1;
258     }
259     
260     int
261     GetErrorCode() const
262     {
263         return m_comp_err;
264     }
265
266     bool
267     operator < (const RegularExpression& rhs) const;
268
269 private:
270     //------------------------------------------------------------------
271     // Member variables
272     //------------------------------------------------------------------
273     std::string m_re;   ///< A copy of the original regular expression text
274     int m_comp_err;     ///< Error code for the regular expression compilation
275     regex_t m_preg;     ///< The compiled regular expression
276     int     m_compile_flags; ///< Stores the flags from the last compile.
277 };
278
279 } // namespace lldb_private
280
281 #endif  // #if defined(__cplusplus)
282 #endif  // liblldb_DBRegex_h_