diff options
author | Nadrieril Feneanar | 2019-04-06 00:43:09 +0200 |
---|---|---|
committer | GitHub | 2019-04-06 00:43:09 +0200 |
commit | 5eccde86fc3ccdeb34c9f8bb44de33d25e77f30c (patch) | |
tree | 76d0f0fc848887d2945d586b58847575ca31d0da /dhall_core/src/printer.rs | |
parent | f78af6d1e7f6c1dc39bde6cf97138327004ddb06 (diff) | |
parent | 6a675a13fcfafa057c44db84c3b0ca3b344cfdab (diff) |
Merge pull request #48 from Nadrieril/exprf
Move recursion out of Expr for enhanced genericity
Diffstat (limited to '')
-rw-r--r-- | dhall_core/src/printer.rs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/dhall_core/src/printer.rs b/dhall_core/src/printer.rs index 1d1b063..746b863 100644 --- a/dhall_core/src/printer.rs +++ b/dhall_core/src/printer.rs @@ -8,6 +8,12 @@ impl<S, A: Display> Display for Expr<S, A> { } } +impl<S, A: Display> Display for SubExpr<S, A> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + self.as_ref().fmt(f) + } +} + // There is a one-to-one correspondence between the formatter and the grammar. Each phase is // named after a corresponding grammar group, and the structure of the formatter reflects // the relationship between the corresponding grammar rules. This leads to the nice property @@ -29,7 +35,7 @@ impl<S, A: Display> Expr<S, A> { f: &mut fmt::Formatter, phase: PrintPhase, ) -> Result<(), fmt::Error> { - use crate::Expr::*; + use crate::ExprF::*; use PrintPhase::*; match self { _ if phase == Paren => { @@ -137,7 +143,7 @@ impl<S, A: Display> Expr<S, A> { write!(f, " : ")?; b.fmt(f)?; } - Expr::BinOp(op, a, b) => { + ExprF::BinOp(op, a, b) => { // Precedence is magically handled by the ordering of BinOps. if phase > PrintPhase::BinOp(*op) { return self.fmt_phase(f, Paren); @@ -146,7 +152,7 @@ impl<S, A: Display> Expr<S, A> { write!(f, " {} ", op)?; b.fmt_phase(f, PrintPhase::BinOp(*op))?; } - Expr::App(a, args) => { + ExprF::App(a, args) => { if phase > PrintPhase::App { return self.fmt_phase(f, Paren); } @@ -211,6 +217,16 @@ impl<S, A: Display> Expr<S, A> { } } +impl<S, A: Display> SubExpr<S, A> { + fn fmt_phase( + &self, + f: &mut fmt::Formatter, + phase: PrintPhase, + ) -> Result<(), fmt::Error> { + self.0.as_ref().fmt_phase(f, phase) + } +} + fn fmt_list<T, I, F>( open: &str, sep: &str, @@ -233,7 +249,7 @@ where f.write_str(close) } -impl<S, A: Display> Display for InterpolatedText<S, A> { +impl<SubExpr: Display + Clone> Display for InterpolatedText<SubExpr> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.write_str("\"")?; for x in self.iter() { @@ -446,7 +462,7 @@ impl Display for Scheme { } } -impl Display for V { +impl<Label: Display> Display for V<Label> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let V(x, n) = self; x.fmt(f)?; |