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 node as a single string with prefix and suffix
38 pub fn doc_comment(&self) -> Option<&str> {
39 let kind = self.kind();
41 CommentKind { shape, doc: Some(_) } => {
42 let prefix = kind.prefix();
43 let text = &self.text()[prefix.len()..];
44 let text = if shape == CommentShape::Block {
45 text.strip_suffix("*/").unwrap_or(text)
56 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
57 pub struct CommentKind {
58 pub shape: CommentShape,
59 pub doc: Option<CommentPlacement>,
62 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
63 pub enum CommentShape {
69 pub fn is_line(self) -> bool {
70 self == CommentShape::Line
73 pub fn is_block(self) -> bool {
74 self == CommentShape::Block
78 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
79 pub enum CommentPlacement {
85 const BY_PREFIX: [(&'static str, CommentKind); 9] = [
86 ("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
87 ("/***", CommentKind { shape: CommentShape::Block, doc: None }),
88 ("////", CommentKind { shape: CommentShape::Line, doc: None }),
89 ("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
90 ("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
91 ("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }),
92 ("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }),
93 ("//", CommentKind { shape: CommentShape::Line, doc: None }),
94 ("/*", CommentKind { shape: CommentShape::Block, doc: None }),
97 pub(crate) fn from_text(text: &str) -> CommentKind {
98 let &(_prefix, kind) = CommentKind::BY_PREFIX
100 .find(|&(prefix, _kind)| text.starts_with(prefix))
105 pub fn prefix(&self) -> &'static str {
107 CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
112 impl ast::Whitespace {
113 pub fn spans_multiple_lines(&self) -> bool {
114 let text = self.text();
115 text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
119 pub struct QuoteOffsets {
120 pub quotes: (TextRange, TextRange),
121 pub contents: TextRange,
125 fn new(literal: &str) -> Option<QuoteOffsets> {
126 let left_quote = literal.find('"')?;
127 let right_quote = literal.rfind('"')?;
128 if left_quote == right_quote {
129 // `literal` only contains one quote
133 let start = TextSize::from(0);
134 let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"');
135 let right_quote = TextSize::try_from(right_quote).unwrap();
136 let end = TextSize::of(literal);
138 let res = QuoteOffsets {
139 quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
140 contents: TextRange::new(left_quote, right_quote),
146 pub trait IsString: AstToken {
147 fn quote_offsets(&self) -> Option<QuoteOffsets> {
148 let text = self.text();
149 let offsets = QuoteOffsets::new(text)?;
150 let o = self.syntax().text_range().start();
151 let offsets = QuoteOffsets {
152 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
153 contents: offsets.contents + o,
157 fn text_range_between_quotes(&self) -> Option<TextRange> {
158 self.quote_offsets().map(|it| it.contents)
160 fn open_quote_text_range(&self) -> Option<TextRange> {
161 self.quote_offsets().map(|it| it.quotes.0)
163 fn close_quote_text_range(&self) -> Option<TextRange> {
164 self.quote_offsets().map(|it| it.quotes.1)
168 impl IsString for ast::String {}
171 pub fn is_raw(&self) -> bool {
172 self.text().starts_with('r')
174 pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
175 let contents_range = self.text_range_between_quotes()?;
176 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
177 Some(range + contents_range.start())
180 pub fn value(&self) -> Option<Cow<'_, str>> {
182 let text = self.text();
184 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
185 return Some(Cow::Borrowed(text));
188 let text = self.text();
189 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
191 let mut buf = String::new();
192 let mut text_iter = text.chars();
193 let mut has_error = false;
194 unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
198 (Ok(c), false) => buf.push(c),
199 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
201 buf.reserve_exact(text.len());
202 buf.push_str(&text[..char_range.start]);
205 (Err(_), _) => has_error = true,
208 match (has_error, buf.capacity() == 0) {
210 (false, true) => Some(Cow::Borrowed(text)),
211 (false, false) => Some(Cow::Owned(buf)),
216 impl IsString for ast::ByteString {}
218 impl ast::ByteString {
219 pub fn is_raw(&self) -> bool {
220 self.text().starts_with("br")
223 pub fn value(&self) -> Option<Cow<'_, [u8]>> {
225 let text = self.text();
227 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
228 return Some(Cow::Borrowed(text.as_bytes()));
231 let text = self.text();
232 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
234 let mut buf: Vec<u8> = Vec::new();
235 let mut text_iter = text.chars();
236 let mut has_error = false;
237 unescape_literal(text, Mode::ByteStr, &mut |char_range, unescaped_char| match (
241 (Ok(c), false) => buf.push(c as u8),
242 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
244 buf.reserve_exact(text.len());
245 buf.extend_from_slice(text[..char_range.start].as_bytes());
248 (Err(_), _) => has_error = true,
251 match (has_error, buf.capacity() == 0) {
253 (false, true) => Some(Cow::Borrowed(text.as_bytes())),
254 (false, false) => Some(Cow::Owned(buf)),
260 pub enum FormatSpecifier {
277 pub trait HasFormatSpecifier: AstToken {
280 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
282 fn lex_format_specifier<F>(&self, mut callback: F)
284 F: FnMut(TextRange, FormatSpecifier),
286 let char_ranges = if let Some(char_ranges) = self.char_ranges() {
291 let mut chars = char_ranges.iter().peekable();
293 while let Some((range, first_char)) = chars.next() {
296 // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
297 if let Some((_, Ok('{'))) = chars.peek() {
298 // Escaped format specifier, `{{`
303 callback(*range, FormatSpecifier::Open);
305 // check for integer/identifier
308 .and_then(|next| next.1.as_ref().ok())
314 read_integer(&mut chars, &mut callback);
316 c if c == '_' || c.is_alphabetic() => {
318 read_identifier(&mut chars, &mut callback);
323 if let Some((_, Ok(':'))) = chars.peek() {
324 skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
326 // check for fill/align
327 let mut cloned = chars.clone().take(2);
330 .and_then(|next| next.1.as_ref().ok())
332 .unwrap_or_default();
335 .and_then(|next| next.1.as_ref().ok())
337 .unwrap_or_default();
340 // alignment specifier, first char specifies fillment
343 FormatSpecifier::Fill,
348 FormatSpecifier::Align,
356 FormatSpecifier::Align,
367 .and_then(|next| next.1.as_ref().ok())
374 FormatSpecifier::Sign,
382 if let Some((_, Ok('#'))) = chars.peek() {
385 FormatSpecifier::NumberSign,
391 let mut cloned = chars.clone().take(2);
392 let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
393 let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
395 if first == Some('0') && second != Some('$') {
396 skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
402 .and_then(|next| next.1.as_ref().ok())
407 read_integer(&mut chars, &mut callback);
408 if let Some((_, Ok('$'))) = chars.peek() {
411 FormatSpecifier::DollarSign,
416 c if c == '_' || c.is_alphabetic() => {
417 read_identifier(&mut chars, &mut callback);
419 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
424 FormatSpecifier::QuestionMark,
429 // can be either width (indicated by dollar sign, or type in which case
430 // the next sign has to be `}`)
432 chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
435 Some('$') => skip_char_and_emit(
437 FormatSpecifier::DollarSign,
443 FormatSpecifier::Close,
455 if let Some((_, Ok('.'))) = chars.peek() {
456 skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
460 .and_then(|next| next.1.as_ref().ok())
467 FormatSpecifier::Asterisk,
472 read_integer(&mut chars, &mut callback);
473 if let Some((_, Ok('$'))) = chars.peek() {
476 FormatSpecifier::DollarSign,
481 c if c == '_' || c.is_alphabetic() => {
482 read_identifier(&mut chars, &mut callback);
483 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
490 FormatSpecifier::DollarSign,
503 .and_then(|next| next.1.as_ref().ok())
510 FormatSpecifier::QuestionMark,
514 c if c == '_' || c.is_alphabetic() => {
515 read_identifier(&mut chars, &mut callback);
517 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
522 FormatSpecifier::QuestionMark,
531 if let Some((_, Ok('}'))) = chars.peek() {
532 skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
538 while let Some((_, Ok(next_char))) = chars.peek() {
539 if next_char == &'{' {
548 fn skip_char_and_emit<'a, I, F>(
549 chars: &mut std::iter::Peekable<I>,
550 emit: FormatSpecifier,
553 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
554 F: FnMut(TextRange, FormatSpecifier),
556 let (range, _) = chars.next().unwrap();
557 callback(*range, emit);
560 fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
562 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
563 F: FnMut(TextRange, FormatSpecifier),
565 let (mut range, c) = chars.next().unwrap();
566 assert!(c.as_ref().unwrap().is_ascii_digit());
567 while let Some((r, Ok(next_char))) = chars.peek() {
568 if next_char.is_ascii_digit() {
570 range = range.cover(*r);
575 callback(range, FormatSpecifier::Integer);
578 fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
580 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
581 F: FnMut(TextRange, FormatSpecifier),
583 let (mut range, c) = chars.next().unwrap();
584 assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_');
585 while let Some((r, Ok(next_char))) = chars.peek() {
586 if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
588 range = range.cover(*r);
593 callback(range, FormatSpecifier::Identifier);
598 impl HasFormatSpecifier for ast::String {
601 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
602 let text = self.text();
603 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
604 let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
606 let mut res = Vec::with_capacity(text.len());
607 unescape_literal(text, Mode::Str, &mut |range, unescaped_char| {
609 TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap())
619 impl ast::IntNumber {
620 pub fn radix(&self) -> Radix {
621 match self.text().get(..2).unwrap_or_default() {
622 "0b" => Radix::Binary,
623 "0o" => Radix::Octal,
624 "0x" => Radix::Hexadecimal,
629 pub fn value(&self) -> Option<u128> {
630 let token = self.syntax();
632 let mut text = token.text();
633 if let Some(suffix) = self.suffix() {
634 text = &text[..text.len() - suffix.len()]
637 let radix = self.radix();
638 text = &text[radix.prefix_len()..];
641 if text.contains('_') {
642 buf = text.replace('_', "");
646 let value = u128::from_str_radix(text, radix as u32).ok()?;
650 pub fn suffix(&self) -> Option<&str> {
651 let text = self.text();
652 let radix = self.radix();
653 let mut indices = text.char_indices();
654 if radix != Radix::Decimal {
658 let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
659 Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
660 _ => |(_, c)| c.is_ascii_alphabetic(),
662 let (suffix_start, _) = indices.find(is_suffix_start)?;
663 Some(&text[suffix_start..])
667 impl ast::FloatNumber {
668 pub fn suffix(&self) -> Option<&str> {
669 let text = self.text();
670 let mut indices = text.char_indices();
671 let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
672 if c == 'e' || c == 'E' {
673 suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
675 Some(&text[suffix_start..])
679 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
688 pub const ALL: &'static [Radix] =
689 &[Radix::Binary, Radix::Octal, Radix::Decimal, Radix::Hexadecimal];
691 const fn prefix_len(&self) -> usize {
701 use crate::ast::{self, make, FloatNumber, IntNumber};
703 fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
704 assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
707 fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
708 assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
712 fn test_float_number_suffix() {
713 check_float_suffix("123.0", None);
714 check_float_suffix("123f32", "f32");
715 check_float_suffix("123.0e", None);
716 check_float_suffix("123.0e4", None);
717 check_float_suffix("123.0ef32", "f32");
718 check_float_suffix("123.0E4f32", "f32");
719 check_float_suffix("1_2_3.0_f32", "f32");
723 fn test_int_number_suffix() {
724 check_int_suffix("123", None);
725 check_int_suffix("123i32", "i32");
726 check_int_suffix("1_0_1_l_o_l", "l_o_l");
727 check_int_suffix("0b11", None);
728 check_int_suffix("0o11", None);
729 check_int_suffix("0xff", None);
730 check_int_suffix("0b11u32", "u32");
731 check_int_suffix("0o11u32", "u32");
732 check_int_suffix("0xffu32", "u32");
735 fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
737 ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
745 fn test_string_escape() {
746 check_string_value(r"foobar", "foobar");
747 check_string_value(r"\foobar", None);
748 check_string_value(r"\nfoobar", "\nfoobar");
749 check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");