[Haskell-cafe] Parsec question

Levi Stephen levi.stephen at optusnet.com.au
Thu Jun 21 02:04:54 EDT 2007


Hi,

Fairly new to Haskell and trying some parsec. (Also, new to 
parsers/interpreters)

I had come up with this, which works, but I can't help thinking there's 
a better way :)

|> newtype Identifier = Identifier String
> newtype Literal = StringLiteral String -- to be extended later
> data Primary = PrimaryLiteral Literal | PrimaryIdentifier Identifier
>
||> primary = do {
>        i <- identifier;
>        return $ PrimaryIdentifier i; }
>    <|>   do {
>        l <- stringLiteral;
>        return $ PrimaryLiteral l; }
>
||> identifier = do
>    i <- many1 letter
>    return $ Identifier i
>
> stringLiteral = do
>    (char '\'')
>    s <- manyTill anyChar (char '\'')
>    return $ StringLiteral s

Is there a way through combining types/parsers that the double do block in primary could
be avoided?

I understand it's necessary right now because the parsers identifier and stringLiteral
return different types, so I can't just write:

> i <- identifier <|> stringLiteral

So, I'm not sure whether my types need work, the parsers, or if this is the simplest way.

Thanks,

Levi
lstephen.wordpress.com

PS, have lurked on the list for a while, enjoy the discussions and content.
|



More information about the Haskell-Cafe mailing list