Last active
May 25, 2022 07:32
-
-
Save deemp/85d4bac3d14df2a8e135dda25fddd2e4 to your computer and use it in GitHub Desktop.
This file contains 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
{-# LANGUAGE LambdaCase #-} | |
module Lib | |
where | |
import Text.Megaparsec | |
import Data.Void (Void) | |
import Text.Megaparsec.Char (digitChar) | |
import Data.Function ((&)) | |
type Parser = Parsec Void String | |
main :: IO () | |
main = do | |
let input = "11111333" | |
parse p "p" input & \case | |
Left e -> error $ show e | |
Right r -> print r -- should print (11111, 333) | |
return () | |
where | |
p :: Parser (Int, Int) | |
p = | |
do | |
l <- takeP (Just "Limited input: 5 characters") 5 | |
l' <- takeRest | |
setInput l | |
a <- customInt-- TODO limit input to 5 characters just for this `customInt` | |
setInput l' | |
b <- customInt -- take the remaining 3 (already limited) | |
return (a, b) | |
customInt :: Parser Int | |
customInt = read <$> many digitChar | |
{- | |
ghci> main | |
(11111,333) | |
1:5: | |
| | |
1 | 1111 | |
| ^ | |
unexpected end of input | |
expecting Limited input: 5 characters | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment