Skip to content

Commit

Permalink
AK: Add GenericLexer::retreat()
Browse files Browse the repository at this point in the history
This allows going back one character at a time, and then re-consume
previously consumed chars.
The code I need this for looks something like this:

    ASSERT(lexer.consume_specific('\\'));
    if (lexer.next_is("foo"))
        ...
    lexer.retreat();
    lexer.consume_escaped_character();  // This expects lexer.peek() == '\\'
  • Loading branch information
linusg authored and awesomekling committed Oct 29, 2020
1 parent ffd1e48 commit 1daa515
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions AK/GenericLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ bool GenericLexer::next_is(const char* expected) const
return true;
}

// Go back to the previous character
void GenericLexer::retreat()
{
ASSERT(m_index > 0);
m_index--;
}

// Consume a character and advance the parser index
char GenericLexer::consume()
{
Expand Down
2 changes: 2 additions & 0 deletions AK/GenericLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class GenericLexer {
bool next_is(StringView) const;
bool next_is(const char*) const;

void retreat();

char consume();
bool consume_specific(char);
bool consume_specific(StringView);
Expand Down

0 comments on commit 1daa515

Please sign in to comment.