summaryrefslogtreecommitdiffstats
path: root/src/qdoc/tokenizer.h
blob: 77b6bb193eb47fad5eff79113c6af708d09d8d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef TOKENIZER_H
#define TOKENIZER_H

#include "location.h"

#include <QtCore/qfile.h>
#include <QtCore/qstack.h>
#include <QtCore/qstring.h>

QT_BEGIN_NAMESPACE

/*
  Here come the C++ tokens we support.  The first part contains
  all-purpose tokens; then come keywords.

  If you add a keyword, make sure to modify the keyword array in
  tokenizer.cpp as well, and possibly adjust Tok_FirstKeyword and
  Tok_LastKeyword.
*/
enum {
    Tok_Eoi,
    Tok_Ampersand,
    Tok_Aster,
    Tok_Caret,
    Tok_LeftParen,
    Tok_RightParen,
    Tok_LeftParenAster,
    Tok_Equal,
    Tok_LeftBrace,
    Tok_RightBrace,
    Tok_Semicolon,
    Tok_Colon,
    Tok_LeftAngle,
    Tok_RightAngle,
    Tok_Comma,
    Tok_Ellipsis,
    Tok_Gulbrandsen,
    Tok_LeftBracket,
    Tok_RightBracket,
    Tok_Tilde,
    Tok_SomeOperator,
    Tok_Number,
    Tok_String,
    Tok_Doc,
    Tok_Comment,
    Tok_Ident,
    Tok_At,
    Tok_char,
    Tok_class,
    Tok_const,
    Tok_double,
    Tok_int,
    Tok_long,
    Tok_operator,
    Tok_short,
    Tok_signed,
    Tok_typename,
    Tok_unsigned,
    Tok_void,
    Tok_volatile,
    Tok_int64,
    Tok_QPrivateSignal,
    Tok_FirstKeyword = Tok_char,
    Tok_LastKeyword = Tok_QPrivateSignal
};

/*
  The Tokenizer class implements lexical analysis of C++ source
  files.

  Not every operator or keyword of C++ is recognized; only those
  that are interesting to us. Some Qt keywords or macros are also
  recognized.
*/

class Tokenizer
{
public:
    Tokenizer(const Location &loc, QByteArray in);
    Tokenizer(const Location &loc, QFile &file);

    ~Tokenizer();

    int getToken();
    void setParsingFnOrMacro(bool macro) { m_parsingMacro = macro; }

    [[nodiscard]] const Location &location() const { return m_tokLoc; }
    [[nodiscard]] QString previousLexeme() const;
    [[nodiscard]] QString lexeme() const;
    [[nodiscard]] QString version() const { return m_version; }
    [[nodiscard]] int parenDepth() const { return m_parenDepth; }
    [[nodiscard]] int bracketDepth() const { return m_bracketDepth; }

    static void initialize();
    static void terminate();
    static bool isTrue(const QString &condition);

private:
    void init();
    void start(const Location &loc);
    /*
     Represents the maximum amount of characters that can appear in a
     block-comment.

     When a block-comment with more characters than the maximum amount is
     encountered, a warning is issued.
    */
    enum { yyLexBufSize = 524288 };

    int getch() { return m_pos == m_in.size() ? EOF : m_in[m_pos++]; }

    inline int getChar()
    {
        if (m_ch == EOF)
            return EOF;
        if (m_lexLen < yyLexBufSize - 1) {
            m_lex[m_lexLen++] = (char)m_ch;
            m_lex[m_lexLen] = '\0';
        } else if (!token_too_long_warning_was_issued) {
            location().warning(
                u"The content is too long.\n"_qs,
                u"The maximum amount of characters for this content is %1.\n"_qs.arg(yyLexBufSize) +
                "Consider splitting it or reducing its size."
            );

            token_too_long_warning_was_issued = true;
        }
        m_curLoc.advance(QChar(m_ch));
        int ch = getch();
        if (ch == EOF)
            return EOF;
        // cast explicitly to make sure the value of ch
        // is in range [0..255] to avoid assert messages
        // when using debug CRT that checks its input.
        return int(uint(uchar(ch)));
    }

    int getTokenAfterPreprocessor();
    void pushSkipping(bool skip);
    bool popSkipping();

    Location m_tokLoc;
    Location m_curLoc;
    char *m_lexBuf1 { nullptr };
    char *m_lexBuf2 { nullptr };
    char *m_prevLex { nullptr };
    char *m_lex { nullptr };
    size_t m_lexLen {};
    QStack<bool> m_preprocessorSkipping;
    int m_numPreprocessorSkipping {};
    int m_braceDepth {};
    int m_parenDepth {};
    int m_bracketDepth {};
    int m_ch {};

    QString m_version {};
    bool m_parsingMacro {};

    // Used to ensure that the warning that is issued when a token is
    // too long to fit into our fixed sized buffer is not repeated for each
    // character of that token after the last saved one.
    // The flag is reset whenever a new token is requested, so as to allow
    // reporting all such tokens that are too long during a single execution.
    bool token_too_long_warning_was_issued{false};

protected:
    QByteArray m_in {};
    int m_pos {};
};

QT_END_NAMESPACE

#endif