Browse Source

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

master
Riyyi 2 years ago
parent
commit
2408be335d
  1. 50
      src/util/json/parser.cpp
  2. 5
      src/util/json/parser.h

50
src/util/json/parser.cpp

@ -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) Token Parser::peek()
{ {
for (; !reachedEnd(); ++m_index) { assert(!isEOF());
if (peek().type == type) { return (*m_tokens)[m_index];
m_index++;
return true;
}
} }
return false; Token Parser::consume()
}
Token Parser::peek()
{ {
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(); while (!isEOF() && peek().type != type) {
m_index++; ++m_index;
return token; }
} }
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;
} }

5
src/util/json/parser.h

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

Loading…
Cancel
Save