mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-30 19:30:06 +00:00
package json; JSON5 support
This commit is contained in:
@@ -3,19 +3,35 @@ package json
|
||||
import "core:mem"
|
||||
|
||||
// NOTE(bill): is_valid will not check for duplicate keys
|
||||
is_valid :: proc(data: string) -> bool {
|
||||
p := make_parser(data, mem.nil_allocator());
|
||||
is_valid :: proc(data: string, spec := Specification.JSON) -> bool {
|
||||
p := make_parser(data, spec, mem.nil_allocator());
|
||||
if p.spec == Specification.JSON5 {
|
||||
return validate_value(&p);
|
||||
}
|
||||
return validate_object(&p);
|
||||
}
|
||||
|
||||
validate_object_key :: proc(p: ^Parser) -> bool {
|
||||
tok := p.curr_token;
|
||||
if p.spec == Specification.JSON5 {
|
||||
if tok.kind == Kind.String {
|
||||
expect_token(p, Kind.String);
|
||||
return true;
|
||||
} else if tok.kind == Kind.Ident {
|
||||
expect_token(p, Kind.Ident);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
err := expect_token(p, Kind.String);
|
||||
return err == Error.None;
|
||||
}
|
||||
validate_object :: proc(p: ^Parser) -> bool {
|
||||
if err := expect_token(p, Kind.Open_Brace); err != Error.None {
|
||||
return false;
|
||||
}
|
||||
|
||||
for p.curr_token.kind != Kind.Close_Brace {
|
||||
tok := p.curr_token;
|
||||
if tok_err := expect_token(p, Kind.String); tok_err != Error.None {
|
||||
if !validate_object_key(p) {
|
||||
return false;
|
||||
}
|
||||
if colon_err := expect_token(p, Kind.Colon); colon_err != Error.None {
|
||||
@@ -26,11 +42,18 @@ validate_object :: proc(p: ^Parser) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Disallow trailing commas for the time being
|
||||
if allow_token(p, Kind.Comma) {
|
||||
continue;
|
||||
if p.spec == Specification.JSON5 {
|
||||
// Allow trailing commas
|
||||
if allow_token(p, Kind.Comma) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
// Disallow trailing commas
|
||||
if allow_token(p, Kind.Comma) {
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +108,7 @@ validate_value :: proc(p: ^Parser) -> bool {
|
||||
return true;
|
||||
case Kind.String:
|
||||
advance_token(p);
|
||||
return is_valid_string_literal(token.text);
|
||||
return is_valid_string_literal(token.text, p.spec);
|
||||
|
||||
case Kind.Open_Brace:
|
||||
return validate_object(p);
|
||||
|
||||
Reference in New Issue
Block a user