Browse Source

Reader: Improve HashMap logic

master
Riyyi 2 years ago
parent
commit
b4742ef380
  1. 8
      src/reader.cpp

8
src/reader.cpp

@ -180,13 +180,12 @@ ValuePtr Reader::readHashMap()
auto hash_map = makePtr<HashMap>(); auto hash_map = makePtr<HashMap>();
while (!isEOF() && peek().type != Token::Type::BraceClose) { while (!isEOF() && peek().type != Token::Type::BraceClose) {
auto key = readImpl(); auto key = readImpl();
auto value = readImpl();
if (key == nullptr && value == nullptr) { if (key == nullptr || isEOF()) {
break; break;
} }
if (key == nullptr || value == nullptr) { if (peek().type == Token::Type::BraceClose) {
Error::the().add("hash-map requires an even-sized list"); Error::the().add("hash-map requires an even-sized list");
return nullptr; return nullptr;
} }
@ -196,12 +195,15 @@ ValuePtr Reader::readHashMap()
return nullptr; return nullptr;
} }
auto value = readImpl();
std::string keyString = is<String>(key.get()) ? std::static_pointer_cast<String>(key)->data() : std::static_pointer_cast<Keyword>(key)->keyword(); std::string keyString = is<String>(key.get()) ? std::static_pointer_cast<String>(key)->data() : std::static_pointer_cast<Keyword>(key)->keyword();
hash_map->add(keyString, value); hash_map->add(keyString, value);
} }
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // } if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
Error::the().add("expected '}', got EOF"); Error::the().add("expected '}', got EOF");
return nullptr;
} }
return hash_map; return hash_map;

Loading…
Cancel
Save