From 2408be335d48102cb4baa85814af2e471e3e3aaf Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 20 Jul 2022 23:34:31 +0200 Subject: [PATCH] Util: Rename two functions to be more in line with GenericLexer --- src/util/json/parser.cpp | 50 +++++++++++++++++++--------------------- src/util/json/parser.h | 5 ++-- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/util/json/parser.cpp b/src/util/json/parser.cpp index f84c61d..274f948 100644 --- a/src/util/json/parser.cpp +++ b/src/util/json/parser.cpp @@ -72,7 +72,7 @@ Value Parser::parse() break; } - if (!reachedEnd()) { + if (!isEOF()) { m_job->printErrorLine(peek(), "multiple root elements"); } @@ -81,33 +81,28 @@ Value Parser::parse() // ----------------------------------------- -bool Parser::reachedEnd() +bool Parser::isEOF() { return m_index >= m_tokens->size(); } -bool Parser::seekForward(Token::Type type) +Token Parser::peek() { - for (; !reachedEnd(); ++m_index) { - if (peek().type == type) { - m_index++; - return true; - } - } - - return false; + assert(!isEOF()); + return (*m_tokens)[m_index]; } -Token Parser::peek() +Token Parser::consume() { - return m_tokens->at(m_index); + assert(!isEOF()); + return (*m_tokens)[m_index++]; } -Token Parser::consume() +void Parser::ignoreUntil(Token::Type type) { - Token token = peek(); - m_index++; - return token; + while (!isEOF() && peek().type != type) { + ++m_index; + } } Value Parser::consumeLiteral() @@ -313,14 +308,15 @@ Value Parser::consumeArray() m_job->printErrorLine(token, message.c_str()); // After an error, try to find the closing bracket - seekForward(Token::Type::BracketClose); + ignoreUntil(Token::Type::BracketClose); + m_index++; }; Value array = Value::Type::Array; Token token; for (;;) { // EOF - if (reachedEnd()) { + if (isEOF()) { reportError(m_tokens->at(m_index - 1), "expecting closing ']' at end"); break; } @@ -354,7 +350,7 @@ Value Parser::consumeArray() } // EOF - if (reachedEnd()) { + if (isEOF()) { reportError(token, "expecting closing ']' at end"); break; } @@ -384,7 +380,8 @@ Value Parser::consumeObject() m_job->printErrorLine(token, message.c_str()); // After an error, try to find the closing brace - seekForward(Token::Type::BraceClose); + ignoreUntil(Token::Type::BraceClose); + m_index++; }; Value object = Value::Type::Object; @@ -393,7 +390,7 @@ Value Parser::consumeObject() std::map unique; for (;;) { // EOF - if (reachedEnd()) { + if (isEOF()) { reportError(m_tokens->at(m_index - 1), "expecting closing '}' at end"); break; } @@ -416,7 +413,8 @@ Value Parser::consumeObject() m_index--; Value tmpName = consumeString(); if (tmpName.m_type != Value::Type::String) { - seekForward(Token::Type::BraceClose); + ignoreUntil(Token::Type::BraceClose); + m_index++; break; } @@ -430,7 +428,7 @@ Value Parser::consumeObject() unique.insert({ name, 0 }); // EOF - if (reachedEnd()) { + if (isEOF()) { reportError(token, "expecting colon, not 'EOF'"); reportError(token, "expecting closing '}' at end"); break; @@ -444,7 +442,7 @@ Value Parser::consumeObject() } // EOF - if (reachedEnd()) { + if (isEOF()) { reportError(token, "expecting value, not 'EOF'"); reportError(token, "expecting closing '}' at end"); break; @@ -473,7 +471,7 @@ Value Parser::consumeObject() } // EOF - if (reachedEnd()) { + if (isEOF()) { reportError(token, "expecting closing '}' at end"); break; } diff --git a/src/util/json/parser.h b/src/util/json/parser.h index 1f9d943..88d4f69 100644 --- a/src/util/json/parser.h +++ b/src/util/json/parser.h @@ -25,11 +25,10 @@ public: Value parse(); private: - bool reachedEnd(); - bool seekForward(Token::Type type); - + bool isEOF(); Token peek(); Token consume(); + void ignoreUntil(Token::Type type); Value consumeLiteral(); Value consumeNumber();