[Haskell-cafe] Parsec question

Michael Mossey mpm at alumni.caltech.edu
Fri Apr 17 03:52:55 EDT 2009

Here's what I've got so far.

-- Text is considered everything up to //. However, the problem
-- is that this consumes the //.
parseText = manyTill anyChar (try (string "//"))

-- Because the // is already consumed, parseKeyword just grabs
-- the available letters.
parseKeyword :: Parser String
parseKeyword = many1 letter

-- Test function.
parseSome = do t1 <- parseText
                k1 <- parseKeyword
                t2 <- parseText
                return (t1,k1,t2)

On "some text//keyword more text//" this gives

("some text","keyword"," more text")

On "some text//keyword more text"

this gives the error "expecting //"

I wonder how I can get the manyTill to be happy with eof before finding the //? I tried

parseText = manyTill anyChar (try (string "//") <|> eof)

but got a type error.

minh thu wrote:
> 2009/4/17 Michael P Mossey <mpm at alumni.caltech.edu>:
>> I want to write a parser that can read a file with this format: the file has
>> sections which are demarcated by keywords. Keywords always begin with two
>> forward slashes and consist of letters, digits, and underscore. The text can
>> be anything, including special characters. For instance:
>> //keyword some text
>> and more text //another_keyword and) some { more text
>> //ya_keyword $$
>> -- text
>> I'm not sure how to write a parser that considers anything but a double
>> slash to be a valid part of the text.
> Maybe you can use a combination of 'many', 'noneOf' or 'manyTill' ?
> Cheers,
> Thu

More information about the Haskell-Cafe mailing list