Skip to content

A very small monadic parser combinator in Haskell.

Notifications You must be signed in to change notification settings

sfluorine/parcombinator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Example

import Parser
import Data.Char

data Value = I Integer
           | F Float
           deriving Show

parseNumber :: Parser Value
parseNumber = F <$> parseFloat <|> I <$> parseInteger

parseFloat :: Parser Float
parseFloat = read <$> do
  l <- token (many $ satisfy isDigit) -- token is parsing zero or more space (' ') before many $ satisfy isDigit
  dot <- char '.'
  r <- many $ satisfy isDigit
  return $ l ++ [dot] ++ r

parseInteger :: Parser Integer
parseInteger = read <$> (many $ satisfy isDigit)

main :: IO ()
main = do
  f <- return $ runParser parseNumber "34.5"
  i <- return $ runParser parseNumber "34"
  case f of
    (Right (F f)) -> putStrLn $ "Float: " ++ show f
  case i of
    (Right (I i)) -> putStrLn $ "Integer: " ++ show i

About

A very small monadic parser combinator in Haskell.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published