From b21c60471614d516b046a206090c03b91da4604e Mon Sep 17 00:00:00 2001 From: NanoTech Date: Sun, 11 Dec 2016 20:23:33 -0600 Subject: Format ListLit and OptionalLit --- src/core.rs | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/core.rs b/src/core.rs index 3afddb5..9758830 100644 --- a/src/core.rs +++ b/src/core.rs @@ -342,8 +342,14 @@ impl<'i, S, A: Display> Expr<'i, S, A> { write!(f, ") → ")?; d.fmt_b(f) } - &ListLit(_, _) => f.write_str("ListLit"), - &OptionalLit(_, _) => f.write_str("OptionalLit"), + &ListLit(ref t, ref es) => { + fmt_list("[", "] : List ", es, f, |e, f| e.fmt(f))?; + t.fmt_e(f) + } + &OptionalLit(ref t, ref es) => { + fmt_list("[", "] : Optional ", es, f, |e, f| e.fmt(f))?; + t.fmt_e(f) + } &Merge(ref a, ref b, ref c) => { write!(f, "merge ")?; a.fmt_e(f)?; @@ -388,27 +394,6 @@ impl<'i, S, A: Display> Expr<'i, S, A> { fn fmt_f(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use Expr::*; - fn fmt_list, - F: Fn(T, &mut fmt::Formatter) -> Result<(), fmt::Error>> - (open: &str, - close: &str, - it: I, - f: &mut fmt::Formatter, - func: F) - -> Result<(), fmt::Error> - where I: IntoIterator, - F: Fn(T, &mut fmt::Formatter) -> Result<(), fmt::Error> - { - f.write_str(open)?; - for (i, x) in it.into_iter().enumerate() { - if i > 0 { - f.write_str(", ")?; - } - func(x, f)?; - } - f.write_str(close) - } match self { &Var(a) => a.fmt(f), &Const(k) => k.fmt(f), @@ -440,6 +425,25 @@ impl<'i, S, A: Display> Expr<'i, S, A> { } } +fn fmt_list(open: &str, + close: &str, + it: I, + f: &mut fmt::Formatter, + func: F) + -> Result<(), fmt::Error> + where I: IntoIterator, + F: Fn(T, &mut fmt::Formatter) -> Result<(), fmt::Error> +{ + f.write_str(open)?; + for (i, x) in it.into_iter().enumerate() { + if i > 0 { + f.write_str(", ")?; + } + func(x, f)?; + } + f.write_str(close) +} + impl Display for Const { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { ::fmt(self, f) -- cgit v1.2.3