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 case_start = cur_pos!();
let ctx = Context { let ctx = Context {
is_break_allowed: true, is_break_allowed: true,
..self.ctx() ..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 mut cons = vec![];
let is_case = is!("case"); let is_case = is!("case");
let start_of_case = cur_pos!(); let case_start = cur_pos!();
bump!(); bump!();
cases.extend(cur.take());
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 { } else {
if let Some(previous) = span_of_previous_default { if let Some(previous) = span_of_previous_default {
syntax_error!(SyntaxError::MultipleDefault { previous }); syntax_error!(SyntaxError::MultipleDefault { previous });
} }
span_of_previous_default = Some(span!(start_of_case)); span_of_previous_default = Some(span!(case_start));
None None
}; };
expect!(':'); expect!(':');
cur = Some(SwitchCase { while !eof!() && !is_one_of!("case", "default", '}') {
span: span!(case_start), cons.push(p.parse_stmt_list_item(false)?);
}
cases.push(SwitchCase {
span: Span::new(case_start, p.input.prev_span().hi(), Default::default()),
test, test,
cons: vec![], cons,
}); });
} else {
match cur {
Some(ref mut cur) => {
cur.cons.push(p.parse_stmt_list_item(false)?);
}
None => unexpected!(),
}
} }
Ok(()) 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),