1 //! There are many AstNodes, but only a few tokens, so we hand-write them here.
5 convert::{TryFrom, TryInto},
8 use rustc_lexer::unescape::{unescape_literal, Mode};
11 ast::{self, AstToken},
16 pub fn kind(&self) -> CommentKind {
17 CommentKind::from_text(self.text())
20 pub fn is_inner(&self) -> bool {
21 self.kind().doc == Some(CommentPlacement::Inner)
24 pub fn is_outer(&self) -> bool {
25 self.kind().doc == Some(CommentPlacement::Outer)
28 pub fn prefix(&self) -> &'static str {
29 let &(prefix, _kind) = CommentKind::BY_PREFIX
31 .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
36 /// Returns the textual content of a doc comment block as a single string.
37 /// That is, strips leading `///` (+ optional 1 character of whitespace),
38 /// trailing `*/`, trailing whitespace and then joins the lines.
39 pub fn doc_comment(&self) -> Option<&str> {
40 let kind = self.kind();
42 CommentKind { shape, doc: Some(_) } => {
43 let prefix = kind.prefix();
44 let text = &self.text()[prefix.len()..];
45 let ws = text.chars().next().filter(|c| c.is_whitespace());
46 let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
48 CommentShape::Block if text.ends_with("*/") => {
49 Some(&text[..text.len() - "*/".len()])
59 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
60 pub struct CommentKind {
61 pub shape: CommentShape,
62 pub doc: Option<CommentPlacement>,
65 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
66 pub enum CommentShape {
72 pub fn is_line(self) -> bool {
73 self == CommentShape::Line
76 pub fn is_block(self) -> bool {
77 self == CommentShape::Block
81 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
82 pub enum CommentPlacement {
88 const BY_PREFIX: [(&'static str, CommentKind); 9] = [
89 ("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
90 ("/***", CommentKind { shape: CommentShape::Block, doc: None }),
91 ("////", CommentKind { shape: CommentShape::Line, doc: None }),
92 ("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
93 ("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
94 ("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }),
95 ("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }),
96 ("//", CommentKind { shape: CommentShape::Line, doc: None }),
97 ("/*", CommentKind { shape: CommentShape::Block, doc: None }),
100 pub(crate) fn from_text(text: &str) -> CommentKind {
101 let &(_prefix, kind) = CommentKind::BY_PREFIX
103 .find(|&(prefix, _kind)| text.starts_with(prefix))
108 fn prefix(&self) -> &'static str {
109 let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap();
114 impl ast::Whitespace {
115 pub fn spans_multiple_lines(&self) -> bool {
116 let text = self.text();
117 text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
121 pub struct QuoteOffsets {
122 pub quotes: (TextRange, TextRange),
123 pub contents: TextRange,
127 fn new(literal: &str) -> Option<QuoteOffsets> {
128 let left_quote = literal.find('"')?;
129 let right_quote = literal.rfind('"')?;
130 if left_quote == right_quote {
131 // `literal` only contains one quote
135 let start = TextSize::from(0);
136 let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"');
137 let right_quote = TextSize::try_from(right_quote).unwrap();
138 let end = TextSize::of(literal);
140 let res = QuoteOffsets {
141 quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
142 contents: TextRange::new(left_quote, right_quote),
149 pub fn is_raw(&self) -> bool {
150 self.text().starts_with('r')
152 pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
153 let contents_range = self.text_range_between_quotes()?;
154 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
155 Some(range + contents_range.start())
158 pub fn value(&self) -> Option<Cow<'_, str>> {
160 let text = self.text();
162 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
163 return Some(Cow::Borrowed(text));
166 let text = self.text();
167 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
169 let mut buf = String::new();
170 let mut text_iter = text.chars();
171 let mut has_error = false;
172 unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
176 (Ok(c), false) => buf.push(c),
177 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
179 buf.reserve_exact(text.len());
180 buf.push_str(&text[..char_range.start]);
183 (Err(_), _) => has_error = true,
186 match (has_error, buf.capacity() == 0) {
188 (false, true) => Some(Cow::Borrowed(text)),
189 (false, false) => Some(Cow::Owned(buf)),
193 pub fn quote_offsets(&self) -> Option<QuoteOffsets> {
194 let text = self.text();
195 let offsets = QuoteOffsets::new(text)?;
196 let o = self.syntax().text_range().start();
197 let offsets = QuoteOffsets {
198 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
199 contents: offsets.contents + o,
203 pub fn text_range_between_quotes(&self) -> Option<TextRange> {
204 self.quote_offsets().map(|it| it.contents)
206 pub fn open_quote_text_range(&self) -> Option<TextRange> {
207 self.quote_offsets().map(|it| it.quotes.0)
209 pub fn close_quote_text_range(&self) -> Option<TextRange> {
210 self.quote_offsets().map(|it| it.quotes.1)
214 impl ast::ByteString {
215 pub fn is_raw(&self) -> bool {
216 self.text().starts_with("br")
221 pub enum FormatSpecifier {
238 pub trait HasFormatSpecifier: AstToken {
241 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
243 fn lex_format_specifier<F>(&self, mut callback: F)
245 F: FnMut(TextRange, FormatSpecifier),
247 let char_ranges = if let Some(char_ranges) = self.char_ranges() {
252 let mut chars = char_ranges.iter().peekable();
254 while let Some((range, first_char)) = chars.next() {
257 // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
258 if let Some((_, Ok('{'))) = chars.peek() {
259 // Escaped format specifier, `{{`
264 callback(*range, FormatSpecifier::Open);
266 // check for integer/identifier
269 .and_then(|next| next.1.as_ref().ok())
275 read_integer(&mut chars, &mut callback);
277 c if c == '_' || c.is_alphabetic() => {
279 read_identifier(&mut chars, &mut callback);
284 if let Some((_, Ok(':'))) = chars.peek() {
285 skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
287 // check for fill/align
288 let mut cloned = chars.clone().take(2);
291 .and_then(|next| next.1.as_ref().ok())
293 .unwrap_or_default();
296 .and_then(|next| next.1.as_ref().ok())
298 .unwrap_or_default();
301 // alignment specifier, first char specifies fillment
304 FormatSpecifier::Fill,
309 FormatSpecifier::Align,
317 FormatSpecifier::Align,
328 .and_then(|next| next.1.as_ref().ok())
335 FormatSpecifier::Sign,
343 if let Some((_, Ok('#'))) = chars.peek() {
346 FormatSpecifier::NumberSign,
352 let mut cloned = chars.clone().take(2);
353 let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
354 let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
356 if first == Some('0') && second != Some('$') {
357 skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
363 .and_then(|next| next.1.as_ref().ok())
368 read_integer(&mut chars, &mut callback);
369 if let Some((_, Ok('$'))) = chars.peek() {
372 FormatSpecifier::DollarSign,
377 c if c == '_' || c.is_alphabetic() => {
378 read_identifier(&mut chars, &mut callback);
380 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
385 FormatSpecifier::QuestionMark,
390 // can be either width (indicated by dollar sign, or type in which case
391 // the next sign has to be `}`)
393 chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
396 Some('$') => skip_char_and_emit(
398 FormatSpecifier::DollarSign,
404 FormatSpecifier::Close,
416 if let Some((_, Ok('.'))) = chars.peek() {
417 skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
421 .and_then(|next| next.1.as_ref().ok())
428 FormatSpecifier::Asterisk,
433 read_integer(&mut chars, &mut callback);
434 if let Some((_, Ok('$'))) = chars.peek() {
437 FormatSpecifier::DollarSign,
442 c if c == '_' || c.is_alphabetic() => {
443 read_identifier(&mut chars, &mut callback);
444 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
451 FormatSpecifier::DollarSign,
464 .and_then(|next| next.1.as_ref().ok())
471 FormatSpecifier::QuestionMark,
475 c if c == '_' || c.is_alphabetic() => {
476 read_identifier(&mut chars, &mut callback);
478 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
483 FormatSpecifier::QuestionMark,
492 if let Some((_, Ok('}'))) = chars.peek() {
493 skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
499 while let Some((_, Ok(next_char))) = chars.peek() {
510 fn skip_char_and_emit<'a, I, F>(
511 chars: &mut std::iter::Peekable<I>,
512 emit: FormatSpecifier,
515 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
516 F: FnMut(TextRange, FormatSpecifier),
518 let (range, _) = chars.next().unwrap();
519 callback(*range, emit);
522 fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
524 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
525 F: FnMut(TextRange, FormatSpecifier),
527 let (mut range, c) = chars.next().unwrap();
528 assert!(c.as_ref().unwrap().is_ascii_digit());
529 while let Some((r, Ok(next_char))) = chars.peek() {
530 if next_char.is_ascii_digit() {
532 range = range.cover(*r);
537 callback(range, FormatSpecifier::Integer);
540 fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
542 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
543 F: FnMut(TextRange, FormatSpecifier),
545 let (mut range, c) = chars.next().unwrap();
546 assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_');
547 while let Some((r, Ok(next_char))) = chars.peek() {
548 if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
550 range = range.cover(*r);
555 callback(range, FormatSpecifier::Identifier);
560 impl HasFormatSpecifier for ast::String {
563 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
564 let text = self.text();
565 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
566 let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
568 let mut res = Vec::with_capacity(text.len());
569 unescape_literal(text, Mode::Str, &mut |range, unescaped_char| {
571 TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap())
581 impl ast::IntNumber {
582 pub fn radix(&self) -> Radix {
583 match self.text().get(..2).unwrap_or_default() {
584 "0b" => Radix::Binary,
585 "0o" => Radix::Octal,
586 "0x" => Radix::Hexadecimal,
591 pub fn value(&self) -> Option<u128> {
592 let token = self.syntax();
594 let mut text = token.text();
595 if let Some(suffix) = self.suffix() {
596 text = &text[..text.len() - suffix.len()]
599 let radix = self.radix();
600 text = &text[radix.prefix_len()..];
603 if text.contains('_') {
604 buf = text.replace('_', "");
608 let value = u128::from_str_radix(text, radix as u32).ok()?;
612 pub fn suffix(&self) -> Option<&str> {
613 let text = self.text();
614 let radix = self.radix();
615 let mut indices = text.char_indices();
616 if radix != Radix::Decimal {
620 let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
621 Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
622 _ => |(_, c)| c.is_ascii_alphabetic(),
624 let (suffix_start, _) = indices.find(is_suffix_start)?;
625 Some(&text[suffix_start..])
629 impl ast::FloatNumber {
630 pub fn suffix(&self) -> Option<&str> {
631 let text = self.text();
632 let mut indices = text.char_indices();
633 let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
634 if c == 'e' || c == 'E' {
635 suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
637 Some(&text[suffix_start..])
641 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
650 pub const ALL: &'static [Radix] =
651 &[Radix::Binary, Radix::Octal, Radix::Decimal, Radix::Hexadecimal];
653 const fn prefix_len(&self) -> usize {
663 use crate::ast::{self, make, FloatNumber, IntNumber};
665 fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
666 assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
669 fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
670 assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
674 fn test_float_number_suffix() {
675 check_float_suffix("123.0", None);
676 check_float_suffix("123f32", "f32");
677 check_float_suffix("123.0e", None);
678 check_float_suffix("123.0e4", None);
679 check_float_suffix("123.0ef32", "f32");
680 check_float_suffix("123.0E4f32", "f32");
681 check_float_suffix("1_2_3.0_f32", "f32");
685 fn test_int_number_suffix() {
686 check_int_suffix("123", None);
687 check_int_suffix("123i32", "i32");
688 check_int_suffix("1_0_1_l_o_l", "l_o_l");
689 check_int_suffix("0b11", None);
690 check_int_suffix("0o11", None);
691 check_int_suffix("0xff", None);
692 check_int_suffix("0b11u32", "u32");
693 check_int_suffix("0o11u32", "u32");
694 check_int_suffix("0xffu32", "u32");
697 fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
699 ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
707 fn test_string_escape() {
708 check_string_value(r"foobar", "foobar");
709 check_string_value(r"\foobar", None);
710 check_string_value(r"\nfoobar", "\nfoobar");
711 check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");