From 0f3d5966bff8a4903cbaddd88172a389d004e52f Mon Sep 17 00:00:00 2001 From: Duncan Coutts <duncan@haskell.org> Date: Fri, 21 Mar 2008 18:13:00 +0000 Subject: [PATCH] Make parsing of licenses more robust Added an UnknownLicense String constructor so that we can remember unknown license names and report them and prevent them from being uploaded to hackage, but not fail hard with a parse error upon encountering an unknown license. This does not add any new licesnes or and new valid parses of existing licenses. The parsing phase now allows an optional version like "GPL-3" however that would still be classified as an unknown license at the moment. The point is it's no longer a parse error but a semantic error instead. --- Distribution/License.hs | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/Distribution/License.hs b/Distribution/License.hs index 8966678adc..ae28501f06 100644 --- a/Distribution/License.hs +++ b/Distribution/License.hs @@ -49,9 +49,12 @@ module Distribution.License ( License(..) ) where -import Distribution.Text (Text(..)) +import Distribution.Version (Version) + +import Distribution.Text (Text(..), display) import qualified Distribution.Compat.ReadP as Parse import qualified Text.PrettyPrint as Disp +import qualified Data.Char as Char (isAlphaNum) -- |This datatype indicates the license under which your package is -- released. It is also wise to add your license to each source file @@ -67,7 +70,6 @@ data License = --TODO: * deprecate BSD4 -- * add optional gpl versions -- * add MIT license --- * fix parsing to be more permissive -- | GNU Public License. Source code must accompany alterations. GPL --(Maybe Version) @@ -92,8 +94,38 @@ data License = -- | Some other license. | OtherLicense + + -- | Not a recognised license. + -- Allows us to deal with future extensions more gracefully. + | UnknownLicense String deriving (Read, Show, Eq) +--TODO: use knownLicenses to give a better parse error message +--knownLicenses :: [License] +--knownLicenses = [GPL Nothing, LGPL Nothing, BSD3, BSD4, MIT +-- ,PublicDomain, AllRightsReserved, OtherLicense] + instance Text License where - disp = Disp.text . show - parse = Parse.readS_to_P reads +--disp (GPL version) = "GPL" <> showOptionalVersion version +--disp (LGPL version) = "LGPL" <> showOptionalVersion version + disp (UnknownLicense other) = Disp.text other + disp other = Disp.text (show other) + + parse = do + name <- Parse.munch1 Char.isAlphaNum + version <- Parse.option Nothing (Parse.char '-' >> fmap Just parse) + -- We parse an optional version but do not yet allow it on any known + -- license. However parsing the version will allow forwards compatability + -- for when we do introduce optional (L)GPL license versions. + return $ case (name, version :: Maybe Version) of + ("GPL", Nothing) -> GPL + ("LGPL", Nothing) -> LGPL + -- ("GPL", version) -> GPL version + -- ("LGPL", version) -> LGPL version + ("BSD3", Nothing) -> BSD3 + -- ("MIT", Nothing) -> MIT + ("PublicDomain", Nothing) -> PublicDomain + ("AllRightsReserved", Nothing) -> AllRightsReserved + ("OtherLicense", Nothing) -> OtherLicense + _ -> UnknownLicense $ name + ++ maybe "" (('-':) . display) version -- GitLab