Fix span of the switch statement. (#553)

This commit is contained in:
David Sherret 2020-01-02 02:46:56 -05:00 committed by 강동윤
parent 05b7328002
commit 5f6b11348a

View File

@ -415,57 +415,49 @@ impl<'a, I: Tokens> Parser<'a, I> {
let discriminant = self.include_in_expr(true).parse_expr()?; let discriminant = self.include_in_expr(true).parse_expr()?;
expect!(')'); expect!(')');
let mut cur = None;
let mut cases = vec![]; let mut cases = vec![];
let mut span_of_previous_default = None; let mut span_of_previous_default = None;
expect!('{'); expect!('{');
while !eof!() && !is!('}') { let ctx = Context {
let case_start = cur_pos!(); is_break_allowed: true,
let ctx = Context { ..self.ctx()
is_break_allowed: true, };
..self.ctx()
};
self.with_ctx(ctx).parse_with(|p| { self.with_ctx(ctx).parse_with(|p| {
if is_one_of!("case", "default") { while is_one_of!("case", "default") {
let is_case = is!("case"); let mut cons = vec![];
let start_of_case = cur_pos!(); let is_case = is!("case");
bump!(); let case_start = cur_pos!();
cases.extend(cur.take()); bump!();
let test = if is_case { let test = if is_case {
p.include_in_expr(true).parse_expr().map(Some)? p.include_in_expr(true).parse_expr().map(Some)?
} else {
if let Some(previous) = span_of_previous_default {
syntax_error!(SyntaxError::MultipleDefault { previous });
}
span_of_previous_default = Some(span!(start_of_case));
None
};
expect!(':');
cur = Some(SwitchCase {
span: span!(case_start),
test,
cons: vec![],
});
} else { } else {
match cur { if let Some(previous) = span_of_previous_default {
Some(ref mut cur) => { syntax_error!(SyntaxError::MultipleDefault { previous });
cur.cons.push(p.parse_stmt_list_item(false)?);
}
None => unexpected!(),
} }
span_of_previous_default = Some(span!(case_start));
None
};
expect!(':');
while !eof!() && !is_one_of!("case", "default", '}') {
cons.push(p.parse_stmt_list_item(false)?);
} }
Ok(()) cases.push(SwitchCase {
})? span: Span::new(case_start, p.input.prev_span().hi(), Default::default()),
} test,
cons,
});
}
Ok(())
})?;
// eof or rbrace // eof or rbrace
expect!('}'); expect!('}');
cases.extend(cur);
Ok(Stmt::Switch(SwitchStmt { Ok(Stmt::Switch(SwitchStmt {
span: span!(switch_start), span: span!(switch_start),