choice ps tries to apply the parsers in the list ps in order,
until one of them succeeds. Returns the value of the succeeding
parser.
Moduleparsers-0.12.12Haskell2010
Text.Parser.Combinators
Alternative parser combinators
- 1 class
- 21 values
- Packageparsers-0.12.12
- Exports24
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceCombinators.hs
Parsing Combinators
23 declarationsoption x p tries to apply parser p. If p fails without
consuming input, it returns the value x, otherwise the value
returned by p.
priority = option 0 (digitToInt <$> digit)One or none.
It is useful for modelling any computation that is allowed to fail.
Examples
Using the Alternative instance of Control.Monad.Except, the following functions:
import Control.Monad.ExceptcanFail = throwError "it failed" :: Except String Intfinal = return 42 :: Except String Int
Can be combined by allowing the first function to fail:
runExcept $ canFail *> finalLeft "it failed"
runExcept $ optional canFail *> finalRight 42
skipOptional p tries to apply parser p. It will parse p or nothing.
It only fails if p fails after consuming input. It discards the result
of p. (Plays the role of parsec's optional, which conflicts with Applicative's optional)
between open close p parses open, followed by p and close.
Returns the value returned by p.
braces = between (symbol "{") (symbol "}")p `surroundedBy` f is p surrounded by f. Shortcut for between f f p.
As in between, returns the value returned by p.
One or more.
Examples
some (putStr "la")lalalalalalalalala... * goes on forever *
some Nothingnothing
take 5 <$> some (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case some parser will attempt to
parse parser one or more times until it fails.
Zero or more.
Examples
many (putStr "la")lalalalalalalalala... * goes on forever *
many NothingJust []
take 5 <$> many (Just 1)* hangs forever *
Note that this function can be used with Parsers based on
Applicatives. In that case many parser will attempt to
parse parser zero or more times until it fails.
sepBy p sep parses zero or more occurrences of p, separated
by sep. Returns a list of values returned by p.
commaSep p = p `sepBy` (symbol ",")sepBy1 p sep parses one or more occurrences of p, separated
by sep. Returns a list of values returned by p.
sepByNonEmpty p sep parses one or more occurrences of p, separated
by sep. Returns a non-empty list of values returned by p.
sepEndBy1 p sep parses one or more occurrences of p,
separated and optionally ended by sep. Returns a list of values
returned by p.
sepEndByNonEmpty p sep parses one or more occurrences of p,
separated and optionally ended by sep. Returns a non-empty list of values
returned by p.
sepEndBy p sep parses zero or more occurrences of p,
separated and optionally ended by sep, ie. haskell style
statements. Returns a list of values returned by p.
haskellStatements = haskellStatement `sepEndBy` semiendBy1 p sep parses one or more occurrences of p, separated
and ended by sep. Returns a list of values returned by p.
endByNonEmpty p sep parses one or more occurrences of p, separated
and ended by sep. Returns a non-empty list of values returned by p.
endBy p sep parses zero or more occurrences of p, separated
and ended by sep. Returns a list of values returned by p.
cStatements = cStatement `endBy` semicount n p parses n occurrences of p. If n is smaller or
equal to zero, the parser equals to return []. Returns a list of
n values returned by p.
chainl p op x parses zero or more occurrences of p,
separated by op. Returns a value obtained by a left associative
application of all functions returned by op to the values returned
by p. If there are zero occurrences of p, the value x is
returned.
chainr p op x parses zero or more occurrences of p,
separated by op Returns a value obtained by a right associative
application of all functions returned by op to the values returned
by p. If there are no occurrences of p, the value x is
returned.
chainl1 p op x parses one or more occurrences of p,
separated by op Returns a value obtained by a left associative
application of all functions returned by op to the values returned
by p. . This parser can for example be used to eliminate left
recursion which typically occurs in expression grammars.
expr = term `chainl1` addop
term = factor `chainl1` mulop
factor = parens expr <|> integer
mulop = (*) <$ symbol "*"
<|> div <$ symbol "/"
addop = (+) <$ symbol "+"
<|> (-) <$ symbol "-"chainr1 p op x parses one or more occurrences of p,
separated by op Returns a value obtained by a right associative
application of all functions returned by op to the values returned
by p.
manyTill p end applies parser p zero or more times until
parser end succeeds. Returns the list of values returned by p.
This parser can be used to scan comments:
simpleComment = do{ string "<!--"
; manyTill anyChar (try (string "-->"))
}Note the overlapping parsers anyChar and string "-->", and
therefore the use of the try combinator.
Parsing Class
1 declarationAdditional functionality needed to describe parsers independent of input type.
Methods
try :: m a -> m aTake a parser that may consume input, and on failure, go back to where we started and fail as if we didn't consume input.
(<?>) :: m a -> String -> m ainfixr 0Give a parser a name
skipMany :: m a -> m ()A version of many that discards its input. Specialized because it can often be implemented more cheaply.
skipSome :: m a -> m ()skipSome papplies the parserpone or more times, skipping its result. (aka skipMany1 in parsec)unexpected :: String -> m aUsed to emit an error on an unexpected token
eof :: m ()This parser only succeeds at the end of the input. This is not a primitive parser but it is defined using notFollowedBy.
eof = notFollowedBy anyChar <?> "end of input"notFollowedBy :: Show a => m a -> m ()notFollowedBy ponly succeeds when parserpfails. This parser does not consume any input. This parser can be used to implement the 'longest match' rule. For example, when recognizing keywords (for examplelet), we want to make sure that a keyword is not followed by a legal identifier character, in which case the keyword is actually an identifier (for examplelets). We can program this behaviour as follows:keywordLet = try $ string "let" <* notFollowedBy alphaNum
Instances15Parsing, …
Parsing GetDefined in parsers-0.12.12 · Text.Parser.CombinatorsParsing ReadPDefined in parsers-0.12.12 · Text.Parser.CombinatorsChunk t => Parsing (Parser t)Defined in parsers-0.12.12 · Text.Parser.CombinatorsParsing m => Parsing (Unhighlighted m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unlined m)Defined in parsers-0.12.12 · Text.Parser.TokenParsing m => Parsing (Unspaced m)Defined in parsers-0.12.12 · Text.Parser.Token(Parsing m, Monad m) => Parsing (IdentityT m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m) => Parsing (ReaderT e m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m) => Parsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m) => Parsing (StateT s m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Stream s m t, Show t) => Parsing (ParsecT s u m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Combinators(Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m)Defined in parsers-0.12.12 · Text.Parser.Combinators