Util: Rename two functions to be more in line with GenericLexer

This commit is contained in:
Riyyi
2022-07-20 23:34:31 +02:00
parent 42143fed71
commit 2408be335d
2 changed files with 27 additions and 30 deletions
+25 -27
View File
@@ -72,7 +72,7 @@ Value Parser::parse()
break; break;
} }
if (!reachedEnd()) { if (!isEOF()) {
m_job->printErrorLine(peek(), "multiple root elements"); 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(); return m_index >= m_tokens->size();
} }
bool Parser::seekForward(Token::Type type)
{
for (; !reachedEnd(); ++m_index) {
if (peek().type == type) {
m_index++;
return true;
}
}
return false;
}
Token Parser::peek() Token Parser::peek()
{ {
return m_tokens->at(m_index); assert(!isEOF());
return (*m_tokens)[m_index];
} }
Token Parser::consume() Token Parser::consume()
{ {
Token token = peek(); assert(!isEOF());
m_index++; return (*m_tokens)[m_index++];
return token; }
void Parser::ignoreUntil(Token::Type type)
{
while (!isEOF() && peek().type != type) {
++m_index;
}
} }
Value Parser::consumeLiteral() Value Parser::consumeLiteral()
@@ -313,14 +308,15 @@ Value Parser::consumeArray()
m_job->printErrorLine(token, message.c_str()); m_job->printErrorLine(token, message.c_str());
// After an error, try to find the closing bracket // After an error, try to find the closing bracket
seekForward(Token::Type::BracketClose); ignoreUntil(Token::Type::BracketClose);
m_index++;
}; };
Value array = Value::Type::Array; Value array = Value::Type::Array;
Token token; Token token;
for (;;) { for (;;) {
// EOF // EOF
if (reachedEnd()) { if (isEOF()) {
reportError(m_tokens->at(m_index - 1), "expecting closing ']' at end"); reportError(m_tokens->at(m_index - 1), "expecting closing ']' at end");
break; break;
} }
@@ -354,7 +350,7 @@ Value Parser::consumeArray()
} }
// EOF // EOF
if (reachedEnd()) { if (isEOF()) {
reportError(token, "expecting closing ']' at end"); reportError(token, "expecting closing ']' at end");
break; break;
} }
@@ -384,7 +380,8 @@ Value Parser::consumeObject()
m_job->printErrorLine(token, message.c_str()); m_job->printErrorLine(token, message.c_str());
// After an error, try to find the closing brace // After an error, try to find the closing brace
seekForward(Token::Type::BraceClose); ignoreUntil(Token::Type::BraceClose);
m_index++;
}; };
Value object = Value::Type::Object; Value object = Value::Type::Object;
@@ -393,7 +390,7 @@ Value Parser::consumeObject()
std::map<std::string, uint8_t> unique; std::map<std::string, uint8_t> unique;
for (;;) { for (;;) {
// EOF // EOF
if (reachedEnd()) { if (isEOF()) {
reportError(m_tokens->at(m_index - 1), "expecting closing '}' at end"); reportError(m_tokens->at(m_index - 1), "expecting closing '}' at end");
break; break;
} }
@@ -416,7 +413,8 @@ Value Parser::consumeObject()
m_index--; m_index--;
Value tmpName = consumeString(); Value tmpName = consumeString();
if (tmpName.m_type != Value::Type::String) { if (tmpName.m_type != Value::Type::String) {
seekForward(Token::Type::BraceClose); ignoreUntil(Token::Type::BraceClose);
m_index++;
break; break;
} }
@@ -430,7 +428,7 @@ Value Parser::consumeObject()
unique.insert({ name, 0 }); unique.insert({ name, 0 });
// EOF // EOF
if (reachedEnd()) { if (isEOF()) {
reportError(token, "expecting colon, not 'EOF'"); reportError(token, "expecting colon, not 'EOF'");
reportError(token, "expecting closing '}' at end"); reportError(token, "expecting closing '}' at end");
break; break;
@@ -444,7 +442,7 @@ Value Parser::consumeObject()
} }
// EOF // EOF
if (reachedEnd()) { if (isEOF()) {
reportError(token, "expecting value, not 'EOF'"); reportError(token, "expecting value, not 'EOF'");
reportError(token, "expecting closing '}' at end"); reportError(token, "expecting closing '}' at end");
break; break;
@@ -473,7 +471,7 @@ Value Parser::consumeObject()
} }
// EOF // EOF
if (reachedEnd()) { if (isEOF()) {
reportError(token, "expecting closing '}' at end"); reportError(token, "expecting closing '}' at end");
break; break;
} }
+2 -3
View File
@@ -25,11 +25,10 @@ public:
Value parse(); Value parse();
private: private:
bool reachedEnd(); bool isEOF();
bool seekForward(Token::Type type);
Token peek(); Token peek();
Token consume(); Token consume();
void ignoreUntil(Token::Type type);
Value consumeLiteral(); Value consumeLiteral();
Value consumeNumber(); Value consumeNumber();