Skip to content

Commit

Permalink
Add initState and initPosState to Text.Megaparsec.State
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev135 authored and mrkkrp committed Aug 30, 2023
1 parent 9a04bde commit 53377ef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
*Megaparsec follows [SemVer](https://semver.org/).*

## Unreleased

* Added the functions `initialState` and `initialPosState` to
`Text.Megaparsec.State`. [Issue
449](https://github.com/mrkkrp/megaparsec/issues/449).

## Megaparsec 9.5.0

* Dropped a number of redundant constraints here and there. [PR
Expand Down
18 changes: 0 additions & 18 deletions Text/Megaparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -295,24 +295,6 @@ runParserT' p s = do
Error e ->
(s', Left (toBundle (e :| stateParseErrors s')))

-- | Given the name of source file and the input construct the initial state
-- for a parser.
initialState :: String -> s -> State s e
initialState name s =
State
{ stateInput = s,
stateOffset = 0,
statePosState =
PosState
{ pstateInput = s,
pstateOffset = 0,
pstateSourcePos = initialPos name,
pstateTabWidth = defaultTabWidth,
pstateLinePrefix = ""
},
stateParseErrors = []
}

----------------------------------------------------------------------------
-- Signaling parse errors

Expand Down
39 changes: 39 additions & 0 deletions Text/Megaparsec/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
-- @since 6.5.0
module Text.Megaparsec.State
( State (..),
initialState,
PosState (..),
initialPosState,
)
where

Expand Down Expand Up @@ -74,6 +76,24 @@ deriving instance

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

-- | Given the name of the source file and the input construct the initial
-- state for a parser.
--
-- @since 9.6.0
initialState ::
-- | Name of the file the input is coming from
FilePath ->
-- | Input
s ->
State s e
initialState name s =
State
{ stateInput = s,
stateOffset = 0,
statePosState = initialPosState name s,
stateParseErrors = []
}

-- | A special kind of state that is used to calculate line\/column
-- positions on demand.
--
Expand All @@ -93,3 +113,22 @@ data PosState s = PosState
deriving (Show, Eq, Data, Typeable, Generic)

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

-- | Given the name of source file and the input construct the initial
-- positional state.
--
-- @since 9.6.0
initialPosState ::
-- | Name of the file the input is coming from
FilePath ->
-- | Input
s ->
PosState s
initialPosState name s =
PosState
{ pstateInput = s,
pstateOffset = 0,
pstateSourcePos = initialPos name,
pstateTabWidth = defaultTabWidth,
pstateLinePrefix = ""
}

0 comments on commit 53377ef

Please sign in to comment.