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
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-18 02:20:04 +00:00
|
|
|
#include <AK/Result.h>
|
2020-08-09 09:34:26 +00:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class GenericLexer {
|
|
|
|
public:
|
2021-11-10 23:55:02 +00:00
|
|
|
constexpr explicit GenericLexer(StringView input)
|
2021-04-22 03:19:39 +00:00
|
|
|
: m_input(input)
|
|
|
|
{
|
|
|
|
}
|
2020-08-09 09:34:26 +00:00
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
constexpr size_t tell() const { return m_index; }
|
|
|
|
constexpr size_t tell_remaining() const { return m_input.length() - m_index; }
|
2020-08-09 09:34:26 +00:00
|
|
|
|
2020-09-25 11:10:36 +00:00
|
|
|
StringView remaining() const { return m_input.substring_view(m_index); }
|
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
constexpr bool is_eof() const { return m_index >= m_input.length(); }
|
|
|
|
|
|
|
|
constexpr char peek(size_t offset = 0) const
|
|
|
|
{
|
|
|
|
return (m_index + offset < m_input.length()) ? m_input[m_index + offset] : '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool next_is(char expected) const
|
|
|
|
{
|
|
|
|
return peek() == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool next_is(StringView expected) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < expected.length(); ++i)
|
|
|
|
if (peek(i) != expected[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr bool next_is(char const* expected) const
|
2021-04-22 03:19:39 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; expected[i] != '\0'; ++i)
|
|
|
|
if (peek(i) != expected[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void retreat()
|
|
|
|
{
|
|
|
|
VERIFY(m_index > 0);
|
|
|
|
--m_index;
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:58:38 +00:00
|
|
|
constexpr void retreat(size_t count)
|
|
|
|
{
|
|
|
|
VERIFY(m_index >= count);
|
|
|
|
m_index -= count;
|
|
|
|
}
|
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
constexpr char consume()
|
|
|
|
{
|
|
|
|
VERIFY(!is_eof());
|
|
|
|
return m_input[m_index++];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr bool consume_specific(const T& next)
|
|
|
|
{
|
|
|
|
if (!next_is(next))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if constexpr (requires { next.length(); }) {
|
|
|
|
ignore(next.length());
|
|
|
|
} else {
|
|
|
|
ignore(sizeof(next));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:32:59 +00:00
|
|
|
#ifndef KERNEL
|
2022-04-01 17:58:27 +00:00
|
|
|
bool consume_specific(String const& next)
|
2021-04-22 03:19:39 +00:00
|
|
|
{
|
|
|
|
return consume_specific(StringView { next });
|
|
|
|
}
|
2022-02-15 21:32:59 +00:00
|
|
|
#endif
|
2021-04-22 03:19:39 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr bool consume_specific(char const* next)
|
2021-04-22 03:19:39 +00:00
|
|
|
{
|
|
|
|
return consume_specific(StringView { next });
|
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
constexpr char consume_escaped_character(char escape_char = '\\', StringView escape_map = "n\nr\rt\tb\bf\f")
|
2021-04-22 03:19:39 +00:00
|
|
|
{
|
|
|
|
if (!consume_specific(escape_char))
|
|
|
|
return consume();
|
2020-08-09 09:34:26 +00:00
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
auto c = consume();
|
2020-08-09 09:34:26 +00:00
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
for (size_t i = 0; i < escape_map.length(); i += 2) {
|
|
|
|
if (c == escape_map[i])
|
|
|
|
return escape_map[i + 1];
|
|
|
|
}
|
2020-08-09 09:34:26 +00:00
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
return c;
|
|
|
|
}
|
2020-10-28 09:29:03 +00:00
|
|
|
|
2020-08-09 09:34:26 +00:00
|
|
|
StringView consume(size_t count);
|
|
|
|
StringView consume_all();
|
|
|
|
StringView consume_line();
|
2020-08-21 13:51:57 +00:00
|
|
|
StringView consume_until(char);
|
2022-04-01 17:58:27 +00:00
|
|
|
StringView consume_until(char const*);
|
2022-01-24 21:25:53 +00:00
|
|
|
StringView consume_until(StringView);
|
2020-09-26 10:21:14 +00:00
|
|
|
StringView consume_quoted_string(char escape_char = 0);
|
2022-02-15 21:32:59 +00:00
|
|
|
#ifndef KERNEL
|
2020-09-26 10:21:14 +00:00
|
|
|
String consume_and_unescape_string(char escape_char = '\\');
|
2022-02-15 21:32:59 +00:00
|
|
|
#endif
|
2020-08-09 09:34:26 +00:00
|
|
|
|
2021-08-18 02:20:04 +00:00
|
|
|
enum class UnicodeEscapeError {
|
|
|
|
MalformedUnicodeEscape,
|
|
|
|
UnicodeEscapeOverflow,
|
|
|
|
};
|
|
|
|
|
2022-02-15 21:32:59 +00:00
|
|
|
#ifndef KERNEL
|
2021-08-18 02:20:04 +00:00
|
|
|
Result<u32, UnicodeEscapeError> consume_escaped_code_point(bool combine_surrogate_pairs = true);
|
2022-02-15 21:32:59 +00:00
|
|
|
#endif
|
2021-08-18 02:20:04 +00:00
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
constexpr void ignore(size_t count = 1)
|
|
|
|
{
|
|
|
|
count = min(count, m_input.length() - m_index);
|
|
|
|
m_index += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void ignore_until(char stop)
|
|
|
|
{
|
|
|
|
while (!is_eof() && peek() != stop) {
|
|
|
|
++m_index;
|
|
|
|
}
|
|
|
|
ignore();
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr void ignore_until(char const* stop)
|
2021-04-22 03:19:39 +00:00
|
|
|
{
|
|
|
|
while (!is_eof() && !next_is(stop)) {
|
|
|
|
++m_index;
|
|
|
|
}
|
|
|
|
ignore(__builtin_strlen(stop));
|
|
|
|
}
|
2020-09-27 10:20:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Conditions are used to match arbitrary characters. You can use lambdas,
|
|
|
|
* ctype functions, or is_any_of() and its derivatives (see below).
|
|
|
|
* A few examples:
|
|
|
|
* - `if (lexer.next_is(isdigit))`
|
|
|
|
* - `auto name = lexer.consume_while([](char c) { return isalnum(c) || c == '_'; });`
|
|
|
|
* - `lexer.ignore_until(is_any_of("<^>"));`
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Test the next character against a Condition
|
2021-04-22 03:19:39 +00:00
|
|
|
template<typename TPredicate>
|
|
|
|
constexpr bool next_is(TPredicate pred) const
|
2020-09-27 10:20:08 +00:00
|
|
|
{
|
2021-04-22 03:19:39 +00:00
|
|
|
return pred(peek());
|
2020-09-27 10:20:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
// Consume and return characters while `pred` returns true
|
|
|
|
template<typename TPredicate>
|
|
|
|
StringView consume_while(TPredicate pred)
|
2020-09-27 10:20:08 +00:00
|
|
|
{
|
|
|
|
size_t start = m_index;
|
2021-04-22 03:19:39 +00:00
|
|
|
while (!is_eof() && pred(peek()))
|
|
|
|
++m_index;
|
2020-09-27 10:20:08 +00:00
|
|
|
size_t length = m_index - start;
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return {};
|
|
|
|
return m_input.substring_view(start, length);
|
|
|
|
}
|
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
// Consume and return characters until `pred` return true
|
|
|
|
template<typename TPredicate>
|
|
|
|
StringView consume_until(TPredicate pred)
|
2020-09-27 10:20:08 +00:00
|
|
|
{
|
|
|
|
size_t start = m_index;
|
2021-04-22 03:19:39 +00:00
|
|
|
while (!is_eof() && !pred(peek()))
|
|
|
|
++m_index;
|
2020-09-27 10:20:08 +00:00
|
|
|
size_t length = m_index - start;
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return {};
|
|
|
|
return m_input.substring_view(start, length);
|
|
|
|
}
|
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
// Ignore characters while `pred` returns true
|
|
|
|
template<typename TPredicate>
|
|
|
|
constexpr void ignore_while(TPredicate pred)
|
2020-09-27 10:20:08 +00:00
|
|
|
{
|
2021-04-22 03:19:39 +00:00
|
|
|
while (!is_eof() && pred(peek()))
|
|
|
|
++m_index;
|
2020-09-27 10:20:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
// Ignore characters until `pred` return true
|
2020-09-27 10:20:08 +00:00
|
|
|
// We don't skip the stop character as it may not be a unique value
|
2021-04-22 03:19:39 +00:00
|
|
|
template<typename TPredicate>
|
|
|
|
constexpr void ignore_until(TPredicate pred)
|
2020-09-27 10:20:08 +00:00
|
|
|
{
|
2021-04-22 03:19:39 +00:00
|
|
|
while (!is_eof() && !pred(peek()))
|
|
|
|
++m_index;
|
2020-09-27 10:20:08 +00:00
|
|
|
}
|
2020-08-09 09:34:26 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
StringView m_input;
|
|
|
|
size_t m_index { 0 };
|
2021-08-18 02:20:04 +00:00
|
|
|
|
|
|
|
private:
|
2022-02-15 21:32:59 +00:00
|
|
|
#ifndef KERNEL
|
2021-08-18 02:20:04 +00:00
|
|
|
Result<u32, UnicodeEscapeError> decode_code_point();
|
|
|
|
Result<u32, UnicodeEscapeError> decode_single_or_paired_surrogate(bool combine_surrogate_pairs);
|
2022-02-15 21:32:59 +00:00
|
|
|
#endif
|
2020-08-09 09:34:26 +00:00
|
|
|
};
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
constexpr auto is_any_of(StringView values)
|
2020-08-31 14:54:27 +00:00
|
|
|
{
|
|
|
|
return [values](auto c) { return values.contains(c); };
|
|
|
|
}
|
|
|
|
|
2022-03-26 21:27:04 +00:00
|
|
|
constexpr auto is_not_any_of(StringView values)
|
|
|
|
{
|
|
|
|
return [values](auto c) { return !values.contains(c); };
|
|
|
|
}
|
|
|
|
|
2020-09-27 10:44:03 +00:00
|
|
|
constexpr auto is_path_separator = is_any_of("/\\");
|
|
|
|
constexpr auto is_quote = is_any_of("'\"");
|
2020-08-09 09:34:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::GenericLexer;
|
2020-08-31 14:54:27 +00:00
|
|
|
using AK::is_any_of;
|
2020-08-09 09:34:26 +00:00
|
|
|
using AK::is_path_separator;
|
|
|
|
using AK::is_quote;
|