Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 1 | //===-- Regex.cpp - Regular Expression matcher implementation -------------===// |
| 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1ce8362 | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 13 | |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Regex.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "regex_impl.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 19 | #include <string> |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Chris Lattner | 1ce8362 | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 21 | |
Chandler Carruth | 1ab16f8 | 2016-09-01 09:31:02 +0000 | [diff] [blame] | 22 | Regex::Regex() : preg(nullptr), error(REG_BADPAT) {} |
George Rimar | a9ff072 | 2016-09-01 08:00:28 +0000 | [diff] [blame] | 23 | |
Benjamin Kramer | 92d8998 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 24 | Regex::Regex(StringRef regex, unsigned Flags) { |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 25 | unsigned flags = 0; |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 26 | preg = new llvm_regex(); |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 27 | preg->re_endp = regex.end(); |
| 28 | if (Flags & IgnoreCase) |
| 29 | flags |= REG_ICASE; |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 30 | if (Flags & Newline) |
| 31 | flags |= REG_NEWLINE; |
Eli Bendersky | 10f22d7 | 2012-11-28 19:00:02 +0000 | [diff] [blame] | 32 | if (!(Flags & BasicRegex)) |
| 33 | flags |= REG_EXTENDED; |
| 34 | error = llvm_regcomp(preg, regex.data(), flags|REG_PEND); |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 35 | } |
| 36 | |
George Rimar | d8dfeec | 2016-09-02 08:44:46 +0000 | [diff] [blame] | 37 | Regex::Regex(Regex &®ex) { |
| 38 | preg = regex.preg; |
| 39 | error = regex.error; |
| 40 | regex.preg = nullptr; |
| 41 | regex.error = REG_BADPAT; |
| 42 | } |
| 43 | |
Chris Lattner | 1ce8362 | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 44 | Regex::~Regex() { |
David Blaikie | 7a23804 | 2014-01-02 19:04:59 +0000 | [diff] [blame] | 45 | if (preg) { |
| 46 | llvm_regfree(preg); |
| 47 | delete preg; |
| 48 | } |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 49 | } |
| 50 | |
George Burgess IV | b71bc44 | 2017-04-18 01:04:05 +0000 | [diff] [blame] | 51 | bool Regex::isValid(std::string &Error) const { |
Chris Lattner | 37d8015 | 2009-09-26 21:27:04 +0000 | [diff] [blame] | 52 | if (!error) |
| 53 | return true; |
| 54 | |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 55 | size_t len = llvm_regerror(error, preg, nullptr, 0); |
Chris Lattner | 37d8015 | 2009-09-26 21:27:04 +0000 | [diff] [blame] | 56 | |
Alexey Samsonov | 96dd18c | 2013-08-08 17:32:45 +0000 | [diff] [blame] | 57 | Error.resize(len - 1); |
Chris Lattner | 37d8015 | 2009-09-26 21:27:04 +0000 | [diff] [blame] | 58 | llvm_regerror(error, preg, &Error[0], len); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /// getNumMatches - In a valid regex, return the number of parenthesized |
| 63 | /// matches it contains. |
| 64 | unsigned Regex::getNumMatches() const { |
| 65 | return preg->re_nsub; |
| 66 | } |
| 67 | |
Benjamin Kramer | 92d8998 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 68 | bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches){ |
George Rimar | d8dfeec | 2016-09-02 08:44:46 +0000 | [diff] [blame] | 69 | if (error) |
| 70 | return false; |
| 71 | |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 72 | unsigned nmatch = Matches ? preg->re_nsub+1 : 0; |
| 73 | |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 74 | // pmatch needs to have at least one element. |
Chris Lattner | f08d2db | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 75 | SmallVector<llvm_regmatch_t, 8> pm; |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 76 | pm.resize(nmatch > 0 ? nmatch : 1); |
| 77 | pm[0].rm_so = 0; |
| 78 | pm[0].rm_eo = String.size(); |
| 79 | |
| 80 | int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND); |
| 81 | |
| 82 | if (rc == REG_NOMATCH) |
| 83 | return false; |
| 84 | if (rc != 0) { |
| 85 | // regexec can fail due to invalid pattern or running out of memory. |
| 86 | error = rc; |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // There was a match. |
| 91 | |
| 92 | if (Matches) { // match position requested |
Chris Lattner | 37d8015 | 2009-09-26 21:27:04 +0000 | [diff] [blame] | 93 | Matches->clear(); |
| 94 | |
Chris Lattner | 1ce8362 | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 95 | for (unsigned i = 0; i != nmatch; ++i) { |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 96 | if (pm[i].rm_so == -1) { |
| 97 | // this group didn't match |
| 98 | Matches->push_back(StringRef()); |
| 99 | continue; |
| 100 | } |
Chris Lattner | 0687ec7 | 2011-04-09 06:29:24 +0000 | [diff] [blame] | 101 | assert(pm[i].rm_eo >= pm[i].rm_so); |
Torok Edwin | e14d4cd | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 102 | Matches->push_back(StringRef(String.data()+pm[i].rm_so, |
| 103 | pm[i].rm_eo-pm[i].rm_so)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
Daniel Dunbar | eb85711 | 2010-02-17 20:08:42 +0000 | [diff] [blame] | 109 | |
| 110 | std::string Regex::sub(StringRef Repl, StringRef String, |
| 111 | std::string *Error) { |
| 112 | SmallVector<StringRef, 8> Matches; |
| 113 | |
| 114 | // Reset error, if given. |
| 115 | if (Error && !Error->empty()) *Error = ""; |
| 116 | |
| 117 | // Return the input if there was no match. |
| 118 | if (!match(String, &Matches)) |
| 119 | return String; |
| 120 | |
| 121 | // Otherwise splice in the replacement string, starting with the prefix before |
| 122 | // the match. |
| 123 | std::string Res(String.begin(), Matches[0].begin()); |
| 124 | |
| 125 | // Then the replacement string, honoring possible substitutions. |
| 126 | while (!Repl.empty()) { |
| 127 | // Skip to the next escape. |
| 128 | std::pair<StringRef, StringRef> Split = Repl.split('\\'); |
| 129 | |
| 130 | // Add the skipped substring. |
| 131 | Res += Split.first; |
| 132 | |
| 133 | // Check for terminimation and trailing backslash. |
| 134 | if (Split.second.empty()) { |
| 135 | if (Repl.size() != Split.first.size() && |
| 136 | Error && Error->empty()) |
| 137 | *Error = "replacement string contained trailing backslash"; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | // Otherwise update the replacement string and interpret escapes. |
| 142 | Repl = Split.second; |
| 143 | |
| 144 | // FIXME: We should have a StringExtras function for mapping C99 escapes. |
| 145 | switch (Repl[0]) { |
| 146 | // Treat all unrecognized characters as self-quoting. |
| 147 | default: |
| 148 | Res += Repl[0]; |
| 149 | Repl = Repl.substr(1); |
| 150 | break; |
| 151 | |
| 152 | // Single character escapes. |
| 153 | case 't': |
| 154 | Res += '\t'; |
| 155 | Repl = Repl.substr(1); |
| 156 | break; |
| 157 | case 'n': |
| 158 | Res += '\n'; |
| 159 | Repl = Repl.substr(1); |
| 160 | break; |
| 161 | |
| 162 | // Decimal escapes are backreferences. |
| 163 | case '0': case '1': case '2': case '3': case '4': |
| 164 | case '5': case '6': case '7': case '8': case '9': { |
| 165 | // Extract the backreference number. |
| 166 | StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789")); |
| 167 | Repl = Repl.substr(Ref.size()); |
| 168 | |
| 169 | unsigned RefValue; |
| 170 | if (!Ref.getAsInteger(10, RefValue) && |
| 171 | RefValue < Matches.size()) |
| 172 | Res += Matches[RefValue]; |
| 173 | else if (Error && Error->empty()) |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 174 | *Error = ("invalid backreference string '" + Twine(Ref) + "'").str(); |
Daniel Dunbar | eb85711 | 2010-02-17 20:08:42 +0000 | [diff] [blame] | 175 | break; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // And finally the suffix. |
| 181 | Res += StringRef(Matches[0].end(), String.end() - Matches[0].end()); |
| 182 | |
| 183 | return Res; |
| 184 | } |
Peter Collingbourne | fe8cd75 | 2013-08-05 17:47:59 +0000 | [diff] [blame] | 185 | |
Alp Toker | d0d1a74 | 2013-12-12 02:51:58 +0000 | [diff] [blame] | 186 | // These are the special characters matched in functions like "p_ere_exp". |
| 187 | static const char RegexMetachars[] = "()^$|*+?.[]\\{}"; |
| 188 | |
Peter Collingbourne | fe8cd75 | 2013-08-05 17:47:59 +0000 | [diff] [blame] | 189 | bool Regex::isLiteralERE(StringRef Str) { |
| 190 | // Check for regex metacharacters. This list was derived from our regex |
| 191 | // implementation in regcomp.c and double checked against the POSIX extended |
| 192 | // regular expression specification. |
Alp Toker | d0d1a74 | 2013-12-12 02:51:58 +0000 | [diff] [blame] | 193 | return Str.find_first_of(RegexMetachars) == StringRef::npos; |
Peter Collingbourne | fe8cd75 | 2013-08-05 17:47:59 +0000 | [diff] [blame] | 194 | } |
Hans Wennborg | 6f4f77b | 2013-12-12 00:06:41 +0000 | [diff] [blame] | 195 | |
| 196 | std::string Regex::escape(StringRef String) { |
| 197 | std::string RegexStr; |
Hans Wennborg | 6f4f77b | 2013-12-12 00:06:41 +0000 | [diff] [blame] | 198 | for (unsigned i = 0, e = String.size(); i != e; ++i) { |
Alp Toker | d0d1a74 | 2013-12-12 02:51:58 +0000 | [diff] [blame] | 199 | if (strchr(RegexMetachars, String[i])) |
Hans Wennborg | 6f4f77b | 2013-12-12 00:06:41 +0000 | [diff] [blame] | 200 | RegexStr += '\\'; |
Alp Toker | d0d1a74 | 2013-12-12 02:51:58 +0000 | [diff] [blame] | 201 | RegexStr += String[i]; |
Hans Wennborg | 6f4f77b | 2013-12-12 00:06:41 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return RegexStr; |
| 205 | } |