Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: allow array cast syntax #19999

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3181,7 +3181,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
c.error('cannot cast string to `${tt}` outside `unsafe`, use ${tt}(s.str) instead',
node.pos)
} else if final_from_sym.kind == .array && !from_type.is_ptr() && to_type != ast.string_type
&& !(to_type.has_flag(.option) && from_type.idx() == to_type.idx()) {
&& !(to_type.has_flag(.option) && from_type.idx() == to_type.idx())
&& to_sym.kind != final_from_sym.kind {
ft := c.table.type_to_str(from_type)
tt := c.table.type_to_str(to_type)
c.error('cannot cast array `${ft}` to `${tt}`', node.pos)
Expand Down
6 changes: 4 additions & 2 deletions vlib/v/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
if p.expecting_type {
// parse json.decode type (`json.decode([]User, s)`)
node = p.name_expr()
} else if p.is_amp && p.peek_tok.kind == .rsbr {
} else if (p.is_amp && p.peek_tok.kind == .rsbr)
|| (p.peek_tok.kind == .rsbr && (p.peek_token(3).kind == .lpar
|| p.is_array_cast(3)) && p.tok.line_nr == p.peek_token(3).line_nr) {
mut n := 2
mut peek_n_tok := p.peek_token(n)
for peek_n_tok.kind in [.name, .dot] {
for peek_n_tok.kind in [.name, .dot, .lsbr, .rsbr] {
n++
peek_n_tok = p.peek_token(n)
}
Expand Down
27 changes: 21 additions & 6 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -539,28 +539,43 @@ fn (p &Parser) is_fn_type_decl() bool {
return true
}

fn (p &Parser) is_array_type() bool {
mut i := 1
@[inline]
fn (p &Parser) is_array_cast(offset int) bool {
name_offset := p.is_array_type_from_offset(offset)
if name_offset != -1 {
return p.peek_token(name_offset + 1).kind == .lpar
}
return false
}

@[inline]
fn (p &Parser) is_array_type_from_offset(offset int) int {
mut tok := p.tok
line_nr := p.tok.line_nr
mut i := offset

for {
tok = p.peek_token(i)
if tok.line_nr != line_nr {
return false
return -1
}
if tok.kind in [.name, .amp] {
return true
return i
}
if tok.kind == .eof {
if tok.kind !in [.number, .name, .amp, .lsbr, .rsbr] {
break
}
i++
if tok.kind == .lsbr || tok.kind != .rsbr {
continue
}
}
return false
return -1
}

@[inline]
fn (p &Parser) is_array_type() bool {
return p.is_array_type_from_offset(1) != -1
}

fn (mut p Parser) open_scope() {
Expand Down