Using Parsec for lexing&parsing I'm creating list of Tokens from input [Char] stream using Parsec v3. The definition of Token looks like this:

data Token = CharKeyword | OpeningBracket | Identifier String | Natural Int


As result of calling parse lexComb "" inputStream i have [Token]. Everything OK so far.

Now i want to parse this list of tokens using another parse invocation, but i've found myself writing such boilerplate code for every Token constructor:

psOpBracket = tokenPrim s np p
    where
        s _ = "'{'"
        p (OpeningBracket) = Just Nothing
        p _ = Nothing
        np pos _ _  = pos


another example:

psPacketName = tokenPrim s np p
    where
        s x = x
        p (Identifier x) = Just x
        p _ = Nothing
        np pos _ _  = pos


I wonder if it's right to use tokenPrim at all. And if so, is there any way to automatically generate parsers for every token? Maybe Template Haskell?
I've solved it by using Text.ParserCombinators.Parsec.GeneralizedToken from MissingH package:

psOpBracket = tokeng (\x -&gt; case x of TokOpBracket -&gt; Just Nothing ; _ -&gt; Nothing)


I had to use tokenPrim for cases when Token constructor takes arguments, though.