| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> |
| 3 | SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: MIT |
| 6 | */ |
| 7 | |
| 8 | #ifndef KSYNTAXHIGHLIGHTING_STATE_P_H |
| 9 | #define KSYNTAXHIGHLIGHTING_STATE_P_H |
| 10 | |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <QSharedData> |
| 14 | #include <QStringList> |
| 15 | |
| 16 | namespace KSyntaxHighlighting |
| 17 | { |
| 18 | class Context; |
| 19 | |
| 20 | class StateData : public QSharedData |
| 21 | { |
| 22 | friend class State; |
| 23 | friend class AbstractHighlighter; |
| 24 | friend std::size_t qHash(const StateData &, std::size_t); |
| 25 | |
| 26 | public: |
| 27 | StateData() = default; |
| 28 | |
| 29 | static StateData *reset(State &state); |
| 30 | static StateData *detach(State &state); |
| 31 | |
| 32 | static StateData *get(const State &state) |
| 33 | { |
| 34 | return state.d.data(); |
| 35 | } |
| 36 | |
| 37 | std::size_t size() const |
| 38 | { |
| 39 | return m_contextStack.size(); |
| 40 | } |
| 41 | |
| 42 | void push(Context *context, QStringList &&captures); |
| 43 | |
| 44 | /** |
| 45 | * Pop the number of elements given from the top of the current stack. |
| 46 | * Will not pop the initial element. |
| 47 | * @param popCount number of elements to pop |
| 48 | * @return false if one has tried to pop the initial context, else true |
| 49 | */ |
| 50 | bool pop(int popCount); |
| 51 | |
| 52 | Context *topContext() const |
| 53 | { |
| 54 | return m_contextStack.back().context; |
| 55 | } |
| 56 | |
| 57 | const QStringList &topCaptures() const |
| 58 | { |
| 59 | return m_contextStack.back().captures; |
| 60 | } |
| 61 | |
| 62 | struct StackValue { |
| 63 | Context *context; |
| 64 | QStringList captures; |
| 65 | |
| 66 | bool operator==(const StackValue &other) const |
| 67 | { |
| 68 | return context == other.context && captures == other.captures; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | private: |
| 73 | /** |
| 74 | * definition id to filter out invalid states |
| 75 | */ |
| 76 | uint64_t m_defId = 0; |
| 77 | |
| 78 | /** |
| 79 | * the context stack combines the active context + valid captures |
| 80 | */ |
| 81 | std::vector<StackValue> m_contextStack; |
| 82 | }; |
| 83 | |
| 84 | inline std::size_t qHash(const StateData::StackValue &stackValue, std::size_t seed = 0) |
| 85 | { |
| 86 | return qHashMulti(seed, args: stackValue.context, args: stackValue.captures); |
| 87 | } |
| 88 | |
| 89 | inline std::size_t qHash(const StateData &k, std::size_t seed = 0) |
| 90 | { |
| 91 | return qHashMulti(seed, args: k.m_defId, args: qHashRange(first: k.m_contextStack.begin(), last: k.m_contextStack.end(), seed)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #endif |
| 96 | |