aboutsummaryrefslogtreecommitdiff
path: root/src/wrapper.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/wrapper.rs')
-rw-r--r--src/wrapper.rs99
1 files changed, 3 insertions, 96 deletions
diff --git a/src/wrapper.rs b/src/wrapper.rs
index 00f67cd..860b6b7 100644
--- a/src/wrapper.rs
+++ b/src/wrapper.rs
@@ -3,7 +3,6 @@ use crate::detection::inside_proc_macro;
use crate::location::LineColumn;
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
use core::fmt::{self, Debug, Display};
-use core::iter::FromIterator;
use core::ops::RangeBounds;
use core::str::FromStr;
use std::panic;
@@ -286,15 +285,7 @@ impl Debug for LexError {
impl Display for LexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
- #[cfg(not(no_lexerror_display))]
LexError::Compiler(e) => Display::fmt(e, f),
- #[cfg(no_lexerror_display)]
- LexError::Compiler(_e) => Display::fmt(
- &fallback::LexError {
- span: fallback::Span::call_site(),
- },
- f,
- ),
LexError::Fallback(e) => Display::fmt(e, f),
}
}
@@ -406,7 +397,6 @@ impl Span {
}
}
- #[cfg(not(no_hygiene))]
pub fn mixed_site() -> Self {
if inside_proc_macro() {
Span::Compiler(proc_macro::Span::mixed_site())
@@ -426,13 +416,7 @@ impl Span {
pub fn resolved_at(&self, other: Span) -> Span {
match (self, other) {
- #[cfg(not(no_hygiene))]
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
-
- // Name resolution affects semantics, but location is only cosmetic
- #[cfg(no_hygiene)]
- (Span::Compiler(_), Span::Compiler(_)) => other,
-
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
_ => mismatch(),
}
@@ -440,13 +424,7 @@ impl Span {
pub fn located_at(&self, other: Span) -> Span {
match (self, other) {
- #[cfg(not(no_hygiene))]
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
-
- // Name resolution affects semantics, but location is only cosmetic
- #[cfg(no_hygiene)]
- (Span::Compiler(_), Span::Compiler(_)) => *self,
-
(Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
_ => mismatch(),
}
@@ -470,12 +448,6 @@ impl Span {
#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
match self {
- #[cfg(proc_macro_span)]
- Span::Compiler(s) => {
- let proc_macro::LineColumn { line, column } = s.start();
- LineColumn { line, column }
- }
- #[cfg(not(proc_macro_span))]
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.start(),
}
@@ -484,33 +456,11 @@ impl Span {
#[cfg(span_locations)]
pub fn end(&self) -> LineColumn {
match self {
- #[cfg(proc_macro_span)]
- Span::Compiler(s) => {
- let proc_macro::LineColumn { line, column } = s.end();
- LineColumn { line, column }
- }
- #[cfg(not(proc_macro_span))]
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.end(),
}
}
- #[cfg(super_unstable)]
- pub fn before(&self) -> Span {
- match self {
- Span::Compiler(s) => Span::Compiler(s.before()),
- Span::Fallback(s) => Span::Fallback(s.before()),
- }
- }
-
- #[cfg(super_unstable)]
- pub fn after(&self) -> Span {
- match self {
- Span::Compiler(s) => Span::Compiler(s.after()),
- Span::Fallback(s) => Span::Fallback(s.after()),
- }
- }
-
pub fn join(&self, other: Span) -> Option<Span> {
let ret = match (self, other) {
#[cfg(proc_macro_span)]
@@ -630,20 +580,14 @@ impl Group {
pub fn span_open(&self) -> Span {
match self {
- #[cfg(not(no_group_open_close))]
Group::Compiler(g) => Span::Compiler(g.span_open()),
- #[cfg(no_group_open_close)]
- Group::Compiler(g) => Span::Compiler(g.span()),
Group::Fallback(g) => Span::Fallback(g.span_open()),
}
}
pub fn span_close(&self) -> Span {
match self {
- #[cfg(not(no_group_open_close))]
Group::Compiler(g) => Span::Compiler(g.span_close()),
- #[cfg(no_group_open_close)]
- Group::Compiler(g) => Span::Compiler(g.span()),
Group::Fallback(g) => Span::Fallback(g.span_close()),
}
}
@@ -704,27 +648,7 @@ impl Ident {
pub fn new_raw(string: &str, span: Span) -> Self {
match span {
- #[cfg(not(no_ident_new_raw))]
Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new_raw(string, s)),
- #[cfg(no_ident_new_raw)]
- Span::Compiler(s) => {
- let _ = proc_macro::Ident::new(string, s);
- // At this point the un-r#-prefixed string is known to be a
- // valid identifier. Try to produce a valid raw identifier by
- // running the `TokenStream` parser, and unwrapping the first
- // token as an `Ident`.
- let raw_prefixed = format!("r#{}", string);
- if let Ok(ts) = raw_prefixed.parse::<proc_macro::TokenStream>() {
- let mut iter = ts.into_iter();
- if let (Some(proc_macro::TokenTree::Ident(mut id)), None) =
- (iter.next(), iter.next())
- {
- id.set_span(s);
- return Ident::Compiler(id);
- }
- }
- panic!("not allowed as a raw identifier: `{}`", raw_prefixed)
- }
Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
}
}
@@ -826,7 +750,7 @@ macro_rules! unsuffixed_integers {
impl Literal {
pub unsafe fn from_str_unchecked(repr: &str) -> Self {
if inside_proc_macro() {
- Literal::Compiler(compiler_literal_from_str(repr).expect("invalid literal"))
+ Literal::Compiler(proc_macro::Literal::from_str(repr).expect("invalid literal"))
} else {
Literal::Fallback(fallback::Literal::from_str_unchecked(repr))
}
@@ -949,7 +873,8 @@ impl FromStr for Literal {
fn from_str(repr: &str) -> Result<Self, Self::Err> {
if inside_proc_macro() {
- compiler_literal_from_str(repr).map(Literal::Compiler)
+ let literal = proc_macro::Literal::from_str(repr)?;
+ Ok(Literal::Compiler(literal))
} else {
let literal = fallback::Literal::from_str(repr)?;
Ok(Literal::Fallback(literal))
@@ -957,24 +882,6 @@ impl FromStr for Literal {
}
}
-fn compiler_literal_from_str(repr: &str) -> Result<proc_macro::Literal, LexError> {
- #[cfg(not(no_literal_from_str))]
- {
- proc_macro::Literal::from_str(repr).map_err(LexError::Compiler)
- }
- #[cfg(no_literal_from_str)]
- {
- let tokens = proc_macro_parse(repr)?;
- let mut iter = tokens.into_iter();
- if let (Some(proc_macro::TokenTree::Literal(literal)), None) = (iter.next(), iter.next()) {
- if literal.to_string().len() == repr.len() {
- return Ok(literal);
- }
- }
- Err(LexError::call_site())
- }
-}
-
impl Display for Literal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {