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