1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the tools applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef PARSER_H |
30 | #define PARSER_H |
31 | |
32 | #include "symbols.h" |
33 | |
34 | #include <stack> |
35 | |
36 | QT_BEGIN_NAMESPACE |
37 | |
38 | class Parser |
39 | { |
40 | public: |
41 | Parser():index(0), displayWarnings(true), displayNotes(true) {} |
42 | Symbols symbols; |
43 | int index; |
44 | bool displayWarnings; |
45 | bool displayNotes; |
46 | |
47 | struct IncludePath |
48 | { |
49 | inline explicit IncludePath(const QByteArray &_path) |
50 | : path(_path), isFrameworkPath(false) {} |
51 | QByteArray path; |
52 | bool isFrameworkPath; |
53 | }; |
54 | QList<IncludePath> includes; |
55 | |
56 | std::stack<QByteArray, QByteArrayList> currentFilenames; |
57 | |
58 | inline bool hasNext() const { return (index < symbols.size()); } |
59 | inline Token next() { if (index >= symbols.size()) return NOTOKEN; return symbols.at(index++).token; } |
60 | inline Token peek() { if (index >= symbols.size()) return NOTOKEN; return symbols.at(index).token; } |
61 | bool test(Token); |
62 | void next(Token); |
63 | void next(Token, const char *msg); |
64 | inline void prev() {--index;} |
65 | inline Token lookup(int k = 1); |
66 | inline const Symbol &symbol_lookup(int k = 1) { return symbols.at(index-1+k);} |
67 | inline Token token() { return symbols.at(index-1).token;} |
68 | inline QByteArray lexem() { return symbols.at(index-1).lexem();} |
69 | inline QByteArray unquotedLexem() { return symbols.at(index-1).unquotedLexem();} |
70 | inline const Symbol &symbol() { return symbols.at(index-1);} |
71 | |
72 | Q_NORETURN void error(int rollback); |
73 | Q_NORETURN void error(const char *msg = nullptr); |
74 | void warning(const char * = nullptr); |
75 | void note(const char * = nullptr); |
76 | |
77 | }; |
78 | |
79 | inline bool Parser::test(Token token) |
80 | { |
81 | if (index < symbols.size() && symbols.at(index).token == token) { |
82 | ++index; |
83 | return true; |
84 | } |
85 | return false; |
86 | } |
87 | |
88 | inline Token Parser::lookup(int k) |
89 | { |
90 | const int l = index - 1 + k; |
91 | return l < symbols.size() ? symbols.at(l).token : NOTOKEN; |
92 | } |
93 | |
94 | inline void Parser::next(Token token) |
95 | { |
96 | if (!test(token)) |
97 | error(); |
98 | } |
99 | |
100 | inline void Parser::next(Token token, const char *msg) |
101 | { |
102 | if (!test(token)) |
103 | error(msg); |
104 | } |
105 | |
106 | QT_END_NAMESPACE |
107 | |
108 | #endif |
109 |