如何在Haskell中运行Parsec测试?

问题描述 投票:0回答:1

[最近,我开始学习使用Hackage库(尤其是Parsec)来解析器的实现。到目前为止,我已经将此代码作为简单计算器功能之一来测试整数加法:

import Text.Parsec hiding(digit)
import Data.Functor

type Parser a = Parsec String () a

digit :: Parser Char
digit = oneOf ['0'..'9']

number :: Parser Integer
number = read <$> many1 digit

addition = do
    lhv <- number
    spaces
    char '+'
    spaces
    rhv <- number
    return $ lhv + rhv  

由于缺少启动GHCI解析器的线索(我的操作系统是Windows 10,我有点困惑。我想在命令提示符中输入什么以进行一些测试?

parsing haskell parsec
1个回答
0
投票

您可以使用parseTest中的Text.Parsec module功能。 GHCI中的示例会话:

~/g/scripts $ stack ghci parser.hs
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/sibi/.ghci
[1 of 1] Compiling Main             ( /home/sibi/github/scripts/parser.hs, interpreted )
Ok, one module loaded.
Loaded GHCi configuration from /tmp/haskell-stack-ghci/452ad586/ghci-script
λ> :t addition
addition
  :: ParsecT String () Data.Functor.Identity.Identity Integer
λ> :t parseTest
parseTest
  :: (Stream s Data.Functor.Identity.Identity t, Show a) =>
     Parsec s () a -> s -> IO ()
λ> parseTest addition "3 + 2"
5
© www.soinside.com 2019 - 2024. All rights reserved.