aboutsummaryrefslogtreecommitdiffstats
path: root/parser/lexer.h
blob: 874805325ce89bd769854ce35a5c5e912f868bc4 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * This file is part of the API Extractor project.
 *
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 * Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
 *
 * Contact: PySide team <contact@pyside.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */


#ifndef LEXER_H
#define LEXER_H

#include "symbol.h"

#include <QtCore/QString>
#include <cstdlib>
#include <cassert>

struct NameSymbol;
class Lexer;
class Control;

typedef void (Lexer::*scan_fun_ptr)();

class Token
{
public:
    int kind;
    std::size_t position;
    std::size_t size;
    char const *text;

    union {
        const NameSymbol *symbol;
        std::size_t right_brace;
    } extra;
};

class LocationTable
{
private:
    LocationTable(const LocationTable &source);
    void operator = (const LocationTable &source);

public:
    inline LocationTable(std::size_t size = 1024)
            : lines(0),
            line_count(0),
            current_line(0) {
        resize(size);
    }

    inline ~LocationTable() {
        free(lines);
    }

    inline std::size_t size() const {
        return line_count;
    }

    void resize(std::size_t size) {
        Q_ASSERT(size > 0);
        lines = (std::size_t*) ::realloc(lines, sizeof(std::size_t) * size);
        line_count = size;
    }

    void positionAt(std::size_t offset, int *line, int *column) const {
        positionAt(offset, (int) current_line, line, column);
    }

    void positionAt(std::size_t offset, int max_line, int *line, int *column) const;

    inline std::size_t &operator[](int index) {
        return lines[index];
    }

private:
    std::size_t *lines;
    std::size_t line_count;
    std::size_t current_line;

    friend class Lexer;
};

class TokenStream
{
private:
    TokenStream(const TokenStream &);
    void operator = (const TokenStream &);

public:
    inline TokenStream(std::size_t size = 1024)
            : tokens(0),
            index(0),
            token_count(0) {
        resize(size);
    }

    inline ~TokenStream() {
        ::free(tokens);
    }

    inline std::size_t size() const {
        return token_count;
    }

    inline std::size_t cursor() const {
        return index;
    }

    inline void rewind(int i) {
        index = i;
    }

    void resize(std::size_t size) {
        Q_ASSERT(size > 0);
        tokens = (Token*) ::realloc(tokens, sizeof(Token) * size);
        token_count = size;
    }

    inline std::size_t nextToken() {
        return index++;
    }

    inline int lookAhead(std::size_t i = 0) const {
        return tokens[index + i].kind;
    }

    inline int kind(std::size_t i) const {
        return tokens[i].kind;
    }

    inline std::size_t position(std::size_t i) const {
        return tokens[i].position;
    }

    inline const NameSymbol *symbol(std::size_t i) const {
        return tokens[i].extra.symbol;
    }

    inline std::size_t matchingBrace(std::size_t i) const {
        return tokens[i].extra.right_brace;
    }

    inline Token &operator[](int index) {
        return tokens[index];
    }

    inline const Token &token(int index) const {
        return tokens[index];
    }

private:
    Token *tokens;
    std::size_t index;
    std::size_t token_count;

private:
    friend class Lexer;
};

class LocationManager
{
    LocationManager(LocationManager const &__other);
    void operator = (LocationManager const &__other);

public:
    LocationManager(TokenStream &__token_stream,
                    LocationTable &__location_table,
                    LocationTable &__line_table):
            token_stream(__token_stream),
            location_table(__location_table),
            line_table(__line_table) {}

    void positionAt(std::size_t offset, int *line, int *column,
                    QString *filename) const;

    void extract_line(int offset, int *line, QString *filename) const;

    TokenStream &token_stream;
    LocationTable &location_table;
    LocationTable &line_table;
};

class Lexer
{
public:
    Lexer(LocationManager &__location, Control *__control):
            _M_location(__location),
            token_stream(_M_location.token_stream),
            location_table(_M_location.location_table),
            line_table(_M_location.line_table),
            control(__control) {}

    void tokenize(const char *contents, std::size_t size);

    LocationManager &_M_location;
    TokenStream &token_stream;
    LocationTable &location_table;
    LocationTable &line_table;

private:
    void reportError(const QString& msg);

    void initialize_scan_table();
    void scan_newline();
    void scan_white_spaces();
    void scan_identifier_or_keyword();
    void scan_identifier_or_literal();
    void scan_int_constant();
    void scan_char_constant();
    void scan_string_constant();
    void scan_invalid_input();
    void scan_preprocessor();

    // keywords
    void scanKeyword0();
    void scanKeyword2();
    void scanKeyword3();
    void scanKeyword4();
    void scanKeyword5();
    void scanKeyword6();
    void scanKeyword7();
    void scanKeyword8();
    void scanKeyword9();
    void scanKeyword10();
    void scanKeyword11();
    void scanKeyword12();
    void scanKeyword13();
    void scanKeyword14();
    void scanKeyword16();

    // operators
    void scan_not();
    void scan_remainder();
    void scan_and();
    void scan_left_paren();
    void scan_right_paren();
    void scan_star();
    void scan_plus();
    void scan_comma();
    void scan_minus();
    void scan_dot();
    void scan_divide();
    void scan_colon();
    void scan_semicolon();
    void scan_less();
    void scan_equal();
    void scan_greater();
    void scan_question();
    void scan_left_bracket();
    void scan_right_bracket();
    void scan_xor();
    void scan_left_brace();
    void scan_or();
    void scan_right_brace();
    void scan_tilde();
    void scan_EOF();

private:
    Control *control;
    const unsigned char *cursor;
    const unsigned char *begin_buffer;
    const unsigned char *end_buffer;
    std::size_t index;

    static scan_fun_ptr s_scan_table[];
    static scan_fun_ptr s_scan_keyword_table[];
    static bool s_initialized;
};

#endif // LEXER_H

// kate: space-indent on; indent-width 2; replace-tabs on;