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

source code of syntax-highlighting/src/lib/state_p.h