Skip to content

Commit e13d214

Browse files
bacchanaliamergify-bot
authored and
mergify-bot
committed
Replace parser hack with lexer character classes
1 parent 81cac84 commit e13d214

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cabal-install/src/Distribution/Client/ScriptUtils.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import Distribution.Client.Types
4242
import Distribution.FieldGrammar
4343
( parseFieldGrammar, takeFields )
4444
import Distribution.Fields
45-
( ParseResult, Field(..), SectionArg(..), parseFatalFailure, readFields )
45+
( ParseResult, parseFatalFailure, readFields )
4646
import Distribution.PackageDescription.FieldGrammar
4747
( executableFieldGrammar )
4848
import Distribution.PackageDescription.PrettyPrint
@@ -83,6 +83,7 @@ import Control.Exception
8383
( bracket )
8484
import qualified Data.ByteString.Char8 as BS
8585
import Data.ByteString.Lazy ()
86+
import qualified Data.Set as S
8687
import System.Directory
8788
( canonicalizePath, doesFileExist, getTemporaryDirectory, removeDirectoryRecursive )
8889
import 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
361361
lLocalPackages :: Lens' ProjectBaseContext [PackageSpecifier UnresolvedSourcePackage]
362362
lLocalPackages 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

Comments
 (0)