David,<br>thank you for your helpful explanations. So I need to define a new datatype encapsulating the two I want<br>to parse and then create a new Stream, is that correct?<br>Reading your comments and more of the haddock documentation of Parsec,<br>
this came to my mind: say I want to skip over some binary parts of the input (which is a PDF, containing ascii and<br>deflated parts as well as images). I found the functions setPosition/getPosition, which seem to<br>build on the notion of line and column number. Is there a way to say that I want to skip<br>
N bytes along the stream? I didn't find an obvious one myself when browsing through the docs.<br><br>Thanks again,<br>Christian<br><br><br><div class="gmail_quote">2010/8/6 David Virebayre <span dir="ltr"><<a href="mailto:dav.vire%2Bhaskell@gmail.com">dav.vire+haskell@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">On Fri, Aug 6, 2010 at 11:03 AM, C Gosch <<a href="mailto:ch.gosch@googlemail.com">ch.gosch@googlemail.com</a>> wrote:<br>
<br>
> You're right, I probably used the wrong words .. I meant that apparently the<br>
> tokens Parsec uses are of type Char, and I would actually at some point<br>
> like to continue parsing, but using different tokens. Sorry if I still got<br>
> it wrong, I'm new :) I can post some code later, as I don't have it here<br>
> right now.<br>
<br>
</div>Parsec (at least version 3) uses any type of token you want.<br>
<br>
Quick example off the top of my head, I didn't check if it compiles:<br>
<br>
-- you have to make lists of your token type an instance of Stream :<br>
<br>
instance Stream [ MyTokenType ] Identity MyTokenType where<br>
uncons [] = return Nothing<br>
uncons (x:xs) = return $ Just (x,xs)<br>
<br>
-- your parser type is going to look like this :<br>
<br>
type MyParser a = ParsecT [MyTokenType] () Identity a<br>
<br>
-- assuming your toke type looks like this<br>
<br>
Data MyTokenType = A Char<br>
| B Word8<br>
deriving (Show)<br>
<br>
<br>
-- you need a basic parser from which you can make more complicated ones<br>
<br>
satisChar :: ( Char -> Bool ) -> MyParser Char<br>
satisChar f = tokenPrim prt pos match<br>
where<br>
prt = show<br>
pos p _l _cs = incSourceLine p 1<br>
match (A c) = if f c then Just c else Nothing<br>
match _ = Nothing<br>
<br>
satisBin :: ( Word8 -> Bool ) -> MyParser Word8<br>
satisBin f = tokenPrim prt pos match<br>
where<br>
prt = show<br>
pos p _l _cs = incSourceLine p 1<br>
match (B w) = if f w then Just w else Nothing<br>
match _ = Nothing<br>
<br>
-- You can define basic parsers like this<br>
<br>
-- parse any letter<br>
letter = satisChar (const True)<br>
<br>
-- parse a specific char, it will return<br>
char c = satisChar (==c)<br>
<br>
-- parse any binary word<br>
binary = satisBin (const True)<br>
<br>
-- parse a specific binary<br>
word w = satisBin (==w)<br>
<br>
-- now you can combine this to make more complicated parsers.<br>
<br>
...<br>
</blockquote></div><br>