From e047d9eb5ef1c8bee1d437dac9837fd774a17315 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 14 Jan 2019 15:51:52 +0000 Subject: [PATCH] Update `package json` parser to store the end position on the values --- core/encoding/json/parser.odin | 16 ++++++++++++++-- core/encoding/json/types.odin | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/encoding/json/parser.odin b/core/encoding/json/parser.odin index 374f8589f..688a98a49 100644 --- a/core/encoding/json/parser.odin +++ b/core/encoding/json/parser.odin @@ -6,6 +6,7 @@ import "core:strconv" Parser :: struct { tok: Tokenizer, + prev_token: Token, curr_token: Token, spec: Specification, allocator: mem.Allocator, @@ -31,11 +32,17 @@ parse :: proc(data: []byte, spec := Specification.JSON, allocator := context.all return parse_object(&p); } +token_end_pos :: proc(tok: Token) -> Pos { + end := tok.pos; + end.offset += len(tok.text); + return end; +} + advance_token :: proc(p: ^Parser) -> (Token, Error) { err: Error; - prev := p.curr_token; + p.prev_token := p.curr_token; p.curr_token, err = get_token(&p.tok); - return prev, err; + return p.prev_token, err; } @@ -60,6 +67,8 @@ expect_token :: proc(p: ^Parser, kind: Kind) -> Error { parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) { value.pos = p.curr_token.pos; + defer value.end = token_end_pos(p.prev_token); + token := p.curr_token; switch token.kind { case Kind.Null: @@ -124,6 +133,7 @@ parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) { parse_array :: proc(p: ^Parser) -> (value: Value, err: Error) { value.pos = p.curr_token.pos; + defer value.end = token_end_pos(p.prev_token); if err = expect_token(p, Kind.Open_Bracket); err != Error.None { return; } @@ -192,6 +202,8 @@ parse_object_key :: proc(p: ^Parser) -> (key: string, err: Error) { parse_object :: proc(p: ^Parser) -> (value: Value, err: Error) { value.pos = p.curr_token.pos; + defer value.end = token_end_pos(p.prev_token); + if err = expect_token(p, Kind.Open_Brace); err != Error.None { value.pos = p.curr_token.pos; return; diff --git a/core/encoding/json/types.odin b/core/encoding/json/types.odin index f10136ad0..6973d3dc5 100644 --- a/core/encoding/json/types.odin +++ b/core/encoding/json/types.odin @@ -16,7 +16,7 @@ Array :: distinct [dynamic]Value; Object :: distinct map[string]Value; Value :: struct { - pos: Pos, + pos, end: Pos, value: union { Null, Integer,