Refactor implementation of many and arguments.

Clean up recursion in the implementation of `many`, so that AltP is
inside BindP, instead of the other way around.
This commit is contained in:
Paolo Capriotti 2012-08-01 22:24:27 +01:00
parent 573162700c
commit 972fe8b4a4
2 changed files with 11 additions and 6 deletions

View File

@ -251,7 +251,7 @@ argument p (Mod _ d g) = mkParser d g (ArgReader p)
-- command line, all following arguments are included in the result, even if
-- they start with @'-'@.
arguments :: (String -> Maybe a) -> Mod ArgumentFields [a] -> Parser [a]
arguments p m = args1 <|> pure (fromMaybe [] def)
arguments p m = set_default <$> args
where
Mod _ (DefaultProp def sdef) g = m
show_def = sdef <*> def
@ -262,10 +262,13 @@ arguments p m = args1 <|> pure (fromMaybe [] def)
props = mkProps mempty g
props' = (mkProps mempty g) { propShowDefault = show_def }
args1 = ((Just <$> arg') <|> (ddash *> pure Nothing)) `BindP` \x -> case x of
Nothing -> many arg
Just a -> fmap (a:) args
args = args1 <|> pure []
args = optional arg_or_ddash `BindP` \mx -> case mx of
Nothing -> pure []
Just Nothing -> many arg
Just (Just x) -> (x:) <$> args
arg_or_ddash = (Just <$> arg') <|> (ddash *> pure Nothing)
set_default [] = fromMaybe [] def
set_default xs = xs
arg = liftOpt (Option (ArgReader p) props)
arg' = liftOpt (Option (ArgReader p') props')

View File

@ -115,7 +115,9 @@ instance Applicative Parser where
instance Alternative Parser where
empty = NilP Nothing
(<|>) = AltP
many p = some p <|> pure []
many p = optional p `BindP` \mx -> case mx of
Nothing -> pure []
Just x -> (x:) <$> many p
some p = p `BindP` (\r -> (r:) <$> many p)
-- | Result after a parse error.