Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
symbols.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5#ifndef SYMBOLS_H
6#define SYMBOLS_H
7
8#include "token.h"
9#include <qdebug.h>
10#include <qhash.h>
11#include <qlist.h>
12#include <qstack.h>
13#include <qstring.h>
14#include <qset.h>
15
17
18//#define USE_LEXEM_STORE
19
21{
22 inline SubArray() = default;
23 inline SubArray(const QByteArray &a):array(a),from(0), len(a.size()){}
24 inline SubArray(const char *s):array(s),from(0) { len = array.size(); }
26 : array(a), from(from), len(len)
27 {
28 }
32 inline bool operator==(const SubArray &other) const {
33 if (len != other.len)
34 return false;
35 const auto begin = array.cbegin() + from;
36 const auto end = begin + len;
37 const auto other_begin = other.array.cbegin() + other.from;
38 return std::equal(begin, end, other_begin);
39 }
40};
41
42inline size_t qHash(const SubArray &key)
43{
44 return qHash(QLatin1StringView(key.array.constData() + key.from, key.len));
45}
46
47
48struct Symbol
49{
50
51#ifdef USE_LEXEM_STORE
52 typedef QHash<SubArray, QHashDummyValue> LexemStore;
53 static LexemStore lexemStore;
54
55 inline Symbol() : lineNum(-1),token(NOTOKEN){}
56 inline Symbol(int lineNum, Token token):
58 inline Symbol(int lineNum, Token token, const QByteArray &lexem):
60 inline Symbol(int lineNum, Token token, const QByteArray &lexem, int from, int len):
62 LexemStore::const_iterator it = lexemStore.constFind(SubArray(lexem, from, len));
63
64 if (it != lexemStore.constEnd()) {
65 lex = it.key().array;
66 } else {
67 lex = lexem.mid(from, len);
68 lexemStore.insert(lex, QHashDummyValue());
69 }
70 }
71 int lineNum;
73 inline QByteArray unquotedLexem() const { return lex.mid(1, lex.length()-2); }
74 inline QByteArray lexem() const { return lex; }
75 inline operator QByteArray() const { return lex; }
77
78#else
79
80 inline Symbol() = default;
84 {
85 }
88 {
89 }
90 int lineNum = -1;
91 Token token = NOTOKEN;
92 inline QByteArray lexem() const { return lex.mid(from, len); }
93 inline QByteArray unquotedLexem() const { return lex.mid(from+1, len-2); }
94 inline operator SubArray() const { return SubArray(lex, from, len); }
95 bool operator==(const Symbol& o) const
96 {
97 return SubArray(lex, from, len) == SubArray(o.lex, o.from, o.len);
98 }
102
103#endif
104};
106
108
114};
116
117class SymbolStack : public QStack<SafeSymbols>
118{
119public:
120 inline bool hasNext() {
121 while (!isEmpty() && top().index >= top().symbols.size())
122 pop();
123 return !isEmpty();
124 }
125 inline Token next() {
126 while (!isEmpty() && top().index >= top().symbols.size())
127 pop();
128 if (isEmpty())
129 return NOTOKEN;
130 return top().symbols.at(top().index++).token;
131 }
132 bool test(Token);
133 inline const Symbol &symbol() const { return top().symbols.at(top().index-1); }
134 inline Token token() { return symbol().token; }
135 inline QByteArray lexem() const { return symbol().lexem(); }
137
138 bool dontReplaceSymbol(const QByteArray &name) const;
140};
141
143{
144 qsizetype stackPos = size() - 1;
145 while (stackPos >= 0 && at(stackPos).index >= at(stackPos).symbols.size())
146 --stackPos;
147 if (stackPos < 0)
148 return false;
149 if (at(stackPos).symbols.at(at(stackPos).index).token == token) {
150 next();
151 return true;
152 }
153 return false;
154}
155
157{
158 auto matchesName = [&name](const SafeSymbols &sf) {
159 return name == sf.expandedMacro || sf.excludedSymbols.contains(name);
160 };
161 return std::any_of(cbegin(), cend(), matchesName);
162}
163
165{
167 for (const SafeSymbols &sf : *this) {
168 set << sf.expandedMacro;
169 set += sf.excludedSymbols;
170 }
171 return set;
172}
173
175
176#endif // SYMBOLS_H
\inmodule QtCore
Definition qbytearray.h:57
qsizetype length() const noexcept
Same as size().
Definition qbytearray.h:479
QByteArray & insert(qsizetype i, QByteArrayView data)
QByteArray mid(qsizetype index, qsizetype len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos.
\inmodule QtCore
Definition qhash.h:818
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
const_iterator cend() const noexcept
Definition qlist.h:614
const_iterator cbegin() const noexcept
Definition qlist.h:613
Definition qset.h:18
const_iterator constEnd() const noexcept
Definition qset.h:143
const_iterator constFind(const T &value) const
Definition qset.h:161
\inmodule QtCore
Definition qstack.h:13
SafeSymbols & top()
Returns a reference to the stack's top item.
Definition qstack.h:19
SafeSymbols pop()
Removes the top item from the stack and returns it.
Definition qstack.h:18
QSet< QByteArray > excludeSymbols() const
Definition symbols.h:164
QByteArray unquotedLexem()
Definition symbols.h:136
const Symbol & symbol() const
Definition symbols.h:133
Token token()
Definition symbols.h:134
bool dontReplaceSymbol(const QByteArray &name) const
Definition symbols.h:156
bool hasNext()
Definition symbols.h:120
QByteArray lexem() const
Definition symbols.h:135
Token next()
Definition symbols.h:125
bool test(Token)
Definition symbols.h:142
QSet< QString >::iterator it
Token token
Definition keywords.cpp:444
Combined button and popup list for selecting options.
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLuint GLuint end
GLuint name
GLenum array
GLenum GLsizei len
GLdouble s
[6]
Definition qopenglext.h:235
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:145
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:163
ptrdiff_t qsizetype
Definition qtypes.h:70
QFuture< QSet< QChar > > set
[10]
QSharedPointer< T > other(t)
[5]
QAction * at
TokenType token
qsizetype index
Definition symbols.h:113
Symbols symbols
Definition symbols.h:110
QSet< QByteArray > excludedSymbols
Definition symbols.h:112
QByteArray expandedMacro
Definition symbols.h:111
QByteArray array
Definition symbols.h:29
SubArray(const char *s)
Definition symbols.h:24
qsizetype len
Definition symbols.h:31
bool operator==(const SubArray &other) const
Definition symbols.h:32
qsizetype from
Definition symbols.h:30
SubArray()=default
SubArray(const QByteArray &a)
Definition symbols.h:23
SubArray(const QByteArray &a, qsizetype from, qsizetype len)
Definition symbols.h:25
qsizetype from
Definition symbols.h:100
Symbol(int lineNum, Token token)
Definition symbols.h:81
Token token
Definition symbols.h:91
bool operator==(const Symbol &o) const
Definition symbols.h:95
int lineNum
Definition symbols.h:90
Symbol()=default
Symbol(int lineNum, Token token, const QByteArray &lexem, qsizetype from, qsizetype len)
Definition symbols.h:86
QByteArray lexem() const
Definition symbols.h:92
QByteArray lex
Definition symbols.h:99
QByteArray unquotedLexem() const
Definition symbols.h:93
Symbol(int lineNum, Token token, const QByteArray &lexem)
Definition symbols.h:82
size_t qHash(const SubArray &key)
Definition symbols.h:42
QList< Symbol > Symbols
Definition symbols.h:107
Token
Definition token.h:194