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

View File

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