yamlet
Safe HaskellNone
LanguageGHC2021

Yamlet.Internal.Parser.Monad

Description

A backtracking parser over the bytes of UTF-8 encoded text.

This module is intended for internal use only, and may change without warning in subsequent releases.

Synopsis

Parser

data P a Source #

Instances

Instances details
Applicative P Source # 
Instance details

Defined in Yamlet.Internal.Parser.Monad

Methods

pure :: a -> P a

(<*>) :: P (a -> b) -> P a -> P b

liftA2 :: (a -> b -> c) -> P a -> P b -> P c

(*>) :: P a -> P b -> P b

(<*) :: P a -> P b -> P a

Functor P Source # 
Instance details

Defined in Yamlet.Internal.Parser.Monad

Methods

fmap :: (a -> b) -> P a -> P b

(<$) :: a -> P b -> P a

Monad P Source # 
Instance details

Defined in Yamlet.Internal.Parser.Monad

Methods

(>>=) :: P a -> (a -> P b) -> P b

(>>) :: P a -> P b -> P b

return :: a -> P a

data Env Source #

The input of the parser.

Constructors

Env 

Fields

  • array :: !Array
     
  • base :: !Int

    The index of the first byte of the input.

  • end :: !Int

    The index past the last byte that the parser can read.

  • streamEnd :: !Int

    The index past the last byte of the input. A document ends before it at a document marker.

  • handles :: !(Map Text Text)

    The tag handles of the current document.

data ParseError Source #

An error that no backtracking can recover from: an error at the index with the message, or a failure at the index in the environment, whose message the caller of the parser finds.

Constructors

ParseError !Int !String 
UnexpectedParseError !Env !Int 

runParser :: Env -> Int -> P a -> Either ParseError (Maybe a, Int, Int) Source #

Run a parser from the given index. Return the result, the index after it and the furthest failure.

Combinators

(<|>) :: P a -> P a -> P a infixl 3 Source #

Ordered choice. Try the second parser if the first one fails.

many :: P a -> P [a] Source #

Zero or more times. Stop if the parser succeeds without input.

many_ :: P a -> P () Source #

Zero or more times, discard the results.

optional :: P a -> P (Maybe a) Source #

optional_ :: P a -> P () Source #

option :: a -> P a -> P a Source #

notFollowedBy :: P a -> P () Source #

Succeed without input if the parser fails. An error of the parser stays an error, as in <|>.

Primitives

pos :: P Int Source #

setPos :: Int -> P () Source #

furthest :: P Int Source #

The furthest position at which a parser failed so far.

advance :: Int -> P () Source #

addTagBytes :: Int -> P Int Source #

Add the bytes that the prefix of a %TAG directive adds to a tag, and return the bytes that such prefixes added so far. A parser that fails drops its bytes, so only the tags of the result count.

peek :: P Word8 Source #

The byte at the current position, 0 at the end of the input.

peekAt :: Int -> P Word8 Source #

The byte at the given distance from the current position.

guardP :: Bool -> P () Source #

throwAt :: Int -> String -> P a Source #

Stop with an error at the given index.

throwUnexpected :: Int -> P a Source #

Stop at the index, with an error whose message the caller of the parser finds.

withEnd :: Int -> P a -> P a Source #

Run a parser that cannot read past the given index.

withHandles :: Map Text Text -> P a -> P a Source #

char :: Word8 -> P () Source #

skipWhile :: (Word8 -> Bool) -> P () Source #

scan :: (Env -> Int -> Int) -> P () Source #

Move to the index that the function computes from the current one.

data Scanned a Source #

The result of a scanning loop.

Constructors

Done !Int !a

The value and the index after it.

NoMatch !Int

The input does not match. The index is the location of the mismatch.

Failed !Int !String

An error at the index.

withScan :: (Env -> Int -> Scanned a) -> P a Source #

Run a pure loop over the input from the current position.

Input access

byteAt :: Env -> Int -> Word8 Source #

byteBefore :: Env -> Int -> Word8 Source #

The byte before the index, 0 at the start of the input.

slice :: Env -> Int -> Int -> Text Source #

toOffset :: Env -> Int -> Offset Source #