I am new to Parsec (and for parsers in general), and I am having some problems with this parser, I wrote:
list = char '(' *> many (spaces *> some letter) <* spaces <* char ')'
The idea is to analyze lists in this format (I work with s-expressions):
(firstElement secondElement thirdElement and so on)
I wrote this code to check it out:
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 "( )"
This is the result I get:
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
As you can see, it does not work when there is a space between the last element of the list and the space ) . I donβt understand why the empty space is not consumed by spaces , which I set immediately before <* char ')' . What stupid mistake did I make?
source share