1 //! There are many AstNodes, but only a few tokens, so we hand-write them here.
5 use rustc_lexer::unescape::{unescape_literal, Mode};
13 pub fn kind(&self) -> CommentKind {
14 CommentKind::from_text(self.text())
17 pub fn is_doc(&self) -> bool {
18 self.kind().doc.is_some()
21 pub fn is_inner(&self) -> bool {
22 self.kind().doc == Some(CommentPlacement::Inner)
25 pub fn is_outer(&self) -> bool {
26 self.kind().doc == Some(CommentPlacement::Outer)
29 pub fn prefix(&self) -> &'static str {
30 let &(prefix, _kind) = CommentKind::BY_PREFIX
32 .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
37 /// Returns the textual content of a doc comment node as a single string with prefix and suffix
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 text = if shape == CommentShape::Block {
46 text.strip_suffix("*/").unwrap_or(text)
57 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
58 pub struct CommentKind {
59 pub shape: CommentShape,
60 pub doc: Option<CommentPlacement>,
63 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
64 pub enum CommentShape {
70 pub fn is_line(self) -> bool {
71 self == CommentShape::Line
74 pub fn is_block(self) -> bool {
75 self == CommentShape::Block
79 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
80 pub enum CommentPlacement {
86 const BY_PREFIX: [(&'static str, CommentKind); 9] = [
87 ("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
88 ("/***", CommentKind { shape: CommentShape::Block, doc: None }),
89 ("////", CommentKind { shape: CommentShape::Line, doc: None }),
90 ("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
91 ("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
92 ("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }),
93 ("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }),
94 ("//", CommentKind { shape: CommentShape::Line, doc: None }),
95 ("/*", CommentKind { shape: CommentShape::Block, doc: None }),
98 pub(crate) fn from_text(text: &str) -> CommentKind {
99 let &(_prefix, kind) = CommentKind::BY_PREFIX
101 .find(|&(prefix, _kind)| text.starts_with(prefix))
106 pub fn prefix(&self) -> &'static str {
108 CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
113 impl ast::Whitespace {
114 pub fn spans_multiple_lines(&self) -> bool {
115 let text = self.text();
116 text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
120 pub struct QuoteOffsets {
121 pub quotes: (TextRange, TextRange),
122 pub contents: TextRange,
126 fn new(literal: &str) -> Option<QuoteOffsets> {
127 let left_quote = literal.find('"')?;
128 let right_quote = literal.rfind('"')?;
129 if left_quote == right_quote {
130 // `literal` only contains one quote
134 let start = TextSize::from(0);
135 let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"');
136 let right_quote = TextSize::try_from(right_quote).unwrap();
137 let end = TextSize::of(literal);
139 let res = QuoteOffsets {
140 quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
141 contents: TextRange::new(left_quote, right_quote),
147 pub trait IsString: AstToken {
148 fn quote_offsets(&self) -> Option<QuoteOffsets> {
149 let text = self.text();
150 let offsets = QuoteOffsets::new(text)?;
151 let o = self.syntax().text_range().start();
152 let offsets = QuoteOffsets {
153 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
154 contents: offsets.contents + o,
158 fn text_range_between_quotes(&self) -> Option<TextRange> {
159 self.quote_offsets().map(|it| it.contents)
161 fn open_quote_text_range(&self) -> Option<TextRange> {
162 self.quote_offsets().map(|it| it.quotes.0)
164 fn close_quote_text_range(&self) -> Option<TextRange> {
165 self.quote_offsets().map(|it| it.quotes.1)
169 impl IsString for ast::String {}
172 pub fn is_raw(&self) -> bool {
173 self.text().starts_with('r')
175 pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
176 let contents_range = self.text_range_between_quotes()?;
177 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
178 Some(range + contents_range.start())
181 pub fn value(&self) -> Option<Cow<'_, str>> {
183 let text = self.text();
185 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
186 return Some(Cow::Borrowed(text));
189 let text = self.text();
190 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
192 let mut buf = String::new();
193 let mut text_iter = text.chars();
194 let mut has_error = false;
195 unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
199 (Ok(c), false) => buf.push(c),
200 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
202 buf.reserve_exact(text.len());
203 buf.push_str(&text[..char_range.start]);
206 (Err(_), _) => has_error = true,
209 match (has_error, buf.capacity() == 0) {
211 (false, true) => Some(Cow::Borrowed(text)),
212 (false, false) => Some(Cow::Owned(buf)),
217 impl IsString for ast::ByteString {}
219 impl ast::ByteString {
220 pub fn is_raw(&self) -> bool {
221 self.text().starts_with("br")
224 pub fn value(&self) -> Option<Cow<'_, [u8]>> {
226 let text = self.text();
228 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
229 return Some(Cow::Borrowed(text.as_bytes()));
232 let text = self.text();
233 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
235 let mut buf: Vec<u8> = Vec::new();
236 let mut text_iter = text.chars();
237 let mut has_error = false;
238 unescape_literal(text, Mode::ByteStr, &mut |char_range, unescaped_char| match (
242 (Ok(c), false) => buf.push(c as u8),
243 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
245 buf.reserve_exact(text.len());
246 buf.extend_from_slice(text[..char_range.start].as_bytes());
249 (Err(_), _) => has_error = true,
252 match (has_error, buf.capacity() == 0) {
254 (false, true) => Some(Cow::Borrowed(text.as_bytes())),
255 (false, false) => Some(Cow::Owned(buf)),
261 pub enum FormatSpecifier {
278 pub trait HasFormatSpecifier: AstToken {
281 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
283 fn lex_format_specifier<F>(&self, mut callback: F)
285 F: FnMut(TextRange, FormatSpecifier),
287 let char_ranges = match self.char_ranges() {
288 Some(char_ranges) => char_ranges,
291 let mut chars = char_ranges
293 .filter_map(|(range, res)| Some((*range, *res.as_ref().ok()?)))
296 while let Some((range, first_char)) = chars.next() {
297 if let '{' = first_char {
298 // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
299 if let Some((_, '{')) = chars.peek() {
300 // Escaped format specifier, `{{`
305 callback(range, FormatSpecifier::Open);
307 // check for integer/identifier
308 let (_, int_char) = chars.peek().copied().unwrap_or_default();
311 '0'..='9' => read_integer(&mut chars, &mut callback),
313 c if c == '_' || c.is_alphabetic() => {
314 read_identifier(&mut chars, &mut callback)
319 if let Some((_, ':')) = chars.peek() {
320 skip_char_and_emit(&mut chars, FormatSpecifier::Colon, &mut callback);
322 // check for fill/align
323 let mut cloned = chars.clone().take(2);
324 let (_, first) = cloned.next().unwrap_or_default();
325 let (_, second) = cloned.next().unwrap_or_default();
328 // alignment specifier, first char specifies fillment
329 skip_char_and_emit(&mut chars, FormatSpecifier::Fill, &mut callback);
330 skip_char_and_emit(&mut chars, FormatSpecifier::Align, &mut callback);
333 if let '<' | '^' | '>' = first {
336 FormatSpecifier::Align,
344 match chars.peek().copied().unwrap_or_default().1 {
346 skip_char_and_emit(&mut chars, FormatSpecifier::Sign, &mut callback);
352 if let Some((_, '#')) = chars.peek() {
353 skip_char_and_emit(&mut chars, FormatSpecifier::NumberSign, &mut callback);
357 let mut cloned = chars.clone().take(2);
358 let first = cloned.next().map(|next| next.1);
359 let second = cloned.next().map(|next| next.1);
361 if first == Some('0') && second != Some('$') {
362 skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
366 match chars.peek().copied().unwrap_or_default().1 {
368 read_integer(&mut chars, &mut callback);
369 if let Some((_, '$')) = chars.peek() {
372 FormatSpecifier::DollarSign,
377 c if c == '_' || c.is_alphabetic() => {
378 read_identifier(&mut chars, &mut callback);
380 if chars.peek().map(|&(_, c)| c) == Some('?') {
383 FormatSpecifier::QuestionMark,
388 // can be either width (indicated by dollar sign, or type in which case
389 // the next sign has to be `}`)
390 let next = chars.peek().map(|&(_, c)| c);
393 Some('$') => skip_char_and_emit(
395 FormatSpecifier::DollarSign,
401 FormatSpecifier::Close,
413 if let Some((_, '.')) = chars.peek() {
414 skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
416 match chars.peek().copied().unwrap_or_default().1 {
420 FormatSpecifier::Asterisk,
425 read_integer(&mut chars, &mut callback);
426 if let Some((_, '$')) = chars.peek() {
429 FormatSpecifier::DollarSign,
434 c if c == '_' || c.is_alphabetic() => {
435 read_identifier(&mut chars, &mut callback);
436 if chars.peek().map(|&(_, c)| c) != Some('$') {
441 FormatSpecifier::DollarSign,
452 match chars.peek().copied().unwrap_or_default().1 {
456 FormatSpecifier::QuestionMark,
460 c if c == '_' || c.is_alphabetic() => {
461 read_identifier(&mut chars, &mut callback);
463 if chars.peek().map(|&(_, c)| c) == Some('?') {
466 FormatSpecifier::QuestionMark,
475 if let Some((_, '}')) = chars.peek() {
476 skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
482 fn skip_char_and_emit<I, F>(
483 chars: &mut std::iter::Peekable<I>,
484 emit: FormatSpecifier,
487 I: Iterator<Item = (TextRange, char)>,
488 F: FnMut(TextRange, FormatSpecifier),
490 let (range, _) = chars.next().unwrap();
491 callback(range, emit);
494 fn read_integer<I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
496 I: Iterator<Item = (TextRange, char)>,
497 F: FnMut(TextRange, FormatSpecifier),
499 let (mut range, c) = chars.next().unwrap();
500 assert!(c.is_ascii_digit());
501 while let Some(&(r, next_char)) = chars.peek() {
502 if next_char.is_ascii_digit() {
504 range = range.cover(r);
509 callback(range, FormatSpecifier::Integer);
512 fn read_identifier<I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
514 I: Iterator<Item = (TextRange, char)>,
515 F: FnMut(TextRange, FormatSpecifier),
517 let (mut range, c) = chars.next().unwrap();
518 assert!(c.is_alphabetic() || c == '_');
519 while let Some(&(r, next_char)) = chars.peek() {
520 if next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
522 range = range.cover(r);
527 callback(range, FormatSpecifier::Identifier);
532 impl HasFormatSpecifier for ast::String {
535 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
536 let text = self.text();
537 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
538 let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
540 let mut res = Vec::with_capacity(text.len());
541 unescape_literal(text, Mode::Str, &mut |range, unescaped_char| {
543 TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap())
553 impl ast::IntNumber {
554 pub fn radix(&self) -> Radix {
555 match self.text().get(..2).unwrap_or_default() {
556 "0b" => Radix::Binary,
557 "0o" => Radix::Octal,
558 "0x" => Radix::Hexadecimal,
563 pub fn split_into_parts(&self) -> (&str, &str, &str) {
564 let radix = self.radix();
565 let (prefix, mut text) = self.text().split_at(radix.prefix_len());
567 let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
568 Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
569 _ => |(_, c)| c.is_ascii_alphabetic(),
573 if let Some((suffix_start, _)) = text.char_indices().find(is_suffix_start) {
574 let (text2, suffix2) = text.split_at(suffix_start);
579 (prefix, text, suffix)
582 pub fn value(&self) -> Option<u128> {
583 let (_, text, _) = self.split_into_parts();
584 let value = u128::from_str_radix(&text.replace("_", ""), self.radix() as u32).ok()?;
588 pub fn suffix(&self) -> Option<&str> {
589 let (_, _, suffix) = self.split_into_parts();
590 if suffix.is_empty() {
598 impl ast::FloatNumber {
599 pub fn suffix(&self) -> Option<&str> {
600 let text = self.text();
601 let mut indices = text.char_indices();
602 let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
603 if c == 'e' || c == 'E' {
604 suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
606 Some(&text[suffix_start..])
610 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
619 pub const ALL: &'static [Radix] =
620 &[Radix::Binary, Radix::Octal, Radix::Decimal, Radix::Hexadecimal];
622 const fn prefix_len(self) -> usize {
632 use crate::ast::{self, make, FloatNumber, IntNumber};
634 fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
635 assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
638 fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
639 assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
643 fn test_float_number_suffix() {
644 check_float_suffix("123.0", None);
645 check_float_suffix("123f32", "f32");
646 check_float_suffix("123.0e", None);
647 check_float_suffix("123.0e4", None);
648 check_float_suffix("123.0ef32", "f32");
649 check_float_suffix("123.0E4f32", "f32");
650 check_float_suffix("1_2_3.0_f32", "f32");
654 fn test_int_number_suffix() {
655 check_int_suffix("123", None);
656 check_int_suffix("123i32", "i32");
657 check_int_suffix("1_0_1_l_o_l", "l_o_l");
658 check_int_suffix("0b11", None);
659 check_int_suffix("0o11", None);
660 check_int_suffix("0xff", None);
661 check_int_suffix("0b11u32", "u32");
662 check_int_suffix("0o11u32", "u32");
663 check_int_suffix("0xffu32", "u32");
666 fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
668 ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
676 fn test_string_escape() {
677 check_string_value(r"foobar", "foobar");
678 check_string_value(r"\foobar", None);
679 check_string_value(r"\nfoobar", "\nfoobar");
680 check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");