| #include <vector> |
| 6 | #include <map> |
| 7 | #include <optional> |
| 8 |
| 9 | class LanguageTree; |
| 10 | class Pattern; |
| 11 | struct Grammar; |
| 12 |
| 13 | // A language and the other names it goes by. An alias with a display name of its own (html, |
| 14 | // under markup) is a language of its own here, as it is in the list a user picks from. |
| 15 | struct LanguageGroup |
| 16 | { |
| 17 | std::string id; |
| 18 | std::string name; |
| 19 | std::vector<std::string> aliases; |
| 20 | }; |
| 21 |
| 22 | struct RematchOptions |
| 23 | { |
| 24 | std::string token; |
| 25 | size_t reach; |
| 26 | int j; |
| 27 | }; |
| 28 |
| 29 | class SyntaxHighlighter |
| 30 | { |
| 31 | public: |
| 32 | SyntaxHighlighter(const std::string& languages); |
| 33 |
| 34 | // Safe to call from several threads at once. |
| 35 | TokenList tokenize(const std::string& text, const std::string& language) const; |
| 36 |
| 37 | std::map<std::string, std::string> languages() const; |
| 38 |
| 39 | std::string languageName(const std::string& language) const; |
| 40 |
| 41 | std::vector<LanguageGroup> languageGroups() const; |
| 42 |
| 43 | private: |
| 44 | // An embedded-language token, where it is in the text and where its code starts once it is |
| 45 | // joined with the others of its run. |
| 46 | struct EmbeddedPart |
| 47 | { |
| 48 | TokenListPtr node; |
| 49 | size_t offset; |
| 50 | size_t start; |
| 51 | }; |
| 52 |
| 53 | // The last search of a greedy pattern over the text being tokenized. The text does not |
| 54 | // change and a search sees the text before its start, so one that found nothing from `from` |
| 55 | // finds nothing further on, and one that found a match at `start` finds it again from |
| 56 | // anywhere up to there. |
| 57 | struct GreedySearch |
| 58 | { |
| 59 | const Pattern* pattern; |
| 60 | size_t from = 0; |
| 61 | bool found = false; |
| 62 | size_t start = 0; |
| 63 | size_t length = 0; |
| 64 | size_t lookbehind = 0; |
| 65 | }; |
| 66 |
| 67 | // What one call to the public tokenize keeps while it runs. It lives on that call's stack, |
| 68 | // never in the highlighter, which threads share. |
| 69 | struct State |
| 70 | { |
| 71 | int depth = 0; |
| 72 | size_t searchesLeft = 0; |
| 73 | int searchesToGiveUp = 2; |
| 74 |
| 75 | // Those of the text being tokenized are the ones from searchesFrom on. A nested |
| 76 | // tokenize, of another text, stacks its own above them and drops them when it returns. |
| 77 | std::vector<GreedySearch> searches; |
| 78 | size_t searchesFrom = 0; |
| 79 | }; |
| 80 |
| 81 | TokenList tokenize(State& state, std::string_view text, const Grammar* grammar) const; |
| 82 | bool search(State& state, const Pattern& pattern, size_t pos, std::string_view text, size_t& start, size_t& length, size_t& lookbehindLength) const; |
| 83 | std::string_view matchGreedy(State& state, const Pattern& pattern, bool& success, size_t& pos, std::string_view text) const; |
| 84 | void highlightEmbedded(State& state, TokenList& tokenList, size_t pos, std::string_view text, const Grammar* fixed, const Grammar*& named) const; |
| 85 | void highlightParts(State& state, std::vector<EmbeddedPart>& parts, std::string& content, std::string_view text, const Grammar* language) const; |
| 86 | void matchGrammar(State& state, std::string_view text, TokenList& tokenList, const Grammar* grammar, TokenListPtr startNode, size_t startPos, RematchOptions* rematch) const; |
| 87 |
| 88 | std::shared_ptr<LanguageTree> m_tree; |
| 89 | }; |