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 +++++++++++++-------- dhall/tests/parser/success/annotationsB.txt | 2 +- dhall/tests/parser/success/builtinNameAsFieldB.txt | 2 +- dhall/tests/parser/success/fieldsB.txt | 2 +- dhall/tests/parser/success/listB.txt | 2 +- dhall/tests/parser/success/operatorsB.txt | 2 +- dhall/tests/parser/success/quotedBoundVariableB.txt | 2 +- .../success/recordProjectionByExpressionB.txt | 2 +- .../unit/FunctionApplicationMultipleArgsB.txt | 2 +- .../parser/success/unit/ListLitEmptyPrecedenceB.txt | 2 +- .../success/unit/ListLitNonEmptyAnnotatedB.txt | 2 +- dhall/tests/parser/success/unit/SomeXYZB.txt | 2 +- dhall/tests/parser/success/unit/import/HeadersB.txt | 2 +- .../success/unit/import/HeadersDoubleHashB.txt | 2 +- .../unit/import/HeadersDoubleHashPrecedenceB.txt | 2 +- .../success/unit/import/HeadersHashPrecedenceB.txt | 2 +- .../success/unit/import/HeadersInteriorHashB.txt | 2 +- .../parser/success/unit/import/inlineUsingB.txt | 2 +- 20 files changed, 52 insertions(+), 27 deletions(-) (limited to 'dhall') 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) => { diff --git a/dhall/tests/parser/success/annotationsB.txt b/dhall/tests/parser/success/annotationsB.txt index e4d2113..676d8bb 100644 --- a/dhall/tests/parser/success/annotationsB.txt +++ b/dhall/tests/parser/success/annotationsB.txt @@ -1 +1 @@ -([] : List Natural) # ([1, 2, 3]) # (([1, 2, 3]) : List Natural) : List Natural +([] : List Natural) # [1, 2, 3] # ([1, 2, 3] : List Natural) : List Natural diff --git a/dhall/tests/parser/success/builtinNameAsFieldB.txt b/dhall/tests/parser/success/builtinNameAsFieldB.txt index f767e78..a4ecc3a 100644 --- a/dhall/tests/parser/success/builtinNameAsFieldB.txt +++ b/dhall/tests/parser/success/builtinNameAsFieldB.txt @@ -1 +1 @@ -let Prelude = https://prelude.dhall-lang.org/package.dhall in (Prelude.`List`).map +let Prelude = https://prelude.dhall-lang.org/package.dhall in Prelude.`List`.map diff --git a/dhall/tests/parser/success/fieldsB.txt b/dhall/tests/parser/success/fieldsB.txt index d091e6d..e709f20 100644 --- a/dhall/tests/parser/success/fieldsB.txt +++ b/dhall/tests/parser/success/fieldsB.txt @@ -1 +1 @@ -(({ foo = { bar = { baz = 1 } } }.foo).bar).baz +{ foo = { bar = { baz = 1 } } }.foo.bar.baz diff --git a/dhall/tests/parser/success/listB.txt b/dhall/tests/parser/success/listB.txt index f6d50d0..31a6233 100644 --- a/dhall/tests/parser/success/listB.txt +++ b/dhall/tests/parser/success/listB.txt @@ -1 +1 @@ -[[+1, +2, +3], ([+1, +2, +3]) : List Integer, [] : List Integer] +[[+1, +2, +3], [+1, +2, +3] : List Integer, [] : List Integer] diff --git a/dhall/tests/parser/success/operatorsB.txt b/dhall/tests/parser/success/operatorsB.txt index 31db4ce..3f987aa 100644 --- a/dhall/tests/parser/success/operatorsB.txt +++ b/dhall/tests/parser/success/operatorsB.txt @@ -1 +1 @@ -{ foo = False && Natural/even (1 + 2 * 3) || True == False != True } ∧ { bar = (["ABC" ++ "DEF"]) # (["GHI"]) } ⫽ { baz = True } : { baz : Bool, foo : Bool } ⩓ { bar : List Text } +{ foo = False && Natural/even (1 + 2 * 3) || True == False != True } ∧ { bar = ["ABC" ++ "DEF"] # ["GHI"] } ⫽ { baz = True } : { baz : Bool, foo : Bool } ⩓ { bar : List Text } diff --git a/dhall/tests/parser/success/quotedBoundVariableB.txt b/dhall/tests/parser/success/quotedBoundVariableB.txt index 6f5b7bf..a9f0499 100644 --- a/dhall/tests/parser/success/quotedBoundVariableB.txt +++ b/dhall/tests/parser/success/quotedBoundVariableB.txt @@ -1 +1 @@ -((λ(`Natural/even` : Natural → Bool) → `Natural/even`) Natural/odd) 0 +(λ(`Natural/even` : Natural → Bool) → `Natural/even`) Natural/odd 0 diff --git a/dhall/tests/parser/success/recordProjectionByExpressionB.txt b/dhall/tests/parser/success/recordProjectionByExpressionB.txt index 14af94f..01ca8c2 100644 --- a/dhall/tests/parser/success/recordProjectionByExpressionB.txt +++ b/dhall/tests/parser/success/recordProjectionByExpressionB.txt @@ -1 +1 @@ -let example1 = λ(A : Type) → λ(B : Type) → λ(C : { x : A, y : B }) → C.({ x : A }) : { x : A } in let example2 = λ(A : Type) → λ(B : Type) → λ(C : { p : A, q : B }) → C.(let r = { p : A } in r) : { p : A } in let A = Natural in let B = Text in ((example1 A) B) { x = 10, y = "Text" } ∧ ((example2 A) B) { p = 10, q = "Text" } : { p : A, x : A } +let example1 = λ(A : Type) → λ(B : Type) → λ(C : { x : A, y : B }) → C.({ x : A }) : { x : A } in let example2 = λ(A : Type) → λ(B : Type) → λ(C : { p : A, q : B }) → C.(let r = { p : A } in r) : { p : A } in let A = Natural in let B = Text in example1 A B { x = 10, y = "Text" } ∧ example2 A B { p = 10, q = "Text" } : { p : A, x : A } diff --git a/dhall/tests/parser/success/unit/FunctionApplicationMultipleArgsB.txt b/dhall/tests/parser/success/unit/FunctionApplicationMultipleArgsB.txt index a1b1c55..71bbe20 100644 --- a/dhall/tests/parser/success/unit/FunctionApplicationMultipleArgsB.txt +++ b/dhall/tests/parser/success/unit/FunctionApplicationMultipleArgsB.txt @@ -1 +1 @@ -((f x) y) z +f x y z diff --git a/dhall/tests/parser/success/unit/ListLitEmptyPrecedenceB.txt b/dhall/tests/parser/success/unit/ListLitEmptyPrecedenceB.txt index 604143e..74149c2 100644 --- a/dhall/tests/parser/success/unit/ListLitEmptyPrecedenceB.txt +++ b/dhall/tests/parser/success/unit/ListLitEmptyPrecedenceB.txt @@ -1 +1 @@ -[] : (List T) U +[] : List T U diff --git a/dhall/tests/parser/success/unit/ListLitNonEmptyAnnotatedB.txt b/dhall/tests/parser/success/unit/ListLitNonEmptyAnnotatedB.txt index 5144935..3e563c7 100644 --- a/dhall/tests/parser/success/unit/ListLitNonEmptyAnnotatedB.txt +++ b/dhall/tests/parser/success/unit/ListLitNonEmptyAnnotatedB.txt @@ -1 +1 @@ -([x, y]) : List T +[x, y] : List T diff --git a/dhall/tests/parser/success/unit/SomeXYZB.txt b/dhall/tests/parser/success/unit/SomeXYZB.txt index 42be8b2..8eb59ac 100644 --- a/dhall/tests/parser/success/unit/SomeXYZB.txt +++ b/dhall/tests/parser/success/unit/SomeXYZB.txt @@ -1 +1 @@ -((Some x) y) z +(Some x) y z diff --git a/dhall/tests/parser/success/unit/import/HeadersB.txt b/dhall/tests/parser/success/unit/import/HeadersB.txt index 337a497..47eb98e 100644 --- a/dhall/tests/parser/success/unit/import/HeadersB.txt +++ b/dhall/tests/parser/success/unit/import/HeadersB.txt @@ -1 +1 @@ -https://example.com/foo using (x) +https://example.com/foo using x diff --git a/dhall/tests/parser/success/unit/import/HeadersDoubleHashB.txt b/dhall/tests/parser/success/unit/import/HeadersDoubleHashB.txt index bf808c8..31e19eb 100644 --- a/dhall/tests/parser/success/unit/import/HeadersDoubleHashB.txt +++ b/dhall/tests/parser/success/unit/import/HeadersDoubleHashB.txt @@ -1 +1 @@ -https://example.com/foo using (./headers sha256:0000000000000000000000000000000000000000000000000000000000000000) sha256:1111111111111111111111111111111111111111111111111111111111111111 +https://example.com/foo using ./headers sha256:0000000000000000000000000000000000000000000000000000000000000000 sha256:1111111111111111111111111111111111111111111111111111111111111111 diff --git a/dhall/tests/parser/success/unit/import/HeadersDoubleHashPrecedenceB.txt b/dhall/tests/parser/success/unit/import/HeadersDoubleHashPrecedenceB.txt index bf808c8..31e19eb 100644 --- a/dhall/tests/parser/success/unit/import/HeadersDoubleHashPrecedenceB.txt +++ b/dhall/tests/parser/success/unit/import/HeadersDoubleHashPrecedenceB.txt @@ -1 +1 @@ -https://example.com/foo using (./headers sha256:0000000000000000000000000000000000000000000000000000000000000000) sha256:1111111111111111111111111111111111111111111111111111111111111111 +https://example.com/foo using ./headers sha256:0000000000000000000000000000000000000000000000000000000000000000 sha256:1111111111111111111111111111111111111111111111111111111111111111 diff --git a/dhall/tests/parser/success/unit/import/HeadersHashPrecedenceB.txt b/dhall/tests/parser/success/unit/import/HeadersHashPrecedenceB.txt index 15d38db..e2d5899 100644 --- a/dhall/tests/parser/success/unit/import/HeadersHashPrecedenceB.txt +++ b/dhall/tests/parser/success/unit/import/HeadersHashPrecedenceB.txt @@ -1 +1 @@ -https://example.com/foo using (./headers sha256:0000000000000000000000000000000000000000000000000000000000000000) +https://example.com/foo using ./headers sha256:0000000000000000000000000000000000000000000000000000000000000000 diff --git a/dhall/tests/parser/success/unit/import/HeadersInteriorHashB.txt b/dhall/tests/parser/success/unit/import/HeadersInteriorHashB.txt index 15d38db..e2d5899 100644 --- a/dhall/tests/parser/success/unit/import/HeadersInteriorHashB.txt +++ b/dhall/tests/parser/success/unit/import/HeadersInteriorHashB.txt @@ -1 +1 @@ -https://example.com/foo using (./headers sha256:0000000000000000000000000000000000000000000000000000000000000000) +https://example.com/foo using ./headers sha256:0000000000000000000000000000000000000000000000000000000000000000 diff --git a/dhall/tests/parser/success/unit/import/inlineUsingB.txt b/dhall/tests/parser/success/unit/import/inlineUsingB.txt index 1434d72..b36afc0 100644 --- a/dhall/tests/parser/success/unit/import/inlineUsingB.txt +++ b/dhall/tests/parser/success/unit/import/inlineUsingB.txt @@ -1 +1 @@ -https://example.com/foo using ([{ mapKey = "Authorization", mapValue = "token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4" }]) +https://example.com/foo using [{ mapKey = "Authorization", mapValue = "token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4" }] -- cgit v1.2.3