Add a new special case to Parser::look_ahead. · rust-lang/rust@100f3fd (original) (raw)

Original file line number Diff line number Diff line change
@@ -1118,6 +1118,35 @@ impl<'a> Parser<'a> {
1118 1118 return looker(&self.token);
1119 1119 }
1120 1120
1121 +// Typically around 98% of the `dist > 0` cases have `dist == 1`, so we
1122 +// have a fast special case for that.
1123 +if dist == 1 {
1124 +// The index is zero because the tree cursor's index always points
1125 +// to the next token to be gotten.
1126 +match self.token_cursor.tree_cursor.look_ahead(0) {
1127 +Some(tree) => {
1128 +// Indexing stayed within the current token tree.
1129 +return match tree {
1130 +TokenTree::Token(token, _) => looker(token),
1131 +TokenTree::Delimited(dspan, _, delim, _) => {
1132 +looker(&Token::new(token::OpenDelim(*delim), dspan.open))
1133 +}
1134 +};
1135 +}
1136 +None => {
1137 +// The tree cursor lookahead went (one) past the end of the
1138 +// current token tree. Try to return a close delimiter.
1139 +if let Some(&(_, span, _, delim)) = self.token_cursor.stack.last()
1140 + && delim != Delimiter::Invisible
1141 +{
1142 +// We are not in the outermost token stream, so we have
1143 +// delimiters. Also, those delimiters are not skipped.
1144 +return looker(&Token::new(token::CloseDelim(delim), span.close));
1145 +}
1146 +}
1147 +}
1148 +}
1149 +
1121 1150 // Just clone the token cursor and use `next`, skipping delimiters as
1122 1151 // necessary. Slow but simple.
1123 1152 let mut cursor = self.token_cursor.clone();