File: State.hs

package info (click to toggle)
haskell-megaparsec 9.5.0-1
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 352 kB
  • sloc: haskell: 3,661; makefile: 6
file content (95 lines) | stat: -rw-r--r-- 2,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module      :  Text.Megaparsec.State
-- Copyright   :  © 2015–present Megaparsec contributors
--                © 2007 Paolo Martini
--                © 1999–2001 Daan Leijen
-- License     :  FreeBSD
--
-- Maintainer  :  Mark Karpov <markkarpov92@gmail.com>
-- Stability   :  experimental
-- Portability :  portable
--
-- Definition of Megaparsec's 'State'.
--
-- @since 6.5.0
module Text.Megaparsec.State
  ( State (..),
    PosState (..),
  )
where

import Control.DeepSeq (NFData)
import Data.Data (Data)
import Data.Typeable (Typeable)
import GHC.Generics
import {-# SOURCE #-} Text.Megaparsec.Error (ParseError)
import Text.Megaparsec.Pos

-- | This is the Megaparsec's state parametrized over stream type @s@ and
-- custom error component type @e@.
data State s e = State
  { -- | The rest of input to process
    stateInput :: s,
    -- | Number of processed tokens so far
    --
    -- @since 7.0.0
    stateOffset :: {-# UNPACK #-} !Int,
    -- | State that is used for line\/column calculation
    --
    -- @since 7.0.0
    statePosState :: PosState s,
    -- | Collection of “delayed” 'ParseError's in reverse order. This means
    -- that the last registered error is the first element of the list.
    --
    -- @since 8.0.0
    stateParseErrors :: [ParseError s e]
  }
  deriving (Typeable, Generic)

deriving instance
  ( Show (ParseError s e),
    Show s
  ) =>
  Show (State s e)

deriving instance
  ( Eq (ParseError s e),
    Eq s
  ) =>
  Eq (State s e)

deriving instance
  ( Data e,
    Data (ParseError s e),
    Data s
  ) =>
  Data (State s e)

instance (NFData s, NFData (ParseError s e)) => NFData (State s e)

-- | A special kind of state that is used to calculate line\/column
-- positions on demand.
--
-- @since 7.0.0
data PosState s = PosState
  { -- | The rest of input to process
    pstateInput :: s,
    -- | Offset corresponding to beginning of 'pstateInput'
    pstateOffset :: !Int,
    -- | Source position corresponding to beginning of 'pstateInput'
    pstateSourcePos :: !SourcePos,
    -- | Tab width to use for column calculation
    pstateTabWidth :: Pos,
    -- | Prefix to prepend to offending line
    pstateLinePrefix :: String
  }
  deriving (Show, Eq, Data, Typeable, Generic)

instance (NFData s) => NFData (PosState s)