In fact, I use NanoParsec and now it is flexible enough to parse things like "apdu #x3F #xbE #xEf", I include the whole source, hope it helps
import froid.util.Log
import frege.data.Char hiding (isNumber)
import frege.data.NanoParsec
import Control.Concurrent as C
import io.github.mchav.tryfrege.Utilities
data Command = Apdu [Byte]
| Reset
| Finish
| Exception
derive Show Command
hexdigit = satisfy (Char.isHexDigit :: Char -> Bool)
h1 :: Parser [] Char [Char]
h1 = do
a <- expect '#'
b <- expect 'x'
c <- hexdigit
d <- hexdigit
return $ [c, d]
h2 :: Parser [] Char [[Char]]
h2 = do
spaces *> h1 `sepBy` spaces
h3 :: Parser [] Char [[Char]]
h3 = do
a <- spaces *> some letter <* spaces
b <- h2
return $ a:b
h4 :: Parser [] Char [[Char]]
h4 = do
spaces *> h3 <* spaces
parseDriver :: Parser [] Char [[Char]] -> ([] Char) -> IO [[Char]]
parseDriver p s = case runid p s of
(Left msg, str) -> do
error $ "Parse failed!" ++ (reporterror str msg)
(Right r, str) -> do
if (null str) then return r
else error $ "Parse failed: " ++ (reporterror str "Tokens left")
digitToByte = intToByte . digitToInt
parseJobs input = do
r <- parseDriver h4 (unpacked input)
case r of
['a','p','d','u']:d -> return $ Apdu $ map (\[a,b]-> (digitToByte a) * 16 + digitToByte b) d
[['r','e','s','e','t']] -> return Reset
[['f','i','n','i','s','h']] -> return Finish
[['e','x','c','e','p','t','i','o','n']] -> return Exception
_ -> return Exception
在 2018年1月28日星期日 UTC+8上午4:47:17,Marimuthu Madasamy写道: