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_inner(&self) -> bool {
18 self.kind().doc == Some(CommentPlacement::Inner)
21 pub fn is_outer(&self) -> bool {
22 self.kind().doc == Some(CommentPlacement::Outer)
25 pub fn prefix(&self) -> &'static str {
26 let &(prefix, _kind) = CommentKind::BY_PREFIX
28 .find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
33 /// Returns the textual content of a doc comment node as a single string with prefix and suffix
35 pub fn doc_comment(&self) -> Option<&str> {
36 let kind = self.kind();
38 CommentKind { shape, doc: Some(_) } => {
39 let prefix = kind.prefix();
40 let text = &self.text()[prefix.len()..];
41 let text = if shape == CommentShape::Block {
42 text.strip_suffix("*/").unwrap_or(text)
53 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
54 pub struct CommentKind {
55 pub shape: CommentShape,
56 pub doc: Option<CommentPlacement>,
59 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
60 pub enum CommentShape {
66 pub fn is_line(self) -> bool {
67 self == CommentShape::Line
70 pub fn is_block(self) -> bool {
71 self == CommentShape::Block
75 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
76 pub enum CommentPlacement {
82 const BY_PREFIX: [(&'static str, CommentKind); 9] = [
83 ("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
84 ("/***", CommentKind { shape: CommentShape::Block, doc: None }),
85 ("////", CommentKind { shape: CommentShape::Line, doc: None }),
86 ("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
87 ("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
88 ("/**", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Outer) }),
89 ("/*!", CommentKind { shape: CommentShape::Block, doc: Some(CommentPlacement::Inner) }),
90 ("//", CommentKind { shape: CommentShape::Line, doc: None }),
91 ("/*", CommentKind { shape: CommentShape::Block, doc: None }),
94 pub(crate) fn from_text(text: &str) -> CommentKind {
95 let &(_prefix, kind) = CommentKind::BY_PREFIX
97 .find(|&(prefix, _kind)| text.starts_with(prefix))
102 pub fn prefix(&self) -> &'static str {
104 CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
109 impl ast::Whitespace {
110 pub fn spans_multiple_lines(&self) -> bool {
111 let text = self.text();
112 text.find('\n').map_or(false, |idx| text[idx + 1..].contains('\n'))
116 pub struct QuoteOffsets {
117 pub quotes: (TextRange, TextRange),
118 pub contents: TextRange,
122 fn new(literal: &str) -> Option<QuoteOffsets> {
123 let left_quote = literal.find('"')?;
124 let right_quote = literal.rfind('"')?;
125 if left_quote == right_quote {
126 // `literal` only contains one quote
130 let start = TextSize::from(0);
131 let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"');
132 let right_quote = TextSize::try_from(right_quote).unwrap();
133 let end = TextSize::of(literal);
135 let res = QuoteOffsets {
136 quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
137 contents: TextRange::new(left_quote, right_quote),
143 pub trait IsString: AstToken {
144 fn quote_offsets(&self) -> Option<QuoteOffsets> {
145 let text = self.text();
146 let offsets = QuoteOffsets::new(text)?;
147 let o = self.syntax().text_range().start();
148 let offsets = QuoteOffsets {
149 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
150 contents: offsets.contents + o,
154 fn text_range_between_quotes(&self) -> Option<TextRange> {
155 self.quote_offsets().map(|it| it.contents)
157 fn open_quote_text_range(&self) -> Option<TextRange> {
158 self.quote_offsets().map(|it| it.quotes.0)
160 fn close_quote_text_range(&self) -> Option<TextRange> {
161 self.quote_offsets().map(|it| it.quotes.1)
165 impl IsString for ast::String {}
168 pub fn is_raw(&self) -> bool {
169 self.text().starts_with('r')
171 pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
172 let contents_range = self.text_range_between_quotes()?;
173 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
174 Some(range + contents_range.start())
177 pub fn value(&self) -> Option<Cow<'_, str>> {
179 let text = self.text();
181 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
182 return Some(Cow::Borrowed(text));
185 let text = self.text();
186 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
188 let mut buf = String::new();
189 let mut text_iter = text.chars();
190 let mut has_error = false;
191 unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
195 (Ok(c), false) => buf.push(c),
196 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
198 buf.reserve_exact(text.len());
199 buf.push_str(&text[..char_range.start]);
202 (Err(_), _) => has_error = true,
205 match (has_error, buf.capacity() == 0) {
207 (false, true) => Some(Cow::Borrowed(text)),
208 (false, false) => Some(Cow::Owned(buf)),
213 impl IsString for ast::ByteString {}
215 impl ast::ByteString {
216 pub fn is_raw(&self) -> bool {
217 self.text().starts_with("br")
220 pub fn value(&self) -> Option<Cow<'_, [u8]>> {
222 let text = self.text();
224 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
225 return Some(Cow::Borrowed(text.as_bytes()));
228 let text = self.text();
229 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
231 let mut buf: Vec<u8> = Vec::new();
232 let mut text_iter = text.chars();
233 let mut has_error = false;
234 unescape_literal(text, Mode::ByteStr, &mut |char_range, unescaped_char| match (
238 (Ok(c), false) => buf.push(c as u8),
239 (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
241 buf.reserve_exact(text.len());
242 buf.extend_from_slice(text[..char_range.start].as_bytes());
245 (Err(_), _) => has_error = true,
248 match (has_error, buf.capacity() == 0) {
250 (false, true) => Some(Cow::Borrowed(text.as_bytes())),
251 (false, false) => Some(Cow::Owned(buf)),
257 pub enum FormatSpecifier {
274 pub trait HasFormatSpecifier: AstToken {
277 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>>;
279 fn lex_format_specifier<F>(&self, mut callback: F)
281 F: FnMut(TextRange, FormatSpecifier),
283 let char_ranges = match self.char_ranges() {
284 Some(char_ranges) => char_ranges,
287 let mut chars = char_ranges.iter().peekable();
289 while let Some((range, first_char)) = chars.next() {
292 // Format specifier, see syntax at https://doc.rust-lang.org/std/fmt/index.html#syntax
293 if let Some((_, Ok('{'))) = chars.peek() {
294 // Escaped format specifier, `{{`
299 callback(*range, FormatSpecifier::Open);
301 // check for integer/identifier
304 .and_then(|next| next.1.as_ref().ok())
310 read_integer(&mut chars, &mut callback);
312 c if c == '_' || c.is_alphabetic() => {
314 read_identifier(&mut chars, &mut callback);
319 if let Some((_, Ok(':'))) = 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);
326 .and_then(|next| next.1.as_ref().ok())
328 .unwrap_or_default();
331 .and_then(|next| next.1.as_ref().ok())
333 .unwrap_or_default();
336 // alignment specifier, first char specifies fillment
339 FormatSpecifier::Fill,
344 FormatSpecifier::Align,
352 FormatSpecifier::Align,
363 .and_then(|next| next.1.as_ref().ok())
370 FormatSpecifier::Sign,
378 if let Some((_, Ok('#'))) = chars.peek() {
381 FormatSpecifier::NumberSign,
387 let mut cloned = chars.clone().take(2);
388 let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
389 let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied();
391 if first == Some('0') && second != Some('$') {
392 skip_char_and_emit(&mut chars, FormatSpecifier::Zero, &mut callback);
398 .and_then(|next| next.1.as_ref().ok())
403 read_integer(&mut chars, &mut callback);
404 if let Some((_, Ok('$'))) = chars.peek() {
407 FormatSpecifier::DollarSign,
412 c if c == '_' || c.is_alphabetic() => {
413 read_identifier(&mut chars, &mut callback);
415 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
420 FormatSpecifier::QuestionMark,
425 // can be either width (indicated by dollar sign, or type in which case
426 // the next sign has to be `}`)
428 chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
431 Some('$') => skip_char_and_emit(
433 FormatSpecifier::DollarSign,
439 FormatSpecifier::Close,
451 if let Some((_, Ok('.'))) = chars.peek() {
452 skip_char_and_emit(&mut chars, FormatSpecifier::Dot, &mut callback);
456 .and_then(|next| next.1.as_ref().ok())
463 FormatSpecifier::Asterisk,
468 read_integer(&mut chars, &mut callback);
469 if let Some((_, Ok('$'))) = chars.peek() {
472 FormatSpecifier::DollarSign,
477 c if c == '_' || c.is_alphabetic() => {
478 read_identifier(&mut chars, &mut callback);
479 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
486 FormatSpecifier::DollarSign,
499 .and_then(|next| next.1.as_ref().ok())
506 FormatSpecifier::QuestionMark,
510 c if c == '_' || c.is_alphabetic() => {
511 read_identifier(&mut chars, &mut callback);
513 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
518 FormatSpecifier::QuestionMark,
528 Some((_, Ok('}'))) => {
529 skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
531 Some((_, _)) | None => continue,
535 while let Some((_, Ok(next_char))) = chars.peek() {
536 if next_char == &'{' {
545 fn skip_char_and_emit<'a, I, F>(
546 chars: &mut std::iter::Peekable<I>,
547 emit: FormatSpecifier,
550 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
551 F: FnMut(TextRange, FormatSpecifier),
553 let (range, _) = chars.next().unwrap();
554 callback(*range, emit);
557 fn read_integer<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
559 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
560 F: FnMut(TextRange, FormatSpecifier),
562 let (mut range, c) = chars.next().unwrap();
563 assert!(c.as_ref().unwrap().is_ascii_digit());
564 while let Some((r, Ok(next_char))) = chars.peek() {
565 if next_char.is_ascii_digit() {
567 range = range.cover(*r);
572 callback(range, FormatSpecifier::Integer);
575 fn read_identifier<'a, I, F>(chars: &mut std::iter::Peekable<I>, callback: &mut F)
577 I: Iterator<Item = &'a (TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>,
578 F: FnMut(TextRange, FormatSpecifier),
580 let (mut range, c) = chars.next().unwrap();
581 assert!(c.as_ref().unwrap().is_alphabetic() || *c.as_ref().unwrap() == '_');
582 while let Some((r, Ok(next_char))) = chars.peek() {
583 if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() {
585 range = range.cover(*r);
590 callback(range, FormatSpecifier::Identifier);
595 impl HasFormatSpecifier for ast::String {
598 ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
599 let text = self.text();
600 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
601 let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
603 let mut res = Vec::with_capacity(text.len());
604 unescape_literal(text, Mode::Str, &mut |range, unescaped_char| {
606 TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap())
616 impl ast::IntNumber {
617 pub fn radix(&self) -> Radix {
618 match self.text().get(..2).unwrap_or_default() {
619 "0b" => Radix::Binary,
620 "0o" => Radix::Octal,
621 "0x" => Radix::Hexadecimal,
626 pub fn split_into_parts(&self) -> (&str, &str, &str) {
627 let radix = self.radix();
628 let (prefix, mut text) = self.text().split_at(radix.prefix_len());
630 let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
631 Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
632 _ => |(_, c)| c.is_ascii_alphabetic(),
636 if let Some((suffix_start, _)) = text.char_indices().find(is_suffix_start) {
637 let (text2, suffix2) = text.split_at(suffix_start);
642 (prefix, text, suffix)
645 pub fn value(&self) -> Option<u128> {
646 let (_, text, _) = self.split_into_parts();
647 let value = u128::from_str_radix(&text.replace("_", ""), self.radix() as u32).ok()?;
651 pub fn suffix(&self) -> Option<&str> {
652 let (_, _, suffix) = self.split_into_parts();
653 if suffix.is_empty() {
661 impl ast::FloatNumber {
662 pub fn suffix(&self) -> Option<&str> {
663 let text = self.text();
664 let mut indices = text.char_indices();
665 let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
666 if c == 'e' || c == 'E' {
667 suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
669 Some(&text[suffix_start..])
673 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
682 pub const ALL: &'static [Radix] =
683 &[Radix::Binary, Radix::Octal, Radix::Decimal, Radix::Hexadecimal];
685 const fn prefix_len(self) -> usize {
695 use crate::ast::{self, make, FloatNumber, IntNumber};
697 fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
698 assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
701 fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
702 assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
706 fn test_float_number_suffix() {
707 check_float_suffix("123.0", None);
708 check_float_suffix("123f32", "f32");
709 check_float_suffix("123.0e", None);
710 check_float_suffix("123.0e4", None);
711 check_float_suffix("123.0ef32", "f32");
712 check_float_suffix("123.0E4f32", "f32");
713 check_float_suffix("1_2_3.0_f32", "f32");
717 fn test_int_number_suffix() {
718 check_int_suffix("123", None);
719 check_int_suffix("123i32", "i32");
720 check_int_suffix("1_0_1_l_o_l", "l_o_l");
721 check_int_suffix("0b11", None);
722 check_int_suffix("0o11", None);
723 check_int_suffix("0xff", None);
724 check_int_suffix("0b11u32", "u32");
725 check_int_suffix("0o11u32", "u32");
726 check_int_suffix("0xffu32", "u32");
729 fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
731 ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
739 fn test_string_escape() {
740 check_string_value(r"foobar", "foobar");
741 check_string_value(r"\foobar", None);
742 check_string_value(r"\nfoobar", "\nfoobar");
743 check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");