Skip to content

Commit e44d57c

Browse files
committed
Misc improvements to implementation of #1470
1 parent 3626a54 commit e44d57c

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/Data/Attoparsec/Args.hs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ViewPatterns #-}
23
{- | This module implements the following:
34
45
* Parsing of command line arguments for the stack command
@@ -60,10 +61,11 @@ module Data.Attoparsec.Args
6061
) where
6162

6263
import Control.Applicative
64+
import Control.Monad.Catch (MonadThrow)
6365
import Data.Attoparsec.Text ((<?>))
6466
import qualified Data.Attoparsec.Text as P
65-
import Data.Attoparsec.Types (Parser, IResult(..))
6667
import Data.Conduit
68+
import Data.Conduit.Attoparsec
6769
import qualified Data.Conduit.Binary as CB
6870
import Data.Conduit.Text(decodeUtf8)
6971
import Data.Char (isSpace)
@@ -84,7 +86,7 @@ parseArgs mode = P.parseOnly (argsParser mode)
8486

8587
-- | A basic argument parser. It supports space-separated text, and
8688
-- string quotation with identity escaping: \x -> x.
87-
argsParser :: EscapingMode -> Parser Text [String]
89+
argsParser :: EscapingMode -> P.Parser [String]
8890
argsParser mode = many (P.skipSpace *> (quoted <|> unquoted)) <*
8991
P.skipSpace <* (P.endOfInput <?> "unterminated string")
9092
where
@@ -100,9 +102,8 @@ argsParser mode = many (P.skipSpace *> (quoted <|> unquoted)) <*
100102
-- | Parser to extract the stack command line embedded inside a comment
101103
-- after validating the placement and formatting rules for a valid
102104
-- interpreter specification.
103-
104-
interpParser :: String -> Parser Text String
105-
interpParser progName = P.option "" sheBangLine *> interpComment
105+
interpreterArgsParser :: String -> P.Parser String
106+
interpreterArgsParser progName = P.option "" sheBangLine *> interpreterComment
106107
where
107108
sheBangLine = P.string "#!"
108109
*> P.manyTill P.anyChar P.endOfLine
@@ -121,7 +122,7 @@ interpParser progName = P.option "" sheBangLine *> interpComment
121122

122123
lineComment = comment "--" P.endOfLine
123124
blockComment = comment "{-" (P.string "-}" <?> "unterminated block comment")
124-
interpComment = lineComment <|> blockComment
125+
interpreterComment = lineComment <|> blockComment
125126

126127
-- | Use 'withArgs' on result of 'getInterpreterArgs'.
127128
withInterpreterArgs :: String -> ([String] -> Bool -> IO a) -> IO a
@@ -131,7 +132,6 @@ withInterpreterArgs progName inner = do
131132

132133
-- | Extract stack arguments from a correctly placed and correctly formatted
133134
-- comment when it is being used as an interpreter
134-
135135
getInterpreterArgs :: String -> IO ([String], Bool)
136136
getInterpreterArgs progName = do
137137
args0 <- getArgs
@@ -151,19 +151,10 @@ getInterpreterArgs progName = do
151151
else return (args0, False)
152152
_ -> return (args0, False)
153153

154-
sinkInterpreterArgs :: Monad m => String -> Sink Text m (Maybe [String])
155-
sinkInterpreterArgs progName =
156-
await
157-
>>= maybe (return Nothing) parseCommand
158-
>>= maybe (return Nothing) parseArgs'
159-
where
160-
parseCommand = continueParse . (P.parse $ interpParser progName)
161-
162-
continueParse (Done _ r) = return (Just (pack r))
163-
continueParse (Fail _ _ _) = return Nothing
164-
continueParse (Partial k) = await
165-
>>= maybe (return Nothing) (continueParse . k)
166-
167-
parseArgs' txt = case P.parseOnly (argsParser Escaping) txt of
168-
Right args -> return $ Just args
169-
_ -> return Nothing
154+
sinkInterpreterArgs :: MonadThrow m => String -> Sink Text m (Maybe [String])
155+
sinkInterpreterArgs progName = do
156+
eArgs <- sinkParserEither (interpreterArgsParser progName)
157+
case eArgs of
158+
Right (P.parseOnly (argsParser Escaping) . pack -> Right args) ->
159+
return $ Just args
160+
_ -> return Nothing

0 commit comments

Comments
 (0)