From 4ff536cb593aa022e71042ab4fe008e2381b0680 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 20 Mar 2020 11:46:46 +0000 Subject: Remove more unnecessary parens in printer --- dhall/src/syntax/ast/expr.rs | 4 ++-- dhall/src/syntax/ast/import.rs | 20 ++++++++++++++++++++ dhall/src/syntax/text/printer.rs | 21 +++++++++++++-------- 3 files changed, 35 insertions(+), 10 deletions(-) (limited to 'dhall/src/syntax') diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index 8023771..b53e6cb 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -258,8 +258,8 @@ impl Expr { } // Empty enum to indicate that no error can occur -enum X {} -fn trivial_result(x: Result) -> T { +pub(crate) enum X {} +pub(crate) fn trivial_result(x: Result) -> T { match x { Ok(x) => x, Err(e) => match e {}, diff --git a/dhall/src/syntax/ast/import.rs b/dhall/src/syntax/ast/import.rs index 75d7946..c45fe51 100644 --- a/dhall/src/syntax/ast/import.rs +++ b/dhall/src/syntax/ast/import.rs @@ -1,3 +1,5 @@ +use crate::syntax::trivial_result; + /// The beginning of a file path which anchors subsequent path components #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum FilePrefix { @@ -75,6 +77,12 @@ impl URL { headers, }) } + pub fn map_ref<'a, SE2>( + &'a self, + f: impl FnOnce(&'a SE) -> SE2, + ) -> URL { + trivial_result(self.traverse_ref(|x| Ok(f(x)))) + } } impl ImportTarget { @@ -90,6 +98,12 @@ impl ImportTarget { Missing => Missing, }) } + pub fn map_ref<'a, SE2>( + &'a self, + f: impl FnOnce(&'a SE) -> SE2, + ) -> ImportTarget { + trivial_result(self.traverse_ref(|x| Ok(f(x)))) + } } impl Import { @@ -103,4 +117,10 @@ impl Import { hash: self.hash.clone(), }) } + pub fn map_ref<'a, SE2>( + &'a self, + f: impl FnOnce(&'a SE) -> SE2, + ) -> Import { + trivial_result(self.traverse_ref(|x| Ok(f(x)))) + } } diff --git a/dhall/src/syntax/text/printer.rs b/dhall/src/syntax/text/printer.rs index e9584bb..2b7bc2e 100644 --- a/dhall/src/syntax/text/printer.rs +++ b/dhall/src/syntax/text/printer.rs @@ -8,17 +8,23 @@ use std::fmt::{self, Display}; // of automatically getting all the parentheses and precedences right. #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] enum PrintPhase { + // `expression` Base, + // `operator-expression` Operator, + // All the operator `*-expression`s BinOp(ast::BinOp), + // `application-expression` App, + // `import-expression` Import, + // `primitive-expression` Primitive, } // Wraps an Expr with a phase, so that phase selection can be done separate from the actual // printing. -#[derive(Clone)] +#[derive(Copy, Clone)] struct PhasedExpr<'a>(&'a Expr, PrintPhase); impl<'a> PhasedExpr<'a> { @@ -58,7 +64,7 @@ impl UnspannedExpr { ), SomeLit(e) => SomeLit(e.phase(PrintPhase::Import)), ExprKind::App(f, a) => ExprKind::App( - f.phase(PrintPhase::Import), + f.phase(PrintPhase::App), a.phase(PrintPhase::Import), ), Field(a, b) => Field(a.phase(Primitive), b), @@ -67,6 +73,9 @@ impl UnspannedExpr { Completion(a, b) => { Completion(a.phase(Primitive), b.phase(Primitive)) } + ExprKind::Import(a) => { + ExprKind::Import(a.map_ref(|x| x.phase(PrintPhase::Import))) + } e => e, } } @@ -84,7 +93,6 @@ impl UnspannedExpr { | Pi(_, _, _) | Let(_, _, _, _) | EmptyListLit(_) - | NEListLit(_) | SomeLit(_) | Merge(_, _, _) | ToMap(_, _) @@ -92,10 +100,7 @@ impl UnspannedExpr { // Precedence is magically handled by the ordering of BinOps. ExprKind::BinOp(op, _, _) => phase > PrintPhase::BinOp(*op), ExprKind::App(_, _) => phase > PrintPhase::App, - Field(_, _) - | Projection(_, _) - | ProjectionByExpr(_, _) - | Completion(_, _) => phase > PrintPhase::Import, + Completion(_, _) => phase > PrintPhase::Import, _ => false, }; @@ -413,7 +418,7 @@ impl Display for Import { write!(f, "?{}", q)? } if let Some(h) = &url.headers { - write!(f, " using ({})", h)? + write!(f, " using {}", h)? } } Env(s) => { -- cgit v1.2.3