This commit is contained in:
sfja 2024-12-11 01:03:16 +01:00
parent 1640915ddb
commit 9221f62f92
2 changed files with 11 additions and 7 deletions

View File

@ -1,6 +1,7 @@
#include "json.hpp"
#include <cstdlib>
#include <format>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
@ -165,7 +166,7 @@ auto Parser::parse_val() -> Res<std::unique_ptr<Value>>
}
step();
auto value = parse_val();
if (value.ok()) {
if (not value.ok()) {
return value.err();
}
fields.insert_or_assign(key, std::move(value.val()));
@ -183,7 +184,7 @@ auto Parser::parse_val() -> Res<std::unique_ptr<Value>>
}
step();
auto value = parse_val();
if (value.ok()) {
if (not value.ok()) {
return value.err();
}
fields.insert_or_assign(key, std::move(value.val()));
@ -192,30 +193,32 @@ auto Parser::parse_val() -> Res<std::unique_ptr<Value>>
if (curtyp() != TokTyp::RBrace) {
return unexpected_tok_err(TokTyp::RBrace, "malformed object");
}
step();
return Res<std::unique_ptr<Value>>(
std::make_unique<Object>(std::move(fields)));
}
case TokTyp::LBracket: {
step();
ArrayValues values;
if (curtyp() != TokTyp::RBrace) {
if (curtyp() != TokTyp::RBracket) {
auto value = parse_val();
if (value.ok()) {
if (not value.ok()) {
return value.err();
}
values.push_back(std::move(value.val()));
while (curtyp() == TokTyp::Comma) {
step();
auto value = parse_val();
if (value.ok()) {
if (not value.ok()) {
return value.err();
}
values.push_back(std::move(value.val()));
}
}
if (curtyp() != TokTyp::RBrace) {
return unexpected_tok_err(TokTyp::RBrace, "malformed object");
if (curtyp() != TokTyp::RBracket) {
return unexpected_tok_err(TokTyp::RBracket, "malformed array");
}
step();
return Res<std::unique_ptr<Value>>(
std::make_unique<Array>(std::move(values)));
}

View File

@ -311,6 +311,7 @@ private:
auto tok = this->lexer.next();
if (not tok.ok())
return tok.err();
this->cur = tok;
return {};
}
inline auto curtyp() const -> TokTyp { return this->cur.val().typ; }