Skip to content

Commit

Permalink
add initState and initPosState to Megaparsec.State
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev135 committed Jul 16, 2023
1 parent 86a2bed commit f600e2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
22 changes: 2 additions & 20 deletions Text/Megaparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ runParser ::
-- | Input for parser
s ->
Either (ParseErrorBundle s e) a
runParser p name s = snd $ runParser' p (initialState name s)
runParser p name s = snd $ runParser' p (initState name s)

-- | The function is similar to 'runParser' with the difference that it
-- accepts and returns the parser state. This allows us e.g. to specify
Expand Down Expand Up @@ -265,7 +265,7 @@ runParserT ::
-- | Input for parser
s ->
m (Either (ParseErrorBundle s e) a)
runParserT p name s = snd <$> runParserT' p (initialState name s)
runParserT p name s = snd <$> runParserT' p (initState name s)

-- | This function is similar to 'runParserT', but like 'runParser'' it
-- accepts and returns parser state. This is thus the most general way to
Expand Down 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
26 changes: 26 additions & 0 deletions Text/Megaparsec/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
module Text.Megaparsec.State
( State (..),
PosState (..),
initPosState,
initState
)
where

Expand Down Expand Up @@ -93,3 +95,27 @@ 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.
initPosState :: String -> s -> PosState s
initPosState name s =
PosState
{ pstateInput = s,
pstateOffset = 0,
pstateSourcePos = initialPos name,
pstateTabWidth = defaultTabWidth,
pstateLinePrefix = ""
}


-- | Given the name of source file and the input construct the initial state
-- for a parser.
initState :: String -> s -> State s e
initState name s =
State
{ stateInput = s,
stateOffset = 0,
statePosState = initPosState name s,
stateParseErrors = []
}

0 comments on commit f600e2c

Please sign in to comment.