2020-08-09 09:34:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Benoit Lormeau <blormeau@outlook.com>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-09 09:34:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/GenericLexer.h>
|
2020-10-02 13:21:30 +00:00
|
|
|
#include <AK/String.h>
|
2020-09-26 10:21:14 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-08-09 09:34:26 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:21:14 +00:00
|
|
|
/*
|
|
|
|
* 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)
|
2020-08-09 09:34:26 +00:00
|
|
|
{
|
2020-09-27 10:44:03 +00:00
|
|
|
if (!next_is(is_quote))
|
2020-08-09 09:34:26 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
char quote_char = consume();
|
|
|
|
size_t start = m_index;
|
2020-09-26 10:21:14 +00:00
|
|
|
while (!is_eof()) {
|
|
|
|
if (next_is(escape_char))
|
|
|
|
m_index++;
|
|
|
|
else if (next_is(quote_char))
|
|
|
|
break;
|
2020-08-09 09:34:26 +00:00
|
|
|
m_index++;
|
2020-09-26 10:21:14 +00:00
|
|
|
}
|
2020-08-09 09:34:26 +00:00
|
|
|
size_t length = m_index - start;
|
|
|
|
|
|
|
|
if (peek() != quote_char) {
|
2020-09-26 10:21:14 +00:00
|
|
|
// Restore the index in case the string is unterminated
|
|
|
|
m_index = start - 1;
|
2020-08-09 09:34:26 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:21:14 +00:00
|
|
|
// Ignore closing quote
|
2020-08-09 09:34:26 +00:00
|
|
|
ignore();
|
|
|
|
|
|
|
|
return m_input.substring_view(start, length);
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:21:14 +00:00
|
|
|
String GenericLexer::consume_and_unescape_string(char escape_char)
|
|
|
|
{
|
|
|
|
auto view = consume_quoted_string(escape_char);
|
|
|
|
if (view.is_null())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
StringBuilder builder;
|
2020-10-19 06:35:20 +00:00
|
|
|
for (size_t i = 0; i < view.length(); ++i)
|
|
|
|
builder.append(consume_escaped_character(escape_char));
|
2020-09-26 10:21:14 +00:00
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
2020-08-09 09:34:26 +00:00
|
|
|
}
|