parsec2-1.0.0: Monadic parser combinators

Portabilityportable
Stabilityprovisional
MaintainerAntoine Latter <aslatter@gmail.com>

Text.ParserCombinators.Parsec.Combinator

Description

Commonly used generic combinators

Synopsis

Documentation

choice :: [GenParser tok st a] -> GenParser tok st aSource

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.

count :: Int -> GenParser tok st a -> GenParser tok st [a]Source

count 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.

between :: GenParser tok st open -> GenParser tok st close -> GenParser tok st a -> GenParser tok st aSource

between open close p parses open, followed by p and close. Returns the value returned by p.

  braces  = between (symbol "{") (symbol "}")

option :: a -> GenParser tok st a -> GenParser tok st aSource

option 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 (do{ d <- digit
                          ; return (digitToInt d)
                          })

optionMaybe :: GenParser tok st a -> GenParser tok st (Maybe a)Source

optionMaybe p tries to apply parser p. If p fails without consuming input, it return Nothing, otherwise it returns Just the value returned by p.

optional :: GenParser tok st a -> GenParser tok st ()Source

optional 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.

skipMany1 :: GenParser tok st a -> GenParser tok st ()Source

skipMany1 p applies the parser p one or more times, skipping its result.

many1 :: GenParser tok st a -> GenParser tok st [a]Source

many1 p applies the parser p one or more times. Returns a list of the returned values of p.

  word  = many1 letter

sepBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]Source

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 :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]Source

sepBy1 p sep parses one or more occurrences of p, separated by sep. Returns a list of values returned by p.

endBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]Source

endBy p sep parses zero or more occurrences of p, seperated and ended by sep. Returns a list of values returned by p.

   cStatements  = cStatement `endBy` semi

endBy1 :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]Source

endBy1 p sep parses one or more occurrences of p, seperated and ended by sep. Returns a list of values returned by p.

sepEndBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]Source

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` semi

sepEndBy1 :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a]Source

sepEndBy1 p sep parses one or more occurrences of p, separated and optionally ended by sep. Returns a list of values returned by p.

chainl :: GenParser tok st a -> GenParser tok st (a -> a -> a) -> a -> GenParser tok st aSource

chainl p op x parser 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.

chainl1 :: GenParser tok st a -> GenParser tok st (a -> a -> a) -> GenParser tok st aSource

chainl1 p op x parser 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   =   do{ symbol "*"; return (*)   }
          <|> do{ symbol "/"; return (div) }

  addop   =   do{ symbol "+"; return (+) }
          <|> do{ symbol "-"; return (-) }

chainr :: GenParser tok st a -> GenParser tok st (a -> a -> a) -> a -> GenParser tok st aSource

chainr p op x parser 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.

chainr1 :: GenParser tok st a -> GenParser tok st (a -> a -> a) -> GenParser tok st aSource

chainr1 p op x parser 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.

eof :: Show tok => GenParser tok st ()Source

This parser only succeeds at the end of the input. This is not a primitive parser but it is defined using notFollowedBy.

  eof  = notFollowedBy anyToken <?> "end of input"

notFollowedBy :: Show tok => GenParser tok st tok -> GenParser tok st ()Source

notFollowedBy p only succeeds when parser p fails. This parser does not consume any input. This parser can be used to implement the 'longest match' rule. For example, when recognizing keywords (for example let), 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 example lets). We can program this behaviour as follows:

  keywordLet  = try (do{ string "let"
                       ; notFollowedBy alphaNum
                       })

manyTill :: GenParser tok st a -> GenParser tok st end -> GenParser tok st [a]Source

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.

lookAhead :: GenParser tok st a -> GenParser tok st aSource

lookAhead p parses p without consuming any input.

anyToken :: Show tok => GenParser tok st tokSource

The parser anyToken accepts any kind of token. It is for example used to implement eof. Returns the accepted token.