Skip to content

Commit a70feff

Browse files
committed
treewide: Expr.Types: NExprF: NSelect: move optional default as first arg
This allows to reuse `Nothing` & `Just` logic between uses. This also leads to more streamlined code.
1 parent 996f91b commit a70feff

File tree

10 files changed

+28
-39
lines changed

10 files changed

+28
-39
lines changed

src/Nix/Eval.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ eval (NBinary op larg rarg) =
129129
lav <- larg
130130
evalBinary op lav rarg
131131

132-
eval (NSelect aset attr alt ) =
132+
eval (NSelect alt aset attr) =
133133
do
134134
let useAltOrReportMissing (s, ks) = fromMaybe (attrMissing ks $ pure s) alt
135135

src/Nix/Expr/Shorthands.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ mkFunction :: Params NExpr -> NExpr -> NExpr
175175
mkFunction params = Fix . NAbs params
176176

177177
-- | General dot-reference with optional alternative if the jey does not exist.
178-
getRefOrDefault :: NExpr -> Text -> Maybe NExpr -> NExpr
179-
getRefOrDefault obj name alt = Fix $ NSelect obj (mkSelector name) alt
178+
getRefOrDefault :: Maybe NExpr -> NExpr -> Text -> NExpr
179+
getRefOrDefault alt obj name = Fix $ NSelect alt obj (mkSelector name)
180180

181181
-- ** Base functor builders for basic expressions builders *sic
182182

@@ -294,14 +294,14 @@ recAttrsE pairs = mkRecSet $ uncurry ($=) <$> pairs
294294

295295
-- | Dot-reference into an attribute set: @attrSet.k@
296296
(@.) :: NExpr -> Text -> NExpr
297-
(@.) obj name = getRefOrDefault obj name Nothing
297+
(@.) = getRefOrDefault Nothing
298298
infix 9 @.
299299

300300
-- | Dot-reference into an attribute set with alternative if the key does not exist.
301301
--
302302
-- > s.x or y
303303
(@.<|>) :: NExpr -> Text -> NExpr -> NExpr
304-
(@.<|>) obj name alt = getRefOrDefault obj name $ pure alt
304+
(@.<|>) obj name alt = getRefOrDefault (pure alt ) obj name
305305
infix 9 @.<|>
306306

307307
-- | Function application (@' '@ in @f x@)

src/Nix/Expr/Types.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,14 @@ data NExprF r
530530
--
531531
-- > NBinary NPlus x y ~ x + y
532532
-- > NBinary NApp f x ~ f x
533-
| NSelect !r !(NAttrPath r) !(Maybe r)
533+
| NSelect !(Maybe r) !r !(NAttrPath r)
534534
-- 2021-05-15: NOTE: Default value should be first argument to leverage partial application.
535535
-- Cascading change diff is not that big.
536536
-- ^ Dot-reference into an attribute set, optionally providing an
537537
-- alternative if the key doesn't exist.
538538
--
539-
-- > NSelect s (x :| []) Nothing ~ s.x
540-
-- > NSelect s (x :| []) (pure y) ~ s.x or y
539+
-- > NSelect Nothing s (x :| []) ~ s.x
540+
-- > NSelect (pure y) s (x :| []) ~ s.x or y
541541
| NHasAttr !r !(NAttrPath r)
542542
-- ^ Ask if a set contains a given attribute path.
543543
--

src/Nix/Expr/Types/Annotated.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,9 @@ nBinary (Ann s1 b) e1@(AnnE s2 _) e2@(AnnE s3 _) =
158158
AnnE (s1 <> s2 <> s3) $ NBinary b e1 e2
159159

160160
nSelectLoc
161-
:: NExprLoc -> Ann SrcSpan (NAttrPath NExprLoc) -> Maybe NExprLoc -> NExprLoc
162-
nSelectLoc e1@(AnnE s1 _) (Ann s2 ats) =
163-
-- 2021-05-16: NOTE: This could been rewritten into function application of @(s3, pure e2)@
164-
-- if @SrcSpan@ was Monoid, which requires @SorcePos@ to be a Monoid, and upstream code prevents it.
165-
-- Question upstream: https://github.com/mrkkrp/megaparsec/issues/450
166-
maybe
167-
( AnnE s1s2 $ NSelect e1 ats Nothing)
168-
(\ e2@(AnnE s3 _) -> AnnE (s1s2 <> s3) $ NSelect e1 ats $ pure e2)
169-
where
170-
s1s2 = s1 <> s2
161+
:: Maybe NExprLoc -> NExprLoc -> Ann SrcSpan (NAttrPath NExprLoc) -> NExprLoc
162+
nSelectLoc Nothing e1@(AnnE s2 _) (Ann s1 ats) = AnnE (s2 <> s1) $ NSelect Nothing e1 ats
163+
nSelectLoc (Just e2@(AnnE s3 _)) e1@(AnnE s2 _) (Ann s1 ats) = AnnE (s3 <> s2 <> s1) $ NSelect (pure e2) e1 ats
171164

172165
nHasAttr :: NExprLoc -> Ann SrcSpan (NAttrPath NExprLoc) -> NExprLoc
173166
nHasAttr e1@(AnnE s1 _) (Ann s2 ats) = AnnE (s1 <> s2) $ NHasAttr e1 ats
@@ -222,8 +215,8 @@ pattern NUnary_ ann op x = AnnFP ann (NUnary op x)
222215
pattern NBinary_ :: SrcSpan -> NBinaryOp -> r -> r -> NExprLocF r
223216
pattern NBinary_ ann op x y = AnnFP ann (NBinary op x y)
224217

225-
pattern NSelect_ :: SrcSpan -> r -> NAttrPath r -> Maybe r -> NExprLocF r
226-
pattern NSelect_ ann x p v = AnnFP ann (NSelect x p v)
218+
pattern NSelect_ :: SrcSpan -> Maybe r -> r -> NAttrPath r -> NExprLocF r
219+
pattern NSelect_ ann v x p = AnnFP ann (NSelect v x p)
227220

228221
pattern NHasAttr_ :: SrcSpan -> r -> NAttrPath r -> NExprLocF r
229222
pattern NHasAttr_ ann x p = AnnFP ann (NHasAttr x p)

src/Nix/Parser.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ nixSelect term =
144144
-> NExprLoc
145145
build t mexpr =
146146
maybe
147-
id
148-
(\ expr t -> (uncurry $ nSelectLoc t) expr)
149-
mexpr
150147
t
148+
(\ (a, m) -> (`nSelectLoc` t) m a)
149+
mexpr
151150

152151
nixSelector :: Parser (Ann SrcSpan (NAttrPath NExprLoc))
153152
nixSelector =
@@ -265,7 +264,7 @@ nixLet = annotateLocation1
265264
(reserved "in" *> nixToplevelForm)
266265
-- Let expressions `let {..., body = ...}' are just desugared
267266
-- into `(rec {..., body = ...}).body'.
268-
letBody = (\x -> NSelect x (StaticKey "body" :| mempty) Nothing) <$> aset
267+
letBody = (\x -> NSelect Nothing x (StaticKey "body" :| mempty)) <$> aset
269268
aset = annotateLocation1 $ NSet Recursive <$> braces nixBinders
270269

271270
nixIf :: Parser NExprLoc

src/Nix/Pretty.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ exprFNixDoc = \case
248248
opInfo
249249
(pretty (operatorName opInfo) <> wrapParens opInfo r1)
250250
where opInfo = getUnaryOperator op
251-
NSelect r' attr o ->
251+
NSelect o r' attr ->
252252
maybe
253253
(mkNixDoc selectOp)
254254
(const leastPrecedence)

src/Nix/Reduce.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ reduce (NBinary_ bann op larg rarg) =
203203
-- 1. The selected expr is indeed a set.
204204
-- 2. The selection AttrPath is a list of StaticKeys.
205205
-- 3. The selected AttrPath exists in the set.
206-
reduce base@(NSelect_ _ _ attrs _)
206+
reduce base@(NSelect_ _ _ _ attrs)
207207
| sAttrPath $ NE.toList attrs = do
208-
(NSelect_ _ aset attrs _) <- sequence base
208+
(NSelect_ _ _ aset attrs) <- sequence base
209209
inspectSet (unFix aset) attrs
210210
| otherwise = sId
211211
where
@@ -378,8 +378,8 @@ pruneTree opts =
378378
(`NLet` body)
379379
(mapMaybe pruneBinding binds)
380380

381-
NSelect (Just aset) attr alt ->
382-
pure $ NSelect aset (pruneKeyName <$> attr) $ join alt
381+
NSelect alt (Just aset) attr ->
382+
pure $ NSelect (join alt) aset $ pruneKeyName <$> attr
383383

384384
-- These are the only short-circuiting binary operators
385385
NBinary NAnd (Just (AnnE _ larg)) _ -> pure larg

src/Nix/TH.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ freeVars e = case unFix e of
6262
(NEnvPath _ ) -> mempty
6363
(NUnary _ expr ) -> freeVars expr
6464
(NBinary _ left right ) -> collectFreeVars left right
65-
(NSelect expr path orExpr) ->
65+
(NSelect orExpr expr path) ->
6666
Set.unions
6767
[ freeVars expr
6868
, pathFree path

tests/ParserTests.hs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,15 @@ case_indented_string_escape =
493493

494494
case_select =
495495
checks
496-
( Fix $ NSelect (var "a")
497-
(StaticKey "e" :| [StaticKey "di", StaticKey "f"])
498-
Nothing
496+
( Fix $ NSelect Nothing (var "a") (StaticKey "e" :| [StaticKey "di", StaticKey "f"])
499497
, "a . e .di. f"
500498
)
501-
( Fix $ NSelect (var "a")
499+
( Fix $ NSelect (pure mkNull) (var "a")
502500
(StaticKey "e" :| [StaticKey "d"])
503-
(pure mkNull)
504501
, "a.e . d or null"
505502
)
506-
( Fix $ NSelect emptySet
507-
(DynamicKey (Plain (DoubleQuoted mempty)) :| mempty) (pure mkNull)
503+
( Fix $ NSelect (pure mkNull) emptySet
504+
(DynamicKey (Plain (DoubleQuoted mempty)) :| mempty)
508505
, "{}.\"\"or null"
509506
)
510507
( Fix $ NBinary NConcat
@@ -534,7 +531,7 @@ case_select_path =
534531
( emptySet @@ mkRelPath "./def"
535532
, "{}./def"
536533
)
537-
( Fix (NSelect emptySet (DynamicKey (Plain $ DoubleQuoted mempty) :| mempty) Nothing) @@ mkRelPath "./def"
534+
( Fix (NSelect Nothing emptySet (DynamicKey (Plain $ DoubleQuoted mempty) :| mempty)) @@ mkRelPath "./def"
538535
, "{}.\"\"./def"
539536
)
540537

tests/PrettyParseTests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ genExpr =
132132
genEnvPath = NEnvPath <$> asciiString
133133
genUnary = liftA2 NUnary Gen.enumBounded genExpr
134134
genBinary = liftA3 NBinary Gen.enumBounded genExpr genExpr
135-
genSelect = liftA3 NSelect genExpr genAttrPath (Gen.maybe genExpr)
135+
genSelect = liftA3 NSelect (Gen.maybe genExpr) genExpr genAttrPath
136136
genHasAttr = liftA2 NHasAttr genExpr genAttrPath
137137
genAbs = liftA2 NAbs genParams genExpr
138138
genLet = liftA2 NLet (fairList genBinding) genExpr

0 commit comments

Comments
 (0)