Skip to content

Instantly share code, notes, and snippets.

@leshow
Created January 9, 2019 19:59
Show Gist options
  • Save leshow/4ed2d9d7666c574c67d4f38e0308b620 to your computer and use it in GitHub Desktop.
Save leshow/4ed2d9d7666c574c67d4f38e0308b620 to your computer and use it in GitHub Desktop.
module Parse where
import Control.Monad
import Data.Char
import Control.Applicative
data Parsed = Digit Integer | Hex Integer | Word String deriving Show
parseHex :: Parsed -> Char -> [Parsed]
parseHex (Hex i) c = if isHexDigit c
then return (Hex ((i * 16) + toInteger (digitToInt c)))
else mzero
parseHex _ _ = mzero
parseDigit :: Parsed -> Char -> [Parsed]
parseDigit (Digit i) c = if isDigit c
then return (Digit ((i * 10) + toInteger (digitToInt c)))
else mzero
parseDigit _ _ = mzero
parseWord :: Parsed -> Char -> [Parsed]
parseWord (Word s) c = if isAlpha c then return (Word (s <> [c])) else mzero
parseWord _ _ = mzero
parse :: Parsed -> Char -> [Parsed]
parse p c = parseHex p c <|> parseDigit p c <|> parseWord p c
parseArg :: String -> [Parsed]
parseArg s = do
initial <- pure (Hex 0) <|> pure (Digit 0) <|> pure (Word "")
foldM parse initial s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment