Antlr3 生成以下输出到System.out
,同时正确执行其他所有操作:
line 0:0 null
line 0:0 null
line 0:0 null
...
它是关于什么的?
附言。问题是在解析过程中我不时通过RecognitionException()
. 看起来null
是这样的结果。而不是null
应该有一些有意义的消息,但是RecognitionException没有这样的构造函数。该怎么办?
grammar Bar;
document: ( CHAR { Foo.validate($CHAR.text); } )+ EOF;
CHAR: 'a'..'z';
Foo
类在同一个包中的某处:
public class Foo {
public static void validate(String txt) throws RecognitionException {
if ("q".equals(txt)) {
throw new RecognitionException();
}
}
}
现在进行单元测试:
public class BarTest {
@Test public void testEverything() throws Exception {
this.parse("abc"); // valid
this.parse("abcq"); // invalid
}
private void parse(String txt) {
CharStream input = new ANTLRStringStream(txt);
BarLexer lexer = new BarLexer(input);
TokenStream tokens = new CommonTokenStream(lexer);
BarParser parser = new BarParser(tokens);
parser.document();
}
}
输出是:
line 0:0 null