@@ -42,7 +42,7 @@ import Distribution.Client.Types
4242import Distribution.FieldGrammar
4343 ( parseFieldGrammar , takeFields )
4444import Distribution.Fields
45- ( ParseResult , Field ( .. ), SectionArg ( .. ), parseFatalFailure , readFields )
45+ ( ParseResult , parseFatalFailure , readFields )
4646import Distribution.PackageDescription.FieldGrammar
4747 ( executableFieldGrammar )
4848import Distribution.PackageDescription.PrettyPrint
@@ -83,6 +83,7 @@ import Control.Exception
8383 ( bracket )
8484import qualified Data.ByteString.Char8 as BS
8585import Data.ByteString.Lazy ()
86+ import qualified Data.Set as S
8687import System.Directory
8788 ( canonicalizePath , doesFileExist , getTemporaryDirectory , removeDirectoryRecursive )
8889import System.FilePath
@@ -258,9 +259,8 @@ updateContextAndWriteProjectFile ctx scriptPath scriptExecutable = do
258259 -- Replace characters which aren't allowed in the executable component name with '_'
259260 -- Prefix with "cabal-script-" to make it clear to end users that the name may be mangled
260261 scriptExeName = " cabal-script-" ++ map censor (takeFileName scriptPath)
261- censor c = case readFields (fromString $ " executable " ++ [c]) of
262- Right [Section _ [SecArgName _ _] _] -> c
263- _ -> ' _'
262+ censor c | c `S.member` ccNamecore = c
263+ | otherwise = ' _'
264264
265265 sourcePackage = fakeProjectSourcePackage projectRoot
266266 & lSrcpkgDescription . L. condExecutables
@@ -361,3 +361,13 @@ lSrcpkgDescription f s = fmap (\x -> s { srcpkgDescription = x }) (f (srcpkgDesc
361361lLocalPackages :: Lens' ProjectBaseContext [PackageSpecifier UnresolvedSourcePackage ]
362362lLocalPackages f s = fmap (\ x -> s { localPackages = x }) (f (localPackages s))
363363{-# inline lLocalPackages #-}
364+
365+ -- Character classes
366+ -- Transcribed from "templates/Lexer.x"
367+ ccSpace , ccCtrlchar , ccPrintable , ccSymbol' , ccParen , ccNamecore :: Set Char
368+ ccSpace = S. fromList " "
369+ ccCtrlchar = S. fromList $ [chr 0x0 .. chr 0x1f ] ++ [chr 0x7f ]
370+ ccPrintable = S. fromList [chr 0x0 .. chr 0xff ] S. \\ ccCtrlchar
371+ ccSymbol' = S. fromList " ,=<>+*&|!$%^@#?/\\ ~"
372+ ccParen = S. fromList " ()[]"
373+ ccNamecore = ccPrintable S. \\ S. unions [ccSpace, S. fromList " :\" {}" , ccParen, ccSymbol']
0 commit comments