我有一个 Parsec 解析器,我主要以应用风格编写。在一种情况下(我唯一一次使用sepBy
)我的eol
解析器有问题。首先,几个定义:
eol = try (string "\n\r")
<|> try (string "\r\n")
<|> string "\n"
<|> string "\r"
<?> "eol"
betaLine = string "BETA " *> (StrandPair <$> p_int <*> p_int <*> p_int <*> (p_int *> p_direction) <*> p_exposure) <* eol
注意:betaLine
完美运行(为简洁起见,我省略了p_int
等的定义:
*HmmPlus> parse betaLine "beta" "BETA 6 11 5 24 -1 oiiio\n"
Right (StrandPair {firstStart = 6, secondStart = 11, pairLength = 5, parallel = Antiparallel, exposure = [Exposed,Buried,Buried,Buried,Exposed]})
这个其他解析器会出现问题,hmmMatchEmissions
:
hmmMatchEmissions = spaces *> (V.fromList <$> sepBy p_logProb spaces) <* eol <?> "matchEmissions"
*HmmPlus> parse hmmMatchEmissions "me" " 2.61196 4.43481 2.86148 2.75135 3.26990 2.87580 3.69681\n"
Left "me" (line 2, column 1):
unexpected end of input
现在,如果我<* eol
从解析器定义中删除,并\n
从行中删除,它确实有效:
*HmmPlus> parse hmmMatchEmissions "me" " 2.61196 4.43481 2.86148 2.75135 3.26990 2.87580 3.69681"
Right (fromList [NonZero 2.61196,NonZero 4.43481,NonZero 2.86148,NonZero 2.75135,NonZero 3.2699,NonZero 2.8758,NonZero 3.69681])
那么,为什么在但不是eol
的情况下工作?betaLine
hmmMatchEmissions
我会注意到这是我唯一使用的地方sepBy
;这可能是一个线索吗?
更新:我做了以下事情,现在失败了:/
reqSpaces = many1 (oneOf " \t")
optSpaces = many (oneOf " \t")
hmmMatchEmissions = optSpaces *> (V.fromList <$> sepBy1 p_logProb reqSpaces) <* eol <?> "matchEmissions"
这是失败的:
*HmmPlus> parse hmmMatchEmissions "me" " 0.123 0.124\n"
Left "me" (line 1, column 10):
unexpected "0"
expecting eol
我会注意到第0
10 列中的意外是0.124
令牌的第一个字符。