我有一個Parsec解析器,我寫的主要是應用程序樣式。在一個案例中(唯一一次我使用sepBy
),我在解析器eol
時遇到了問題。首先,幾個定義:Parsec行結束的麻煩
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])
那麼,爲什麼在betaLine
的情況下工作eol
但不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
我會注意到,在第10列意外0
是0.124
令牌的第一個字符。
猜測:它是'空間',這給你帶來麻煩,而不是'sepBy'本身。嘗試使用不包含換行符的空格定義。 –
我試過這個,它產生了一個不同的故障模式......見上面。 –