I am rewriting this post(same question though, less words).
I rewrote my grammar to just have at the top level this
statement: SELECT EOF!;
#removed all other rules and tokens and still not working
SELECT : ('S'|'s')('E'|'e')('L'|'l')('E'|'e')('C'|'c')('T'|'t');
I then have the following java code(There is no parse method on my Parser like I see on everyone else's posts :( :( )...
@Test
public void testGrammar() throws RecognitionException {
String sql = "select p FROM TABLE p left join p.security s where p.numShares = :shares and s.securityType = :type";
ANTLRStringStream stream = new ANTLRStringStream(sql);
NoSqlLexer lexer = new NoSqlLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
NoSqlParser parser = new NoSqlParser(tokenStream);
try {
CommonTree tree = (CommonTree) parser.statement().getTree();
Assert.fail("should fail parsing");
} catch(Exception e) {
log.info("Exception", e);
}
}
I should get an Exception on parsing but I do not :( :(. This is a very simple grammar with an EOF but it just is not working. Any ideas why? Am I calling the wrong parsing statement?
NOTE: I am trying to make a much bigger problem simpler by cutting the problem down to this simple thing which seems not to work.
full (very small) grammar file is here
grammar NoSql;
options {
output = AST;
language = Java;
}
tokens {
}
@header {
package com.alvazan.orm.parser.antlr;
}
@lexer::header {
package com.alvazan.orm.parser.antlr;
}
@rulecatch {
catch (RecognitionException e)
{
throw e;
}
}
statement: SELECT EOF!;
SELECT : ('S'|'s')('E'|'e')('L'|'l')('E'|'e')('C'|'c')('T'|'t');
WS :
( ' '
| '\t'
| '\r'
| '\n'
)+
{ $channel=HIDDEN; }
;
ESC :
'\\' ('"'|'\''|'\\')
;
thanks, Dean