Make sure to call visitErrorNode instead of visitTerminal when you have an ErrorNode. Also fix listener calls for case where _buildParseTrees is false (adds terminal as children of the rule nodes but won't actually construct the parse tree).

This commit is contained in:
Sam Harwell 2012-09-08 14:55:10 -05:00
parent 36cdba494f
commit 726fe2c983
1 changed files with 14 additions and 9 deletions

View File

@ -364,18 +364,23 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
public Token consume() {
Token o = getCurrentToken();
getInputStream().consume();
TerminalNode tnode = null;
if (_buildParseTrees) {
boolean hasListener = _parseListeners != null && !_parseListeners.isEmpty();
if (_buildParseTrees || hasListener) {
if ( _errHandler.inErrorRecoveryMode(this) ) {
tnode = _ctx.addErrorNode(o);
ErrorNode<Token> node = _ctx.addErrorNode(o);
if (_parseListeners != null) {
for (ParseTreeListener<Token> listener : _parseListeners) {
listener.visitErrorNode(node);
}
}
}
else {
tnode = _ctx.addChild(o);
}
}
if ( _parseListeners != null) {
for (ParseTreeListener<Token> l : _parseListeners) {
l.visitTerminal(tnode);
TerminalNode<Token> node = _ctx.addChild(o);
if (_parseListeners != null) {
for (ParseTreeListener<Token> listener : _parseListeners) {
listener.visitTerminal(node);
}
}
}
}
return o;