module Main where
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Token
import Data.Char
price :: Parser Int -- price in cents
price = lexeme (do{ ds1 <- many1 digit
; char '.'
; ds2 <- count 2 digit
; return (convert 0 (ds1 ++ ds2))
})
<?> "price"
where
convert n [] = n
convert n (d:ds) = convert (10*n + digitToInt d) ds
receipt :: Parser Bool
receipt = do{ ps <- many produkt
; p <- total
; return (sum ps == p)
}
produkt = do{ reserved "return"
; p <- price
; semi
; return (-p)
}
<|> do{ identifier
; p <- price
; semi
; return p
}
<?> "produkt"
total = do{ p <- price
; reserved "total"
; return p
}
runLex :: Show a => Parser a -> String -> IO ()
runLex p input
= run (do{ whiteSpace
; x <- p
; eof
; return x
}) input
run :: Show a => Parser a -> String -> IO ()
run p input
= case (parse p "" input) of
Left err -> do{ putStr "parse error at "
; print err
}
Right x -> print x
---
Compiling Main ( rec.hs, interpreted )
rec.hs:8:10:
Expecting a function type, but found `GenParser tok st a'
Expected type: GenParser tok st a
Inferred type: CharParser st1 a1 -> CharParser st1 a1
Probable cause: `lexeme' is applied to too few arguments in the call
(lexeme (do
ds1 <- many1 digit
char '.'
ds2 <- count 2 digit
return (convert 0 (ds1 ++ ds2))))
In the first argument of `(<?>)', namely
`lexeme (do
ds1 <- many1 digit
char '.'
ds2 <- count 2 digit
return (convert 0 (ds1 ++ ds2)))'
rec.hs:24:23:
Couldn't match `TokenParser st' against `[Char]'
Expected type: TokenParser st
Inferred type: [Char]
In the first argument of `reserved', namely `"return"'
In a 'do' expression: reserved "return"
rec.hs:37:23:
Couldn't match `TokenParser st' against `[Char]'
Expected type: TokenParser st
Inferred type: [Char]
In the first argument of `reserved', namely `"total"'
In a 'do' expression: reserved "total"
rec.hs:43:19:
Couldn't match `GenParser Char ()' against `(->) (TokenParser st)'
Expected type: GenParser Char ()
Inferred type: (->) (TokenParser st)
In a 'do' expression: whiteSpace
In the first argument of `run', namely
`(do
whiteSpace
x <- p
eof
return x)'
Failed, modules loaded: none.