Skip to content

Commit

Permalink
v.token: add inline next_to() and cleanup related calls (#19226)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Aug 28, 2023
1 parent 9d4233d commit 34842b1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion vlib/v/parser/parser.v
Expand Up @@ -2761,7 +2761,7 @@ fn (mut p Parser) name_expr() ast.Expr {
return p.struct_init(p.mod + '.' + p.tok.lit, .normal, is_option) // short_syntax: false
} else if p.peek_tok.kind == .lcbr
&& ((p.inside_if && lit0_is_capital && p.tok.lit.len > 1 && !known_var && language == .v)
|| (p.inside_match_case && p.tok.kind == .name && p.peek_tok.pos - p.tok.pos == p.tok.len)) {
|| (p.inside_match_case && p.tok.kind == .name && p.peek_tok.is_next_to(p.tok))) {
// `if a == Foo{} {...}` or `match foo { Foo{} {...} }`
return p.struct_init(p.mod + '.' + p.tok.lit, .normal, is_option)
} else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/parser/struct.v
Expand Up @@ -586,7 +586,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
if p.tok.kind == .name && p.tok.lit.len > 0 && p.tok.lit[0].is_capital()
&& (p.peek_tok.line_nr != p.tok.line_nr
|| p.peek_tok.kind !in [.name, .amp, .lsbr, .lpar]
|| (p.peek_tok.kind == .lsbr && p.peek_tok.pos - p.tok.pos == p.tok.len)) {
|| (p.peek_tok.kind == .lsbr && p.peek_tok.is_next_to(p.tok))) {
iface_pos := p.tok.pos()
mut iface_name := p.tok.lit
iface_type := p.parse_type()
Expand Down Expand Up @@ -641,7 +641,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
is_mut = true
mut_pos = fields.len
}
if p.peek_tok.kind in [.lt, .lsbr] && p.peek_tok.pos - p.tok.pos == p.tok.len {
if p.peek_tok.kind in [.lt, .lsbr] && p.peek_tok.is_next_to(p.tok) {
p.error_with_pos("no need to add generic type names in generic interface's method",
p.peek_tok.pos())
return ast.InterfaceDecl{}
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/token/token.v
Expand Up @@ -371,6 +371,11 @@ pub fn (t Kind) str() string {
return token.token_str[idx]
}

[inline]
pub fn (t Token) is_next_to(pre_token Token) bool {
return t.pos - pre_token.pos == pre_token.len
}

pub fn (t Token) str() string {
mut s := t.kind.str()
if s.len == 0 {
Expand Down

0 comments on commit 34842b1

Please sign in to comment.