From fbc34741cc439f28618a08670429cbc9e5c45762 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 19 Aug 2024 22:16:04 +0800 Subject: [PATCH] parser: check fn call args without comma between them (related #22021) (#22075) --- .../tests/like_operator_outside_orm_error.out | 2 +- vlib/v/parser/fn.v | 13 ++++++++----- .../tests/fn_call_args_without_comma_1_err.out | 6 ++++++ .../tests/fn_call_args_without_comma_1_err.vv | 7 +++++++ .../tests/fn_call_args_without_comma_2_err.out | 7 +++++++ .../tests/fn_call_args_without_comma_2_err.vv | 10 ++++++++++ .../fn_call_unexpected_eof_rpar_multi_line_err.out | 4 ++-- 7 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 vlib/v/parser/tests/fn_call_args_without_comma_1_err.out create mode 100644 vlib/v/parser/tests/fn_call_args_without_comma_1_err.vv create mode 100644 vlib/v/parser/tests/fn_call_args_without_comma_2_err.out create mode 100644 vlib/v/parser/tests/fn_call_args_without_comma_2_err.vv diff --git a/vlib/v/checker/tests/like_operator_outside_orm_error.out b/vlib/v/checker/tests/like_operator_outside_orm_error.out index 8e0ea9080..ccc6cd4e8 100644 --- a/vlib/v/checker/tests/like_operator_outside_orm_error.out +++ b/vlib/v/checker/tests/like_operator_outside_orm_error.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/like_operator_outside_orm_error.vv:4:15: error: only `c`, `r`, `js` are recognized string prefixes, but you tried to use `like` +vlib/v/checker/tests/like_operator_outside_orm_error.vv:4:15: error: unexpected name `like`, expecting `)` 2 | name := 'Luke' 3 | 4 | println(name like 'L%') diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index dfef16f6e..043d1b648 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -55,7 +55,8 @@ fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr { if p.tok.kind != .rpar { params := p.table.fns[fn_name] or { unsafe { p.table.fns['${p.mod}.${fn_name}'] } }.params if args.len < params.len && p.prev_tok.kind != .comma { - p.unexpected_with_pos(p.prev_tok.pos(), expecting: '`,`') + pos := if p.tok.kind == .eof { p.prev_tok.pos() } else { p.tok.pos() } + p.unexpected_with_pos(pos, expecting: '`,`') } else if args.len > params.len { ok_arg_pos := (args[params.len - 1] or { args[0] }).pos pos := token.Pos{ @@ -64,7 +65,8 @@ fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr { } p.unexpected_with_pos(pos.extend(p.tok.pos()), expecting: '`)`') } else { - p.unexpected_with_pos(p.prev_tok.pos(), expecting: '`)`') + pos := if p.tok.kind == .eof { p.prev_tok.pos() } else { p.tok.pos() } + p.unexpected_with_pos(pos, expecting: '`)`') } } last_pos := p.tok.pos() @@ -119,7 +121,7 @@ fn (mut p Parser) call_args() []ast.CallArg { p.inside_call_args = prev_inside_call_args } mut args := []ast.CallArg{} - for p.tok.kind != .rpar && p.tok.kind != .comma { + for p.tok.kind != .rpar { if p.tok.kind == .eof { return args } @@ -162,9 +164,10 @@ fn (mut p Parser) call_args() []ast.CallArg { comments: comments pos: pos } - if p.tok.kind == .comma { - p.next() + if p.tok.kind != .comma { + break } + p.next() } return args } diff --git a/vlib/v/parser/tests/fn_call_args_without_comma_1_err.out b/vlib/v/parser/tests/fn_call_args_without_comma_1_err.out new file mode 100644 index 000000000..59e6d655f --- /dev/null +++ b/vlib/v/parser/tests/fn_call_args_without_comma_1_err.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/fn_call_args_without_comma_1_err.vv:6:11: error: unexpected number `23`, expecting `,` + 4 | + 5 | fn main() { + 6 | greet(17 23) + | ~~ + 7 | } diff --git a/vlib/v/parser/tests/fn_call_args_without_comma_1_err.vv b/vlib/v/parser/tests/fn_call_args_without_comma_1_err.vv new file mode 100644 index 000000000..3c687a014 --- /dev/null +++ b/vlib/v/parser/tests/fn_call_args_without_comma_1_err.vv @@ -0,0 +1,7 @@ +fn greet(i1 int, i2 int) { + println('${i1} ${i2}') +} + +fn main() { + greet(17 23) +} diff --git a/vlib/v/parser/tests/fn_call_args_without_comma_2_err.out b/vlib/v/parser/tests/fn_call_args_without_comma_2_err.out new file mode 100644 index 000000000..bf02466d0 --- /dev/null +++ b/vlib/v/parser/tests/fn_call_args_without_comma_2_err.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/fn_call_args_without_comma_2_err.vv:8:3: error: unexpected number `23`, expecting `,` + 6 | greet( + 7 | 17 + 8 | 23 + | ~~ + 9 | ) + 10 | } diff --git a/vlib/v/parser/tests/fn_call_args_without_comma_2_err.vv b/vlib/v/parser/tests/fn_call_args_without_comma_2_err.vv new file mode 100644 index 000000000..d53a7c336 --- /dev/null +++ b/vlib/v/parser/tests/fn_call_args_without_comma_2_err.vv @@ -0,0 +1,10 @@ +fn greet(i1 int, i2 int) { + println('${i1} ${i2}') +} + +fn main() { + greet( + 17 + 23 + ) +} diff --git a/vlib/v/parser/tests/fn_call_unexpected_eof_rpar_multi_line_err.out b/vlib/v/parser/tests/fn_call_unexpected_eof_rpar_multi_line_err.out index 9a8ba166c..ca4fdea94 100644 --- a/vlib/v/parser/tests/fn_call_unexpected_eof_rpar_multi_line_err.out +++ b/vlib/v/parser/tests/fn_call_unexpected_eof_rpar_multi_line_err.out @@ -1,5 +1,5 @@ -vlib/v/parser/tests/fn_call_unexpected_eof_rpar_multi_line_err.vv:1:14: error: unexpected eof, expecting `)` +vlib/v/parser/tests/fn_call_unexpected_eof_rpar_multi_line_err.vv:1:15: error: unexpected name `bar`, expecting `)` 1 | println('foo' bar - | ~~~~ + | ~~~ 2 | println('baz') 3 | -- 2.39.5