prefix
}
- /// Returns the textual content of a doc comment block as a single string.
- /// That is, strips leading `///` (+ optional 1 character of whitespace),
- /// trailing `*/`, trailing whitespace and then joins the lines.
+ /// 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().as_str()[prefix.len()..];
- let ws = text.chars().next().filter(|c| c.is_whitespace());
- let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
- match shape {
- CommentShape::Block if text.ends_with("*/") => {
- Some(&text[..text.len() - "*/".len()])
- }
- _ => Some(text),
- }
+ let text = &self.text()[prefix.len()..];
+ let text = if shape == CommentShape::Block {
+ text.strip_suffix("*/").unwrap_or(text)
+ } else {
+ text
+ };
+ Some(text)
}
_ => None,
}
}
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) }),
kind
}
- fn prefix(&self) -> &'static str {
- let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap();
+ pub fn prefix(&self) -> &'static str {
+ let &(prefix, _) =
+ CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
prefix
}
}
}
}
+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::new();
buf.capacity() == 0,
) {
(Ok(c), false) => buf.push(c),
- (Ok(c), true) if Some(c) == text_iter.next() => (),
+ (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]);
(false, false) => Some(Cow::Owned(buf)),
}
}
-
- 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)]
}
_ => {
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();
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()]
}
#[cfg(test)]
mod tests {
- use crate::ast::{make, FloatNumber, IntNumber};
+ 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());
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\\");
+ }
}