[Haskell-cafe] Optional EOF in Parsec.
Martijn van Steenbergen
martijn at van.steenbergen.nl
Sat Apr 4 05:58:48 EDT 2009
Kannan Goundan wrote:
> I've implemented this functionality in a hand-written parser (basically a hack
> that keeps track of whether the last read token was preceded by an EOL,
> without making EOL itself a token). Does anybody have ideas about how to
> do this with Parsec?
You can do exactly the same with Parsec:
* create a lexer that yields a [Token], including EOL tokens;
* write a function of type [Token] -> [(Token, Bool)] that discards EOLs
and tells for each token whether it was preceded by a (now discarded) EOL;
* write your pToken :: Token -> Parsec Token function (I omitted some
type variables there) that recognises one (Token, Bool)-tuple from the
input stream.
Or, perhaps easier:
* create a lexer that yields a [Token], including EOL tokens;
* write a function of type [Token] -> [Token] that discards only those
EOL tokens that aren't needed -- for example, those EOL tokens that
occur when there are no open ['s, then parse those EOL's explicitly in
your parser.
Hope this helps,
Martijn.
More information about the Haskell-Cafe
mailing list