Printing Expected Token Type XXX when a parsing error occurs

243 Views Asked by At

I would like to be able to print this error message using Ragel => Parsing error found at position line:col, Integer expected instead.

Is that possible with Ragel?

Best regards

1

There are 1 best solutions below

0
On

I haven't gotten too far into error handling in Ragel just yet, but I would expect that if you use the error action embedding operators as specified in section 3.2.3 of the Ragel 6.9 Guide, that would override the default message.

You can get the line number by incrementing a counter at each newline, and get the column by taking the current position and subtracting the position of the previous newline, something like this:

newline = '\n' %{ ++lineCounter; linePosition = p; }

action ErrorHandler { 
    column = p - linePosition + 1;
    // Print error message here using lineCounter and column
}

main := (allsortsofstuff | newline)* <>err(ErrorHandler);

Of course, the above may require a bit of tweaking based on exactly what you're doing, but at least it's a starting point.