Search code examples
c#.netparsingerror-handlingirony

How to recover parsing error in Irony Parser using C#?


I am currently using Irony parser and I don't seem to find that much documentation yet. This time I want to do something like the error recovery in Bison, where you get the line and row where the error was. I'm not really sure how to get the error information in a grammar like this for example:

NumberLiteral number = new NumberLiteral("number");

NonTerminal S = new NonTerminal("S");
NonTerminal E = new NonTerminal("E");

S -> E;
E -> E + E
    |E - E
    |E / E
    |E * E
    |number;

this.Root = S;
RegisterOperators(1, "+", "-");
RegisterOperators(2, "*", "/");

And when I enter something like "2++" instead of just showing me there's an error, report that there's been an error on line 1, row 2.


Solution

  • I find it much easier to search the Irony clone on github made by Alxandr: https://github.com/Alxandr/Irony/.

    The structure SourceLocation appears to track the Position, Line and Column.

    From what I can see, the Parser.ParsingContext.CurrentParseTree.ParserMessages of type LogMessageList collection contains the list of parser messages and the locations where the error occurred.

    Syntax errors appear to be in the syntax tree in the form of error tokens. These will be tokens with the TokenCategory of Error. The Location property appears to have the location data.