[Haskell-cafe] parsec: how to get end location
Martijn van Steenbergen
martijn at van.steenbergen.nl
Mon Jun 14 10:15:28 EDT 2010
On 6/14/10 0:10, Roman Cheplyaka wrote:
> Of course most parsers don't consume trailing newlines. But I was
> writing general function to use in many places in the code which would
> recover the end location. In most cases it just subtracts 1 from the
> column number, but what if it just happened so that column number is 1?
Parsers can be composed of lots of functions, but eventually all the
actual consumption of symbols boils down to calls to
Text.Parsec.Prim.tokenPrimEx. This is where you need to address your
problem: find the places in your code where this function is called (or
the derived tokenPrim or token) and intercept there. Hopefully you have
defined your own 'satisfy' function and need only change that one.
Once you've found those places, there's multiple ways to solve the
problem. For example, you could keep track of the last interesting
(=non-whitespace) position. Or you could parse the whitespace in a
There is no way to retroactively intercept these calls, which I think is
a flaw in the design of Parsec. It would have been nice to have a
'runParsecWith myTokenPrimEx', or better yet capture the Parsec
primitives in a type class.
More information about the Haskell-Cafe