Browse Source

Util: Add EOF detection in Parser getArray() getObject()

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

50
src/util/json/parser.cpp

@ -72,7 +72,7 @@ Value Parser::parse()
break; break;
} }
if (m_index < m_tokens->size()) { if (!reachedEnd()) {
m_job->printErrorLine(peek(), "multiple root elements"); m_job->printErrorLine(peek(), "multiple root elements");
} }
@ -81,6 +81,11 @@ Value Parser::parse()
// ----------------------------------------- // -----------------------------------------
bool Parser::reachedEnd()
{
return m_index >= m_tokens->size();
}
Token Parser::peek() Token Parser::peek()
{ {
return (*m_tokens)[m_index]; return (*m_tokens)[m_index];
@ -107,6 +112,10 @@ Token Parser::consume()
bool Parser::consumeSpecific(Token::Type type) bool Parser::consumeSpecific(Token::Type type)
{ {
if (reachedEnd()) {
return false;
}
if (peek().type != type) { if (peek().type != type) {
return false; return false;
} }
@ -320,8 +329,13 @@ Value Parser::getArray()
Value array = Value::Type::Array; Value array = Value::Type::Array;
Token token; Token token;
for (;;) { for (;;) {
token = peek(); // EOF
if (reachedEnd()) {
reportError(m_tokens->at(m_index - 1), "expecting closing ']' at end");
break;
}
token = peek();
if (token.type == Token::Type::Literal) { if (token.type == Token::Type::Literal) {
array.emplace_back(getLiteral()); array.emplace_back(getLiteral());
} }
@ -349,6 +363,12 @@ Value Parser::getArray()
break; break;
} }
// EOF
if (reachedEnd()) {
reportError(token, "expecting closing ']' at end");
break;
}
// Find , or ] // Find , or ]
token = consume(); token = consume();
if (token.type == Token::Type::Comma) { if (token.type == Token::Type::Comma) {
@ -383,6 +403,12 @@ Value Parser::getObject()
std::string name; std::string name;
std::map<std::string, uint8_t> unique; std::map<std::string, uint8_t> unique;
for (;;) { for (;;) {
// EOF
if (reachedEnd()) {
reportError(m_tokens->at(m_index - 1), "expecting closing '}' at end");
break;
}
token = peek(); token = peek();
if (token.type == Token::Type::BraceClose) { if (token.type == Token::Type::BraceClose) {
// Trailing comma // Trailing comma
@ -414,6 +440,13 @@ Value Parser::getObject()
// Add name to hashmap // Add name to hashmap
unique.insert({ name, 0 }); unique.insert({ name, 0 });
// EOF
if (reachedEnd()) {
reportError(token, "expecting colon, not 'EOF'");
reportError(token, "expecting closing '}' at end");
break;
}
// Find : // Find :
token = consume(); token = consume();
if (token.type != Token::Type::Colon) { if (token.type != Token::Type::Colon) {
@ -421,6 +454,13 @@ Value Parser::getObject()
break; break;
} }
// EOF
if (reachedEnd()) {
reportError(token, "expecting value, not 'EOF'");
reportError(token, "expecting closing '}' at end");
break;
}
// Add member (name:value pair) to object // Add member (name:value pair) to object
token = peek(); token = peek();
if (token.type == Token::Type::Literal) { if (token.type == Token::Type::Literal) {
@ -443,6 +483,12 @@ Value Parser::getObject()
break; break;
} }
// EOF
if (reachedEnd()) {
reportError(token, "expecting closing '}' at end");
break;
}
// Find , or } // Find , or }
token = consume(); token = consume();
if (token.type == Token::Type::Comma) { if (token.type == Token::Type::Comma) {

2
src/util/json/parser.h

@ -25,6 +25,8 @@ public:
Value parse(); Value parse();
private: private:
bool reachedEnd();
Token peek(); Token peek();
bool seekForward(Token::Type type); bool seekForward(Token::Type type);

Loading…
Cancel
Save