Skip to content

Commit

Permalink
parser: check fn call args without comma between them
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Aug 19, 2024
1 parent 3965a6c commit dc9c083
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cmd/tools/vdoc/testdata/basic/basic.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Warning: `markdown` exists, but is not updated.
V will continue, since updates can fail due to temporary network problems,
and the existing module `markdown` may still work.
--------------------------------------------------
module basic

const source_root = 'temp' // some const
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/like_operator_outside_orm_error.out
Original file line number Diff line number Diff line change
@@ -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%')
Expand Down
13 changes: 8 additions & 5 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/parser/tests/fn_call_args_without_comma_1_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
7 changes: 7 additions & 0 deletions vlib/v/parser/tests/fn_call_args_without_comma_1_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn greet(i1 int, i2 int) {
println('${i1} ${i2}')
}

fn main() {
greet(17 23)
}
7 changes: 7 additions & 0 deletions vlib/v/parser/tests/fn_call_args_without_comma_2_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
10 changes: 10 additions & 0 deletions vlib/v/parser/tests/fn_call_args_without_comma_2_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn greet(i1 int, i2 int) {
println('${i1} ${i2}')
}

fn main() {
greet(
17
23
)
}
Original file line number Diff line number Diff line change
@@ -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 |

0 comments on commit dc9c083

Please sign in to comment.