Parser: support tilde in paths

This commit is contained in:
Kaushik Chakraborty 2018-04-21 11:47:57 +05:30
parent 86b09103d6
commit bbb72654d7
No known key found for this signature in database
GPG key ID: 9FD43469B156D78D
3 changed files with 13 additions and 3 deletions

View file

@ -290,7 +290,8 @@ instance (MonadFix m, MonadCatch m, MonadThrow m, MonadIO m)
_ -> throwError $ "addPath: failed: nix-store --add " ++ show path
makeAbsolutePath origPath = do
absPath <- if isAbsolute origPath then pure origPath else do
origPathExpanded <- liftIO $ expandHomePath origPath
absPath <- if isAbsolute origPathExpanded then pure origPathExpanded else do
cwd <- do
mres <- lookupVar @_ @(NThunk (Lazy m)) "__cur_file"
case mres of
@ -301,7 +302,8 @@ instance (MonadFix m, MonadCatch m, MonadThrow m, MonadIO m)
++ " __cur_file is in scope,"
++ " but is not a path; it is: "
++ show v
pure $ cwd <///> origPath
pure $ cwd <///> origPathExpanded
_ <- liftIO $ putStrLn $ show absPath
liftIO $ removeDotDotIndirections <$> canonicalizePath absPath
findEnvPath = findEnvPathM
@ -418,6 +420,10 @@ removeDotDotIndirections = intercalate "/" . go [] . splitOn "/"
go (_:s) ("..":rest) = go s rest
go s (this:rest) = go (this:s) rest
expandHomePath :: FilePath -> IO FilePath
expandHomePath ('~' : xs) = flip (++) xs <$> getHomeDirectory
expandHomePath p = return p
-- Given a path, determine the nix file to load
pathToDefaultNixFile :: FilePath -> IO FilePath
pathToDefaultNixFile p = do

View file

@ -126,7 +126,7 @@ nixList :: Parser NExprLoc
nixList = annotateLocation1 (brackets (NList <$> many nixTerm) <?> "list")
pathChar :: Char -> Bool
pathChar x = isAlpha x || isDigit x || x == '.' || x == '_' || x == '-' || x == '+'
pathChar x = isAlpha x || isDigit x || x == '.' || x == '_' || x == '-' || x == '+' || x == '~'
slash :: Parser Char
slash = try (char '/' <* notFollowedBy (satisfy (\x -> x == '/' || x == '*' || isSpace x)))

View file

@ -53,6 +53,10 @@ case_constant_path = do
assertParseFail "/"
assertParseFail "a/"
assertParseFail "a/def/"
assertParseFail "~"
assertParseFail "~/"
assertParseText "~/a" $ mkPath False "~/a"
assertParseText "~/a/b" $ mkPath False "~/a/b"
case_constant_uri = do
assertParseText "a:a" $ mkUri "a:a"