Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 <qhashfunctions.h>
11#include <qlist.h>
12#include <qstack.h>
13#include <qstring.h>
14#include <qset.h>
15
17
19{
20 inline SubArray() = default;
21 inline SubArray(const QByteArray &a):array(a),from(0), len(a.size()){}
22 inline SubArray(const char *s):array(s),from(0) { len = array.size(); }
30 inline bool operator==(const SubArray &other) const {
31 if (len != other.len)
32 return false;
33 const auto begin = array.cbegin() + from;
34 const auto end = begin + len;
35 const auto other_begin = other.array.cbegin() + other.from;
36 return std::equal(begin, end, other_begin);
37 }
38};
39
40inline size_t qHash(const SubArray &key, size_t seed = 0)
41{
42 return qHash(QLatin1StringView(key.array.constData() + key.from, key.len), seed);
43}
44
45
46struct Symbol
47{
48 inline Symbol() = default;
52 {
53 }
58 int lineNum = -1;
59 Token token = NOTOKEN;
60 inline QByteArray lexem() const { return lex.mid(from, len); }
61 inline QByteArray unquotedLexem() const { return lex.mid(from+1, len-2); }
62 inline operator SubArray() const { return SubArray(lex, from, len); }
63 bool operator==(const Symbol& o) const
64 {
65 return SubArray(lex, from, len) == SubArray(o.lex, o.from, o.len);
66 }
70};
72
73typedef QList<Symbol> Symbols;
74
82
83class SymbolStack : public QStack<SafeSymbols>
84{
85public:
86 inline bool hasNext() {
87 while (!isEmpty() && top().index >= top().symbols.size())
88 pop();
89 return !isEmpty();
90 }
91 inline Token next() {
92 while (!isEmpty() && top().index >= top().symbols.size())
93 pop();
94 if (isEmpty())
95 return NOTOKEN;
96 return top().symbols.at(top().index++).token;
97 }
98 bool test(Token);
99 inline const Symbol &symbol() const { return top().symbols.at(top().index-1); }
100 inline Token token() { return symbol().token; }
101 inline QByteArray lexem() const { return symbol().lexem(); }
103
104 bool dontReplaceSymbol(const QByteArray &name) const;
105 QSet<QByteArray> excludeSymbols() const;
106};
107
109{
110 qsizetype stackPos = size() - 1;
111 while (stackPos >= 0 && at(stackPos).index >= at(stackPos).symbols.size())
112 --stackPos;
113 if (stackPos < 0)
114 return false;
115 if (at(stackPos).symbols.at(at(stackPos).index).token == token) {
116 next();
117 return true;
118 }
119 return false;
120}
121
123{
124 auto matchesName = [&name](const SafeSymbols &sf) {
125 return name == sf.expandedMacro || sf.excludedSymbols.contains(name);
126 };
127 return std::any_of(cbegin(), cend(), matchesName);
128}
129
130inline QSet<QByteArray> SymbolStack::excludeSymbols() const
131{
132 QSet<QByteArray> set;
133 for (const SafeSymbols &sf : *this) {
134 set << sf.expandedMacro;
135 set += sf.excludedSymbols;
136 }
137 return set;
138}
139
141
142#endif // SYMBOLS_H
\inmodule QtCore
Definition qbytearray.h:57
QByteArray mid(qsizetype index, qsizetype len=-1) const &
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
const_iterator cend() const noexcept
Definition qlist.h:631
const_iterator cbegin() const noexcept
Definition qlist.h:630
\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:130
QByteArray unquotedLexem()
Definition symbols.h:102
const Symbol & symbol() const
Definition symbols.h:99
Token token()
Definition symbols.h:100
bool dontReplaceSymbol(const QByteArray &name) const
Definition symbols.h:122
bool hasNext()
Definition symbols.h:86
QByteArray lexem() const
Definition symbols.h:101
Token next()
Definition symbols.h:91
bool test(Token)
Definition symbols.h:108
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
GLdouble s
[6]
Definition qopenglext.h:235
GLenum array
GLenum GLsizei len
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:158
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
ptrdiff_t qsizetype
Definition qtypes.h:165
QFuture< QSet< QChar > > set
[10]
QSharedPointer< T > other(t)
[5]
QAction * at
qsizetype index
Definition symbols.h:79
Symbols symbols
Definition symbols.h:76
QSet< QByteArray > excludedSymbols
Definition symbols.h:78
QByteArray expandedMacro
Definition symbols.h:77
QByteArray array
Definition symbols.h:27
SubArray(const char *s)
Definition symbols.h:22
qsizetype len
Definition symbols.h:29
bool operator==(const SubArray &other) const
Definition symbols.h:30
qsizetype from
Definition symbols.h:28
SubArray()=default
SubArray(const QByteArray &a)
Definition symbols.h:21
SubArray(const QByteArray &a, qsizetype from, qsizetype len)
Definition symbols.h:23
qsizetype from
Definition symbols.h:68
Symbol(int lineNum, Token token)
Definition symbols.h:49
Token token
Definition symbols.h:59
bool operator==(const Symbol &o) const
Definition symbols.h:63
int lineNum
Definition symbols.h:58
Symbol()=default
Symbol(int lineNum, Token token, const QByteArray &lexem, qsizetype from, qsizetype len)
Definition symbols.h:54
QByteArray lexem() const
Definition symbols.h:60
QByteArray lex
Definition symbols.h:67
QByteArray unquotedLexem() const
Definition symbols.h:61
Symbol(int lineNum, Token token, const QByteArray &lexem)
Definition symbols.h:50
size_t qHash(const SubArray &key, size_t seed=0)
Definition symbols.h:40
QList< Symbol > Symbols
Definition symbols.h:73