diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-04-19 17:01:26 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-04-20 08:33:25 +1000 |
| commit | f1c32c10c476c5a77c662ce70cb04639cda3eb4b (patch) | |
| tree | 88eb4217c8ea15a0d11d89f8b6d1f1c2d476d557 /compiler/rustc_parse | |
| parent | d235ac7801367afcdd0712c43dd53fab7d6ff95b (diff) | |
| download | rust-f1c32c10c476c5a77c662ce70cb04639cda3eb4b.tar.gz rust-f1c32c10c476c5a77c662ce70cb04639cda3eb4b.zip | |
Move desugaring code into its own function.
It's not hot, so shouldn't be within the always inlined part.
Diffstat (limited to 'compiler/rustc_parse')
| -rw-r--r-- | compiler/rustc_parse/src/parser/mod.rs | 127 |
1 files changed, 60 insertions, 67 deletions
diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 20f8ab8a6e7..925d6ac405b 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -261,7 +261,7 @@ impl TokenCursor { /// This always-inlined version should only be used on hot code paths. #[inline(always)] fn inlined_next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) { - let (token, spacing) = loop { + loop { if !self.frame.open_delim { self.frame.open_delim = true; return ( @@ -269,17 +269,20 @@ impl TokenCursor { Spacing::Alone, ); } else if let Some((tree, spacing)) = self.frame.tree_cursor.next_with_spacing() { - match tree { - TokenTree::Token(token) => { - break (token, spacing); - } + return match tree { + TokenTree::Token(token) => match (desugar_doc_comments, &token) { + (true, &Token { kind: token::DocComment(_, attr_style, data), span }) => { + self.desugar(attr_style, data, span) + } + _ => (token, spacing), + }, TokenTree::Delimited(sp, delim, tts) => { // Set `open_delim` to true here because we deal with it immediately. let frame = TokenCursorFrame::new(sp, delim, true, tts, false); self.stack.push(mem::replace(&mut self.frame, frame)); - return (Token::new(token::OpenDelim(delim), sp.open), Spacing::Alone); + (Token::new(token::OpenDelim(delim), sp.open), Spacing::Alone) } - } + }; } else if !self.frame.close_delim { self.frame.close_delim = true; return ( @@ -291,69 +294,59 @@ impl TokenCursor { } else { return (Token::new(token::Eof, DUMMY_SP), Spacing::Alone); } - }; + } + } - match (desugar_doc_comments, &token) { - (true, &Token { kind: token::DocComment(_, attr_style, data), span }) => { - // Searches for the occurrences of `"#*` and returns the minimum number of `#`s - // required to wrap the text. - let mut num_of_hashes = 0; - let mut count = 0; - for ch in data.as_str().chars() { - count = match ch { - '"' => 1, - '#' if count > 0 => count + 1, - _ => 0, - }; - num_of_hashes = cmp::max(num_of_hashes, count); - } + fn desugar(&mut self, attr_style: AttrStyle, data: Symbol, span: Span) -> (Token, Spacing) { + // Searches for the occurrences of `"#*` and returns the minimum number of `#`s + // required to wrap the text. + let mut num_of_hashes = 0; + let mut count = 0; + for ch in data.as_str().chars() { + count = match ch { + '"' => 1, + '#' if count > 0 => count + 1, + _ => 0, + }; + num_of_hashes = cmp::max(num_of_hashes, count); + } - let delim_span = DelimSpan::from_single(span); - let body = TokenTree::Delimited( - delim_span, - token::Bracket, - [ - TokenTree::token(token::Ident(sym::doc, false), span), - TokenTree::token(token::Eq, span), - TokenTree::token( - TokenKind::lit(token::StrRaw(num_of_hashes), data, None), - span, - ), - ] - .iter() - .cloned() - .collect::<TokenStream>(), - ); + let delim_span = DelimSpan::from_single(span); + let body = TokenTree::Delimited( + delim_span, + token::Bracket, + [ + TokenTree::token(token::Ident(sym::doc, false), span), + TokenTree::token(token::Eq, span), + TokenTree::token(TokenKind::lit(token::StrRaw(num_of_hashes), data, None), span), + ] + .iter() + .cloned() + .collect::<TokenStream>(), + ); - self.stack.push(mem::replace( - &mut self.frame, - TokenCursorFrame::new( - delim_span, - token::NoDelim, - false, - if attr_style == AttrStyle::Inner { - [ - TokenTree::token(token::Pound, span), - TokenTree::token(token::Not, span), - body, - ] - .iter() - .cloned() - .collect::<TokenStream>() - } else { - [TokenTree::token(token::Pound, span), body] - .iter() - .cloned() - .collect::<TokenStream>() - }, - false, - ), - )); - - self.next(/* desugar_doc_comments */ false) - } - _ => (token, spacing), - } + self.stack.push(mem::replace( + &mut self.frame, + TokenCursorFrame::new( + delim_span, + token::NoDelim, + false, + if attr_style == AttrStyle::Inner { + [TokenTree::token(token::Pound, span), TokenTree::token(token::Not, span), body] + .iter() + .cloned() + .collect::<TokenStream>() + } else { + [TokenTree::token(token::Pound, span), body] + .iter() + .cloned() + .collect::<TokenStream>() + }, + false, + ), + )); + + self.next(/* desugar_doc_comments */ false) } } |
