parser: remove extra scopes from if & match & fix scope end_pos

pull/6154/head
joe-conigliaro 2020-08-18 01:51:25 +10:00
parent 6ba6a22a77
commit c01fd4ac58
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 10 additions and 5 deletions

View File

@ -52,7 +52,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
})
}
branches << ast.IfBranch{
stmts: p.parse_block()
stmts: if prev_guard { p.parse_block_no_scope(false) } else { p.parse_block() }
pos: start_pos.extend(end_pos)
body_pos: body_pos.extend(p.tok.position())
comments: comments
@ -242,14 +242,15 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
branch_last_pos := p.tok.position()
// p.warn('match block')
p.inside_match_body = true
stmts := p.parse_block()
stmts := p.parse_block_no_scope(false)
p.close_scope()
p.inside_match_body = false
post_comments := p.eat_comments()
pos := token.Position{
line_nr: branch_first_pos.line_nr
pos: branch_first_pos.pos
len: branch_last_pos.pos - branch_first_pos.pos + branch_last_pos.len
}
post_comments := p.eat_comments()
branches << ast.MatchBranch{
exprs: exprs
stmts: stmts
@ -258,7 +259,6 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
is_else: is_else
post_comments: post_comments
}
p.close_scope()
if p.tok.kind == .rcbr || (is_else && no_lcbr) {
break
}

View File

@ -298,7 +298,12 @@ pub fn (mut p Parser) open_scope() {
}
pub fn (mut p Parser) close_scope() {
p.scope.end_pos = p.tok.pos
// p.scope.end_pos = p.tok.pos
// NOTE: since this is usually called after `p.parse_block()`
// ie. when `prev_tok` is rcbr `}` we most likely want `prev_tok`
// we could do the following, but probably not needed in 99% of cases:
// `end_pos = if p.prev_tok.kind == .rcbr { p.prev_tok.pos } else { p.tok.pos }`
p.scope.end_pos = p.prev_tok.pos
p.scope.parent.children << p.scope
p.scope = p.scope.parent
}