Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,16 @@ mod imp;
#[cfg(span_locations)]
mod location;

#[cfg(procmacro2_semver_exempt)]
mod num;
#[cfg(procmacro2_semver_exempt)]
#[allow(dead_code)]
mod rustc_literal_escaper;

use crate::extra::DelimSpan;
use crate::marker::{ProcMacroAutoTraits, MARKER};
#[cfg(procmacro2_semver_exempt)]
use crate::rustc_literal_escaper::MixedUnit;
use core::cmp::Ordering;
use core::fmt::{self, Debug, Display};
use core::hash::{Hash, Hasher};
Expand All @@ -182,6 +190,10 @@ use std::path::PathBuf;
#[cfg_attr(docsrs, doc(cfg(feature = "span-locations")))]
pub use crate::location::LineColumn;

#[cfg(procmacro2_semver_exempt)]
#[cfg_attr(docsrs, doc(cfg(procmacro2_semver_exempt)))]
pub use crate::rustc_literal_escaper::EscapeError;

/// An abstract stream of tokens, or more concretely a sequence of token trees.
///
/// This type provides interfaces for iterating over token trees and for
Expand Down Expand Up @@ -1263,6 +1275,112 @@ impl Literal {
self.inner.subspan(range).map(Span::_new)
}

/// Returns the unescaped string value if this is a string literal.
#[cfg(procmacro2_semver_exempt)]
pub fn str_value(&self) -> Result<String, ConversionErrorKind> {
let repr = self.to_string();

if repr.starts_with('"') && repr[1..].ends_with('"') {
let quoted = &repr[1..repr.len() - 1];
let mut value = String::with_capacity(quoted.len());
let mut error = None;
rustc_literal_escaper::unescape_str(quoted, |_range, res| match res {
Ok(ch) => value.push(ch),
Err(err) => {
if err.is_fatal() {
error = Some(ConversionErrorKind::FailedToUnescape(err));
}
}
});
return match error {
Some(error) => Err(error),
None => Ok(value),
};
}

if repr.starts_with('r') {
if let Some(raw) = get_raw(&repr[1..]) {
return Ok(raw.to_owned());
}
}

Err(ConversionErrorKind::InvalidLiteralKind)
}

/// Returns the unescaped string value (including nul terminator) if this is
/// a c-string literal.
#[cfg(procmacro2_semver_exempt)]
pub fn cstr_value(&self) -> Result<Vec<u8>, ConversionErrorKind> {
let repr = self.to_string();

if repr.starts_with("c\"") && repr[2..].ends_with('"') {
let quoted = &repr[2..repr.len() - 1];
let mut value = Vec::with_capacity(quoted.len());
let mut error = None;
rustc_literal_escaper::unescape_c_str(quoted, |_range, res| match res {
Ok(MixedUnit::Char(ch)) => {
value.extend_from_slice(ch.get().encode_utf8(&mut [0; 4]).as_bytes());
}
Ok(MixedUnit::HighByte(byte)) => value.push(byte.get()),
Err(err) => {
if err.is_fatal() {
error = Some(ConversionErrorKind::FailedToUnescape(err));
}
}
});
return match error {
Some(error) => Err(error),
None => {
value.push(b'\0');
Ok(value)
}
};
}

if repr.starts_with("cr") {
if let Some(raw) = get_raw(&repr[2..]) {
let mut value = Vec::with_capacity(raw.len() + 1);
value.extend_from_slice(raw.as_bytes());
value.push(b'\0');
return Ok(value);
}
}

Err(ConversionErrorKind::InvalidLiteralKind)
}

/// Returns the unescaped string value if this is a byte string literal.
#[cfg(procmacro2_semver_exempt)]
pub fn byte_str_value(&self) -> Result<Vec<u8>, ConversionErrorKind> {
let repr = self.to_string();

if repr.starts_with("b\"") && repr[2..].ends_with('"') {
let quoted = &repr[2..repr.len() - 1];
let mut value = Vec::with_capacity(quoted.len());
let mut error = None;
rustc_literal_escaper::unescape_byte_str(quoted, |_range, res| match res {
Ok(byte) => value.push(byte),
Err(err) => {
if err.is_fatal() {
error = Some(ConversionErrorKind::FailedToUnescape(err));
}
}
});
return match error {
Some(error) => Err(error),
None => Ok(value),
};
}

if repr.starts_with("br") {
if let Some(raw) = get_raw(&repr[2..]) {
return Ok(raw.as_bytes().to_owned());
}
}

Err(ConversionErrorKind::InvalidLiteralKind)
}

// Intended for the `quote!` macro to use when constructing a proc-macro2
// token out of a macro_rules $:literal token, which is already known to be
// a valid literal. This avoids reparsing/validating the literal's string
Expand Down Expand Up @@ -1299,6 +1417,33 @@ impl Display for Literal {
}
}

/// Error when retrieving a string literal's unescaped value.
#[cfg(procmacro2_semver_exempt)]
#[derive(Debug, PartialEq, Eq)]
pub enum ConversionErrorKind {
/// The literal is of the right string kind, but its contents are malformed
/// in a way that cannot be unescaped to a value.
FailedToUnescape(EscapeError),
/// The literal is not of the string kind whose value was requested, for
/// example byte string vs UTF-8 string.
InvalidLiteralKind,
}

// ###"..."### -> ...
#[cfg(procmacro2_semver_exempt)]
fn get_raw(repr: &str) -> Option<&str> {
let pounds = repr.len() - repr.trim_start_matches('#').len();
if repr.len() >= pounds + 1 + 1 + pounds
&& repr[pounds..].starts_with('"')
&& repr.trim_end_matches('#').len() + pounds == repr.len()
&& repr[..repr.len() - pounds].ends_with('"')
{
Some(&repr[pounds + 1..repr.len() - pounds - 1])
} else {
None
}
}

/// Public implementation details for the `TokenStream` type, such as iterators.
pub mod token_stream {
use crate::marker::{ProcMacroAutoTraits, MARKER};
Expand Down
17 changes: 17 additions & 0 deletions src/num.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TODO: use NonZero<char> in Rust 1.89+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct NonZeroChar(char);

impl NonZeroChar {
pub fn new(ch: char) -> Option<Self> {
if ch == '\0' {
None
} else {
Some(NonZeroChar(ch))
}
}

pub fn get(self) -> char {
self.0
}
}
Loading