CommentKind::from_text(self.text())
}
+ pub fn is_inner(&self) -> bool {
+ self.kind().doc == Some(CommentPlacement::Inner)
+ }
+
+ pub fn is_outer(&self) -> bool {
+ self.kind().doc == Some(CommentPlacement::Outer)
+ }
+
pub fn prefix(&self) -> &'static str {
let &(prefix, _kind) = CommentKind::BY_PREFIX
.iter()
.unwrap();
prefix
}
+
+ /// Returns the textual content of a doc comment node as a single string with prefix and suffix
+ /// removed.
+ pub fn doc_comment(&self) -> Option<&str> {
+ let kind = self.kind();
+ match kind {
+ CommentKind { shape, doc: Some(_) } => {
+ let prefix = kind.prefix();
+ let text = &self.text()[prefix.len()..];
+ let text = if shape == CommentShape::Block {
+ text.strip_suffix("*/").unwrap_or(text)
+ } else {
+ text
+ };
+ Some(text)
+ }
+ _ => None,
+ }
+ }
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
}
impl CommentKind {
- const BY_PREFIX: [(&'static str, CommentKind); 8] = [
+ const BY_PREFIX: [(&'static str, CommentKind); 9] = [
("/**/", CommentKind { shape: CommentShape::Block, doc: None }),
+ ("/***", CommentKind { shape: CommentShape::Block, doc: None }),
("////", CommentKind { shape: CommentShape::Line, doc: None }),
("///", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Outer) }),
("//!", CommentKind { shape: CommentShape::Line, doc: Some(CommentPlacement::Inner) }),
.unwrap();
kind
}
+
+ pub fn prefix(&self) -> &'static str {
+ let &(prefix, _) =
+ CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
+ prefix
+ }
}
impl ast::Whitespace {
}
}
+pub trait IsString: AstToken {
+ fn quote_offsets(&self) -> Option<QuoteOffsets> {
+ let text = self.text();
+ let offsets = QuoteOffsets::new(text)?;
+ let o = self.syntax().text_range().start();
+ let offsets = QuoteOffsets {
+ quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
+ contents: offsets.contents + o,
+ };
+ Some(offsets)
+ }
+ fn text_range_between_quotes(&self) -> Option<TextRange> {
+ self.quote_offsets().map(|it| it.contents)
+ }
+ fn open_quote_text_range(&self) -> Option<TextRange> {
+ self.quote_offsets().map(|it| it.quotes.0)
+ }
+ fn close_quote_text_range(&self) -> Option<TextRange> {
+ self.quote_offsets().map(|it| it.quotes.1)
+ }
+}
+
+impl IsString for ast::String {}
+
impl ast::String {
pub fn is_raw(&self) -> bool {
self.text().starts_with('r')
pub fn value(&self) -> Option<Cow<'_, str>> {
if self.is_raw() {
- let text = self.text().as_str();
+ let text = self.text();
let text =
&text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
return Some(Cow::Borrowed(text));
}
- let text = self.text().as_str();
+ let text = self.text();
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
- let mut buf = String::with_capacity(text.len());
+ let mut buf = String::new();
+ let mut text_iter = text.chars();
let mut has_error = false;
- unescape_literal(text, Mode::Str, &mut |_, unescaped_char| match unescaped_char {
- Ok(c) => buf.push(c),
- Err(_) => has_error = true,
+ unescape_literal(text, Mode::Str, &mut |char_range, unescaped_char| match (
+ unescaped_char,
+ buf.capacity() == 0,
+ ) {
+ (Ok(c), false) => buf.push(c),
+ (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
+ (Ok(c), true) => {
+ buf.reserve_exact(text.len());
+ buf.push_str(&text[..char_range.start]);
+ buf.push(c);
+ }
+ (Err(_), _) => has_error = true,
});
- if has_error {
- return None;
+ match (has_error, buf.capacity() == 0) {
+ (true, _) => None,
+ (false, true) => Some(Cow::Borrowed(text)),
+ (false, false) => Some(Cow::Owned(buf)),
}
- // FIXME: don't actually allocate for borrowed case
- let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) };
- Some(res)
- }
-
- pub fn quote_offsets(&self) -> Option<QuoteOffsets> {
- let text = self.text().as_str();
- let offsets = QuoteOffsets::new(text)?;
- let o = self.syntax().text_range().start();
- let offsets = QuoteOffsets {
- quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
- contents: offsets.contents + o,
- };
- Some(offsets)
- }
- pub fn text_range_between_quotes(&self) -> Option<TextRange> {
- self.quote_offsets().map(|it| it.contents)
- }
- pub fn open_quote_text_range(&self) -> Option<TextRange> {
- self.quote_offsets().map(|it| it.quotes.0)
- }
- pub fn close_quote_text_range(&self) -> Option<TextRange> {
- self.quote_offsets().map(|it| it.quotes.1)
}
}
+impl IsString for ast::ByteString {}
+
impl ast::ByteString {
pub fn is_raw(&self) -> bool {
self.text().starts_with("br")
}
+
+ pub fn value(&self) -> Option<Cow<'_, [u8]>> {
+ if self.is_raw() {
+ let text = self.text();
+ let text =
+ &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
+ return Some(Cow::Borrowed(text.as_bytes()));
+ }
+
+ let text = self.text();
+ let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
+
+ let mut buf: Vec<u8> = Vec::new();
+ let mut text_iter = text.chars();
+ let mut has_error = false;
+ unescape_literal(text, Mode::ByteStr, &mut |char_range, unescaped_char| match (
+ unescaped_char,
+ buf.capacity() == 0,
+ ) {
+ (Ok(c), false) => buf.push(c as u8),
+ (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (),
+ (Ok(c), true) => {
+ buf.reserve_exact(text.len());
+ buf.extend_from_slice(&text[..char_range.start].as_bytes());
+ buf.push(c as u8);
+ }
+ (Err(_), _) => has_error = true,
+ });
+
+ match (has_error, buf.capacity() == 0) {
+ (true, _) => None,
+ (false, true) => Some(Cow::Borrowed(text.as_bytes())),
+ (false, false) => Some(Cow::Owned(buf)),
+ }
+ }
}
#[derive(Debug)]
}
c if c == '_' || c.is_alphabetic() => {
read_identifier(&mut chars, &mut callback);
+
+ if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
+ == Some('?')
+ {
+ skip_char_and_emit(
+ &mut chars,
+ FormatSpecifier::QuestionMark,
+ &mut callback,
+ );
+ }
+
// can be either width (indicated by dollar sign, or type in which case
// the next sign has to be `}`)
let next =
chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
+
match next {
Some('$') => skip_char_and_emit(
&mut chars,
}
c if c == '_' || c.is_alphabetic() => {
read_identifier(&mut chars, &mut callback);
+
+ if chars.peek().and_then(|next| next.1.as_ref().ok()).copied()
+ == Some('?')
+ {
+ skip_char_and_emit(
+ &mut chars,
+ FormatSpecifier::QuestionMark,
+ &mut callback,
+ );
+ }
}
_ => {}
}
}
_ => {
while let Some((_, Ok(next_char))) = chars.peek() {
- match next_char {
- '{' => break,
- _ => {}
+ if next_char == &'{' {
+ break;
}
chars.next();
}
fn char_ranges(
&self,
) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> {
- let text = self.text().as_str();
+ let text = self.text();
let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start();
}
impl ast::IntNumber {
- const SUFFIXES: &'static [&'static str] = &[
- "u8", "u16", "u32", "u64", "u128", "usize", // Unsigned.
- "i8", "i16", "i32", "i64", "i128", "isize", // Signed.
- ];
-
pub fn radix(&self) -> Radix {
match self.text().get(..2).unwrap_or_default() {
"0b" => Radix::Binary,
pub fn value(&self) -> Option<u128> {
let token = self.syntax();
- let mut text = token.text().as_str();
+ let mut text = token.text();
if let Some(suffix) = self.suffix() {
text = &text[..text.len() - suffix.len()]
}
text = &text[radix.prefix_len()..];
let buf;
- if text.contains("_") {
+ if text.contains('_') {
buf = text.replace('_', "");
text = buf.as_str();
};
pub fn suffix(&self) -> Option<&str> {
let text = self.text();
- // FIXME: don't check a fixed set of suffixes, `1_0_1_l_o_l` is valid
- // syntax, suffix is `l_o_l`.
- ast::IntNumber::SUFFIXES.iter().chain(ast::FloatNumber::SUFFIXES.iter()).find_map(
- |suffix| {
- if text.ends_with(suffix) {
- return Some(&text[text.len() - suffix.len()..]);
- }
- None
- },
- )
+ let radix = self.radix();
+ let mut indices = text.char_indices();
+ if radix != Radix::Decimal {
+ indices.next()?;
+ indices.next()?;
+ }
+ let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
+ Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
+ _ => |(_, c)| c.is_ascii_alphabetic(),
+ };
+ let (suffix_start, _) = indices.find(is_suffix_start)?;
+ Some(&text[suffix_start..])
}
}
impl ast::FloatNumber {
- const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
pub fn suffix(&self) -> Option<&str> {
let text = self.text();
- ast::FloatNumber::SUFFIXES.iter().find_map(|suffix| {
- if text.ends_with(suffix) {
- return Some(&text[text.len() - suffix.len()..]);
- }
- None
- })
+ let mut indices = text.char_indices();
+ let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
+ if c == 'e' || c == 'E' {
+ suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
+ }
+ Some(&text[suffix_start..])
}
}
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use crate::ast::{self, make, FloatNumber, IntNumber};
+
+ fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
+ assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
+ }
+
+ fn check_int_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
+ assert_eq!(IntNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into());
+ }
+
+ #[test]
+ fn test_float_number_suffix() {
+ check_float_suffix("123.0", None);
+ check_float_suffix("123f32", "f32");
+ check_float_suffix("123.0e", None);
+ check_float_suffix("123.0e4", None);
+ check_float_suffix("123.0ef32", "f32");
+ check_float_suffix("123.0E4f32", "f32");
+ check_float_suffix("1_2_3.0_f32", "f32");
+ }
+
+ #[test]
+ fn test_int_number_suffix() {
+ check_int_suffix("123", None);
+ check_int_suffix("123i32", "i32");
+ check_int_suffix("1_0_1_l_o_l", "l_o_l");
+ check_int_suffix("0b11", None);
+ check_int_suffix("0o11", None);
+ check_int_suffix("0xff", None);
+ check_int_suffix("0b11u32", "u32");
+ check_int_suffix("0o11u32", "u32");
+ check_int_suffix("0xffu32", "u32");
+ }
+
+ fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) {
+ assert_eq!(
+ ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) }
+ .value()
+ .as_deref(),
+ expected.into()
+ );
+ }
+
+ #[test]
+ fn test_string_escape() {
+ check_string_value(r"foobar", "foobar");
+ check_string_value(r"\foobar", None);
+ check_string_value(r"\nfoobar", "\nfoobar");
+ check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");
+ }
+}