2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-09-10 12:05:40 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module json2
|
|
|
|
|
|
|
|
// `Any` is a sum type that lists the possible types to be decoded and used.
|
2021-03-30 09:40:20 +02:00
|
|
|
pub type Any = Null | []Any | bool | f32 | f64 | i64 | int | map[string]Any | string |
|
|
|
|
u64
|
2020-11-29 14:54:45 +01:00
|
|
|
|
2020-09-10 12:05:40 +02:00
|
|
|
// `Null` struct is a simple representation of the `null` value in JSON.
|
2020-11-29 14:54:45 +01:00
|
|
|
pub struct Null {
|
2021-02-26 07:36:02 +01:00
|
|
|
is_null bool = true
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2020-09-10 12:05:40 +02:00
|
|
|
|
|
|
|
struct Parser {
|
|
|
|
mut:
|
2021-02-26 07:36:02 +01:00
|
|
|
scanner &Scanner
|
|
|
|
p_tok Token
|
|
|
|
tok Token
|
|
|
|
n_tok Token
|
2020-11-29 14:54:45 +01:00
|
|
|
n_level int
|
2020-10-09 16:11:55 +02:00
|
|
|
convert_type bool = true
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) next() {
|
|
|
|
p.p_tok = p.tok
|
|
|
|
p.tok = p.n_tok
|
2020-10-09 16:11:55 +02:00
|
|
|
p.n_tok = p.scanner.scan()
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 07:36:02 +01:00
|
|
|
fn (mut p Parser) next_with_err() ? {
|
|
|
|
p.next()
|
|
|
|
if p.tok.kind == .error {
|
|
|
|
return error(p.emit_error(p.tok.lit.bytestr()))
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:36:02 +01:00
|
|
|
fn (p Parser) emit_error(msg string) string {
|
|
|
|
line := p.tok.line
|
|
|
|
column := p.tok.col + p.tok.lit.len
|
|
|
|
return '[x.json2] $msg ($line:$column)'
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: copied from v.util to avoid the entire module and its functions
|
|
|
|
// from being imported. remove later once -skip-unused is enabled by default.
|
|
|
|
fn skip_bom(file_content string) string {
|
|
|
|
mut raw_text := file_content
|
|
|
|
// BOM check
|
|
|
|
if raw_text.len >= 3 {
|
2020-09-10 12:05:40 +02:00
|
|
|
unsafe {
|
2021-02-26 07:36:02 +01:00
|
|
|
c_text := raw_text.str
|
2020-09-10 12:05:40 +02:00
|
|
|
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
|
|
|
|
// skip three BOM bytes
|
|
|
|
offset_from_begin := 3
|
2021-02-26 07:36:02 +01:00
|
|
|
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
return raw_text
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 07:36:02 +01:00
|
|
|
fn new_parser(srce string, convert_type bool) Parser {
|
|
|
|
src := skip_bom(srce)
|
|
|
|
return Parser{
|
|
|
|
scanner: &Scanner{
|
|
|
|
text: src.bytes()
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
convert_type: convert_type
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2020-10-09 16:11:55 +02:00
|
|
|
}
|
2020-09-10 12:05:40 +02:00
|
|
|
|
2020-10-09 16:11:55 +02:00
|
|
|
fn (mut p Parser) decode() ?Any {
|
2021-02-26 07:36:02 +01:00
|
|
|
p.next()
|
|
|
|
p.next_with_err() ?
|
|
|
|
fi := p.decode_value() ?
|
2020-10-09 16:11:55 +02:00
|
|
|
if p.tok.kind != .eof {
|
2021-02-26 07:36:02 +01:00
|
|
|
return error(p.emit_error('invalid token `$p.tok.kind`'))
|
2020-10-09 16:11:55 +02:00
|
|
|
}
|
|
|
|
return fi
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) decode_value() ?Any {
|
2020-10-09 16:11:55 +02:00
|
|
|
if p.n_level == 500 {
|
2021-02-26 07:36:02 +01:00
|
|
|
return error(p.emit_error('reached maximum nesting level of 500'))
|
2020-10-09 16:11:55 +02:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
if (p.tok.kind == .lsbr && p.n_tok.kind == .lcbr)
|
|
|
|
|| (p.p_tok.kind == p.tok.kind && p.tok.kind == .lsbr) {
|
2020-09-10 12:05:40 +02:00
|
|
|
p.n_level++
|
|
|
|
}
|
|
|
|
match p.tok.kind {
|
|
|
|
.lsbr {
|
2020-10-09 16:11:55 +02:00
|
|
|
return p.decode_array()
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
.lcbr {
|
2020-10-09 16:11:55 +02:00
|
|
|
return p.decode_object()
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
.int_, .float {
|
|
|
|
tl := p.tok.lit.bytestr()
|
|
|
|
kind := p.tok.kind
|
|
|
|
p.next_with_err() ?
|
|
|
|
if p.convert_type {
|
2021-03-22 15:45:29 +01:00
|
|
|
if kind == .float {
|
|
|
|
return Any(tl.f64())
|
|
|
|
}
|
|
|
|
return Any(tl.i64())
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
return Any(tl)
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
.bool_ {
|
|
|
|
lit := p.tok.lit.bytestr()
|
|
|
|
p.next_with_err() ?
|
2021-03-22 15:45:29 +01:00
|
|
|
if p.convert_type {
|
|
|
|
return Any(lit.bool())
|
|
|
|
}
|
|
|
|
return Any(lit)
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
.null {
|
|
|
|
p.next_with_err() ?
|
2021-03-22 15:45:29 +01:00
|
|
|
if p.convert_type {
|
|
|
|
return Any(null)
|
|
|
|
}
|
|
|
|
return Any('null')
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
.str_ {
|
|
|
|
str := p.tok.lit.bytestr()
|
|
|
|
p.next_with_err() ?
|
|
|
|
return Any(str)
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-02-26 07:36:02 +01:00
|
|
|
return error(p.emit_error('invalid token `$p.tok.kind`'))
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 16:11:55 +02:00
|
|
|
return Any{}
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) decode_array() ?Any {
|
|
|
|
mut items := []Any{}
|
2021-02-26 07:36:02 +01:00
|
|
|
p.next_with_err() ?
|
2020-09-10 12:05:40 +02:00
|
|
|
for p.tok.kind != .rsbr {
|
2020-11-29 14:54:45 +01:00
|
|
|
item := p.decode_value() ?
|
2020-09-10 12:05:40 +02:00
|
|
|
items << item
|
2021-02-26 07:36:02 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.next_with_err() ?
|
|
|
|
if p.tok.kind == .rsbr || p.tok.kind == .rcbr {
|
|
|
|
return error(p.emit_error('invalid token `$p.tok.lit'))
|
|
|
|
}
|
|
|
|
} else if p.tok.kind == .rsbr {
|
2020-09-10 12:05:40 +02:00
|
|
|
break
|
2021-02-26 07:36:02 +01:00
|
|
|
} else {
|
|
|
|
return error(p.emit_error("unknown token '$p.tok.lit' when decoding array."))
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
p.next_with_err() ?
|
2020-09-10 12:05:40 +02:00
|
|
|
return Any(items)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut p Parser) decode_object() ?Any {
|
2020-11-29 14:54:45 +01:00
|
|
|
mut fields := map[string]Any{}
|
2021-02-26 07:36:02 +01:00
|
|
|
p.next_with_err() ?
|
2020-09-10 12:05:40 +02:00
|
|
|
for p.tok.kind != .rcbr {
|
2021-02-26 07:36:02 +01:00
|
|
|
is_key := p.tok.kind == .str_ && p.n_tok.kind == .colon
|
2020-09-10 12:05:40 +02:00
|
|
|
if !is_key {
|
2021-02-26 07:36:02 +01:00
|
|
|
return error(p.emit_error('invalid token `$p.tok.kind`, expecting `str_`'))
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
cur_key := p.tok.lit.bytestr()
|
|
|
|
p.next_with_err() ?
|
|
|
|
p.next_with_err() ?
|
2020-11-29 14:54:45 +01:00
|
|
|
fields[cur_key] = p.decode_value() ?
|
2021-02-26 07:36:02 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.next_with_err() ?
|
|
|
|
if p.tok.kind != .str_ {
|
|
|
|
return error(p.emit_error("unknown token '$p.tok.lit' when decoding object."))
|
|
|
|
}
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
p.next_with_err() ?
|
2020-09-10 12:05:40 +02:00
|
|
|
return Any(fields)
|
|
|
|
}
|