Prechádzať zdrojové kódy

AK: Add GenericLexer::retreat()

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() == '\\'
Linus Groh 4 rokov pred
rodič
commit
1daa5158eb
2 zmenil súbory, kde vykonal 9 pridanie a 0 odobranie
  1. 7 0
      AK/GenericLexer.cpp
  2. 2 0
      AK/GenericLexer.h

+ 7 - 0
AK/GenericLexer.cpp

@@ -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()
 {

+ 2 - 0
AK/GenericLexer.h

@@ -48,6 +48,8 @@ public:
     bool next_is(StringView) const;
     bool next_is(const char*) const;
 
+    void retreat();
+
     char consume();
     bool consume_specific(char);
     bool consume_specific(StringView);