Every char action for column count in Ragel

153 Views Asked by At

What is the preferred way to implement a column counter in a Ragel finite state machine. If it makes any difference, my main machine is a scanner as defined in chapter 6.3 of the Ragel manual. I'm thinking probably that I just need to be able to execute an action for every character consumed (i.e. incrementing a counter), but if there's a better way to do it, I'd love to know.

1

There are 1 best solutions below

0
John Sensebe On

You can keep track of the position of newlines in the input and get the column using the current position whenever you need to, using something like column = p - lineStart + 1, where lineStart is the position just after the previous newline (or the beginning of the file if you're on the first line).