-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Combinators to build command line parsers -- -- multiarg is a parser combinator library to build command line parsers. -- With it you can easily create parsers with options that take more than -- one option argument--for example, I created multiarg due to the -- apparent lack of such ability amongst other parsers. Its basic design -- is loosely inspired by Parsec. -- -- Provides ParserT, a monad you use to build parsers. ParserT is a monad -- transformer, so you can layer it on top of other monads. For instance -- you could layer it on the IO monad so that your parser can perform IO. -- -- It also has a simple, pre-built parser built with the underlying -- combinators, which works for many situtations and shields you from the -- underlying complexity if you don't need it. -- -- See the documentation in the System.Console.MultiArg module for -- details. @package multiarg @version 0.1.0.0 -- | Get the arguments from the command line, ensuring they are properly -- encoded into Unicode. -- -- base 4.3.1.0 has a System.Environment.getArgs that does not return a -- Unicode string. Instead, it simply puts each octet into a different -- Char. Thus its getArgs is broken on UTF-8 and nearly any non-ASCII -- encoding. As a workaround I use System.Environment.UTF8. The downside -- of this is that it requires that the command line be encoded in UTF8, -- regardless of what the default system encoding is. -- -- Unlike base 4.3.1.0, base 4.4.0.0 actually returns a proper Unicode -- string when you call System.Environment.getArgs. (base 4.3.1.0 comes -- with ghc 7.0.4; base 4.4.0.0 comes with ghc 7.2.) The string is -- encoded depending on the default system locale. The only problem is -- that System.Environment.UTF8 apparently simply uses -- System.Environment.getArgs and then assumes that the string it returns -- has not been decoded. In other words, System.Environment.UTF8 assumes -- that System.Environment.getArgs is broken, and when -- System.Environment.getArgs was fixed in base 4.4.0.0, it likely will -- break System.Environment.UTF8. -- -- One obvious solution to this problem is to find some other way to get -- the command line that will not break when base is updated. But it was -- not easy to find such a thing. The other libraries I saw on hackage -- (as of January 6, 2012) had problems, such as breakage on ghc 7.2. -- There is a package that has a simple interface to the UNIX -- setlocale(3) function, but I'm not sure that what it returns easily -- and reliably maps to character encodings that you can use with, say, -- iconv. -- -- So by use of Cabal and preprocessor macors, the code uses utf8-string -- if base is less than 4.4, and uses System.Environment.getArgs if base -- is at least 4.4. -- -- The GHC bug is here: -- -- http://hackage.haskell.org/trac/ghc/ticket/3309 module System.Console.MultiArg.GetArgs -- | Gets the command-line arguments supplied by the program's user. If the -- base package is older than version 4.4, then this function -- assumes the command line is encoded in UTF-8, which is true for many -- newer Unix systems; however, many older systems may use single-byte -- encodings like ISO-8859. In such cases, this function will give -- erroneous results. -- -- If the base package is version 4.4.0 or newer, this function -- simply uses the getArgs that comes with base. That getArgs -- detects the system's default encoding and uses that, so it should give -- accurate results on most systems. getArgs :: IO [String] -- | Gets the name of the program that the user invoked. See documentation -- for getArgs for important caveats that also apply to this -- function. getProgName :: IO String module System.Console.MultiArg.QuickCheckHelpers randText :: Gen Text randSet :: (Ord a, Arbitrary a) => Gen (Set a) newtype WText WText :: Text -> WText unWText :: WText -> Text instance Show WText instance CoArbitrary WText instance Arbitrary WText module System.Console.MultiArg.TextNonEmpty data TextNonEmpty TextNonEmpty :: Char -> Text -> TextNonEmpty instance Show TextNonEmpty instance Eq TextNonEmpty instance CoArbitrary TextNonEmpty instance Arbitrary TextNonEmpty -- | These types represent options. They are abstract and in a separate -- module to prevent you from accidentally making an option with an -- invalid name. Option names cannot have a dash as their first or second -- character, and long option names cannot have an equals sign anywhere -- in the name. module System.Console.MultiArg.Option -- | Short options. Options that are preceded with a single dash on the -- command line and consist of a single letter. That single letter cannot -- be a dash. Any other Unicode character is good (including pathological -- ones like newlines). data ShortOpt unShortOpt :: ShortOpt -> Char -- | This function is partial. It calls error if its argument is a single -- dash. This is the only way to make a short option so it prevents you -- from making one that is invalid. makeShortOpt :: Char -> ShortOpt -- | Long options. Options that are preceded with two dashes on the command -- line and typically consist of an entire mnemonic word, such as -- lines. However, anything that is at least one letter long is -- fine for a long option name. The name must not have a dash as either -- the first or second character and it must be at least one character -- long. It cannot have an equal sign anywhere in its name. Otherwise any -- Unicode character is good (including pathological ones like newlines). data LongOpt unLongOpt :: LongOpt -> Text -- | This function is partial. It calls error if its argument contains text -- that is not a valid long option. This is the only way to make a long -- option so it prevents you from making invalid ones. makeLongOpt :: Text -> LongOpt instance Show ShortOpt instance Eq ShortOpt instance Ord ShortOpt instance Show LongOpt instance Eq LongOpt instance Ord LongOpt instance Arbitrary LongOpt instance Arbitrary ShortOpt -- | Errors. Parsing a command line when a user has entered it correctly is -- easy; doing something sensible when an incorrect line has been entered -- is a bit more difficult. This module exports an Error -- typeclass, which you can declare instances of in order to have your -- own type to represent errors. Or you can use SimpleError, which -- is already an instance of Error. module System.Console.MultiArg.Error -- | Instances of this typeclass represent multiarg errors. You can declare -- instances of this typeclass so that you can use your own type for -- errors. This makes multiarg easy to integrate into your own programs. -- Then you can also easily add other errors, which you can report from -- the parsers you build by calling -- System.Console.MultiArg.Prim.throw. class Error e parseErr :: Error e => Expecting -> Saw -> e -- | A simple type that is already an instance of Error. data SimpleError SimpleError :: Expecting -> Saw -> SimpleError -- | Generates error messages. printError :: SimpleError -> Text -- | Each error consists of two parts: what the parser was expecting to -- see, and what it actually saw. This type holds what the parser -- expected to see. If you just want to give some text to be used in an -- error message, use ExpTextError. To generate a generic error, -- use ExpOtherFailure. data Expecting ExpPendingShortOpt :: ShortOpt -> Expecting ExpExactLong :: LongOpt -> Expecting ExpApproxLong :: (Set LongOpt) -> Expecting ExpLongOptArg :: Expecting ExpPendingShortArg :: Expecting ExpStopper :: Expecting ExpNextArg :: Expecting ExpNonOptionPosArg :: Expecting ExpEnd :: Expecting ExpNonGNUExactLong :: LongOpt -> Expecting ExpMatchingApproxLong :: LongOpt -> (Set LongOpt) -> Expecting ExpNonGNUMatchingApproxLong :: LongOpt -> (Set LongOpt) -> Expecting ExpApproxWord :: (Set Text) -> Expecting ExpOptionOrPosArg :: Expecting ExpTextError :: Text -> Expecting ExpNonPendingShortOpt :: ShortOpt -> Expecting ExpNotFollowedBy :: Expecting ExpOtherFailure :: Expecting -- | Generates an error message from an Expecting. printExpecting :: Expecting -> Text -- | What the parser actually saw. To give some text to be used in the -- error message, use SawTextError. To generate a generic error, -- use SawOtherFailure. data Saw SawNoPendingShorts :: Saw SawWrongPendingShort :: Char -> Saw SawNoArgsLeft :: Saw SawEmptyArg :: Saw SawSingleDashArg :: Saw SawStillPendingShorts :: TextNonEmpty -> Saw SawNotShortArg :: Text -> Saw SawWrongShortArg :: Char -> Saw SawNotLongArg :: Text -> Saw SawWrongLongArg :: Text -> Saw SawNoMatches :: Text -> Saw SawMultipleMatches :: (Set LongOpt) -> Text -> Saw SawNoPendingShortArg :: Saw SawAlreadyStopper :: Saw SawNewStopper :: Saw SawNotStopper :: Saw SawLeadingDashArg :: Text -> Saw SawMoreInput :: Saw SawGNULongOptArg :: Text -> Saw SawNotMatchingApproxLong :: Text -> LongOpt -> Saw SawMatchingApproxLongWithArg :: Text -> Saw SawMultipleApproxMatches :: (Set Text) -> Text -> Saw SawNoOption :: Saw SawNoOptionOrPosArg :: Saw SawTextError :: Text -> Saw SawFollowedBy :: Saw SawOtherFailure :: Saw -- | Generates error messages from a Saw. printSaw :: Saw -> Text instance Show Expecting instance Eq Expecting instance Show Saw instance Eq Saw instance Show SimpleError instance Eq SimpleError instance Arbitrary Saw instance Arbitrary Expecting instance Arbitrary SimpleError instance Error SimpleError -- | Parser primitives. These are the only functions that have access to -- the internals of the parser. module System.Console.MultiArg.Prim -- | Parser a is a parser with user state (), error type -- SimpleError, underlying monad Identity, and result type a. type Parser a = ParserT () SimpleError Identity a -- | ParserE e a is a parser with user state (), error type e, -- underlying monad Identity, and result type a. type ParserE e a = ParserT () e Identity a -- | ParserSE s e a is a parser with user state s, error type e, -- underlying monad Identity, and result type a. type ParserSE s e a = ParserT s e Identity a -- | ParserT s e m a is a parser with user state s, error type e, -- underlying monad m, and result type a. Internally the parser is a -- state monad which keeps track of what is remaining to be parsed. Since -- the parser has an internal state anyway, the user can add to this -- state (this is called the user state.) The parser ignores this user -- state so you can use it however you wish. If you do not need a user -- state, just make it the unit type (). -- -- The parser also includes the notion of failure. Any parser can fail; a -- failed parser affects the behavior of combinators such as combine. The -- failure type should be a instance of -- System.Console.MultiArg.Error.Error. This allows you to define your -- own type and use it for the failure type, which can be useful when -- combining MultiArg with your own program. -- -- The underlying monad is m. This makes ParserT into a monad -- transformer; you can layer it on top of other monads. For instance you -- might layer it on top of the IO monad so that your parser can perform -- IO (for example, by examining the disk to see if arguments that -- specify files are valid.) If you don't need a monad transformer, just -- layer ParserT on top of Identity. data ParserT s e m a -- | The simplest parser runner; has no user state, an underlying monad -- Identity, and error type SimpleError. parse :: [Text] -> Parser a -> Exceptional SimpleError a -- | Runs a parser that has no user state and an underlying monad of -- Identity and is parameterized on the error type. parseE :: [Text] -> ParserE e a -> Exceptional e a -- | Runs a parser that has a user state and an underlying monad Identity. parseSE :: s -> [Text] -> ParserSE s e a -> (Exceptional e a, s) -- | The most complex parser runner. Runs a parser with a user-defined -- state, error type, and underlying monad. Returns the final parse -- result and the final user state, inside of the underlying monad. parseT :: Monad m => s -> [Text] -> ParserT s e m a -> m (Exceptional e a, s) -- | parserMap f p applies function f to the result of parser p. -- First parser p is run. If it succeeds, function f is applied to the -- result and another parser is returned with the result. If it fails, f -- is not applied and a failed parser is returned. This provides the -- implementation for Prelude.Functor.fmap. parserMap :: Monad m => (a -> b) -> ParserT s e m a -> ParserT s e m b -- | good a always succeeds without consuming any input and has -- result a. This provides the implementation for -- Control.Monad.Monad.return and -- Control.Applicative.Applicative.pure. good :: Monad m => a -> ParserT s e m a -- | apply l r applies the function found in parser l to the result of -- parser r. First the l parser is run. If it succeeds, it has a -- resulting function. Then the r parser is run. If it succeeds, the -- function from the l parser is applied to the result of the r parser, -- and a new parser is returned with the result. If either parser l or -- parser r fails, then a failed parser is returned. This provides the -- implementation for <*> in Applicative. apply :: Monad m => ParserT s e m (a -> b) -> ParserT s e m a -> ParserT s e m b -- | Runs the first parser. If it fails without consuming any input, then -- runs the second parser. If the first parser succeeds, then returns the -- result of the first parser. If the first parser fails and consumes -- input, then returns the result of the first parser. This provides the -- implementation for <|> in Alternative. choice :: Monad m => ParserT s e m a -> ParserT s e m a -> ParserT s e m a -- | Combines two parsers into a single parser. The second parser can -- optionally depend upon the result from the first parser. -- -- This applies the first parser. If the first parser succeeds, combine -- then takes the result from the first parser, applies the function -- given to the result from the first parser, and then applies the -- resulting parser. -- -- If the first parser fails, combine will not apply the second function -- but instead will bypass the second parser. -- -- This provides the implementation for >>= in -- Control.Monad.Monad. combine :: Monad m => ParserT s e m a -> (a -> ParserT s e m b) -> ParserT s e m b -- | lookAhead p runs parser p. If p succeeds, lookAhead p -- succeeds without consuming any input. If p fails without consuming any -- input, so does lookAhead. If p fails and consumes input, lookAhead -- also fails and consumes input. If this is undesirable, combine with -- try. lookAhead :: Monad m => ParserT s e m a -> ParserT s e m a -- | several p runs parser p zero or more times and returns all the -- results. This proceeds like this: parser p is run and, if it succeeds, -- the result is saved and parser p is run again. Repeat. Eventually this -- will have to fail. If the last run of parser p fails without consuming -- any input, then several p runs successfully. The state of the parser -- is updated to reflect the successful runs of p. If the last run of -- parser p fails but it consumed input, then several p fails. The state -- of the parser is updated to reflect the state up to and including the -- run that partially consumed input. The parser is left in a failed -- state. -- -- This semantic can come in handy. For example you might run a parser -- multiple times that parses an option and arguments to the option. If -- the arguments fail to parse, then several will fail. several :: Monad m => ParserT s e m a -> ParserT s e m [a] -- | manyTill p e runs parser p repeatedly until parser e succeeds. -- -- More precisely, first it runs parser e. If parser e succeeds, then -- manyTill returns the result of all the preceding successful parses of -- p. If parser e fails (it does not matter whether e consumed any input -- or not), manyTill runs parser p again. What happens next depends on -- whether p succeeded or failed. If p succeeded, then the loop starts -- over by running parser e again. If p failed (it does not matter -- whether it consumed any input or not), then manyTill fails. The state -- of the parser is updated to reflect its state after the failed run of -- p, and the parser is left in a failed state. -- -- Should parser e succeed (as it will on a successful application of -- manyTill), then the parser state will reflect that parser e -- succeeded--that is, if parser e consumes input, that input will be -- consumed in the parser that is returned. Wrap e inside of -- lookAhead if that is undesirable. -- -- Be particularly careful to get the order of the arguments correct. -- Applying this function to reversed arguments will yield bugs that are -- very difficult to diagnose. manyTill :: Monad m => ParserT s e m a -> ParserT s e m end -> ParserT s e m [a] -- | Lifts a computation of the underlying monad into the ParserT monad. -- This provides the implementation for lift. parserLift :: Monad m => m a -> ParserT s e m a -- | Lifts a computation from the IO monad into the ParserT monad. This -- provides the implementation for liftIO. parserIO :: (MonadIO m, Error e) => IO a -> ParserT s e m a -- | throw e always fails without consuming any input and returns a failed -- parser with error state e. throw :: Monad m => e -> ParserT s e m a -- | throwString s always fails without consuming any input. The -- failure contains a record of the string passed in by s. This provides -- the implementation for Control.Monad.Monad.fail. throwString :: (Error e, Monad m) => String -> ParserT s e m a -- | Fail with an unhelpful error message. Usually throw is more useful, -- but this is handy to implement some typeclass instances. genericThrow :: (Monad m, Error e) => ParserT s e m a -- | Runs the parser given. If it succeeds, then returns the result of the -- parser. If it fails and consumes input, returns the result of the -- parser. If it fails without consuming any input, then changes the -- error using the function given. (>) :: Monad m => ParserT s e m a -> e -> ParserT s e m a -- | try p behaves just like p, but if p fails, try p will not consume any -- input. try :: Monad m => ParserT s e m a -> ParserT s e m a -- | Parses only pending short options. Fails without consuming any input -- if there has already been a stopper or if there are no pending short -- options. Fails without consuming any input if there is a pending short -- option, but it does not match the short option given. Succeeds and -- consumes a pending short option if it matches the short option given; -- returns the short option parsed. pendingShortOpt :: (Monad m, Error e) => ShortOpt -> ParserT s e m ShortOpt -- | Parses only non-pending short options. Fails without consuming any -- input if, in order: -- --