Created
April 16, 2018 13:19
-
-
Save naoto-ogawa/45cd02b3580372535b5f0942f9fe9c8b to your computer and use it in GitHub Desktop.
Using Megaparsec with state monad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.State | |
import Data.Void | |
import Text.Megaparsec hiding (State) | |
import Text.Megaparsec.Char | |
import qualified Text.Megaparsec.Char.Lexer as L | |
type Parser = ParsecT Void String (State Int) | |
str :: String | |
str = "11,2,43" | |
main1 :: IO () | |
main1 = case (runState (runParserT numbers "" str) (1::Int)) of | |
(Left err, s) -> putStr (parseErrorPretty err) | |
(Right xs, s) -> print $ (sum xs) + s | |
numbers :: Parser [Int] | |
numbers = do | |
x <- get | |
put (x+5) | |
integer `sepBy` char ',' | |
integer = L.decimal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment