| SyntaxHighlighter::SyntaxHighlighter(const std::string &path) { |
| 6 | m_tree = std::make_shared<LanguageTree>(); |
| 7 | m_tree->load(path); |
| 8 | } |
| 9 |
| 10 | TokenList SyntaxHighlighter::tokenize(const std::string &text, |
| 11 | const std::string &language) { |
| 12 | const Grammar *grammar = m_tree->find(language); |
| 13 | if (grammar) { |
| 14 | return tokenize(text, grammar); |
| 15 | } |
| 16 |
| 17 | return TokenList(text); |
| 18 | } |
| 19 |
| 20 | std::map<std::string, std::string> SyntaxHighlighter::languages() const { |
| 21 | return m_tree->keys(); |
| 22 | } |
| 23 |
| 24 | std::string SyntaxHighlighter::languageName(const std::string &language) const { |
| 25 | return m_tree->languageName(language); |
| 26 | } |
| 27 |
| 28 | TokenList SyntaxHighlighter::tokenize(std::string_view text, |
| 29 | const Grammar *grammar) { |
| 30 | // Grammars may include themselves through "inside" (for example |
| 31 | // brightscript directive-statement -> expression -> brightscript), |
| 32 | // which can recurse on the same text forever and overflow the stack. |
| 33 | constexpr auto kMaxDepth = 32; |
| 34 | if (m_depth >= kMaxDepth) { |
| 35 | return TokenList(text); |
| 36 | } |
| 37 | struct DepthGuard { |
| 38 | int &depth; |
| 39 | DepthGuard(int &depth) : depth(++depth) {} |
| 40 | ~DepthGuard() { --depth; } |
| 41 | } guard(m_depth); |
| 42 |
| 43 | TokenList tokenList(text); |
| 44 | try { |
| 45 | matchGrammar(text, tokenList, grammar, tokenList.head, 0, nullptr); |
| 46 | } catch (...) { |
| 47 | } |
| 48 |
| 49 | return tokenList; |
| 50 | } |
| 51 |
| 52 | void SyntaxHighlighter::matchGrammar(std::string_view text, |
| 53 | TokenList &tokenList, |
| 54 | const Grammar *grammar, |
| 55 | TokenListPtr startNode, size_t startPos, |
| 56 | RematchOptions *rematch) { |
| 57 | // Greedy patterns can recursively rematch the same grammar without entering |
| 58 | // tokenize(), so its grammar-depth counter does not cover this path. |
| 59 | static thread_local unsigned int matchDepth = 0; |
| 60 | constexpr unsigned int kMaxMatchDepth = 32; |
| 61 | if (matchDepth >= kMaxMatchDepth) { |
| 62 | return; |
| 63 | } |
| 64 | struct MatchDepthGuard { |
| 65 | unsigned int &depth; |
| 66 | explicit MatchDepthGuard(unsigned int &value) : depth(value) { ++depth; } |
| 67 | ~MatchDepthGuard() { --depth; } |
| 68 | } guard(matchDepth); |
| 69 |
| 70 | for (const auto &token : grammar->tokens) { |
| 71 | int x = 0; |
| 72 | for (auto j = token.cbegin(); j != token.cend(); ++j) { |
| 73 | if (rematch && rematch->j == x && rematch->token == token.name()) { |
| 74 | return; |
| 75 | } |
| 76 |
| 77 | const auto &pattern = *j->get(); |
| 78 | const auto &inside = pattern.inside(); |
| 79 | const bool greedy = pattern.greedy(); |
| 80 |
| 81 | size_t pos = startPos; |
| 82 |
| 83 | // iterate the token list and keep track of the current token/string |
| 84 | // position |
| 85 | for (TokenListPtr currentNode = startNode->next; |
| 86 | currentNode != tokenList.head; |
| 87 | pos += currentNode->length(), currentNode = currentNode->next) { |
| 88 | if (rematch && pos >= rematch->reach) { |
| 89 | break; |
| 90 | } |
| 91 |
| 92 | if (tokenList.length > text.length()) { |
| 93 | // Something went terribly wrong, ABORT, ABORT! |
| 94 | return; |
| 95 | } |
| 96 |
| 97 | if (currentNode->isSyntax()) { |
| 98 | continue; |
| 99 | } |
| 100 |
| 101 | const auto ¤tText = dynamic_cast<Text &>(*currentNode); |
| 102 | std::string_view str = currentText.value(); |
| 103 |
| 104 | auto removeCount = 1; // this is the to parameter of removeBetween |
| 105 | std::string_view match; |
| 106 | bool matchSuccess = false; |
| 107 | size_t matchIndex = pos; |
| 108 |
| 109 | if (greedy) { |
| 110 | match = pattern.match(matchSuccess, matchIndex, text); |
| 111 | if (!matchSuccess || matchIndex >= text.length()) { |
| 112 | break; |
| 113 | } |
| 114 |
| 115 | auto from = matchIndex; |
| 116 | auto to = matchIndex + match.length(); |
| 117 | auto p = pos; |
| 118 |
| 119 | // find the node that contains the match |
| 120 | p += currentNode->length(); |
| 121 | while (from >= p) { |
| 122 | currentNode = currentNode->next; |
| 123 | p += currentNode->length(); |
| 124 | } |
| 125 | // adjust pos (and p) |
| 126 | p -= currentNode->length(); |
| 127 | pos = p; |
| 128 |
| 129 | // the current node is a Token, then the match starts inside another |
| 130 | // Token, which is invalid |
| 131 | if (currentNode->isSyntax()) { |
| 132 | continue; |
| 133 | } |
| 134 |
| 135 | // find the last node which is affected by this match |
| 136 | for (TokenListPtr k = currentNode; |
| 137 | k != tokenList.head && (p < to || !k->isSyntax()); k = k->next) { |
| 138 | removeCount++; |
| 139 | p += k->length(); |
| 140 | } |
| 141 | removeCount--; |
| 142 |
| 143 | // replace with the new match |
| 144 | str = text.substr(pos, p - pos); |
| 145 | matchIndex -= pos; |
| 146 | } else { |
| 147 | matchIndex = 0; |
| 148 | match = pattern.match(matchSuccess, matchIndex, str); |
| 149 | if (!matchSuccess) { |
| 150 | continue; |
| 151 | } |
| 152 | } |
| 153 |
| 154 | auto from = matchIndex; |
| 155 | auto before = str.substr(0, from); |
| 156 | auto after = str.substr(from + match.length()); |
| 157 |
| 158 | auto reach = pos + str.length(); |
| 159 | if (rematch && reach > rematch->reach) { |
| 160 | rematch->reach = reach; |
| 161 | } |
| 162 |
| 163 | TokenListPtr removeFrom = currentNode->prev; |
| 164 |
| 165 | if (before.size()) { |
| 166 | removeFrom = tokenList.addAfter(removeFrom, before); |
| 167 | pos += before.length(); |
| 168 | } |
| 169 |
| 170 | tokenList.removeRange(removeFrom, removeCount); |
| 171 |
| 172 | TokenList tokenEntries = [&]() { |
| 173 | if (inside) { |
| 174 | return tokenize(match, inside); |
| 175 | } else { |
| 176 | return TokenList(match); |
| 177 | } |
| 178 | }(); |
| 179 |
| 180 | currentNode = tokenList.addAfter(removeFrom, token.name(), |
| 181 | std::move(tokenEntries), |
| 182 | pattern.alias(), match.size()); |
| 183 |
| 184 | if (after.size()) { |
| 185 | tokenList.addAfter(currentNode, after); |
| 186 | } |
| 187 |
| 188 | if (removeCount > 1) { |
| 189 | // at least one Token object was removed, so we have to do some |
| 190 | // rematching this can only happen if the current pattern is greedy |
| 191 | RematchOptions nestedRematch = { |
| 192 | .token = token.name(), .reach = reach, .j = x}; |
| 193 |
| 194 | matchGrammar(text, tokenList, grammar, currentNode->prev, pos, |
| 195 | &nestedRematch); |
| 196 |
| 197 | // the reach might have been extended because of the rematching |
| 198 | if (rematch && nestedRematch.reach > rematch->reach) { |
| 199 | rematch->reach = nestedRematch.reach; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 |
| 204 | ++x; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 |