module Music.Theory.Z12.Morris_1987.Parse (rnrtnmi,pco) where
import Control.Monad
import Data.Char
import Music.Theory.Z12
import Music.Theory.Z12.Morris_1987
import Text.ParserCombinators.Parsec
type P a = GenParser Char () a
is_char :: Char -> P Bool
is_char c =
let f '_' = False
f _ = True
in liftM f (option '_' (char c))
get_int :: P Z12
get_int = liftM (fromInteger . read) (many1 digit)
rnrtnmi :: String -> SRO
rnrtnmi s =
let p = do { r <- rot
; r' <- is_char 'R'
; _ <- char 'T'
; t <- get_int
; m <- is_char 'M'
; i <- is_char 'I'
; eof
; return (SRO r r' t m i) }
rot = option 0 (char 'r' >> get_int)
in either
(\e -> error ("rnRTnMI parse failed\n" ++ show e))
id
(parse p "" s)
pco :: String -> [Z12]
pco s =
let s' = dropWhile isSpace s
s'' = takeWhile (`elem` "0123456789taAebB") s'
f c | c `elem` "taA" = 10
| c `elem` "ebB" = 11
| otherwise = fromInteger (read [c])
in map f s''