0

所以我想用 Boomerang 解析一些 AIS 数据会很有趣,但我遇到了第一个障碍。编译错误令人费解。看到我在试图解决这个问题之前已经在 Boomerang 中解析了类似的东西。

图书馆很简单。我定义了一些基本类型及其解析器/语法:

import           Control.Category      (id, (.))
import           Control.Monad         (forever)
import           Prelude               hiding (id, (.))
import           System.IO             (hFlush, stdout)
import           Text.Boomerang
import           Text.Boomerang.String
import           Text.Boomerang.TH

data MessageType = AIVDM | AIVDO deriving (Enum, Eq, Show)

data AIS = AIS {
              msgType :: MessageType
          } deriving (Eq, Show)

$(makeBoomerangs ''MessageType)
$(makeBoomerangs ''AIS)

messageTypeP :: StringBoomerang () (MessageType :- ())
messageTypeP = rAIVDM . "!AIVDM" <> rAIVDO . "!AIVDO"

aisP :: StringBoomerang () (AIS :- ())
aisP = rAIS . messageTypeP . lit ","

我现在希望支持句子计数值,它位于消息类型之后;我添加IntAIS

data AIS = AIS {
              msgType :: MessageType, sCount :: Int
          } deriving (Eq, Show)

并更改解析器/打印机:

aisP :: StringBoomerang () (AIS :- ())
aisP = rAIS . messageTypeP . lit "," . int

但它无法编译:

• Couldn't match type ‘()’ with ‘Int :- ()’
  Expected type: Boomerang
                   StringError String () (MessageType :- (Int :- ()))
    Actual type: Boomerang StringError String () (MessageType :- ())
• In the second argument of ‘(.)’, namely
    ‘messageTypeP . lit "," . int’
  In the expression: rAIS . messageTypeP . lit "," . int
  In an equation for ‘aisP’:
      aisP = rAIS . messageTypeP . lit "," . int

哎哟。请帮忙?

4

1 回答 1

1

回旋镖应该是多态的。

messageTypeP :: StringBoomerang r (MessageType :- r)

aisP :: StringBoomerang r (AIS :- r)

解释是这r是一堆类型,并且回旋镖从/向其中弹出/推送类型。设置r()强制输入堆栈为空,这会损害这些回旋镖的可重用性。

于 2017-05-05T13:59:05.710 回答