1
1
{-# LANGUAGE OverloadedStrings #-}
2
+ {-# LANGUAGE ViewPatterns #-}
2
3
{- | This module implements the following:
3
4
4
5
* Parsing of command line arguments for the stack command
@@ -60,10 +61,11 @@ module Data.Attoparsec.Args
60
61
) where
61
62
62
63
import Control.Applicative
64
+ import Control.Monad.Catch (MonadThrow )
63
65
import Data.Attoparsec.Text ((<?>) )
64
66
import qualified Data.Attoparsec.Text as P
65
- import Data.Attoparsec.Types (Parser , IResult (.. ))
66
67
import Data.Conduit
68
+ import Data.Conduit.Attoparsec
67
69
import qualified Data.Conduit.Binary as CB
68
70
import Data.Conduit.Text (decodeUtf8 )
69
71
import Data.Char (isSpace )
@@ -84,7 +86,7 @@ parseArgs mode = P.parseOnly (argsParser mode)
84
86
85
87
-- | A basic argument parser. It supports space-separated text, and
86
88
-- string quotation with identity escaping: \x -> x.
87
- argsParser :: EscapingMode -> Parser Text [String ]
89
+ argsParser :: EscapingMode -> P. Parser [String ]
88
90
argsParser mode = many (P. skipSpace *> (quoted <|> unquoted)) <*
89
91
P. skipSpace <* (P. endOfInput <?> " unterminated string" )
90
92
where
@@ -100,9 +102,8 @@ argsParser mode = many (P.skipSpace *> (quoted <|> unquoted)) <*
100
102
-- | Parser to extract the stack command line embedded inside a comment
101
103
-- after validating the placement and formatting rules for a valid
102
104
-- 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
106
107
where
107
108
sheBangLine = P. string " #!"
108
109
*> P. manyTill P. anyChar P. endOfLine
@@ -121,7 +122,7 @@ interpParser progName = P.option "" sheBangLine *> interpComment
121
122
122
123
lineComment = comment " --" P. endOfLine
123
124
blockComment = comment " {-" (P. string " -}" <?> " unterminated block comment" )
124
- interpComment = lineComment <|> blockComment
125
+ interpreterComment = lineComment <|> blockComment
125
126
126
127
-- | Use 'withArgs' on result of 'getInterpreterArgs'.
127
128
withInterpreterArgs :: String -> ([String ] -> Bool -> IO a ) -> IO a
@@ -131,7 +132,6 @@ withInterpreterArgs progName inner = do
131
132
132
133
-- | Extract stack arguments from a correctly placed and correctly formatted
133
134
-- comment when it is being used as an interpreter
134
-
135
135
getInterpreterArgs :: String -> IO ([String ], Bool )
136
136
getInterpreterArgs progName = do
137
137
args0 <- getArgs
@@ -151,19 +151,10 @@ getInterpreterArgs progName = do
151
151
else return (args0, False )
152
152
_ -> return (args0, False )
153
153
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