parser: optimize stmt()

pull/4870/head
yuyi 2020-05-12 23:18:25 +08:00 committed by GitHub
parent f33de1bc64
commit 7837abf6b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 18 deletions

View File

@ -418,6 +418,29 @@ pub fn (mut p Parser) stmt() ast.Stmt {
.key_for {
return p.for_stmt()
}
.name {
if p.peek_tok.kind in [.decl_assign, .comma] {
// `x := ...`
return p.assign_stmt()
} else if p.peek_tok.kind == .colon {
// `label:`
name := p.check_name()
p.next()
return ast.GotoLabel{
name: name
}
} else if p.peek_tok.kind == .name {
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
} else if !p.inside_if_expr && !p.inside_match_body &&
!p.inside_or_expr && p.peek_tok.kind in [.rcbr, .eof] {
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
}
epos := p.tok.position()
return ast.ExprStmt{
expr: p.expr(0)
pos: epos
}
}
.comment {
return p.comment()
}
@ -474,26 +497,9 @@ pub fn (mut p Parser) stmt() ast.Stmt {
}
}
else {
// `x := ...`
if p.tok.kind == .name && p.peek_tok.kind in [.decl_assign, .comma] {
return p.assign_stmt()
} else if p.tok.kind == .name && p.peek_tok.kind == .colon {
// `label:`
name := p.check_name()
p.next()
return ast.GotoLabel{
name: name
}
} else if p.tok.kind == .name && p.peek_tok.kind == .name {
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
} else if p.tok.kind == .name && !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr &&
p.peek_tok.kind in [.rcbr, .eof] {
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
}
epos := p.tok.position()
expr := p.expr(0)
return ast.ExprStmt{
expr: expr
expr: p.expr(0)
pos: epos
}
}