Util: Add GenericLexer class
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Riyyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algorithm> // max, min
|
||||||
|
|
||||||
|
#include "util/genericlexer.h"
|
||||||
|
|
||||||
|
namespace Util {
|
||||||
|
|
||||||
|
GenericLexer::GenericLexer(std::string_view input)
|
||||||
|
: m_input(input)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
GenericLexer::~GenericLexer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------
|
||||||
|
|
||||||
|
size_t GenericLexer::tell() const
|
||||||
|
{
|
||||||
|
return m_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GenericLexer::isEOF() const
|
||||||
|
{
|
||||||
|
return m_index >= m_input.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
char GenericLexer::peek(size_t offset) const
|
||||||
|
{
|
||||||
|
return m_input[std::max(std::min(m_index + offset, m_input.length()), (size_t)0)];
|
||||||
|
}
|
||||||
|
|
||||||
|
void GenericLexer::ignore(size_t count)
|
||||||
|
{
|
||||||
|
m_index += std::min(count, m_input.length() - m_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GenericLexer::retreat(size_t count)
|
||||||
|
{
|
||||||
|
m_index -= std::min(count, m_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
char GenericLexer::consume()
|
||||||
|
{
|
||||||
|
assert(!isEOF());
|
||||||
|
return m_input[m_index++];
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Util
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Riyyi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UTIL_GENERIC_LEXER_H
|
||||||
|
#define UTIL_GENERIC_LEXER_H
|
||||||
|
|
||||||
|
#include <cassert> // assert
|
||||||
|
#include <cstddef> // size_t
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace Util {
|
||||||
|
|
||||||
|
class GenericLexer {
|
||||||
|
public:
|
||||||
|
GenericLexer(std::string_view input);
|
||||||
|
virtual ~GenericLexer();
|
||||||
|
|
||||||
|
// Position
|
||||||
|
|
||||||
|
size_t tell() const;
|
||||||
|
bool isEOF() const;
|
||||||
|
|
||||||
|
// Access
|
||||||
|
|
||||||
|
char peek(size_t offset = 0) const;
|
||||||
|
|
||||||
|
// Modifiers
|
||||||
|
|
||||||
|
void ignore(size_t count = 1);
|
||||||
|
void retreat(size_t count = 1);
|
||||||
|
char consume();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
size_t m_index { 0 };
|
||||||
|
std::string_view m_input;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Util
|
||||||
|
|
||||||
|
#endif // UTIL_GENERIC_LEXER_H
|
||||||
Reference in New Issue
Block a user