I have the following C# program where I want to write a "Filter Syntax" with chainable ANDs ORs and Brackets:
static void Main(string[] args)
{
try
{
var filter = "OWNER(Peter AND Susan) OR OWNER(Bob AND Alice)";
var input = new AntlrInputStream(filter);
var lexer = new ExampleSyntaxLexer(input);
var tokens = new CommonTokenStream(lexer);
var parser = new ExampleSyntaxParser(tokens);
parser.AddErrorListener(new ErrorListenerParser());
lexer.AddErrorListener(new ErrorListenerLexer());
var rootContext = parser.filter();
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
throw e;
}
}
The Grammar:
grammar ExampleSyntax;
/*
* Parser Rules
*/
filter
:command
|BR_ROUND_OPEN filter BR_ROUND_CLOSE
|filter AND filter
|filter OR filter
;
command
:ELEMENT BR_ROUND_OPEN element BR_ROUND_CLOSE
;
element
:BR_ROUND_OPEN element BR_ROUND_CLOSE
|element AND element
|element OR element
|ELEMENT
;
/*
* Lexer Rules
*/
AND
:[aA][nN][dD]
;
OR
:[oO][rR]
;
BR_ROUND_OPEN
:'('
;
BR_ROUND_CLOSE
:')'
;
ELEMENT
:LETTER+
;
WHITESPACE : (' '|'\t')+ -> skip ;
NEWLINE : ('\r'? '\n' | '\r')+ ;
fragment LETTER:[a-zA-ZäÄöÖüÜß]+;
In theory this works very well, the parts are recognized nicely and lots of errors I will find with the 2 error listeners. However, some errors will go through:
var filter = "OWNER(Peter AND Susan))))) OR OWNER(Bob AND Alice)";
and
var filter = "OWNER(Peter AND Susan) ORT OWNER(Bob AND Alice)";
In both cases, ANTLR will recognie everything until the "Error" (in the first part the brackets that are too much and in the second the word ORT) and generate the normal tree for that part but will simply ignore the rest. How can I find such errors? I assume these are not Lexer Errors, as the Lexes are actually defined, but should be some Parser error?