1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef PARSER_H
5#define PARSER_H
6
7#include "symbols.h"
8#include <QtCore/qbytearrayview.h>
9
10#include <stack>
11
12QT_BEGIN_NAMESPACE
13
14class Parser
15{
16public:
17 Symbols symbols;
18 qsizetype index = 0;
19 bool displayWarnings = true;
20 bool displayNotes = true;
21 bool activeQtMode = false;
22 bool qmlMacroWarningIsFatal = false;
23
24 struct IncludePath
25 {
26 inline explicit IncludePath(const QByteArray &_path)
27 : path(_path), isFrameworkPath(false) {}
28 QByteArray path;
29 bool isFrameworkPath;
30 };
31 QList<IncludePath> includes;
32
33 std::stack<QByteArray, QByteArrayList> currentFilenames;
34
35 inline bool hasNext() const { return (index < symbols.size()); }
36 inline Token next() { if (index >= symbols.size()) return NOTOKEN; return symbols.at(i: index++).token; }
37 inline Token peek() { if (index >= symbols.size()) return NOTOKEN; return symbols.at(i: index).token; }
38 bool test(Token);
39 void next(Token);
40 void next(Token, const char *msg);
41 inline void prev() {--index;}
42 inline Token lookup(int k = 1);
43 inline const Symbol &symbol_lookup(int k = 1) { return symbols.at(i: index-1+k);}
44 inline Token token() { return symbols.at(i: index-1).token;}
45 inline QByteArray lexem() { return symbols.at(i: index-1).lexem();}
46 inline QByteArray unquotedLexem() { return symbols.at(i: index-1).unquotedLexem();}
47 inline QByteArrayView lexemView() { return symbols.at(i: index-1).lexemView();}
48 inline QByteArrayView unquotedLexemView() { return symbols.at(i: index-1).unquotedLexemView();}
49 inline const Symbol &symbol() { return symbols.at(i: index-1);}
50 inline const Symbol &symbolAt(qsizetype idx) { return symbols.at(i: idx); }
51
52 Q_NORETURN void error(const Symbol &symbol);
53 Q_NORETURN void error(const char *msg = nullptr);
54 Q_NORETURN void error(const Symbol &symbol, const char *msg);
55 void warning(const char * = nullptr);
56 void warning(const Symbol &sym, QByteArrayView msg);
57 void note(const char * = nullptr);
58 void defaultErrorMsg(const Symbol &sym);
59 void printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym);
60
61};
62
63inline bool Parser::test(Token token)
64{
65 if (index < symbols.size() && symbols.at(i: index).token == token) {
66 ++index;
67 return true;
68 }
69 return false;
70}
71
72inline Token Parser::lookup(int k)
73{
74 const qsizetype l = index - 1 + k;
75 return l < symbols.size() ? symbols.at(i: l).token : NOTOKEN;
76}
77
78inline void Parser::next(Token token)
79{
80 if (!test(token))
81 error();
82}
83
84inline void Parser::next(Token token, const char *msg)
85{
86 if (!test(token))
87 error(msg);
88}
89
90QT_END_NAMESPACE
91
92#endif
93

source code of qtbase/src/tools/moc/parser.h