1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

x.json2: add custom errors (#9523)

This commit is contained in:
Atakan Yenel 2021-03-30 14:29:17 +02:00 committed by GitHub
parent 70b189d751
commit 28018c6fc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,16 @@ mut:
convert_type bool = true
}
struct InvalidTokenError {
msg string
code int
}
struct UnknownTokenError {
msg string
code int
}
fn (mut p Parser) next() {
p.p_tok = p.tok
p.tok = p.n_tok
@ -74,7 +84,9 @@ fn (mut p Parser) decode() ?Any {
p.next_with_err() ?
fi := p.decode_value() ?
if p.tok.kind != .eof {
return error(p.emit_error('invalid token `$p.tok.kind`'))
return IError(&InvalidTokenError{
msg: p.emit_error('invalid token `$p.tok.kind`')
})
}
return fi
}
@ -127,7 +139,9 @@ fn (mut p Parser) decode_value() ?Any {
return Any(str)
}
else {
return error(p.emit_error('invalid token `$p.tok.kind`'))
return IError(&InvalidTokenError{
msg: p.emit_error('invalid token `$p.tok.kind`')
})
}
}
return Any{}
@ -142,12 +156,16 @@ fn (mut p Parser) decode_array() ?Any {
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'))
return IError(&InvalidTokenError{
msg: p.emit_error('invalid token `$p.tok.lit')
})
}
} else if p.tok.kind == .rsbr {
break
} else {
return error(p.emit_error("unknown token '$p.tok.lit' when decoding array."))
return IError(&UnknownTokenError{
msg: p.emit_error("unknown token '$p.tok.lit' when decoding array.")
})
}
}
p.next_with_err() ?
@ -160,7 +178,9 @@ fn (mut p Parser) decode_object() ?Any {
for p.tok.kind != .rcbr {
is_key := p.tok.kind == .str_ && p.n_tok.kind == .colon
if !is_key {
return error(p.emit_error('invalid token `$p.tok.kind`, expecting `str_`'))
return IError(&InvalidTokenError{
msg: p.emit_error('invalid token `$p.tok.kind`, expecting `str_`')
})
}
cur_key := p.tok.lit.bytestr()
p.next_with_err() ?
@ -169,7 +189,9 @@ fn (mut p Parser) decode_object() ?Any {
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."))
return IError(&UnknownTokenError{
msg: p.emit_error("unknown token '$p.tok.lit' when decoding object.")
})
}
}
}