Skip to content

Commit 8edebe3

Browse files
Merge pull request #679 from NixOS/language-nix-non-ascii
Language.Nix.Identifier: idents with non ASCII chars need quoting
2 parents 26e5dba + 3f03407 commit 8edebe3

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

language-nix/src/Language/Nix/Identifier.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ instance HasParser Identifier where
9090
-- identifiers by Nix, see 'nixKeywords'.
9191
parseSimpleIdentifier :: CharParser st tok m Identifier
9292
parseSimpleIdentifier = do
93-
c <- satisfy (\x -> x == '_' || isAlpha x)
94-
cs <- many (satisfy (\x -> x `elem` "_'-" || isAlphaNum x))
93+
c <- satisfy (\x -> x == '_' || (isAscii x && isAlpha x))
94+
cs <- many (satisfy (\x -> x `elem` "_'-" || (isAscii x && isAlphaNum x)))
9595
return (Identifier (c:cs))
9696

9797
-- | 'ReadP' parser for quoted identifiers, i.e. those that /do/ need

language-nix/test/hspec.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Main (main) where
22

33
import Control.Lens
44
import Control.Monad (forM_)
5+
import Data.Char (isAscii, isSpace)
56
import Data.String (fromString)
67
import Language.Nix.Identifier
78
import Test.Hspec
@@ -31,10 +32,17 @@ main = hspec $ do
3132
shouldSatisfy str needsQuoting
3233
prettyShow (ident # str) `shouldBe` "\"" ++ str ++ "\""
3334

34-
stringIdentProperty :: (String -> Bool) -> Property
35+
describe "needsQuoting" $ do
36+
it "if string contains non ASCII characters" $ stringIdentProperty $ \s ->
37+
any (not . isAscii) s ==> needsQuoting s
38+
it "if string contains spaces" $ stringIdentProperty $ \s ->
39+
any isSpace s ==> needsQuoting s
40+
it "if length is zero" $ shouldSatisfy "" needsQuoting
41+
42+
stringIdentProperty :: Testable prop => (String -> prop) -> Property
3543
stringIdentProperty p = property $ \s ->
3644
'\0' `notElem` s ==> classify (needsQuoting s) "need quoting" $ p s
3745

38-
identProperty :: (Identifier -> Bool) -> Property
46+
identProperty :: Testable prop => (Identifier -> prop) -> Property
3947
identProperty p = property $ \i ->
4048
classify (needsQuoting (from ident # i)) "need quoting" $ p i

0 commit comments

Comments
 (0)