pub fn token_fmt(&self, ctx: &MacroContext, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            Kind::Eof | Kind::Error => {
                // NB: marker tokens can't be formatted.
                return Err(fmt::Error);
            }
            Kind::Ident(s) => match s {
                StringSource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                StringSource::Synthetic(id) => {
                    match ctx.storage().with_string(*id, |s| write!(f, "{}", s)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
                StringSource::BuiltIn(builtin) => {
                    write!(f, "{}", builtin)?;
                }
            },
            Kind::Label(s) => match s {
                StringSource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                StringSource::Synthetic(id) => {
                    match ctx.storage().with_string(*id, |s| write!(f, "'{}", s)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
                StringSource::BuiltIn(builtin) => {
                    write!(f, "'{}", builtin)?;
                }
            },
            Kind::Byte(s) => match s {
                CopySource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                CopySource::Inline(b) => {
                    write!(f, "{:?}", b)?;
                }
            },
            Kind::ByteStr(s) => match s {
                StrSource::Text(text) => {
                    let span = if text.wrapped {
                        self.span.narrow(1)
                    } else {
                        self.span
                    };

                    let s = ctx.source().source(span).ok_or(fmt::Error)?;
                    write!(f, "b\"{}\"", s)?;
                }
                StrSource::Synthetic(id) => {
                    match ctx
                        .storage()
                        .with_byte_string(*id, |bytes| write!(f, "{}", FormatBytes(bytes)))
                    {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
            },
            Kind::Char(s) => match s {
                CopySource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                CopySource::Inline(c) => {
                    write!(f, "{:?}", c)?;
                }
            },
            Kind::Number(s) => match s {
                NumberSource::Text(_) => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                NumberSource::Synthetic(id) => {
                    match ctx.storage().with_number(*id, |n| write!(f, "{}", n)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
            },
            Kind::Str(s) => match s {
                StrSource::Text(text) => {
                    let span = if text.wrapped {
                        self.span.narrow(1)
                    } else {
                        self.span
                    };

                    let s = ctx.source().source(span).ok_or(fmt::Error)?;
                    write!(f, "\"{}\"", s)?;
                }
                StrSource::Synthetic(id) => {
                    match ctx.storage().with_string(*id, |s| write!(f, "{:?}", s)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
            },
            other => {
                write!(f, "{}", other)?;
            }
        }

        return Ok(());

        struct FormatBytes<'a>(&'a [u8]);

        impl fmt::Display for FormatBytes<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "b\"")?;

                for b in bytes_escape_default(self.0) {
                    write!(f, "{}", b as char)?;
                }

                write!(f, "\"")?;
                Ok(())
            }
        }

        fn bytes_escape_default(bytes: &[u8]) -> impl Iterator<Item = u8> + '_ {
            bytes.iter().copied().flat_map(std::ascii::escape_default)
        }
    }
/// Format the current token to a formatter.
Format the current token to a formatter.
Format the current token to a formatter .
pub fn token_fmt(&self, ctx: &MacroContext, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            Kind::Eof | Kind::Error => {
                // NB: marker tokens can't be formatted.
                return Err(fmt::Error);
            }
            Kind::Ident(s) => match s {
                StringSource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                StringSource::Synthetic(id) => {
                    match ctx.storage().with_string(*id, |s| write!(f, "{}", s)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
                StringSource::BuiltIn(builtin) => {
                    write!(f, "{}", builtin)?;
                }
            },
            Kind::Label(s) => match s {
                StringSource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                StringSource::Synthetic(id) => {
                    match ctx.storage().with_string(*id, |s| write!(f, "'{}", s)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
                StringSource::BuiltIn(builtin) => {
                    write!(f, "'{}", builtin)?;
                }
            },
            Kind::Byte(s) => match s {
                CopySource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                CopySource::Inline(b) => {
                    write!(f, "{:?}", b)?;
                }
            },
            Kind::ByteStr(s) => match s {
                StrSource::Text(text) => {
                    let span = if text.wrapped {
                        self.span.narrow(1)
                    } else {
                        self.span
                    };
                    let s = ctx.source().source(span).ok_or(fmt::Error)?;
                    write!(f, "b\"{}\"", s)?;
                }
                StrSource::Synthetic(id) => {
                    match ctx
                        .storage()
                        .with_byte_string(*id, |bytes| write!(f, "{}", FormatBytes(bytes)))
                    {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
            },
            Kind::Char(s) => match s {
                CopySource::Text => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                CopySource::Inline(c) => {
                    write!(f, "{:?}", c)?;
                }
            },
            Kind::Number(s) => match s {
                NumberSource::Text(_) => {
                    let s = ctx.source().source(self.span).ok_or(fmt::Error)?;
                    write!(f, "{}", s)?;
                }
                NumberSource::Synthetic(id) => {
                    match ctx.storage().with_number(*id, |n| write!(f, "{}", n)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
            },
            Kind::Str(s) => match s {
                StrSource::Text(text) => {
                    let span = if text.wrapped {
                        self.span.narrow(1)
                    } else {
                        self.span
                    };
                    let s = ctx.source().source(span).ok_or(fmt::Error)?;
                    write!(f, "\"{}\"", s)?;
                }
                StrSource::Synthetic(id) => {
                    match ctx.storage().with_string(*id, |s| write!(f, "{:?}", s)) {
                        Some(result) => result?,
                        None => return Err(fmt::Error),
                    }
                }
            },
            other => {
                write!(f, "{}", other)?;
            }
        }
        return Ok(());
        struct FormatBytes<'a>(&'a [u8]);
        impl fmt::Display for FormatBytes<'_> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "b\"")?;
                for b in bytes_escape_default(self.0) {
                    write!(f, "{}", b as char)?;
                }
                write!(f, "\"")?;
                Ok(())
            }
        }
        fn bytes_escape_default(bytes: &[u8]) -> impl Iterator<Item = u8> + '_ {
            bytes.iter().copied().flat_map(std::ascii::escape_default)
        }
    }
pub fn token_fmt ( & self , ctx : & MacroContext , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result { match & self . kind { Kind :: Eof | Kind :: Error => { // NB: marker tokens can't be formatted. return Err ( fmt :: Error ) ; } Kind :: Ident ( s ) => match s { StringSource :: Text => { let s = ctx . source ( ) . source ( self . span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " " s ) ? ; } StringSource :: Synthetic ( id ) => { match ctx . storage ( ) . with_string ( * id , | s | write ! ( f " " s ) ) { Some ( result ) => result ? , None => return Err ( fmt :: Error ) , } } StringSource :: BuiltIn ( builtin ) => { write ! ( f " " builtin ) ? ; } } , Kind :: Label ( s ) => match s { StringSource :: Text => { let s = ctx . source ( ) . source ( self . span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " " s ) ? ; } StringSource :: Synthetic ( id ) => { match ctx . storage ( ) . with_string ( * id , | s | write ! ( f " " s ) ) { Some ( result ) => result ? , None => return Err ( fmt :: Error ) , } } StringSource :: BuiltIn ( builtin ) => { write ! ( f " " builtin ) ? ; } } , Kind :: Byte ( s ) => match s { CopySource :: Text => { let s = ctx . source ( ) . source ( self . span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " " s ) ? ; } CopySource :: Inline ( b ) => { write ! ( f " " b ) ? ; } } , Kind :: ByteStr ( s ) => match s { StrSource :: Text ( text ) => { let span = if text . wrapped { self . span . narrow ( 1 ) } else { self . span } ; let s = ctx . source ( ) . source ( span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " \" \" " s ) ? ; } StrSource :: Synthetic ( id ) => { match ctx . storage ( ) . with_byte_string ( * id , | bytes | write ! ( f " " FormatBytes ( bytes ) ) ) { Some ( result ) => result ? , None => return Err ( fmt :: Error ) , } } } , Kind :: Char ( s ) => match s { CopySource :: Text => { let s = ctx . source ( ) . source ( self . span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " " s ) ? ; } CopySource :: Inline ( c ) => { write ! ( f " " c ) ? ; } } , Kind :: Number ( s ) => match s { NumberSource :: Text ( _ ) => { let s = ctx . source ( ) . source ( self . span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " " s ) ? ; } NumberSource :: Synthetic ( id ) => { match ctx . storage ( ) . with_number ( * id , | n | write ! ( f " " n ) ) { Some ( result ) => result ? , None => return Err ( fmt :: Error ) , } } } , Kind :: Str ( s ) => match s { StrSource :: Text ( text ) => { let span = if text . wrapped { self . span . narrow ( 1 ) } else { self . span } ; let s = ctx . source ( ) . source ( span ) . ok_or ( fmt :: Error ) ? ; write ! ( f " \" \" " s ) ? ; } StrSource :: Synthetic ( id ) => { match ctx . storage ( ) . with_string ( * id , | s | write ! ( f " " s ) ) { Some ( result ) => result ? , None => return Err ( fmt :: Error ) , } } } , other => { write ! ( f " " other ) ? ; } } return Ok ( ( ) ) ; struct FormatBytes < ' a > ( & ' a [ u8 ] ) ; impl fmt :: Display for FormatBytes < ' _ > { fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result { write ! ( f " \" " ) ? ; for b in bytes_escape_default ( self . 0 ) { write ! ( f " " b as char ) ? ; } write ! ( f " \" " ) ? ; Ok ( ( ) ) } } fn bytes_escape_default ( bytes : & [ u8 ] ) -> impl Iterator < Item = u8 > + ' _ { bytes . iter ( ) . copied ( ) . flat_map ( std :: ascii :: escape_default ) } }
Format the current token to a formatter.
Format the current token to a formatter .
&self
ctx
&MacroContext
f
&mut fmt::Formatter<'_>
&self
ctx
&MacroContext
f
&mut fmt::Formatter<'_>