mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:14:58 +00:00
AK/GenericLexer: constexpr where possible
Problem: - Much of the `GenericLexer` can be `constexpr`, but is not. Solution: - Make it `constexpr` and de-duplicate code. - Extend some of `StringView` with `constexpr` to support. - Add tests to ensure `constexpr` behavior. Note: - Construction of `StringView` from pointer and length is not `constexpr`-compatible at the moment because the VERIFY cannot be, yet.
This commit is contained in:
parent
c2280a907d
commit
254e010c75
6 changed files with 303 additions and 188 deletions
|
@ -10,92 +10,6 @@
|
|||
#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()
|
||||
{
|
||||
VERIFY(m_index > 0);
|
||||
m_index--;
|
||||
}
|
||||
|
||||
// Consume a character and advance the parser index
|
||||
char GenericLexer::consume()
|
||||
{
|
||||
VERIFY(!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)
|
||||
{
|
||||
|
@ -214,46 +128,4 @@ String GenericLexer::consume_and_unescape_string(char 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue