我是新來秒差距(和一般解析器),我有這個分析器我寫了一些麻煩:很難得到一個秒差距解析器跳過空格正確
list = char '(' *> many (spaces *> some letter) <* spaces <* char ')'
的想法是分析列出了此格式(我工作到s表達式):
(firstElement secondElement thirdElement and so on)
我寫了這個代碼來測試它:
import Control.Applicative
import Text.ParserCombinators.Parsec hiding (many)
list = char '(' *> many (spaces *> some letter) <* spaces <* char ')'
test s = do
putStrLn $ "Testing " ++ show s ++ ":"
parseTest list s
putStrLn ""
main = do
test "()"
test "(hello)"
test "(hello world)"
test "(hello world)"
test "(hello world)"
test "()"
ŧ他是輸出I得到:
Testing "()":
[]
Testing "(hello)":
["hello"]
Testing "(hello world)":
["hello","world"]
Testing "(hello world)":
["hello","world"]
Testing "(hello world)":
parse error at (line 1, column 14):
unexpected ")"
expecting space or letter
Testing "()":
parse error at (line 1, column 3):
unexpected ")"
expecting space or letter
正如你可以看到,當有列表的最後一個元素,並關閉)
之間的空白失敗。我不明白爲什麼我在<* char ')'
之前放入的spaces
沒有消耗空白空間。我犯了什麼愚蠢的錯誤?