Browse Source

Util: Add two functions to GenericLexer

- tellRemaining
- consumeSpecific
master
Riyyi 2 years ago
parent
commit
9b3489676e
  1. 19
      src/util/genericlexer.cpp
  2. 2
      src/util/genericlexer.h

19
src/util/genericlexer.cpp

@ -26,6 +26,11 @@ size_t GenericLexer::tell() const
return m_index;
}
size_t GenericLexer::tellRemaining() const
{
return m_input.length() - m_index;
}
bool GenericLexer::isEOF() const
{
return m_index >= m_input.length();
@ -33,7 +38,9 @@ bool GenericLexer::isEOF() const
char GenericLexer::peek(size_t offset) const
{
return m_input[std::max(std::min(m_index + offset, m_input.length()), (size_t)0)];
return (m_index + offset >= 0 && m_index + offset < m_input.length())
? m_input[m_index + offset]
: '\0';
}
void GenericLexer::ignore(size_t count)
@ -52,4 +59,14 @@ char GenericLexer::consume()
return m_input[m_index++];
}
bool GenericLexer::consumeSpecific(const char& character)
{
if (peek() != character) {
return false;
}
ignore();
return true;
}
} // namespace Util

2
src/util/genericlexer.h

@ -21,6 +21,7 @@ public:
// Position
size_t tell() const;
size_t tellRemaining() const;
bool isEOF() const;
// Access
@ -32,6 +33,7 @@ public:
void ignore(size_t count = 1);
void retreat(size_t count = 1);
char consume();
bool consumeSpecific(const char& character);
protected:
size_t m_index { 0 };

Loading…
Cancel
Save