From c01fd4ac58369d79c18bd6577b56b71676dcaa86 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 18 Aug 2020 01:51:25 +1000 Subject: [PATCH] parser: remove extra scopes from if & match & fix scope end_pos --- vlib/v/parser/if_match.v | 8 ++++---- vlib/v/parser/parser.v | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index a6635613f8..ca40c69531 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -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 } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 413d335a97..b266c957f6 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 }