views:

39

answers:

1

I'm trying to match my grammar to an entire string, and have it error out if it cannot consume the entire input. Basically, this pseudo regex:

\whitespace*  [a-zA-Z]+  [-]?  [a-zA-Z]+ \whitespace* $

According to this, EOF should work. So, consider this grammar:

start : CHARS EOF
      ;

CHARS : ('a'..'z')+
      ;

If I set input to "hello" in this code:

        var ss = new Antlr.Runtime.ANTLRStringStream(input);
        var lex = new autocompleteLexer(ss);
        var tokens = new CommonTokenStream(lex);
        var parser = new autocompleteParser(tokens);
        return (BaseTree)(parser.start().Tree);

I get an AST with two children, HELLO and EOF, as expected. But if I set the input to say "hello#", it gives back the same tree, and doesn't complain about the hash at all. I'm not sure what I'm doing wrong.

A: 

I found the problem, it was that exceptions were getting trapped by the Lexer instead of propagating into my code. So, by adding this:

class ThrowAllLexer: queryLexer
{
    public ThrowAllLexer(ICharStream input): base(input) { }
    public override void  ReportError(RecognitionException e) {
        throw e;
}

I now get the proper exceptions, instead of having them be swallowed.

d m