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

scanner: add error for invalid newline rune literal, make errors more informative (#19091)

This commit is contained in:
yuyi 2023-08-09 13:49:47 +08:00 committed by GitHub
parent 6813a12339
commit b7afe6b236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 10 deletions

View File

@ -1472,17 +1472,23 @@ fn (mut s Scanner) ident_char() string {
u := c.runes() u := c.runes()
if u.len != 1 { if u.len != 1 {
if escaped_hex || escaped_unicode { if escaped_hex || escaped_unicode {
s.error('invalid character literal `${orig}` => `${c}` (${u}) (escape sequence did not refer to a singular rune)') s.error_with_pos('invalid character literal `${orig}` => `${c}` (${u}) (escape sequence did not refer to a singular rune)',
lspos)
} else if u.len == 0 { } else if u.len == 0 {
s.add_error_detail_with_pos('use quotes for strings, backticks for characters', s.add_error_detail_with_pos('use quotes for strings, backticks for characters',
lspos) lspos)
s.error('invalid empty character literal `${orig}`') s.error_with_pos('invalid empty character literal `${orig}`', lspos)
} else { } else {
s.add_error_detail_with_pos('use quotes for strings, backticks for characters', s.add_error_detail_with_pos('use quotes for strings, backticks for characters',
lspos) lspos)
s.error('invalid character literal `${orig}` => `${c}` (${u}) (more than one character)') s.error_with_pos('invalid character literal `${orig}` => `${c}` (${u}) (more than one character)',
lspos)
} }
} }
} else if c == '\n' {
s.add_error_detail_with_pos('use quotes for strings, backticks for characters',
lspos)
s.error_with_pos('invalid character literal, use \`\\n\` instead', lspos)
} }
// Escapes a `'` character // Escapes a `'` character
if c == "'" { if c == "'" {
@ -1566,15 +1572,19 @@ fn (mut s Scanner) eat_details() string {
} }
pub fn (mut s Scanner) warn(msg string) { pub fn (mut s Scanner) warn(msg string) {
if s.pref.warns_are_errors {
s.error(msg)
return
}
pos := token.Pos{ pos := token.Pos{
line_nr: s.line_nr line_nr: s.line_nr
pos: s.pos pos: s.pos
col: s.current_column() - 1 col: s.current_column() - 1
} }
s.warn_with_pos(msg, pos)
}
pub fn (mut s Scanner) warn_with_pos(msg string, pos token.Pos) {
if s.pref.warns_are_errors {
s.error_with_pos(msg, pos)
return
}
details := s.eat_details() details := s.eat_details()
if s.pref.output_mode == .stdout && !s.pref.check_only { if s.pref.output_mode == .stdout && !s.pref.check_only {
util.show_compiler_message('warning:', util.show_compiler_message('warning:',
@ -1604,6 +1614,10 @@ pub fn (mut s Scanner) error(msg string) {
pos: s.pos pos: s.pos
col: s.current_column() - 1 col: s.current_column() - 1
} }
s.error_with_pos(msg, pos)
}
pub fn (mut s Scanner) error_with_pos(msg string, pos token.Pos) {
details := s.eat_details() details := s.eat_details()
if s.pref.output_mode == .stdout && !s.pref.check_only { if s.pref.output_mode == .stdout && !s.pref.check_only {
util.show_compiler_message('error:', util.show_compiler_message('error:',

View File

@ -1,10 +1,10 @@
vlib/v/scanner/tests/empty_character_literal_err.vv:2:8: error: invalid empty character literal `` vlib/v/scanner/tests/empty_character_literal_err.vv:2:7: error: invalid empty character literal ``
1 | fn main() { 1 | fn main() {
2 | a := `` 2 | a := ``
| ^ | ^
3 | println(a) 3 | println(a)
4 | } 4 | }
Details: Details:
vlib/v/scanner/tests/empty_character_literal_err.vv:2:7: details: use quotes for strings, backticks for characters vlib/v/scanner/tests/empty_character_literal_err.vv:2:7: details: use quotes for strings, backticks for characters
1 | fn main() { 1 | fn main() {
2 | a := `` 2 | a := ``

View File

@ -0,0 +1,13 @@
vlib/v/scanner/tests/newline_character_literal_err.vv:2:7: error: invalid character literal, use `\n` instead
1 | fn main() {
2 | a := `
| ^
3 | `
4 | println(a)
Details:
vlib/v/scanner/tests/newline_character_literal_err.vv:2:7: details: use quotes for strings, backticks for characters
1 | fn main() {
2 | a := `
| ^
3 | `
4 | println(a)

View File

@ -0,0 +1,5 @@
fn main() {
a := `
`
println(a)
}