Skip to content

Instantly share code, notes, and snippets.

@deemp
Last active May 25, 2022 07:32
Show Gist options
  • Select an option

  • Save deemp/85d4bac3d14df2a8e135dda25fddd2e4 to your computer and use it in GitHub Desktop.

Select an option

Save deemp/85d4bac3d14df2a8e135dda25fddd2e4 to your computer and use it in GitHub Desktop.

Revisions

  1. deemp revised this gist May 25, 2022. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions Lib.hs
    Original file line number Diff line number Diff line change
    @@ -30,3 +30,16 @@ main = do

    customInt :: Parser Int
    customInt = read <$> many digitChar

    {-
    ghci> main
    (11111,333)
    1:5:
    |
    1 | 1111
    | ^
    unexpected end of input
    expecting Limited input: 5 characters
    -}
  2. deemp revised this gist May 25, 2022. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Lib.hs
    Original file line number Diff line number Diff line change
    @@ -30,5 +30,3 @@ main = do

    customInt :: Parser Int
    customInt = read <$> many digitChar
    -- customInt :: Parser Int -- NOTE: can't change this fn
    -- customInt = many digitChar
  3. deemp created this gist May 25, 2022.
    34 changes: 34 additions & 0 deletions Lib.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    {-# 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
    -- customInt :: Parser Int -- NOTE: can't change this fn
    -- customInt = many digitChar