lambda-options-0.9.0.0: A modern command-line parser for Haskell.

Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.LambdaOptions.Parseable

Synopsis

Documentation

class Parseable a where Source

Class describing parseable values. Much like the Read class.

Methods

parse :: [String] -> (Maybe a, Int) Source

Given a sequence of strings, parse returns Nothing and the number of strings consumed if the parse failed. Otherwise, parse returns Just the parsed value and the number of strings consumed.

Element-wise, an entire string must be parsed in the sequence to be considered a successful parse.

Instances

Parseable Char

Parses a single character string.

Parseable Float

Parses a Float using its Read instance.

Parseable Int

Parses an Int using its ReadBounded instance.

Parseable Integer

Parses an Integer using its Read instance.

Parseable Word

Parses a Word using its ReadBounded instance.

Parseable String

Identity parser. Ex: @parse "abc" == (Just "abc", 1)

Parseable () 
Parseable a => Parseable (Maybe a)

Greedily parses a single argument or no argument. Never fails.

Parseable a => Parseable (List a)

Greedily parses arguments item-wise. Never fails. Example: parse (words "5 67 NaN") == (Just (List [5,67]), 2)

(Parseable a, Parseable b) => Parseable (a, b) 
(Parseable a, Parseable b, Parseable c) => Parseable (a, b, c) 

simpleParse :: (String -> Maybe a) -> [String] -> (Maybe a, Int) Source

Turns a parser of a single string into a parser suitable for a Parseable instance.

Useful for implementing a Parseable for a type with a Read instance by supplying readMaybe to this function.

Note: The string is not tokenized in any way before being passed into the input parser.

repeatedParse :: Parseable a => Int -> [String] -> (Maybe [a], Int) Source

Repeatedly applies parse the given number of times, accumulating the results.

Useful for implementing new parsers.

Example:

data Point = Point Float Float Float

instance Parseable Point where
    parse strs = case repeatedParse 3 strs of
        (Just [x,y,z], n) -> (Just (Point x y z), n)`
        (Nothing, n) -> (Nothing, n)