|
| 1 | +#include "CodeService/Config/EditorconfigPattern.h" |
| 2 | +#include <algorithm> |
| 3 | +#include "LuaParser/Lexer/LuaDefine.h" |
| 4 | +#include "Util/StringUtil.h" |
| 5 | + |
| 6 | + |
| 7 | +EditorconfigPattern::EditorconfigPattern() { |
| 8 | + |
| 9 | +} |
| 10 | + |
| 11 | +void EditorconfigPattern::Compile(std::string_view pattern) { |
| 12 | + _patternSource = pattern; |
| 13 | + TextReader reader(_patternSource); |
| 14 | + while (true) { |
| 15 | + auto type = Lex(reader); |
| 16 | + if (type == MatchType::Eof) { |
| 17 | + break; |
| 18 | + } |
| 19 | + |
| 20 | + auto &matchData = _matches.emplace_back(type); |
| 21 | + auto text = reader.GetSaveText(); |
| 22 | + switch (type) { |
| 23 | + case MatchType::Path: { |
| 24 | + matchData.String = text; |
| 25 | + break; |
| 26 | + } |
| 27 | + case MatchType::AnyCharOf: { |
| 28 | + if (text.size() > 2) { |
| 29 | + text = text.substr(1, text.size() - 2); |
| 30 | + matchData.CharSet = std::set<char>(text.begin(), text.end()); |
| 31 | + } |
| 32 | + break; |
| 33 | + } |
| 34 | + case MatchType::NotCharOf: { |
| 35 | + if (text.size() > 3) { |
| 36 | + text = text.substr(2, text.size() - 3); |
| 37 | + matchData.CharSet = std::set<char>(text.begin(), text.end()); |
| 38 | + } |
| 39 | + break; |
| 40 | + } |
| 41 | + case MatchType::StringOf: { |
| 42 | + if (text.size() > 2) { |
| 43 | + text = text.substr(1, text.size() - 2); |
| 44 | + matchData.Strings = string_util::Split(text, ","); |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +EditorconfigPattern::MatchType EditorconfigPattern::Lex(TextReader &reader) { |
| 53 | + reader.ResetBuffer(); |
| 54 | + int ch = reader.GetCurrentChar(); |
| 55 | + switch (ch) { |
| 56 | + case '*': { |
| 57 | + reader.SaveAndNext(); |
| 58 | + if (reader.GetCurrentChar() == '*') { |
| 59 | + reader.SaveAndNext(); |
| 60 | + return MatchType::AnyChars; |
| 61 | + } |
| 62 | + |
| 63 | + return MatchType::AnyCharsExceptSep; |
| 64 | + } |
| 65 | + case '{': { |
| 66 | + reader.SaveAndNext(); |
| 67 | + while (!reader.IsEof()) { |
| 68 | + ch = reader.GetCurrentChar(); |
| 69 | + reader.SaveAndNext(); |
| 70 | + if (ch == '}') { |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + return MatchType::StringOf; |
| 75 | + } |
| 76 | + case '[': { |
| 77 | + reader.SaveAndNext(); |
| 78 | + MatchType type = MatchType::AnyCharOf; |
| 79 | + if (reader.GetCurrentChar() == '!') { |
| 80 | + type = MatchType::NotCharOf; |
| 81 | + reader.SaveAndNext(); |
| 82 | + } |
| 83 | + while (!reader.IsEof()) { |
| 84 | + ch = reader.GetCurrentChar(); |
| 85 | + reader.SaveAndNext(); |
| 86 | + if (ch == ']') { |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return type; |
| 92 | + } |
| 93 | + case '?': { |
| 94 | + reader.SaveAndNext(); |
| 95 | + return MatchType::AnyChar; |
| 96 | + } |
| 97 | + case EOZ: { |
| 98 | + return MatchType::Eof; |
| 99 | + } |
| 100 | + default: { |
| 101 | + reader.SaveAndNext(); |
| 102 | + while (!reader.IsEof()) { |
| 103 | + ch = reader.GetCurrentChar(); |
| 104 | + if (ch == '[' || ch == '{' || ch == '*') { |
| 105 | + break; |
| 106 | + } |
| 107 | + reader.SaveAndNext(); |
| 108 | + } |
| 109 | + |
| 110 | + return MatchType::Path; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +bool EditorconfigPattern::Match(std::string_view filePath) { |
| 116 | + if (filePath.empty()) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + std::string newPath(filePath); |
| 120 | + for (auto &c: newPath) { |
| 121 | + if (c == '\\') { |
| 122 | + c = '/'; |
| 123 | + } |
| 124 | + } |
| 125 | + std::string pathView = newPath; |
| 126 | + |
| 127 | + std::size_t matchProcess = _matches.size(); |
| 128 | + for (; matchProcess > 0; matchProcess--) { |
| 129 | + auto &matchData = _matches[matchProcess - 1]; |
| 130 | + switch (matchData.Type) { |
| 131 | + case MatchType::Path: { |
| 132 | + if (!string_util::EndWith(pathView, matchData.String)) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + pathView = pathView.substr(0, pathView.size() - matchData.String.size()); |
| 136 | + break; |
| 137 | + } |
| 138 | + case MatchType::AnyCharsExceptSep: { |
| 139 | + std::size_t matchIndex = pathView.size(); |
| 140 | + for (; matchIndex > 0; matchIndex--) { |
| 141 | + char ch = pathView[matchIndex - 1]; |
| 142 | + if (ch == '/' || ch == '\\') { |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (matchProcess > 1) { |
| 148 | + auto &nextMatchData = _matches[matchProcess - 2]; |
| 149 | + if (nextMatchData.Type == MatchType::Path) { |
| 150 | + auto pos = pathView.rfind(nextMatchData.String); |
| 151 | + if (pos == std::string_view::npos) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + if (pos + nextMatchData.String.size() < matchIndex) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + matchIndex = pos + nextMatchData.String.size(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + pathView = pathView.substr(0, matchIndex); |
| 162 | + break; |
| 163 | + } |
| 164 | + case MatchType::AnyChars: { |
| 165 | + std::size_t matchIndex = 0; |
| 166 | + if (matchProcess > 1) { |
| 167 | + auto &nextMatchData = _matches[matchProcess - 2]; |
| 168 | + if (nextMatchData.Type == MatchType::Path) { |
| 169 | + auto pos = pathView.rfind(nextMatchData.String); |
| 170 | + if (pos == std::string_view::npos) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + matchIndex = pos + nextMatchData.String.size(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + pathView = pathView.substr(0, matchIndex); |
| 178 | + break; |
| 179 | + } |
| 180 | + case MatchType::AnyChar: { |
| 181 | + if (pathView.empty()) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + pathView = pathView.substr(0, pathView.size() - 1); |
| 185 | + break; |
| 186 | + } |
| 187 | + case MatchType::AnyCharOf: { |
| 188 | + if (pathView.empty()) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + if (matchData.CharSet.count(pathView.back()) == 0) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + pathView = pathView.substr(0, pathView.size() - 1); |
| 196 | + break; |
| 197 | + } |
| 198 | + case MatchType::NotCharOf: { |
| 199 | + if (pathView.empty()) { |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + if (matchData.CharSet.count(pathView.back()) > 0) { |
| 204 | + return false; |
| 205 | + } |
| 206 | + pathView = pathView.substr(0, pathView.size() - 1); |
| 207 | + break; |
| 208 | + } |
| 209 | + case MatchType::StringOf: { |
| 210 | + bool match = false; |
| 211 | + for (auto &s: matchData.Strings) { |
| 212 | + if (string_util::EndWith(pathView, s)) { |
| 213 | + match = true; |
| 214 | + pathView = pathView.substr(0, pathView.size() - s.size()); |
| 215 | + break; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if (!match) { |
| 220 | + return false; |
| 221 | + } |
| 222 | + break; |
| 223 | + } |
| 224 | + default: { |
| 225 | + return false; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + return matchProcess == 0; |
| 231 | +} |
| 232 | + |
| 233 | +std::string_view EditorconfigPattern::GetPattern() { |
| 234 | + return _patternSource; |
| 235 | +} |
| 236 | + |
| 237 | + |
0 commit comments