aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/nim/suggest/sexprlexer.h
blob: 6827f0a97c7d5d0bf8e39db25e4fbf20c0d815d6 (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
/****************************************************************************
**
** Copyright (C) Filippo Cucchetto <filippocucchetto@gmail.com>
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** 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.
**
****************************************************************************/

#pragma once

#include <cctype>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>

namespace Nim {

struct SExprLexer {
    enum Result {
        Finished,
        TokenAvailable,
        Error
    };

    enum TokenType : std::size_t {
        STRING,
        NUMBER,
        IDENTIFIER,
        OPEN_BRACE,
        CLOSE_BRACE,
    };

    struct Token {
        TokenType type;
        std::size_t start;
        std::size_t end;
    };

    SExprLexer(const char *data, std::size_t length)
        : m_data(data)
        , m_dataLength(length)
        , m_pos(0)
    {}

    SExprLexer(const char *data)
        : SExprLexer(data, ::strlen(data))
    {}

    SExprLexer(const std::string &data)
        : SExprLexer(data.c_str(), data.length())
    {}

    const char *data() const
    {
        return m_data;
    }

    std::size_t dataLength() const
    {
        return m_dataLength;
    }

    static size_t tokenLength(Token &token)
    {
        return token.end - token.start + 1;
    }

    std::string tokenValue(Token &token) const
    {
        return std::string(m_data + token.start, tokenLength(token));
    }

    Result next(Token &token)
    {
        while (m_pos < m_dataLength) {
            if (m_data[m_pos] == '(') {
                token = Token{OPEN_BRACE, m_pos, m_pos + 1};
                m_pos++;
                return TokenAvailable;
            } else if (m_data[m_pos] == ')') {
                token = Token{CLOSE_BRACE, m_pos, m_pos + 1};
                m_pos++;
                return TokenAvailable;
            } else if (m_data[m_pos] == '"') {
                Token token_tmp {STRING, m_pos, m_pos};
                char previous = '"';
                m_pos++;
                while (true) {
                    if (m_pos >= m_dataLength)
                        return Error;
                    if (m_data[m_pos] == '"' && previous != '\\')
                        break;
                    previous = m_data[m_pos];
                    m_pos++;
                }
                token_tmp.end = m_pos;
                token = std::move(token_tmp);
                m_pos++;
                return TokenAvailable;
            } else if (std::isdigit(m_data[m_pos])) {
                bool decimal_separator_found = false;
                token = {NUMBER, m_pos, m_pos};
                m_pos++;
                while (true) {
                    if (m_pos >= m_dataLength)
                        break;
                    if (m_data[m_pos] == ',' || m_data[m_pos] == '.') {
                        if (decimal_separator_found)
                            return Error;
                        decimal_separator_found = true;
                    } else if (!std::isdigit(m_data[m_pos]))
                        break;
                    m_pos++;
                }
                token.end = m_pos - 1;
                return TokenAvailable;
            } else if (!std::isspace(m_data[m_pos])) {
                token = {IDENTIFIER, m_pos, m_pos};
                m_pos++;
                while (true) {
                    if (m_pos >= m_dataLength)
                        break;
                    if (std::isspace(m_data[m_pos]) || m_data[m_pos] == '(' || m_data[m_pos] == ')')
                        break;
                    m_pos++;
                }
                token.end = m_pos - 1;
                return TokenAvailable;
            } else {
                m_pos++;
            }
        }
        return Finished;
    }

    static std::vector<Token> parse(const std::string &data)
    {
        return parse(data.data(), data.size());
    }

    static std::vector<Token> parse(const char *data)
    {
        return parse(data, ::strlen(data));
    }

    static std::vector<Token> parse(const char *data, std::size_t data_size)
    {
        std::vector<Token> result;
        SExprLexer lexer(data, data_size);
        Token token {};
        while (lexer.next(token)) {
            result.push_back(token);
            token = {};
        }
        return result;
    }

private:
    const char *m_data = nullptr;
    std::size_t m_dataLength = 0;
    std::size_t m_pos = 0;
};

} // namespace Nim