|
25 | 25 |
|
26 | 26 |
|
27 | 27 | import Prelude hiding ( traceM ) |
28 | | -import Relude.Unsafe as Unsafe |
29 | 28 | import Nix.Utils |
30 | 29 | import Control.Comonad ( Comonad ) |
31 | 30 | import Control.Monad ( foldM ) |
@@ -83,7 +82,11 @@ import System.Posix.Files ( isRegularFile |
83 | 82 | , isDirectory |
84 | 83 | , isSymbolicLink |
85 | 84 | ) |
86 | | -import Text.Regex.TDFA |
| 85 | +import Text.Regex.TDFA ( Regex |
| 86 | + , makeRegex |
| 87 | + , matchOnceText |
| 88 | + , matchAllText |
| 89 | + ) |
87 | 90 |
|
88 | 91 |
|
89 | 92 | -- This is a big module. There is recursive reuse: |
@@ -156,10 +159,10 @@ instance Comonad f => Ord (WValue t f m) where |
156 | 159 |
|
157 | 160 | -- ** Helpers |
158 | 161 |
|
159 | | -nVNull |
| 162 | +nvNull |
160 | 163 | :: MonadNix e t f m |
161 | 164 | => NValue t f m |
162 | | -nVNull = nvConstant NNull |
| 165 | +nvNull = nvConstant NNull |
163 | 166 |
|
164 | 167 | mkNVBool |
165 | 168 | :: MonadNix e t f m |
@@ -221,8 +224,8 @@ attrsetGet k s = |
221 | 224 |
|
222 | 225 | data VersionComponent |
223 | 226 | = VersionComponentPre -- ^ The string "pre" |
224 | | - | VersionComponentString Text -- ^ A string other than "pre" |
225 | | - | VersionComponentNumber Integer -- ^ A number |
| 227 | + | VersionComponentString !Text -- ^ A string other than "pre" |
| 228 | + | VersionComponentNumber !Integer -- ^ A number |
226 | 229 | deriving (Show, Read, Eq, Ord) |
227 | 230 |
|
228 | 231 | versionComponentToString :: VersionComponent -> Text |
@@ -315,20 +318,13 @@ splitMatches numDropped (((_, (start, len)) : captures) : mts) haystack = |
315 | 318 | caps = nvList (f <$> captures) |
316 | 319 | f (a, (s, _)) = |
317 | 320 | bool |
318 | | - nVNull |
| 321 | + nvNull |
319 | 322 | (thunkStr a) |
320 | 323 | (s >= 0) |
321 | 324 |
|
322 | 325 | thunkStr :: Applicative f => ByteString -> NValue t f m |
323 | 326 | thunkStr s = nvStrWithoutContext $ decodeUtf8 s |
324 | 327 |
|
325 | | -elemAt :: [a] -> Int -> Maybe a |
326 | | -elemAt ls i = |
327 | | - list |
328 | | - Nothing |
329 | | - (pure . Unsafe.head) |
330 | | - (drop i ls) |
331 | | - |
332 | 328 | hasKind |
333 | 329 | :: forall a e t f m |
334 | 330 | . (MonadNix e t f m, FromValue a m (NValue t f m)) |
@@ -481,7 +477,7 @@ unsafeGetAttrPosNix nvX nvY = |
481 | 477 | case (x, y) of |
482 | 478 | (NVStr ns, NVSet _ apos) -> |
483 | 479 | maybe |
484 | | - (pure nVNull) |
| 480 | + (pure nvNull) |
485 | 481 | toValue |
486 | 482 | (M.lookup (stringIgnoreContext ns) apos) |
487 | 483 | _xy -> throwError $ ErrorCall $ "Invalid types for builtins.unsafeGetAttrPosNix: " <> show _xy |
@@ -583,19 +579,19 @@ foldl'Nix f z xs = foldM go z =<< fromValue @[NValue t f m] xs |
583 | 579 | where |
584 | 580 | go b a = (`callFunc` a) =<< callFunc f b |
585 | 581 |
|
586 | | -headNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m) |
| 582 | +headNix :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m) |
587 | 583 | headNix = |
588 | | - list |
| 584 | + maybe |
589 | 585 | (throwError $ ErrorCall "builtins.head: empty list") |
590 | | - (pure . Unsafe.head) |
591 | | - <=< fromValue |
| 586 | + (pure) |
| 587 | + . viaNonEmpty head <=< fromValue @[NValue t f m] |
592 | 588 |
|
593 | | -tailNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m) |
| 589 | +tailNix :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m) |
594 | 590 | tailNix = |
595 | | - list |
| 591 | + maybe |
596 | 592 | (throwError $ ErrorCall "builtins.tail: empty list") |
597 | | - (pure . nvList . Unsafe.tail) |
598 | | - <=< fromValue |
| 593 | + (pure . nvList) |
| 594 | + . viaNonEmpty tail <=< fromValue @[NValue t f m] |
599 | 595 |
|
600 | 596 | splitVersionNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m) |
601 | 597 | splitVersionNix v = |
@@ -674,24 +670,19 @@ matchNix pat str = |
674 | 670 | (toValue $ makeNixStringWithoutContext t) |
675 | 671 | (not $ Text.null t) |
676 | 672 |
|
677 | | - maybe |
678 | | - (pure nVNull) |
679 | | - (\case |
680 | | - ("", sarr, "") -> |
681 | | - do |
682 | | - let s = fst <$> elems sarr |
683 | | - nvList <$> |
684 | | - traverse |
685 | | - mkMatch |
686 | | - (bool |
687 | | - id -- (length <= 1) allowed & passes-through here the full string |
688 | | - Unsafe.tail |
689 | | - (length s > 1) |
690 | | - s |
691 | | - ) |
692 | | - _ -> (pure nVNull) |
693 | | - ) |
694 | | - (matchOnceText re s) |
| 673 | + case matchOnceText re s of |
| 674 | + Just ("", sarr, "") -> |
| 675 | + do |
| 676 | + let submatches = fst <$> elems sarr |
| 677 | + nvList <$> |
| 678 | + traverse |
| 679 | + mkMatch |
| 680 | + (case submatches of |
| 681 | + [] -> [] |
| 682 | + [a] -> [a] |
| 683 | + _:xs -> xs -- return only the matched groups, drop the full string |
| 684 | + ) |
| 685 | + _ -> pure nvNull |
695 | 686 |
|
696 | 687 | splitNix |
697 | 688 | :: forall e t f m |
@@ -929,7 +920,7 @@ elemAtNix xs n = |
929 | 920 | maybe |
930 | 921 | (throwError $ ErrorCall $ "builtins.elem: Index " <> show n' <> " too large for list of length " <> show (length xs')) |
931 | 922 | pure |
932 | | - (elemAt xs' n') |
| 923 | + (xs' !!? n') |
933 | 924 |
|
934 | 925 | genListNix |
935 | 926 | :: forall e t f m |
@@ -1022,7 +1013,7 @@ replaceStringsNix tfrom tto ts = |
1022 | 1013 | maybePrefixMatch |
1023 | 1014 |
|
1024 | 1015 | where |
1025 | | - -- When prefix matched something - returns (match, replacement, reminder) |
| 1016 | + -- When prefix matched something - returns (match, replacement, remainder) |
1026 | 1017 | maybePrefixMatch :: Maybe (Text, NixString, Text) |
1027 | 1018 | maybePrefixMatch = formMatchReplaceTailInfo <$> find ((`Text.isPrefixOf` input) . fst) fromKeysToValsMap |
1028 | 1019 | where |
@@ -1526,7 +1517,7 @@ fromJSONNix nvjson = |
1526 | 1517 | NInt |
1527 | 1518 | (floatingOrInteger n) |
1528 | 1519 | A.Bool b -> pure $ mkNVBool b |
1529 | | - A.Null -> pure nVNull |
| 1520 | + A.Null -> pure nvNull |
1530 | 1521 |
|
1531 | 1522 | toJSONNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m) |
1532 | 1523 | toJSONNix = (fmap nvStr . nvalueToJSONNixString) <=< demand |
@@ -1844,7 +1835,7 @@ builtinsList = sequence |
1844 | 1835 | , add2 Normal "match" matchNix |
1845 | 1836 | , add2 Normal "mul" mulNix |
1846 | 1837 | , add0 Normal "nixPath" nixPathNix |
1847 | | - , add0 Normal "null" (pure nVNull) |
| 1838 | + , add0 Normal "null" (pure nvNull) |
1848 | 1839 | , add Normal "parseDrvName" parseDrvNameNix |
1849 | 1840 | , add2 Normal "partition" partitionNix |
1850 | 1841 | --, add Normal "path" path |
|
0 commit comments