Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Created April 4, 2017 03:40
Show Gist options
  • Select an option

  • Save throughnothing/ded5c0c00e712ac3fc13c1ed3861266e to your computer and use it in GitHub Desktop.

Select an option

Save throughnothing/ded5c0c00e712ac3fc13c1ed3861266e to your computer and use it in GitHub Desktop.
Parser Combinator Test
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (log, CONSOLE)
import Control.Plus ((<|>))
import Data.Maybe (Maybe(..))
import Text.Parsing.StringParser (Parser, runParser)
import Text.Parsing.StringParser.Combinators (optionMaybe)
import Text.Parsing.StringParser.String (string)
main :: ∀ e. Eff (console :: CONSOLE | e) Unit
main = do
-- This one finds the `noColon` first
-- Output: (Right (Just "test"))
logParse ((Just <$> noColon) <|> colon) "test"
-- This one fails on the `colon` and never tries the `noColon` branch
-- Output: (Left Expected ':'.)
-- Expected: (Right Nothing)
logParse (colon <|> (Just <$> noColon)) "test"
where
noColon :: Parser String
noColon = string "test"
-- I would expect this function to return `Parser Nothing`
-- in the event that we couldn't find the trailing ':', but
-- it seems to `fail` with an error inside the Parser instead.
colon :: Parser (Maybe String)
colon = optionMaybe $ (string "test" <* string ":")
logParse p s = log $ show $ runParser p s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment