Package.hs 2.92 KB
Newer Older
1
2
3
4
5
6
7
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Package
-- Copyright   :  Isaac Jones 2003-2004
-- 
-- Maintainer  :  Isaac Jones <ijones@syntaxpolice.org>
-- Stability   :  alpha
simonmar's avatar
simonmar committed
8
-- Portability :  portable
9
--
simonmar's avatar
simonmar committed
10
-- Packages.
11

ijones's avatar
ijones committed
12
{- All rights reserved.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

42
module Distribution.Package (
43
	PackageIdentifier(..),
simonmar's avatar
simonmar committed
44
	showPackageId, parsePackageId, parsePackageName,
45
  ) where
46

simonmar's avatar
simonmar committed
47
48
import Distribution.Version
import Distribution.Compat.ReadP as ReadP
49
import Data.Char ( isDigit, isAlphaNum )
simonmar's avatar
simonmar committed
50
import Data.List ( intersperse )
ijones's avatar
ijones committed
51

52
data PackageIdentifier
simonmar's avatar
simonmar committed
53
54
55
56
    = PackageIdentifier {
	pkgName    :: String,
	pkgVersion :: Version
     }
57
     deriving (Read, Show, Eq, Ord)
58

simonmar's avatar
simonmar committed
59
showPackageId :: PackageIdentifier -> String
ijones's avatar
ijones committed
60
showPackageId (PackageIdentifier n (Version [] _)) = n -- if no version, don't show version.
simonmar's avatar
simonmar committed
61
62
63
showPackageId pkgid = 
  pkgName pkgid ++ '-': showVersion (pkgVersion pkgid)

64
parsePackageName :: ReadP r String
65
parsePackageName = do ns <- sepBy1 component (char '-')
simonmar's avatar
simonmar committed
66
                      return (concat (intersperse "-" ns))
67
  where component = do 
68
69
70
	   cs <- munch1 isAlphaNum
	   if all isDigit cs then pfail else return cs
	-- each component must contain an alphabetic character, to avoid
71
	-- ambiguity in identifiers like foo-1 (the 1 is the version number).
simonmar's avatar
simonmar committed
72
73
74
75
76
77

parsePackageId :: ReadP r PackageIdentifier
parsePackageId = do 
  n <- parsePackageName
  v <- (ReadP.char '-' >> parseVersion) <++ return (Version [] [])
  return PackageIdentifier{pkgName=n,pkgVersion=v}