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 Parser():index(0), displayWarnings(true), displayNotes(true) {}
18 Symbols symbols;
19 qsizetype index;
20 bool displayWarnings;
21 bool displayNotes;
22
23 struct IncludePath
24 {
25 inline explicit IncludePath(const QByteArray &_path)
26 : path(_path), isFrameworkPath(false) {}
27 QByteArray path;
28 bool isFrameworkPath;
29 };
30 QList<IncludePath> includes;
31
32 std::stack<QByteArray, QByteArrayList> currentFilenames;
33
34 inline bool hasNext() const { return (index < symbols.size()); }
35 inline Token next() { if (index >= symbols.size()) return NOTOKEN; return symbols.at(i: index++).token; }
36 inline Token peek() { if (index >= symbols.size()) return NOTOKEN; return symbols.at(i: index).token; }
37 bool test(Token);
38 void next(Token);
39 void next(Token, const char *msg);
40 inline void prev() {--index;}
41 inline Token lookup(int k = 1);
42 inline const Symbol &symbol_lookup(int k = 1) { return symbols.at(i: index-1+k);}
43 inline Token token() { return symbols.at(i: index-1).token;}
44 inline QByteArray lexem() { return symbols.at(i: index-1).lexem();}
45 inline QByteArray unquotedLexem() { return symbols.at(i: index-1).unquotedLexem();}
46 inline const Symbol &symbol() { return symbols.at(i: index-1);}
47
48 Q_NORETURN void error(const Symbol &symbol);
49 Q_NORETURN void error(const char *msg = nullptr);
50 void warning(const char * = nullptr);
51 void note(const char * = nullptr);
52 void defaultErrorMsg(const Symbol &sym);
53 void printMsg(QByteArrayView formatStringSuffix, QByteArrayView msg, const Symbol &sym);
54
55};
56
57inline bool Parser::test(Token token)
58{
59 if (index < symbols.size() && symbols.at(i: index).token == token) {
60 ++index;
61 return true;
62 }
63 return false;
64}
65
66inline Token Parser::lookup(int k)
67{
68 const qsizetype l = index - 1 + k;
69 return l < symbols.size() ? symbols.at(i: l).token : NOTOKEN;
70}
71
72inline void Parser::next(Token token)
73{
74 if (!test(token))
75 error();
76}
77
78inline void Parser::next(Token token, const char *msg)
79{
80 if (!test(token))
81 error(msg);
82}
83
84QT_END_NAMESPACE
85
86#endif
87

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