Skip to content

Commit 27e357d

Browse files
Merge #1009: Unify accessor naming
2 parents 8e64e98 + 607e00f commit 27e357d

File tree

11 files changed

+322
-268
lines changed

11 files changed

+322
-268
lines changed

main/Main.hs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
152152
| isEvaluate =
153153
if
154154
| isTrace -> evaluateExprWith nixTracingEvalExprLoc expr
155-
| Just path <- getReduce -> evaluateExprWith (reduction path . coerce) expr
156-
| null getArg || null getArgstr -> evaluateExprWith nixEvalExprLoc expr
155+
| Just path <- getReduce -> evaluateExprWith (reduction path . coerce) expr
156+
| null getArg || null getArgstr -> evaluateExprWith nixEvalExprLoc expr
157157
| otherwise -> processResult printer <=< nixEvalExprLoc (coerce mpath) $ expr
158158
| isXml = fail "Rendering expression trees to XML is not yet implemented"
159159
| isJson = fail "Rendering expression trees to JSON is not implemented"
160-
| getVerbosity >= DebugInfo = liftIO . putStr . ppShow . stripAnnotation $ expr
161-
| isCache , Just path <- mpath = liftIO . writeCache (replaceExtension path "nixc") $ expr
162-
| isParseOnly = void . liftIO . Exception.evaluate . force $ expr
163-
| otherwise =
160+
| getVerbosity >= DebugInfo = liftIO . putStr . ppShow . stripAnnotation $ expr
161+
| isCache , Just path <- mpath = liftIO . writeCache (replaceExtension path "nixc") $ expr
162+
| isParseOnly = void . liftIO . Exception.evaluate . force $ expr
163+
| otherwise =
164164
liftIO .
165165
renderIO
166166
stdout
@@ -178,24 +178,24 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
178178
| isFinder = findAttrs <=< fromValue @(AttrSet StdVal)
179179
| otherwise = printer'
180180
where
181+
-- 2021-05-27: NOTE: With naive fix of the #941
182+
-- This is overall a naive printer implementation, as options should interact/respect one another.
183+
-- A nice question: "Should respect one another to what degree?": Go full combinator way, for which
184+
-- old Nix CLI is nototrious for (and that would mean to reimplement the old Nix CLI),
185+
-- OR: https://github.com/haskell-nix/hnix/issues/172 and have some sane standart/default behaviour for (most) keys.
181186
printer'
182-
| isXml = fun (ignoreContext . toXML) normalForm
183-
-- 2021-05-27: NOTE: With naive fix of the #941
184-
-- This is overall a naive printer implementation, as options should interact/respect one another.
185-
-- A nice question: "Should respect one another to what degree?": Go full combinator way, for which
186-
-- old Nix CLI is nototrious for (and that would mean to reimplement the old Nix CLI),
187-
-- OR: https://github.com/haskell-nix/hnix/issues/172 and have some sane standart/default behaviour for (most) keys.
188-
| isJson = fun (ignoreContext . mempty . toJSONNixString) normalForm
189-
| isStrict = fun (show . prettyNValue) normalForm
190-
| isValues = fun (show . prettyNValueProv) removeEffects
191-
| otherwise = fun (show . prettyNValue) removeEffects
187+
| isXml = out (ignoreContext . toXML) normalForm
188+
| isJson = out (ignoreContext . mempty . toJSONNixString) normalForm
189+
| isStrict = out (show . prettyNValue) normalForm
190+
| isValues = out (show . prettyNValueProv) removeEffects
191+
| otherwise = out (show . prettyNValue) removeEffects
192192
where
193-
fun
193+
out
194194
:: (b -> Text)
195195
-> (a -> StandardIO b)
196196
-> a
197197
-> StdIO
198-
fun g f = liftIO . Text.putStrLn . g <=< f
198+
out transform val = liftIO . Text.putStrLn . transform <=< val
199199

200200
findAttrs
201201
:: AttrSet StdVal
@@ -238,9 +238,9 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
238238
(forceEntry path nv)
239239
(descend &&
240240
deferred
241-
(const False)
242-
(const True)
243-
val
241+
(const False)
242+
(const True)
243+
val
244244
)
245245
)
246246
(pure . pure . Free)

src/Nix/Builtins.hs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,22 +247,20 @@ instance Show VersionComponent where
247247

248248
splitVersion :: Text -> [VersionComponent]
249249
splitVersion s =
250-
whenJust
251-
(\ (x, xs) -> if
252-
| isRight eDigitsPart ->
253-
either
254-
(\ e -> error $ "splitVersion: did hit impossible: '" <> fromString e <> "' while parsing '" <> s <> "'.")
255-
(\ res ->
256-
one (VersionComponentNumber $ fst res)
257-
<> splitVersion (snd res)
258-
)
259-
eDigitsPart
260-
261-
| x `elem` separators -> splitVersion xs
262-
263-
| otherwise -> one charsPart <> splitVersion rest2
264-
)
265-
(Text.uncons s)
250+
(\ (x, xs) -> if
251+
| isRight eDigitsPart ->
252+
either
253+
(\ e -> error $ "splitVersion: did hit impossible: '" <> fromString e <> "' while parsing '" <> s <> "'.")
254+
(\ res ->
255+
one (VersionComponentNumber $ fst res)
256+
<> splitVersion (snd res)
257+
)
258+
eDigitsPart
259+
260+
| x `elem` separators -> splitVersion xs
261+
262+
| otherwise -> one charsPart <> splitVersion rest2
263+
) `whenJust` Text.uncons s
266264
where
267265
-- | Based on https://github.com/NixOS/nix/blob/4ee4fda521137fed6af0446948b3877e0c5db803/src/libexpr/names.cc#L44
268266
separators :: String

src/Nix/Cited.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import Control.Monad.Free ( Free(Pure, Free) )
1616

1717
data Provenance m v =
1818
Provenance
19-
{ _lexicalScope :: Scopes m v
20-
, _originExpr :: NExprLocF (Maybe v)
19+
{ getLexicalScope :: Scopes m v
20+
-- 2021-11-09: NOTE: Better name?
21+
, getOriginExpr :: NExprLocF (Maybe v)
2122
-- ^ When calling the function x: x + 2 with argument x = 3, the
2223
-- 'originExpr' for the resulting value will be 3 + 2, while the
2324
-- 'contextExpr' will be @(x: x + 2) 3@, preserving not only the
@@ -27,8 +28,8 @@ data Provenance m v =
2728

2829
data NCited m v a =
2930
NCited
30-
{ _provenance :: [Provenance m v]
31-
, _cited :: a
31+
{ getProvenance :: [Provenance m v]
32+
, getCited :: a
3233
}
3334
deriving (Generic, Typeable, Functor, Foldable, Traversable, Show)
3435

@@ -37,11 +38,11 @@ instance Applicative (NCited m v) where
3738
(<*>) (NCited xs f) (NCited ys x) = NCited (xs <> ys) (f x)
3839

3940
instance Comonad (NCited m v) where
40-
duplicate p = NCited (_provenance p) p
41-
extract = _cited
41+
duplicate p = NCited (getProvenance p) p
42+
extract = getCited
4243

4344
instance ComonadEnv [Provenance m v] (NCited m v) where
44-
ask = _provenance
45+
ask = getProvenance
4546

4647
$(makeLenses ''Provenance)
4748
$(makeLenses ''NCited)
@@ -55,7 +56,7 @@ class HasCitations m v a where
5556
addProvenance :: Provenance m v -> a -> a
5657

5758
instance HasCitations m v (NCited m v a) where
58-
citations = _provenance
59+
citations = getProvenance
5960
addProvenance x (NCited p v) = NCited (x : p) v
6061

6162
instance HasCitations1 m v f

src/Nix/Expr/Strings.hs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ runAntiquoted nl f _ EscapedNewline = f nl
3939
runAntiquoted _ _ k (Antiquoted r) = k r
4040

4141
-- | Split a stream representing a string with antiquotes on line breaks.
42-
splitLines :: [Antiquoted Text r] -> [[Antiquoted Text r]]
43-
splitLines = uncurry (flip (:)) . go where
44-
go (Plain t : xs) = (Plain l :) <$> foldr f (go xs) ls
42+
splitLines :: forall r . [Antiquoted Text r] -> [[Antiquoted Text r]]
43+
splitLines = uncurry (flip (:)) . go
44+
where
45+
go :: [Antiquoted Text r] -> ([[Antiquoted Text r]], [Antiquoted Text r])
46+
go (Plain t : xs) = (one (Plain l) <>) <$> foldr f (go xs) ls
4547
where
4648
(l : ls) = T.split (== '\n') t
4749
f prefix (finished, current) = ((Plain prefix : current) : finished, mempty)
48-
go (Antiquoted a : xs) = (Antiquoted a :) <$> go xs
49-
go (EscapedNewline : xs) = (EscapedNewline :) <$> go xs
50+
go (Antiquoted a : xs) = (one (Antiquoted a) <>) <$> go xs
51+
go (EscapedNewline : xs) = (one EscapedNewline <>) <$> go xs
5052
go [] = mempty
5153

5254
-- | Join a stream of strings containing antiquotes again. This is the inverse
@@ -108,10 +110,17 @@ stripIndent xs =
108110

109111
escapeCodes :: [(Char, Char)]
110112
escapeCodes =
111-
[('\n', 'n'), ('\r', 'r'), ('\t', 't'), ('\\', '\\'), ('$', '$'), ('"', '"')]
113+
[('\n', 'n'), ('\r', 'r'), ('\t', 't'), ('"', '"'), ('$', '$'), ('\\', '\\')]
112114

113115
fromEscapeCode :: Char -> Maybe Char
114116
fromEscapeCode = (`lookup` (swap <$> escapeCodes))
115117

116118
toEscapeCode :: Char -> Maybe Char
117119
toEscapeCode = (`lookup` escapeCodes)
120+
121+
escapeMap :: [(Text, Text)]
122+
escapeMap =
123+
[("\n", "\\n"), ("\r", "\\r"), ("\t", "\\t"), ("\"", "\\\""), ("${", "\\${"), ("\\", "\\\\")]
124+
125+
escapeString :: Text -> Text
126+
escapeString = applyAll (fmap (uncurry T.replace) escapeMap)

src/Nix/Expr/Types.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,9 @@ getFreeVars e =
776776
(NAbs (ParamSet varname _ pset) expr) ->
777777
Set.difference
778778
-- Include all free variables from the expression and the default arguments
779-
(getFreeVars expr <> (Set.unions $ getFreeVars <$> mapMaybe snd pset))
779+
(getFreeVars expr <> Set.unions (getFreeVars <$> mapMaybe snd pset))
780780
-- But remove the argument name if existing, and all arguments in the parameter set
781-
((one `whenJust` varname) <> (Set.fromList $ fst <$> pset))
781+
((one `whenJust` varname) <> Set.fromList (fst <$> pset))
782782
(NLet bindings expr ) ->
783783
Set.difference
784784
(getFreeVars expr <> bindFreeVars bindings)

src/Nix/Normal.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ normalizeValueF f = run . iterNValueM run (flip go) (fmap Free . sequenceNValue'
100100
(do
101101
i <- ask
102102
when (i > 2000) $ fail "Exceeded maximum normalization depth of 2000 levels"
103-
lifted (lifted $ f t) $ local succ . k
103+
(lifted . lifted)
104+
(f t)
105+
(local succ . k)
104106
)
105107
(pure $ pure t)
106108
b

src/Nix/Parser.hs

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -271,73 +271,78 @@ nixAntiquoted p =
271271
antiquotedLexeme
272272
<|> Plain <$> p
273273

274-
nixString' :: Parser (NString NExprLoc)
275-
nixString' = label "string" $ lexeme $ doubleQuoted <|> indented
274+
escapeCode :: Parser Char
275+
escapeCode =
276+
msum
277+
[ c <$ char e | (c, e) <- escapeCodes ]
278+
<|> anySingle
279+
280+
stringChar
281+
:: Parser ()
282+
-> Parser ()
283+
-> Parser (Antiquoted Text NExprLoc)
284+
-> Parser (Antiquoted Text NExprLoc)
285+
stringChar end escStart esc =
286+
antiquoted
287+
<|> Plain . one <$> char '$'
288+
<|> esc
289+
<|> Plain . fromString <$> some plainChar
290+
where
291+
plainChar :: Parser Char
292+
plainChar =
293+
notFollowedBy (end <|> void (char '$') <|> escStart) *> anySingle
294+
295+
doubleQuoted :: Parser (NString NExprLoc)
296+
doubleQuoted =
297+
label "double quoted string" $
298+
DoubleQuoted . removeEmptyPlains . mergePlain <$>
299+
inQuotationMarks (many $ stringChar quotationMark (void $ char '\\') doubleEscape)
300+
where
301+
inQuotationMarks :: Parser a -> Parser a
302+
inQuotationMarks expr = quotationMark *> expr <* quotationMark
303+
304+
quotationMark :: Parser ()
305+
quotationMark = void $ char '"'
306+
307+
doubleEscape :: Parser (Antiquoted Text r)
308+
doubleEscape = Plain . one <$> (char '\\' *> escapeCode)
309+
310+
311+
indented :: Parser (NString NExprLoc)
312+
indented =
313+
label "indented string" $
314+
stripIndent <$>
315+
inIndentedQuotation (many $ join stringChar indentedQuotationMark indentedEscape)
276316
where
277-
doubleQuoted :: Parser (NString NExprLoc)
278-
doubleQuoted =
279-
label "double quoted string" $
280-
DoubleQuoted . removeEmptyPlains . mergePlain <$>
281-
inQuotationMarks (many $ stringChar quotationMark (void $ char '\\') doubleEscape)
282-
where
283-
inQuotationMarks :: Parser a -> Parser a
284-
inQuotationMarks expr = quotationMark *> expr <* quotationMark
317+
-- | Read escaping inside of the "'' <expr> ''"
318+
indentedEscape :: Parser (Antiquoted Text r)
319+
indentedEscape =
320+
try $
321+
do
322+
indentedQuotationMark
323+
(Plain <$> ("''" <$ char '\'' <|> "$" <$ char '$'))
324+
<|>
325+
do
326+
_ <- char '\\'
327+
c <- escapeCode
285328

286-
quotationMark :: Parser ()
287-
quotationMark = void $ char '"'
329+
pure $
330+
bool
331+
EscapedNewline
332+
(Plain $ one c)
333+
(c /= '\n')
288334

289-
doubleEscape :: Parser (Antiquoted Text r)
290-
doubleEscape = Plain . one <$> (char '\\' *> escapeCode)
335+
-- | Enclosed into indented quatation "'' <expr> ''"
336+
inIndentedQuotation :: Parser a -> Parser a
337+
inIndentedQuotation expr = indentedQuotationMark *> expr <* indentedQuotationMark
291338

292-
indented :: Parser (NString NExprLoc)
293-
indented =
294-
label "indented string" $
295-
stripIndent <$>
296-
inIndentedQuotation (many $ join stringChar indentedQuotationMark indentedEscape)
297-
where
298-
indentedEscape :: Parser (Antiquoted Text r)
299-
indentedEscape =
300-
try $
301-
do
302-
indentedQuotationMark
303-
(Plain <$> ("''" <$ char '\'' <|> "$" <$ char '$'))
304-
<|>
305-
do
306-
_ <- char '\\'
307-
c <- escapeCode
308-
309-
pure $
310-
bool
311-
EscapedNewline
312-
(Plain $ one c)
313-
(c /= '\n')
314-
315-
inIndentedQuotation :: Parser a -> Parser a
316-
inIndentedQuotation expr = indentedQuotationMark *> expr <* indentedQuotationMark
317-
318-
indentedQuotationMark :: Parser ()
319-
indentedQuotationMark = label "\"''\"" . void $ chunk "''"
320-
321-
stringChar
322-
:: Parser ()
323-
-> Parser ()
324-
-> Parser (Antiquoted Text NExprLoc)
325-
-> Parser (Antiquoted Text NExprLoc)
326-
stringChar end escStart esc =
327-
antiquoted
328-
<|> Plain . one <$> char '$'
329-
<|> esc
330-
<|> Plain . fromString <$> some plainChar
331-
where
332-
plainChar :: Parser Char
333-
plainChar =
334-
notFollowedBy (end <|> void (char '$') <|> escStart) *> anySingle
339+
-- | Symbol "''"
340+
indentedQuotationMark :: Parser ()
341+
indentedQuotationMark = label "\"''\"" . void $ chunk "''"
335342

336-
escapeCode :: Parser Char
337-
escapeCode =
338-
msum
339-
[ c <$ char e | (c, e) <- escapeCodes ]
340-
<|> anySingle
343+
344+
nixString' :: Parser (NString NExprLoc)
345+
nixString' = label "string" $ lexeme $ doubleQuoted <|> indented
341346

342347
nixString :: Parser NExprLoc
343348
nixString = annNStr <$> annotateLocation1 nixString'
@@ -587,6 +592,7 @@ nixOperators selector =
587592
one $ binaryR NImpl "->"
588593
]
589594

595+
-- 2021-11-09: NOTE: rename OperatorInfo accessors to `get*`
590596
-- 2021-08-10: NOTE:
591597
-- All this is a sidecar:
592598
-- * This type
@@ -596,11 +602,13 @@ nixOperators selector =
596602
-- * getSpecialOperation
597603
-- can reduced in favour of adding precedence field into @NOperatorDef@.
598604
-- details: https://github.com/haskell-nix/hnix/issues/982
599-
data OperatorInfo = OperatorInfo
600-
{ precedence :: Int
601-
, associativity :: NAssoc
602-
, operatorName :: Text
603-
} deriving (Eq, Ord, Generic, Typeable, Data, Show)
605+
data OperatorInfo =
606+
OperatorInfo
607+
{ precedence :: Int
608+
, associativity :: NAssoc
609+
, operatorName :: Text
610+
}
611+
deriving (Eq, Ord, Generic, Typeable, Data, Show)
604612

605613
detectPrecedence
606614
:: Ord a

0 commit comments

Comments
 (0)