HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletrifecta-2.1.4Haskell2010

Text.Trifecta.Parser

  • 2 types
  • 12 values
  • Packagetrifecta-2.1.4
  • Exports14
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceParser.hs
newtypenewtype 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

Instances15Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus, …

Feeding a parser more more input

6 declarations
datadata Step a
#

A Step allows for incremental parsing, since the parser

  • can be done with a final result

  • have errored

  • can have yielded a partial result with possibly more to come

Constructors

  • StepDone !Rope a

    Parsing is done and has converted the Rope to a final result

  • StepFail !Rope ErrInfo

    Parsing the Rope has failed with an error

  • StepCont !Rope (Result a) (Rope -> Step a)

    The Rope has been partially consumed and already yielded a Result, and if more input is provided, more results can be produced.

    One common scenario for this is to parse log files: after parsing a single line, that data can already be worked with, but there may be more lines to come.

Instances2Functor, Show
  • Functor StepDefined in trifecta-2.1.4 · Text.Trifecta.Parser
  • Show a => Show (Step a)Defined in trifecta-2.1.4 · Text.Trifecta.Parser
valuestarve :: Step a -> Result a
#

Assume all possible input has been given to the parser, execute it to yield a final result.

Parsing

6 declarations
valueparseFromFile :: MonadIO m => Parser a -> String -> m (Maybe a)
#

(parseFromFile p filePath) runs a parser p on the input read from filePath using ByteString.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
valueparseFromFileEx :: MonadIO m => Parser a -> String -> m (Result a)
#

(parseFromFileEx p filePath) runs a parser p on the input read from filePath using ByteString.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)