Skip to content

Commit

Permalink
proc_macro: fix Debug formatting for Pat.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jul 23, 2019
1 parent 9ff39b8 commit 47e7ed3
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::scannerless::Pat as SPat;
pub use proc_macro2::{
Delimiter, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
};
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

pub type Context = crate::context::Context<Pat>;
Expand Down Expand Up @@ -46,7 +48,7 @@ pub fn builtin(cx: &mut Context) -> crate::Grammar {
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Pat(pub Vec<FlatTokenPat<String>>);
pub struct Pat<Pats = Vec<FlatTokenPat<String>>>(pub Pats);

impl FromStr for Pat {
type Err = LexError;
Expand All @@ -72,6 +74,12 @@ impl From<&str> for Pat {
}
}

impl<Pats> From<Pats> for Pat<Pats> {
fn from(pats: Pats) -> Self {
Pat(pats)
}
}

impl From<FlatTokenPat<String>> for Pat {
fn from(pat: FlatTokenPat<String>) -> Self {
Pat(vec![pat])
Expand Down Expand Up @@ -100,7 +108,7 @@ pub enum FlatToken {
Literal(Literal),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FlatTokenPat<S: AsRef<str>> {
Delim(char),
Ident(Option<S>),
Expand All @@ -111,6 +119,51 @@ pub enum FlatTokenPat<S: AsRef<str>> {
Literal,
}

impl<S: AsRef<str>> fmt::Debug for FlatTokenPat<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FlatTokenPat::Delim(c) | FlatTokenPat::Punct { ch: Some(c), .. } => {
write!(f, "\"{}\"", c)
}
FlatTokenPat::Ident(None) => f.write_str("IDENT"),
FlatTokenPat::Ident(Some(ident)) => write!(f, "\"{}\"", ident.as_ref()),
FlatTokenPat::Punct { ch: None, .. } => f.write_str("PUNCT"),
FlatTokenPat::Literal => f.write_str("LITERAL"),
}
}
}

// FIXME(eddyb) can't use `Pats: AsRef<[FlatTokenPat<S>]` as it doesn't constrain `S`.
impl<S: AsRef<str>, Pats: Deref<Target = [FlatTokenPat<S>]>> fmt::Debug for Pat<Pats> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0[..] {
[] => f.write_str("\"\""),
[pat] => pat.fmt(f),
pats => {
let mut was_joint = true;
f.write_str("\"")?;
for pat in pats {
if !was_joint {
f.write_str(" ")?;
}
match pat {
FlatTokenPat::Punct { ch: Some(c), joint } => {
write!(f, "{}", c)?;
was_joint = *joint == Some(true);
}
FlatTokenPat::Ident(Some(ident)) => {
write!(f, "\"{}\"", ident.as_ref())?;
was_joint = false;
}
_ => unreachable!(),
}
}
f.write_str("\"")
}
}
}
}

impl FlatToken {
pub fn span(&self) -> Span {
match self {
Expand Down

0 comments on commit 47e7ed3

Please sign in to comment.