Added option to parse number as integer, disabled by default

This commit is contained in:
jockus
2020-09-15 11:39:34 +01:00
parent 0cd681e6b7
commit 195dbd658d
3 changed files with 10 additions and 8 deletions
+5 -4
View File
@@ -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);
+4 -3
View File
@@ -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);
+1 -1
View File
@@ -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);
} }