2021-09-24 21:13:52 +03:00
|
|
|
// Copyright (c) 2021 Lars Pontoppidan. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module checker
|
|
|
|
|
|
|
|
import toml.ast
|
|
|
|
import toml.ast.walker
|
2021-10-26 16:58:05 +03:00
|
|
|
import toml.util
|
2021-09-24 21:13:52 +03:00
|
|
|
import toml.token
|
|
|
|
import toml.scanner
|
2021-10-26 16:58:05 +03:00
|
|
|
import encoding.utf8
|
2021-11-11 08:27:41 +03:00
|
|
|
import time
|
2021-11-18 20:46:19 +03:00
|
|
|
import strconv
|
2021-09-24 21:13:52 +03:00
|
|
|
|
2021-09-29 19:28:09 +03:00
|
|
|
pub const allowed_basic_escape_chars = [`u`, `U`, `b`, `t`, `n`, `f`, `r`, `"`, `\\`]
|
|
|
|
|
2021-10-27 16:41:22 +03:00
|
|
|
// utf8_max is the largest inclusive value of the Unicodes scalar value ranges.
|
|
|
|
const utf8_max = 0x10FFFF
|
2021-10-27 15:28:46 +03:00
|
|
|
|
2021-09-25 20:31:02 +03:00
|
|
|
// Checker checks a tree of TOML `ast.Value`'s for common errors.
|
2021-09-24 21:13:52 +03:00
|
|
|
pub struct Checker {
|
|
|
|
scanner &scanner.Scanner
|
|
|
|
}
|
|
|
|
|
2021-09-26 07:34:47 +03:00
|
|
|
// check checks the `ast.Value` and all it's children
|
|
|
|
// for common errors.
|
2021-09-25 20:31:02 +03:00
|
|
|
pub fn (c Checker) check(n &ast.Value) ? {
|
2021-09-24 21:13:52 +03:00
|
|
|
walker.walk(c, n) ?
|
|
|
|
}
|
|
|
|
|
2021-09-25 20:31:02 +03:00
|
|
|
fn (c Checker) visit(value &ast.Value) ? {
|
|
|
|
match value {
|
2021-09-29 10:19:49 +03:00
|
|
|
ast.Bool {
|
|
|
|
c.check_boolean(value) ?
|
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
ast.Number {
|
2021-09-25 20:31:02 +03:00
|
|
|
c.check_number(value) ?
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
2021-09-29 10:19:49 +03:00
|
|
|
ast.Quoted {
|
|
|
|
c.check_quoted(value) ?
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
2021-11-11 08:27:41 +03:00
|
|
|
ast.DateTime {
|
|
|
|
c.check_date_time(value) ?
|
|
|
|
}
|
|
|
|
ast.Date {
|
|
|
|
c.check_date(value) ?
|
|
|
|
}
|
|
|
|
ast.Time {
|
|
|
|
c.check_time(value) ?
|
|
|
|
}
|
2021-11-30 20:40:31 +03:00
|
|
|
else {}
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// excerpt returns a string of the token's surroundings
|
2021-09-24 21:13:52 +03:00
|
|
|
fn (c Checker) excerpt(tp token.Position) string {
|
|
|
|
return c.scanner.excerpt(tp.pos, 10)
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// is_hex_bin_oct_prefixed returns true if `hbo` has either
|
|
|
|
// of: `0x`, `0o` or `0b` - as a prefix.
|
|
|
|
// Example: assert is_hex_bin_oct_prefixed('0xAF') == true
|
|
|
|
// Example: assert is_hex_bin_oct_prefixed('xAF') == false
|
|
|
|
fn is_hex_bin_oct_prefixed(hbo string) bool {
|
2021-09-24 21:13:52 +03:00
|
|
|
return hbo.len > 2 && (hbo.starts_with('0x') || hbo.starts_with('0o') || hbo.starts_with('0b'))
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// has_repeating returns true if `str` has one or more repeating
|
|
|
|
// `rune` characters provided in `repeats`.
|
|
|
|
// Example: assert has_repeating('hello__v.', [`.`,`_`]) == true
|
|
|
|
// Example: assert has_repeating('hello_v.', [`.`,`_`]) == false
|
2021-09-24 21:13:52 +03:00
|
|
|
fn has_repeating(str string, repeats []rune) bool {
|
|
|
|
for i, r in str {
|
|
|
|
if r in repeats && i + 1 < str.len {
|
|
|
|
if r == str[i + 1] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// check_number returns an error if `num` is not a valid TOML number.
|
2021-09-24 21:13:52 +03:00
|
|
|
fn (c Checker) check_number(num ast.Number) ? {
|
|
|
|
lit := num.text
|
2021-10-26 13:49:25 +03:00
|
|
|
lit_lower_case := lit.to_lower()
|
2021-09-24 21:13:52 +03:00
|
|
|
if lit in ['0', '0.0', '+0', '-0', '+0.0', '-0.0', '0e0', '+0e0', '-0e0', '0e00'] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if lit.contains('_') {
|
|
|
|
if lit.starts_with('_') || lit.ends_with('_') {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not start or end with `_` in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
if lit.contains('__') {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not have more than one underscore (`_`) in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
mut hex_bin_oct := is_hex_bin_oct_prefixed(lit)
|
2021-10-21 16:37:38 +03:00
|
|
|
mut is_bin, mut is_oct, mut is_hex := false, false, false
|
2021-10-26 13:49:25 +03:00
|
|
|
is_float := lit_lower_case.all_before('e').contains('.')
|
|
|
|
has_exponent_notation := lit_lower_case.contains('e')
|
2021-09-24 21:13:52 +03:00
|
|
|
float_decimal_index := lit.index('.') or { -1 }
|
|
|
|
// mut is_first_digit := byte(lit[0]).is_digit()
|
|
|
|
mut ascii := byte(lit[0]).ascii_str()
|
|
|
|
is_sign_prefixed := lit[0] in [`+`, `-`]
|
2021-10-21 16:37:38 +03:00
|
|
|
mut lit_sans_sign := lit
|
2021-09-24 21:13:52 +03:00
|
|
|
if is_sign_prefixed { // +/- ...
|
2021-10-21 16:37:38 +03:00
|
|
|
lit_sans_sign = lit[1..]
|
2021-10-27 14:31:29 +03:00
|
|
|
hex_bin_oct = is_hex_bin_oct_prefixed(lit_sans_sign)
|
2021-09-24 21:13:52 +03:00
|
|
|
if hex_bin_oct {
|
|
|
|
ascii = byte(lit[0]).ascii_str()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" (hex, octal and binary) can not start with `$ascii` in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
2021-11-27 22:03:26 +03:00
|
|
|
if lit.len > 1 && lit_sans_sign.starts_with('0') && !lit_sans_sign.starts_with('0.') {
|
2021-10-21 16:37:38 +03:00
|
|
|
ascii = byte(lit_sans_sign[0]).ascii_str()
|
2021-09-24 21:13:52 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not start with `$ascii` in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !hex_bin_oct {
|
|
|
|
if !is_float && lit[0] == `0` {
|
2021-10-21 14:12:17 +03:00
|
|
|
if lit[1] in [`B`, `O`, `X`] {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" only lowercase notation in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not start with a zero in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_float && lit[0] == `0` && float_decimal_index > 1 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not start with a zero in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:37:38 +03:00
|
|
|
if has_repeating(lit, [`_`, `.`, `b`, `o`, `x`]) {
|
2021-09-24 21:13:52 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not have $scanner.digit_extras as repeating characters in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
|
|
|
|
if hex_bin_oct {
|
2021-10-21 16:37:38 +03:00
|
|
|
is_bin = lit_sans_sign.starts_with('0b')
|
|
|
|
is_oct = lit_sans_sign.starts_with('0o')
|
|
|
|
is_hex = lit_sans_sign.starts_with('0x')
|
|
|
|
|
|
|
|
lit_sans_sign_and_type_prefix := lit_sans_sign[2..]
|
|
|
|
|
|
|
|
if lit_sans_sign_and_type_prefix.starts_with('_')
|
|
|
|
|| lit_sans_sign_and_type_prefix.ends_with('_') {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not start or end with `_` in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_bin {
|
|
|
|
if !c.is_valid_binary_literal(lit_sans_sign_and_type_prefix) {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid binary number in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
} else if is_oct {
|
|
|
|
if !c.is_valid_octal_literal(lit_sans_sign_and_type_prefix) {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid octal number in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !c.is_valid_hex_literal(lit_sans_sign_and_type_prefix) {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid hexadecimal number in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if has_exponent_notation {
|
2021-10-26 13:49:25 +03:00
|
|
|
if lit_lower_case.all_after('e').starts_with('_')
|
|
|
|
|| lit_lower_case.all_before('e').ends_with('_') {
|
2021-10-22 19:56:43 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
2021-10-26 13:49:25 +03:00
|
|
|
' the exponent in "$lit" can not start nor end with an underscore in ...${c.excerpt(num.pos)}...')
|
2021-10-22 19:56:43 +03:00
|
|
|
}
|
2021-10-26 13:49:25 +03:00
|
|
|
if lit_lower_case.all_after('e').contains('.') {
|
2021-09-24 21:13:52 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" (with exponent) can not have a decimal point in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
2021-10-26 13:49:25 +03:00
|
|
|
if !is_hex && lit_lower_case.count('e') > 1 {
|
2021-09-24 21:13:52 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" (with exponent) can only have one exponent in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_float {
|
|
|
|
if lit.count('.') > 1 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" (float) can only have one decimal point in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
last := lit[lit.len - 1]
|
|
|
|
if last in scanner.digit_extras {
|
|
|
|
ascii = byte(last).ascii_str()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
2021-11-11 08:28:46 +03:00
|
|
|
' numbers like "$lit" (float) can not end with `$ascii` in ...${c.excerpt(num.pos)}...')
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
if lit.contains('_.') || lit.contains('._') {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" (float) can not have underscores before or after the decimal point in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
2021-10-26 13:49:25 +03:00
|
|
|
if lit_lower_case.contains('e.') || lit.contains('.e') {
|
2021-09-24 21:13:52 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
2021-10-26 13:49:25 +03:00
|
|
|
' numbers like "$lit" (float) can not have decimal points on either side of the exponent notation in ...${c.excerpt(num.pos)}...')
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
2021-12-02 12:16:55 +03:00
|
|
|
// Check if it contains other chars than the allowed
|
|
|
|
for r in lit {
|
|
|
|
if r !in [`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `.`, `e`, `E`, `-`, `+`,
|
|
|
|
`_`] {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" (float) can not contain `${byte(r).ascii_str()}` in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
} else {
|
2021-10-21 16:37:38 +03:00
|
|
|
if lit.len > 1 && lit.starts_with('0') && lit[1] !in [`b`, `o`, `x`] {
|
2021-09-24 21:13:52 +03:00
|
|
|
ascii = byte(lit[0]).ascii_str()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' numbers like "$lit" can not start with `$ascii` in ...${c.excerpt(num.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// is_valid_binary_literal returns true if `num` is valid TOML binary literal.
|
2021-10-21 16:37:38 +03:00
|
|
|
fn (c Checker) is_valid_binary_literal(num string) bool {
|
|
|
|
for ch in num {
|
|
|
|
if ch == `_` {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !(ch >= `0` && ch <= `1`) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// is_valid_octal_literal returns true if `num` is valid TOML octal literal.
|
2021-10-21 16:37:38 +03:00
|
|
|
fn (c Checker) is_valid_octal_literal(num string) bool {
|
|
|
|
for ch in num {
|
|
|
|
if ch == `_` {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !(ch >= `0` && ch <= `7`) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// is_valid_hex_literal returns true if `num` is valid TOML hexadecimal literal.
|
2021-10-21 16:37:38 +03:00
|
|
|
fn (c Checker) is_valid_hex_literal(num string) bool {
|
|
|
|
for ch in num {
|
|
|
|
if ch == `_` {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !ch.is_hex_digit() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// check_boolean returns an error if `b` is not a valid TOML boolean.
|
2021-09-24 21:13:52 +03:00
|
|
|
fn (c Checker) check_boolean(b ast.Bool) ? {
|
|
|
|
lit := b.text
|
|
|
|
if lit in ['true', 'false'] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' boolean values like "$lit" can only be `true` or `false` literals, not `$lit` in ...${c.excerpt(b.pos)}...')
|
|
|
|
}
|
2021-09-29 10:19:49 +03:00
|
|
|
|
2021-11-11 08:27:41 +03:00
|
|
|
// check_date_time returns an error if `dt` is not a valid TOML date-time string (RFC 3339).
|
|
|
|
// See also https://ijmacd.github.io/rfc3339-iso8601 for a more
|
|
|
|
// visual representation of the RFC 3339 format.
|
|
|
|
fn (c Checker) check_date_time(dt ast.DateTime) ? {
|
|
|
|
lit := dt.text
|
|
|
|
mut split := []string{}
|
|
|
|
// RFC 3339 Date-Times can be split via 4 separators (` `, `_`, `T` and `t`).
|
|
|
|
if lit.to_lower().contains_any(' _t') {
|
|
|
|
if lit.contains(' ') {
|
|
|
|
split = lit.split(' ')
|
|
|
|
} else if lit.contains('_') {
|
|
|
|
split = lit.split('_')
|
|
|
|
} else if lit.contains('T') {
|
|
|
|
split = lit.split('T')
|
|
|
|
} else if lit.contains('t') {
|
|
|
|
split = lit.split('t')
|
|
|
|
}
|
|
|
|
// Validate the split into date and time parts.
|
|
|
|
if split.len != 2 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" contains too many date/time separators in ...${c.excerpt(dt.pos)}...')
|
|
|
|
}
|
|
|
|
// Re-use date and time validation code for detailed testing of each part
|
|
|
|
c.check_date(ast.Date{
|
|
|
|
text: split[0]
|
|
|
|
pos: token.Position{
|
|
|
|
len: split[0].len
|
|
|
|
line_nr: dt.pos.line_nr
|
|
|
|
pos: dt.pos.pos
|
|
|
|
col: dt.pos.col
|
|
|
|
}
|
|
|
|
}) ?
|
|
|
|
c.check_time(ast.Time{
|
|
|
|
text: split[1]
|
|
|
|
pos: token.Position{
|
|
|
|
len: split[1].len
|
|
|
|
line_nr: dt.pos.line_nr
|
|
|
|
pos: dt.pos.pos + split[0].len
|
|
|
|
col: dt.pos.col + split[0].len
|
|
|
|
}
|
|
|
|
}) ?
|
|
|
|
// Use V's builtin functionality to validate the string
|
|
|
|
time.parse_rfc3339(lit) or {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid RFC 3339 Date-Time format string "$err". In ...${c.excerpt(dt.pos)}...')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid RFC 3339 Date-Time format string in ...${c.excerpt(dt.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check_time returns an error if `date` is not a valid TOML date string (RFC 3339).
|
|
|
|
fn (c Checker) check_date(date ast.Date) ? {
|
|
|
|
lit := date.text
|
|
|
|
parts := lit.split('-')
|
|
|
|
if parts.len != 3 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid RFC 3339 Date format string in ...${c.excerpt(date.pos)}...')
|
|
|
|
}
|
|
|
|
yyyy := parts[0]
|
|
|
|
if yyyy.len != 4 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" does not have a valid RFC 3339 year indication in ...${c.excerpt(date.pos)}...')
|
|
|
|
}
|
|
|
|
mm := parts[1]
|
|
|
|
if mm.len != 2 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" does not have a valid RFC 3339 month indication in ...${c.excerpt(date.pos)}...')
|
|
|
|
}
|
|
|
|
dd := parts[2]
|
|
|
|
if dd.len != 2 {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" does not have a valid RFC 3339 day indication in ...${c.excerpt(date.pos)}...')
|
|
|
|
}
|
|
|
|
// Use V's builtin functionality to validate the string
|
|
|
|
time.parse_rfc3339(lit) or {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid RFC 3339 Date format string "$err". In ...${c.excerpt(date.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check_time returns an error if `t` is not a valid TOML time string (RFC 3339).
|
|
|
|
fn (c Checker) check_time(t ast.Time) ? {
|
|
|
|
lit := t.text
|
|
|
|
// Split any offsets from the time
|
2021-11-29 23:15:22 +03:00
|
|
|
mut offset_splitter := if lit.contains('+') { '+' } else { '-' }
|
|
|
|
parts := lit.split(offset_splitter)
|
|
|
|
mut hhmmss := parts[0].all_before('.')
|
|
|
|
// Check for 2 digits in all fields
|
|
|
|
mut check_length := 8
|
|
|
|
if hhmmss.to_upper().ends_with('Z') {
|
|
|
|
check_length++
|
|
|
|
}
|
|
|
|
if hhmmss.len != check_length {
|
|
|
|
starts_with_zero := hhmmss.starts_with('0')
|
|
|
|
if !starts_with_zero {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" must be zero prefixed in ...${c.excerpt(t.pos)}...')
|
|
|
|
}
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid RFC 3339 Time format string in ...${c.excerpt(t.pos)}...')
|
|
|
|
}
|
2021-11-11 08:27:41 +03:00
|
|
|
// Use V's builtin functionality to validate the time string
|
|
|
|
time.parse_rfc3339(parts[0]) or {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' "$lit" is not a valid RFC 3339 Time format string "$err". In ...${c.excerpt(t.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// check_quoted returns an error if `q` is not a valid quoted TOML string.
|
2021-11-23 17:59:07 +03:00
|
|
|
pub fn (c Checker) check_quoted(q ast.Quoted) ? {
|
2021-09-29 19:28:09 +03:00
|
|
|
lit := q.text
|
|
|
|
quote := q.quote.ascii_str()
|
2021-09-29 10:19:49 +03:00
|
|
|
triple_quote := quote + quote + quote
|
2021-09-29 19:28:09 +03:00
|
|
|
if q.is_multiline && lit.ends_with(triple_quote) {
|
2021-09-29 10:19:49 +03:00
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
2021-10-26 16:58:05 +03:00
|
|
|
' string values like "$lit" has unbalanced quote literals `q.quote` in ...${c.excerpt(q.pos)}...')
|
2021-09-29 19:28:09 +03:00
|
|
|
}
|
|
|
|
c.check_quoted_escapes(q) ?
|
2021-10-26 16:58:05 +03:00
|
|
|
c.check_utf8_validity(q) ?
|
2021-09-29 19:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check_quoted_escapes returns an error for any disallowed escape sequences.
|
|
|
|
// Delimiters in TOML has significant meaning:
|
|
|
|
// '/''' delimits *literal* strings (WYSIWYG / What-you-see-is-what-you-get)
|
|
|
|
// "/""" delimits *basic* strings
|
|
|
|
// Allowed escapes in *basic* strings are:
|
|
|
|
// \b - backspace (U+0008)
|
|
|
|
// \t - tab (U+0009)
|
|
|
|
// \n - linefeed (U+000A)
|
|
|
|
// \f - form feed (U+000C)
|
|
|
|
// \r - carriage return (U+000D)
|
|
|
|
// \" - quote (U+0022)
|
|
|
|
// \\ - backslash (U+005C)
|
2021-10-27 15:28:46 +03:00
|
|
|
// \uXXXX - Unicode (U+XXXX)
|
|
|
|
// \UXXXXXXXX - Unicode (U+XXXXXXXX)
|
2021-09-29 19:28:09 +03:00
|
|
|
fn (c Checker) check_quoted_escapes(q ast.Quoted) ? {
|
|
|
|
// Setup a scanner in stack memory for easier navigation.
|
|
|
|
mut s := scanner.new_simple(q.text) ?
|
|
|
|
|
2021-10-27 15:28:46 +03:00
|
|
|
// See https://toml.io/en/v1.0.0#string for more info on string types.
|
2021-09-29 19:28:09 +03:00
|
|
|
is_basic := q.quote == `\"`
|
2021-11-29 23:15:22 +03:00
|
|
|
contains_newlines := q.text.contains('\n')
|
2021-09-29 19:28:09 +03:00
|
|
|
for {
|
|
|
|
ch := s.next()
|
2021-11-20 20:48:44 +03:00
|
|
|
if ch == scanner.end_of_text {
|
2021-09-29 19:28:09 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
ch_byte := byte(ch)
|
|
|
|
if ch == `\\` {
|
|
|
|
next_ch := byte(s.at())
|
|
|
|
|
|
|
|
if next_ch == `\\` {
|
|
|
|
s.next()
|
|
|
|
continue
|
|
|
|
}
|
2021-10-27 15:28:46 +03:00
|
|
|
|
2021-09-29 19:28:09 +03:00
|
|
|
escape := ch_byte.ascii_str() + next_ch.ascii_str()
|
|
|
|
if is_basic {
|
|
|
|
if q.is_multiline {
|
2021-12-02 13:07:53 +03:00
|
|
|
if next_ch == ` ` {
|
2021-11-29 23:15:22 +03:00
|
|
|
if !contains_newlines {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' can not escape whitespaces in multi-line strings (`\\ `) at `$escape` ($st.line_nr,$st.col) in ...${c.excerpt(q.pos)}...')
|
|
|
|
}
|
2021-12-02 13:07:53 +03:00
|
|
|
// Rest of line must only be space chars from this point on
|
|
|
|
for {
|
|
|
|
ch_ := s.next()
|
|
|
|
if ch_ == scanner.end_of_text || ch_ == `\n` {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !(ch_ == ` ` || ch_ == `\t`) {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' invalid character `${byte(ch_).ascii_str()}` after `$escape` at ($st.line_nr,$st.col) in ...${c.excerpt(q.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 19:28:09 +03:00
|
|
|
}
|
|
|
|
if next_ch in [`\t`, `\n`, ` `] {
|
|
|
|
s.next()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if next_ch !in checker.allowed_basic_escape_chars {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' unknown basic string escape character `$next_ch.ascii_str()` in `$escape` ($st.line_nr,$st.col) in ...${c.excerpt(q.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 15:28:46 +03:00
|
|
|
// Check Unicode escapes
|
|
|
|
if is_basic && escape.to_lower() == '\\u' {
|
|
|
|
// Long type Unicode (\UXXXXXXXX) is a maximum of 10 chars: '\' + 'U' + 8 hex characters
|
|
|
|
// we pass in 10 characters from the `u`/`U` which is the longest possible sequence
|
|
|
|
// of 9 chars plus one extra.
|
|
|
|
if s.remaining() >= 10 {
|
|
|
|
pos := s.state().pos
|
|
|
|
c.check_unicode_escape(s.text[pos..pos + 11]) or {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' escaped Unicode is invalid. $err.msg.capitalize() ($st.line_nr,$st.col) in ...${c.excerpt(q.pos)}...')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pos := s.state().pos
|
|
|
|
c.check_unicode_escape(s.text[pos..]) or {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' escaped Unicode is invalid. $err.msg.capitalize() ($st.line_nr,$st.col) in ...${c.excerpt(q.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 19:28:09 +03:00
|
|
|
}
|
2021-09-29 10:19:49 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:58:05 +03:00
|
|
|
|
2021-10-27 14:31:29 +03:00
|
|
|
// check_utf8_string returns an error if `str` is not valid UTF-8.
|
2021-10-26 16:58:05 +03:00
|
|
|
fn (c Checker) check_utf8_validity(q ast.Quoted) ? {
|
|
|
|
lit := q.text
|
|
|
|
if !utf8.validate_str(lit) {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' the string value "$lit" is not valid UTF-8 in ...${c.excerpt(q.pos)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:28:46 +03:00
|
|
|
// validate_utf8_codepoint_string returns an error if `str` is not a valid Unicode code point.
|
|
|
|
// `str` is expected to be a `string` containing *only* hex values.
|
|
|
|
// Any preludes or prefixes like `0x` could pontentially yield wrong results.
|
|
|
|
fn validate_utf8_codepoint_string(str string) ? {
|
2021-11-18 20:46:19 +03:00
|
|
|
int_val := strconv.parse_int(str, 16, 64) or { i64(-1) }
|
2021-10-27 15:28:46 +03:00
|
|
|
if int_val > checker.utf8_max || int_val < 0 {
|
|
|
|
return error('Unicode code point `$str` is outside the valid Unicode scalar value ranges.')
|
|
|
|
}
|
|
|
|
// Check if the Unicode value is actually in the valid Unicode scalar value ranges.
|
|
|
|
// TODO should probably be transferred / implemented in `utf8.validate(...)` also?
|
|
|
|
if !((int_val >= 0x0000 && int_val <= 0xD7FF) || (int_val >= 0xE000 && int_val <= 0x10FFFF)) {
|
|
|
|
return error('Unicode code point `$str` is not a valid Unicode scalar value.')
|
|
|
|
}
|
|
|
|
bytes := str.bytes()
|
|
|
|
if !utf8.validate(bytes.data, bytes.len) {
|
|
|
|
return error('Unicode code point `$str` is not a valid UTF-8 code point.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check_unicode_escape returns an error if `esc_unicode` is not
|
|
|
|
// a valid Unicode escape sequence. `esc_unicode` is expected to be
|
|
|
|
// prefixed with either `u` or `U`.
|
|
|
|
fn (c Checker) check_unicode_escape(esc_unicode string) ? {
|
|
|
|
if esc_unicode.len < 5 || !esc_unicode.to_lower().starts_with('u') {
|
|
|
|
// Makes sure the input to this function is actually valid.
|
|
|
|
return error('`$esc_unicode` is not a valid escaped Unicode sequence.')
|
|
|
|
}
|
|
|
|
is_long_esc_type := esc_unicode.starts_with('U')
|
|
|
|
mut sequence := esc_unicode[1..]
|
|
|
|
hex_digits_len := if is_long_esc_type { 8 } else { 4 }
|
|
|
|
if sequence.len < hex_digits_len {
|
|
|
|
return error('Unicode escape sequence `$esc_unicode` should be at least $hex_digits_len in length.')
|
|
|
|
}
|
|
|
|
sequence = sequence[..hex_digits_len]
|
|
|
|
// TODO not enforced in BurnSushi testsuite??
|
|
|
|
// if !sequence.is_upper() {
|
|
|
|
// return error('Unicode escape sequence `$esc_unicode` is not in all uppercase.')
|
|
|
|
//}
|
|
|
|
validate_utf8_codepoint_string(sequence.to_upper()) ?
|
|
|
|
if is_long_esc_type {
|
|
|
|
// Long escape type checks
|
|
|
|
} else {
|
|
|
|
// Short escape type checks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check_comment returns an error if the contents of `comment` isn't
|
|
|
|
// a valid TOML comment.
|
|
|
|
pub fn (c Checker) check_comment(comment ast.Comment) ? {
|
|
|
|
lit := comment.text
|
2021-10-26 16:58:05 +03:00
|
|
|
// Setup a scanner in stack memory for easier navigation.
|
|
|
|
mut s := scanner.new_simple(lit) ?
|
|
|
|
for {
|
|
|
|
ch := s.next()
|
2021-11-29 23:15:22 +03:00
|
|
|
if ch == scanner.end_of_text {
|
2021-10-26 16:58:05 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
ch_byte := byte(ch)
|
2021-11-29 23:15:22 +03:00
|
|
|
// Check for carrige return
|
|
|
|
if ch_byte == 0x0D {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' carrige return character `$ch_byte.hex()` is not allowed ($st.line_nr,$st.col) "${byte(s.at()).ascii_str()}" near ...${s.excerpt(st.pos, 10)}...')
|
|
|
|
}
|
2021-10-26 16:58:05 +03:00
|
|
|
// Check for control characters (allow TAB)
|
|
|
|
if util.is_illegal_ascii_control_character(ch_byte) {
|
|
|
|
st := s.state()
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
|
|
|
' control character `$ch_byte.hex()` is not allowed ($st.line_nr,$st.col) "${byte(s.at()).ascii_str()}" near ...${s.excerpt(st.pos, 10)}...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for bad UTF-8 encoding
|
|
|
|
if !utf8.validate_str(lit) {
|
|
|
|
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
2021-10-27 15:28:46 +03:00
|
|
|
' comment "$lit" is not valid UTF-8 in ...${c.excerpt(comment.pos)}...')
|
2021-10-26 16:58:05 +03:00
|
|
|
}
|
|
|
|
}
|