parser: minor simplification of `stmt` (#7550)

pull/7561/head
yuyi 2020-12-25 22:59:13 +08:00 committed by GitHub
parent 8cd74df2f3
commit 3345925883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 39 deletions

View File

@ -629,48 +629,46 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
.key_for { .key_for {
return p.for_stmt() return p.for_stmt()
} }
.name, .key_mut, .key_shared, .key_atomic, .key_static, .mul { .name {
if p.tok.kind == .name { if p.tok.lit == 'sql' {
if p.tok.lit == 'sql' { return p.sql_stmt()
return p.sql_stmt() }
} if p.peek_tok.kind == .colon {
if p.peek_tok.kind == .colon { // `label:`
// `label:` spos := p.tok.position()
spos := p.tok.position() name := p.check_name()
name := p.check_name() p.next()
p.next() if p.tok.kind == .key_for {
if p.tok.kind == .key_for { mut stmt := p.stmt(is_top_level)
mut stmt := p.stmt(is_top_level) match mut stmt {
match mut stmt { ast.ForStmt {
ast.ForStmt { stmt.label = name
stmt.label = name return stmt
return stmt }
} ast.ForInStmt {
ast.ForInStmt { stmt.label = name
stmt.label = name return stmt
return stmt }
} ast.ForCStmt {
ast.ForCStmt { stmt.label = name
stmt.label = name return stmt
return stmt }
} else {
else { assert false
assert false
}
} }
} }
return ast.GotoLabel{
name: name
pos: spos.extend(p.tok.position())
}
} else if p.peek_tok.kind == .name {
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
return ast.Stmt{}
} else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr &&
p.peek_tok.kind in [.rcbr, .eof] && !p.mark_var_as_used(p.tok.lit) {
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
return ast.Stmt{}
} }
return ast.GotoLabel{
name: name
pos: spos.extend(p.tok.position())
}
} else if p.peek_tok.kind == .name {
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
return ast.Stmt{}
} else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr &&
p.peek_tok.kind in [.rcbr, .eof] && !p.mark_var_as_used(p.tok.lit) {
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
return ast.Stmt{}
} }
return p.parse_multi_expr(is_top_level) return p.parse_multi_expr(is_top_level)
} }