[Haskell-cafe] Parsec: Transforming between parsers with different token streams

Maciej Podgurski maciej.podgurski at googlemail.com
Mon Feb 11 16:50:01 EST 2008


I'm writing a simple parser for a line-oriented language using Parsec. A 
group of lines is parsed by a parser that tokens are of type String. 
Each token is again parsed by a parser that tokens are of type Char 
(i.e. this parser has the Parsec type Parser a).

Now I wrote a transforming function that converts a char parser to a 
line parser by running the char parser on the current token of the line 
parser and accepting the token, if the char parser did not fail:

     type LineParser = GenParser String () a

     toLineParser :: Parser a -> LineParser a
     toLineParser p = tokenPrim showLine nextPos testLine
         <?> "how to get the inner error message???"
             showLine l         = "\": " ++ l ++ "\""
             nextPos pos t rest = incSourceLine pos 1
             testLine l         = case parse p "" l of
                                      Left msg   -> Nothing
                                      Right res  -> Just res

My problem is to extract the possible error message of the inner char 
parser (msg) and pass it to the outer line parser. Maybe using tokenPrim 
is no the best approach here. Someone how can help me with that problem?

Besh wishes,


More information about the Haskell-Cafe mailing list