{-|
Module      : Hectoparsec.State
Copyright   : (c) comp 2020
License     : MIT
Maintainer  : onecomputer00@gmail.com
Stability   : stable
Portability : portable

The parser state.
-}
module Hectoparsec.State
    ( -- * Parser state
      State(..)
    , initialState
    , makeErrorAt
    ) where

import Hectoparsec.Error
import Hectoparsec.Pos

-- | The parser state.
data State s = State
    { forall s. State s -> s
stateInput  :: s                   -- ^ The rest of the input to parse.
    , forall s. State s -> Pos
statePos    :: !Pos                -- ^ The current position in the source.
    , forall s. State s -> Int
stateOffset :: {-# UNPACK #-} !Int -- ^ The current offset in the stream.
    }

-- | Creates a 'State' at the start of a stream.
initialState :: FilePath -> s -> State s
initialState :: forall s. FilePath -> s -> State s
initialState FilePath
fp s
s = s -> Pos -> Int -> State s
forall s. s -> Pos -> Int -> State s
State s
s (FilePath -> Pos
initialPos FilePath
fp) Int
0
{-# INLINE initialState #-}

-- | Creates a parse error from an error item located by some parsing state.
makeErrorAt :: State s -> ErrorItem s e l -> ParseError s e l
makeErrorAt :: forall s e l. State s -> ErrorItem s e l -> ParseError s e l
makeErrorAt State s
st ErrorItem s e l
err = Pos -> Int -> ErrorItem s e l -> ParseError s e l
forall s e l. Pos -> Int -> ErrorItem s e l -> ParseError s e l
ParseError (State s -> Pos
forall s. State s -> Pos
statePos State s
st) (State s -> Int
forall s. State s -> Int
stateOffset State s
st) ErrorItem s e l
err
{-# INLINE makeErrorAt #-}