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
|
/*
* Copyright (c) 2020, Benoit Lormeau <blormeau@outlook.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Assertions.h>
#include <AK/GenericLexer.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
namespace AK {
GenericLexer::GenericLexer(const StringView& input)
: m_input(input)
{
}
GenericLexer::~GenericLexer()
{
}
// Tells whether the parser's index has reached input's end
bool GenericLexer::is_eof() const
{
return m_index >= m_input.length();
}
// Returns the current character at the parser index, plus `offset` if specified
char GenericLexer::peek(size_t offset) const
{
return (m_index + offset < m_input.length()) ? m_input[m_index + offset] : '\0';
}
// Tests the next character in the input
bool GenericLexer::next_is(char expected) const
{
return peek() == expected;
}
// Tests if the `expected` string comes next in the input
bool GenericLexer::next_is(StringView expected) const
{
for (size_t i = 0; i < expected.length(); ++i)
if (peek(i) != expected[i])
return false;
return true;
}
// Tests if the `expected` string comes next in the input
bool GenericLexer::next_is(const char* expected) const
{
for (size_t i = 0; expected[i] != '\0'; ++i)
if (peek(i) != expected[i])
return false;
return true;
}
// Go back to the previous character
void GenericLexer::retreat()
{
ASSERT(m_index > 0);
m_index--;
}
// Consume a character and advance the parser index
char GenericLexer::consume()
{
ASSERT(!is_eof());
return m_input[m_index++];
}
// Consume the given character if it is next in the input
bool GenericLexer::consume_specific(char specific)
{
if (peek() != specific)
return false;
ignore();
return true;
}
// Consume the given string if it is next in the input
bool GenericLexer::consume_specific(StringView str)
{
if (!next_is(str))
return false;
ignore(str.length());
return true;
}
// Consume the given string if it is next in the input
bool GenericLexer::consume_specific(const char* str)
{
return consume_specific(StringView(str));
}
// Consume a number of characters
StringView GenericLexer::consume(size_t count)
{
if (count == 0)
return {};
size_t start = m_index;
size_t length = min(count, m_input.length() - m_index);
m_index += length;
return m_input.substring_view(start, length);
}
// Consume the rest of the input
StringView GenericLexer::consume_all()
{
if (is_eof())
return {};
auto rest = m_input.substring_view(m_index, m_input.length() - m_index);
m_index = m_input.length();
return rest;
}
// Consume until a new line is found
StringView GenericLexer::consume_line()
{
size_t start = m_index;
while (!is_eof() && peek() != '\r' && peek() != '\n')
m_index++;
size_t length = m_index - start;
consume_specific('\r');
consume_specific('\n');
if (length == 0)
return {};
return m_input.substring_view(start, length);
}
// Consume and return characters until `stop` is peek'd
// The `stop` character is ignored, as it is user-defined
StringView GenericLexer::consume_until(char stop)
{
size_t start = m_index;
while (!is_eof() && peek() != stop)
m_index++;
size_t length = m_index - start;
ignore();
if (length == 0)
return {};
return m_input.substring_view(start, length);
}
// Consume and return characters until the string `stop` is found
// The `stop` string is ignored, as it is user-defined
StringView GenericLexer::consume_until(const char* stop)
{
size_t start = m_index;
while (!is_eof() && !next_is(stop))
m_index++;
size_t length = m_index - start;
ignore(__builtin_strlen(stop));
if (length == 0)
return {};
return m_input.substring_view(start, length);
}
/*
* Consume a string surrounded by single or double quotes. The returned
* StringView does not include the quotes. An escape character can be provided
* to capture the enclosing quotes. Please note that the escape character will
* still be in the resulting StringView
*/
StringView GenericLexer::consume_quoted_string(char escape_char)
{
if (!next_is(is_quote))
return {};
char quote_char = consume();
size_t start = m_index;
while (!is_eof()) {
if (next_is(escape_char))
m_index++;
else if (next_is(quote_char))
break;
m_index++;
}
size_t length = m_index - start;
if (peek() != quote_char) {
// Restore the index in case the string is unterminated
m_index = start - 1;
return {};
}
// Ignore closing quote
ignore();
return m_input.substring_view(start, length);
}
String GenericLexer::consume_and_unescape_string(char escape_char)
{
auto view = consume_quoted_string(escape_char);
if (view.is_null())
return {};
StringBuilder builder;
for (size_t i = 0; i < view.length(); ++i)
builder.append(consume_escaped_character(escape_char));
return builder.to_string();
}
char GenericLexer::consume_escaped_character(char escape_char, const StringView& escape_map)
{
if (!consume_specific(escape_char))
return consume();
auto c = consume();
for (size_t i = 0; i < escape_map.length(); i += 2) {
if (c == escape_map[i])
return escape_map[i + 1];
}
return c;
}
// Ignore a number of characters (1 by default)
void GenericLexer::ignore(size_t count)
{
count = min(count, m_input.length() - m_index);
m_index += count;
}
// Ignore characters until `stop` is peek'd
// The `stop` character is ignored as it is user-defined
void GenericLexer::ignore_until(char stop)
{
while (!is_eof() && peek() != stop)
m_index++;
ignore();
}
// Ignore characters until the string `stop` is found
// The `stop` string is ignored, as it is user-defined
void GenericLexer::ignore_until(const char* stop)
{
while (!is_eof() && !next_is(stop))
m_index++;
ignore(__builtin_strlen(stop));
}
}
|