Skip to content

Commit

Permalink
Improve recovery after unexpected tokens parsing sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
thombles committed Oct 24, 2017
1 parent a789fa0 commit 779886f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/libsyntax/parse/parser.rs
Expand Up @@ -1033,7 +1033,23 @@ impl<'a> Parser<'a> {
} else {
if let Err(e) = self.expect(t) {
fe(e);
break;
// Attempt to keep parsing if it was a similar separator
if let Some(ref tokens) = t.similar_tokens() {
if tokens.contains(&self.token) {
self.bump();
}
}
// Attempt to keep parsing if it was an omitted separator
match f(self) {
Ok(t) => {
v.push(t);
continue;
},
Err(mut e) => {
e.cancel();
break;
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libsyntax/parse/token.rs
Expand Up @@ -433,6 +433,16 @@ impl Token {
})
}

/// Returns tokens that are likely to be typed accidentally instead of the current token.
/// Enables better error recovery when the wrong token is found.
pub fn similar_tokens(&self) -> Option<Vec<Token>> {
match *self {
Comma => Some(vec![Dot, Lt]),
Semi => Some(vec![Colon]),
_ => None
}
}

/// Returns `true` if the token is either a special identifier or a keyword.
pub fn is_reserved_ident(&self) -> bool {
self.is_special_ident() || self.is_used_keyword() || self.is_unused_keyword()
Expand Down

0 comments on commit 779886f

Please sign in to comment.