trifecta-1.6.2.1: A modern parser combinator library with convenient diagnostics

Copyright | (c) Edward Kmett 2011-2015
License | BSD3
Maintainer | ekmett@gmail.com
Stability | experimental
Portability | non-portable
Safe Haskell | None
Language | Haskell2010

Text.Trifecta.Parser

Contents

- Feeding a parser more more input
- Parsing

Description

Synopsis

- newtype Parser a = Parser {
  - unparser :: forall r. (a -> Err -> It Rope r) -> (Err -> It Rope r) -> (a -> Set String -> Delta -> ByteString -> It Rope r) -> (ErrInfo -> It Rope r) -> Delta -> ByteString -> It Rope r
  }
- manyAccum :: (a -> [a] -> [a]) -> Parser a -> Parser [a]
- data Step a
  - = StepDone !Rope a
  - | StepFail !Rope ErrInfo
  - | StepCont !Rope (Result a) (Rope -> Step a)
- feed :: Reducer t Rope => t -> Step r -> Step r
- starve :: Step a -> Result a
- stepParser :: Parser a -> Delta -> ByteString -> Step a
- stepResult :: Rope -> Result a -> Step a
- stepIt :: It Rope a -> Step a
- parseFromFile :: MonadIO m => Parser a -> String -> m (Maybe a)
- parseFromFileEx :: MonadIO m => Parser a -> String -> m (Result a)
- parseString :: Parser a -> Delta -> String -> Result a
- parseByteString :: Parser a -> Delta -> ByteString -> Result a
- parseTest :: (MonadIO m, Show a) => Parser a -> String -> m ()

# Documentation

newtype Parser a #

The type of a trifecta parser

The first four arguments are behavior continuations:

- epsilon success: the parser has consumed no input and has a result as well as a possible Err; the position and chunk are unchanged (see `pure`)
- epsilon failure: the parser has consumed no input and is failing with the given Err; the position and chunk are unchanged (see `empty`)
- committed success: the parser has consumed input and is yielding the result, set of expected strings that would have permitted this parse to continue, new position, and residual chunk to the continuation.
- committed failure: the parser has consumed input and is failing with a given ErrInfo (user-facing error message)

The remaining two arguments are

- the current position
- the chunk of input currently under analysis

`Parser` is an `Alternative`; trifecta's backtracking behavior encoded as `<|>` is to behave as the leftmost parser which yields a value (regardless of any input being consumed) or which consumes input and fails. That is, a choice of parsers will only yield an epsilon failure if *all* parsers in the choice do. If that is not the desired behavior, see `try`, which turns a committed parser failure into an epsilon failure (at the cost of error information).

Constructors

Parser |
Fields unparser :: forall r. (a -> Err -> It Rope r) -> (Err -> It Rope r) -> (a -> Set String -> Delta -> ByteString -> It Rope r) -> (ErrInfo -> It Rope r) -> Delta -> ByteString -> It Rope r

Instances

Monad Parser # |
Methods (>>=) :: Parser a -> (a -> Parser b) -> Parser b # (>>) :: Parser a -> Parser b -> Parser b # return :: a -> Parser a # fail :: String -> Parser a #
Functor Parser # |
Methods fmap :: (a -> b) -> Parser a -> Parser b # (<$) :: a -> Parser b -> Parser a #
Applicative Parser # |
Methods pure :: a -> Parser a # (<*>) :: Parser (a -> b) -> Parser a -> Parser b # (*>) :: Parser a -> Parser b -> Parser b # (<*) :: Parser a -> Parser b -> Parser a #
Alternative Parser # |
Methods empty :: Parser a # (<|>) :: Parser a -> Parser a -> Parser a # some :: Parser a -> Parser [a] # many :: Parser a -> Parser [a] #
MonadPlus Parser # |
Methods mzero :: Parser a # mplus :: Parser a -> Parser a -> Parser a #
TokenParsing Parser # |
Methods someSpace :: Parser () # nesting :: Parser a -> Parser a # semi :: Parser Char # highlight :: Highlight -> Parser a -> Parser a # token :: Parser a -> Parser a #
CharParsing Parser # |
Methods satisfy :: (Char -> Bool) -> Parser Char # char :: Char -> Parser Char # notChar :: Char -> Parser Char # anyChar :: Parser Char # string :: String -> Parser String # text :: Text -> Parser Text #
LookAheadParsing Parser # |
Methods lookAhead :: Parser a -> Parser a #
Parsing Parser # |
Methods try :: Parser a -> Parser a # (<?>) :: Parser a -> String -> Parser a # skipMany :: Parser a -> Parser () # skipSome :: Parser a -> Parser () # unexpected :: String -> Parser a # eof :: Parser () # notFollowedBy :: Show a => Parser a -> Parser () #
DeltaParsing Parser # |
Methods line :: Parser ByteString # position :: Parser Delta # slicedWith :: (a -> ByteString -> r) -> Parser a -> Parser r # rend :: Parser Rendering # restOfLine :: Parser ByteString #
Errable Parser # |
Methods raiseErr :: Err -> Parser a #
MarkParsing Delta Parser # |
Methods mark :: Parser Delta # release :: Delta -> Parser () #
Semigroup a => Semigroup (Parser a) # |
Methods (<>) :: Parser a -> Parser a -> Parser a # sconcat :: NonEmpty (Parser a) -> Parser a # stimes :: Integral b => b -> Parser a -> Parser a #
Monoid a => Monoid (Parser a) # |
Methods mempty :: Parser a # mappend :: Parser a -> Parser a -> Parser a # mconcat :: [Parser a] -> Parser a #

manyAccum :: (a -> [a] -> [a]) -> Parser a -> Parser [a] #

# Feeding a parser more more input

data Step a #

Constructors

StepDone !Rope a |
StepFail !Rope ErrInfo |
StepCont !Rope (Result a) (Rope -> Step a) |

Instances

Functor Step # |
Methods fmap :: (a -> b) -> Step a -> Step b # (<$) :: a -> Step b -> Step a #
Show a => Show (Step a) # |
Methods showsPrec :: Int -> Step a -> ShowS # show :: Step a -> String # showList :: [Step a] -> ShowS #

feed :: Reducer t Rope => t -> Step r -> Step r #

starve :: Step a -> Result a #

stepParser :: Parser a -> Delta -> ByteString -> Step a #

stepResult :: Rope -> Result a -> Step a #

stepIt :: It Rope a -> Step a #

# Parsing

parseFromFile :: MonadIO m => Parser a -> String -> m (Maybe a) #

`parseFromFile p filePath` runs a parser `p` on the input read from `filePath` using `readFile`. All diagnostic messages emitted over the course of the parse attempt are shown to the user on the console.

    main = do
      result <- parseFromFile numbers "digits.txt"
      case result of
        Nothing -> return ()
        Just a  -> print $ sum a

parseFromFileEx :: MonadIO m => Parser a -> String -> m (Result a) #

`parseFromFileEx p filePath` runs a parser `p` on the input read from `filePath` using `readFile`. Returns all diagnostic messages emitted over the course of the parse and the answer if the parse was successful.

    main = do
      result <- parseFromFileEx (many number) "digits.txt"
      case result of
        Failure xs -> displayLn xs
        Success a  -> print (sum a)

parseString :: Parser a -> Delta -> String -> Result a #

parseByteString :: Parser a -> Delta -> ByteString -> Result a #

`parseByteString p delta i` runs a parser `p` on `i`.

parseTest :: (MonadIO m, Show a) => Parser a -> String -> m () #