@@ -1,64 +0,0 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE TemplateHaskell #-}
3−
4−module Language.Nix.Identifier
5− (
6− -- | Identifiers in Nix are essentially strings. They can be constructed
7− -- (and viewed) with the 'ident' isomorphism. For the sake of convenience,
8− -- @Identifier@s are an instance of the 'IsString' class.
9− --
10− -- Reasonable people restrict themselves to identifiers of the form
11− -- @[a-zA-Z\_][a-zA-Z0-9\_\'\-]*@, because these don't need quoting. The
12− -- methods of the 'Pretty' class can be used to pretty-print an identifier
13− -- with proper quoting:
14− --
15− -- >>> pPrint (ident # "test")
16− -- test
17− -- >>> pPrint (ident # "foo.bar")
18− -- "foo.bar"
19− Identifier
20−
21− , -- | An isomorphism that allows conversion of 'Identifier' from/to the
22− -- standard 'String' type via 'review'.
23− --
24− -- prop> \str -> fromString str == ident # str
25− -- prop> \str -> set ident str undefined == ident # str
26− -- prop> \str -> view ident (review ident str) == str
27− ident
28−
29− , -- | Helper function to quote a given identifier string if necessary.
30− --
31− -- >>> putStrLn (quote "abc")
32− -- abc
33− -- >>> putStrLn (quote "abc.def")
34− -- "abc.def"
35− quote
36−
37− , -- | Checks whether a given string needs quoting when interpreted as an
38− -- 'Identifier'.
39− needsQuoting
40−
41− ) where
42−
43−import Control.DeepSeq
44−import Control.Lens
45−import Data.String
46−import Text.PrettyPrint.HughesPJClass
47−import Text.Regex.Posix
48−
49−declareLenses [d| newtype Identifier = Identifier { ident :: String }
50− deriving (Show, Eq, Ord, IsString)
51− |]
52−
53−instance Pretty Identifier where
54− pPrint i = text (i ^. ident . to quote)
55−
56−instance NFData Identifier where rnf (Identifier str) = rnf str
57−
58−needsQuoting :: String -> Bool
59−needsQuoting str = not (str =~ grammar)
60− where grammar :: String -- TODO: should be a compiled regular expression
61− grammar = "^[a-zA-Z\\_][a-zA-Z0-9\\_\\'\\-]*$"
62−
63−quote :: String -> String
64−quote s = if needsQuoting s then show s else s