mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Added option to parse number as integer, disabled by default
This commit is contained in:
@@ -11,11 +11,12 @@ Parser :: struct {
|
|||||||
spec: Specification,
|
spec: Specification,
|
||||||
allocator: mem.Allocator,
|
allocator: mem.Allocator,
|
||||||
unmarshal_data: any,
|
unmarshal_data: any,
|
||||||
|
parse_integers: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
make_parser :: proc(data: []byte, spec := Specification.JSON, allocator := context.allocator) -> Parser {
|
make_parser :: proc(data: []byte, spec := Specification.JSON, parse_integers := false, allocator := context.allocator) -> Parser {
|
||||||
p: Parser;
|
p: Parser;
|
||||||
p.tok = make_tokenizer(data, spec);
|
p.tok = make_tokenizer(data, spec, parse_integers);
|
||||||
p.spec = spec;
|
p.spec = spec;
|
||||||
p.allocator = allocator;
|
p.allocator = allocator;
|
||||||
assert(p.allocator.procedure != nil);
|
assert(p.allocator.procedure != nil);
|
||||||
@@ -23,9 +24,9 @@ make_parser :: proc(data: []byte, spec := Specification.JSON, allocator := conte
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
parse :: proc(data: []byte, spec := Specification.JSON, allocator := context.allocator) -> (Value, Error) {
|
parse :: proc(data: []byte, spec := Specification.JSON, parse_integers := false, allocator := context.allocator) -> (Value, Error) {
|
||||||
context.allocator = allocator;
|
context.allocator = allocator;
|
||||||
p := make_parser(data, spec, allocator);
|
p := make_parser(data, spec, parse_integers, allocator);
|
||||||
|
|
||||||
if p.spec == Specification.JSON5 {
|
if p.spec == Specification.JSON5 {
|
||||||
return parse_value(&p);
|
return parse_value(&p);
|
||||||
|
|||||||
@@ -42,12 +42,13 @@ Tokenizer :: struct {
|
|||||||
w: int, // current rune width in bytes
|
w: int, // current rune width in bytes
|
||||||
curr_line_offset: int,
|
curr_line_offset: int,
|
||||||
spec: Specification,
|
spec: Specification,
|
||||||
|
parse_integers: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
make_tokenizer :: proc(data: []byte, spec := Specification.JSON) -> Tokenizer {
|
make_tokenizer :: proc(data: []byte, spec := Specification.JSON, parse_integers := false) -> Tokenizer {
|
||||||
t := Tokenizer{pos = {line=1}, data = data, spec = spec};
|
t := Tokenizer{pos = {line=1}, data = data, spec = spec, parse_integers = parse_integers};
|
||||||
next_rune(&t);
|
next_rune(&t);
|
||||||
if t.r == utf8.RUNE_BOM {
|
if t.r == utf8.RUNE_BOM {
|
||||||
next_rune(&t);
|
next_rune(&t);
|
||||||
@@ -217,7 +218,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
|
|||||||
fallthrough;
|
fallthrough;
|
||||||
|
|
||||||
case '0'..'9':
|
case '0'..'9':
|
||||||
token.kind = .Integer;
|
token.kind = t.parse_integers ? .Integer : .Float;
|
||||||
if t.spec == .JSON5 { // Hexadecimal Numbers
|
if t.spec == .JSON5 { // Hexadecimal Numbers
|
||||||
if curr_rune == '0' && (t.r == 'x' || t.r == 'X') {
|
if curr_rune == '0' && (t.r == 'x' || t.r == 'X') {
|
||||||
next_rune(t);
|
next_rune(t);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "core:mem"
|
|||||||
|
|
||||||
// NOTE(bill): is_valid will not check for duplicate keys
|
// NOTE(bill): is_valid will not check for duplicate keys
|
||||||
is_valid :: proc(data: []byte, spec := Specification.JSON) -> bool {
|
is_valid :: proc(data: []byte, spec := Specification.JSON) -> bool {
|
||||||
p := make_parser(data, spec, mem.nil_allocator());
|
p := make_parser(data, spec, false, mem.nil_allocator());
|
||||||
if p.spec == Specification.JSON5 {
|
if p.spec == Specification.JSON5 {
|
||||||
return validate_value(&p);
|
return validate_value(&p);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user