forked from jasder/antlr
alter visitTerminal interface, add visitErrorNode.
This commit is contained in:
parent
528e9c7b86
commit
db1b86af91
|
@ -9,7 +9,7 @@ package org.antlr.v4.runtime;
|
|||
* use Parser.getRuleContext().
|
||||
*/
|
||||
public interface ParseListener<Symbol extends Token> {
|
||||
void visitTerminal(ParserRuleContext<Symbol> ctx, Symbol symbol);
|
||||
void visitTerminal(ParserRuleContext<Symbol> parent, Symbol token);
|
||||
|
||||
/** Enter all but left-recursive rules */
|
||||
void enterNonLRRule(ParserRuleContext<Symbol> ctx);
|
||||
|
|
|
@ -28,11 +28,17 @@
|
|||
*/
|
||||
package org.antlr.v4.runtime;
|
||||
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.atn.ATN;
|
||||
import org.antlr.v4.runtime.atn.ATNSimulator;
|
||||
import org.antlr.v4.runtime.atn.ATNState;
|
||||
import org.antlr.v4.runtime.atn.ParserATNSimulator;
|
||||
import org.antlr.v4.runtime.atn.RuleTransition;
|
||||
import org.antlr.v4.runtime.dfa.DFA;
|
||||
import org.antlr.v4.runtime.misc.*;
|
||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** This is all the parsing support code essentially; most of it is error recovery stuff. */
|
||||
public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>> {
|
||||
|
@ -48,8 +54,10 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
|
|||
}
|
||||
|
||||
@Override
|
||||
public void visitTerminal(ParserRuleContext<Token> ctx, Token token) {
|
||||
System.out.println("consume "+token+" rule "+getRuleNames()[ctx.ruleIndex]+" alt="+ctx.altNum);
|
||||
public void visitTerminal(ParserRuleContext<Token> parent, Token token) {
|
||||
System.out.println("consume "+token+" rule "+
|
||||
getRuleNames()[parent.ruleIndex]+
|
||||
" alt="+parent.altNum);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -295,9 +303,9 @@ public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>
|
|||
// TODO: tree parsers?
|
||||
if ( _errHandler.inErrorRecoveryMode(this) ) {
|
||||
// System.out.println("consume in error recovery mode for "+o);
|
||||
_ctx.addErrorNode((Token) o);
|
||||
_ctx.addErrorNode(o);
|
||||
}
|
||||
else _ctx.addChild((Token)o);
|
||||
else _ctx.addChild(o);
|
||||
}
|
||||
if ( _parseListeners != null) {
|
||||
for (ParseListener<Token> l : _parseListeners) l.visitTerminal(_ctx, o);
|
||||
|
|
|
@ -28,11 +28,17 @@
|
|||
*/
|
||||
package org.antlr.v4.runtime;
|
||||
|
||||
import org.antlr.v4.runtime.atn.*;
|
||||
import org.antlr.v4.runtime.misc.*;
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
import org.antlr.v4.runtime.atn.ATN;
|
||||
import org.antlr.v4.runtime.atn.ATNState;
|
||||
import org.antlr.v4.runtime.misc.NotNull;
|
||||
import org.antlr.v4.runtime.misc.Nullable;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/** A rule invocation record for parsing and tree parsing.
|
||||
*
|
||||
|
@ -173,7 +179,7 @@ public class ParserRuleContext<Symbol extends Token> extends RuleContext {
|
|||
}
|
||||
|
||||
public void addErrorNode(Symbol badToken) {
|
||||
TerminalNodeImpl<Symbol> t = new ErrorNodeImpl<Symbol>(badToken);
|
||||
TerminalNodeImpl<Symbol> t = new ErrorNode<Symbol>(badToken);
|
||||
addChild(t);
|
||||
t.parent = this;
|
||||
}
|
||||
|
|
|
@ -96,8 +96,8 @@ public interface ParseTree extends SyntaxTree {
|
|||
* and deletion as well as during "consume until error recovery set"
|
||||
* upon no viable alternative exceptions.
|
||||
*/
|
||||
public static class ErrorNodeImpl<Symbol extends Token> extends TerminalNodeImpl<Symbol> {
|
||||
public ErrorNodeImpl(Symbol token) {
|
||||
public static class ErrorNode<Symbol extends Token> extends TerminalNodeImpl<Symbol> {
|
||||
public ErrorNode(Symbol token) {
|
||||
super(token);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,8 @@ import org.antlr.v4.runtime.ParserRuleContext;
|
|||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public interface ParseTreeListener<Symbol extends Token> {
|
||||
void visitTerminal(ParserRuleContext<Symbol> ctx, Symbol symbol);
|
||||
void visitTerminal(ParseTree.TerminalNode<Symbol> node);
|
||||
void visitErrorNode(ParseTree.ErrorNode<Symbol> node);
|
||||
void enterEveryRule(ParserRuleContext<Symbol> ctx);
|
||||
void exitEveryRule(ParserRuleContext<Symbol> ctx);
|
||||
}
|
||||
|
|
|
@ -20,19 +20,23 @@ public class ParseTreeVisitor<T> {
|
|||
public T visitChildren(ParserRuleContext<? extends Token> ctx) {
|
||||
T result = null;
|
||||
for (ParseTree c : ctx.children) {
|
||||
if ( c instanceof ParseTree.RuleNode) {
|
||||
if ( c instanceof ParseTree.RuleNode ) {
|
||||
ParseTree.RuleNode r = (ParseTree.RuleNode)c;
|
||||
ParserRuleContext<?> rctx = (ParserRuleContext<? extends Token>)r.getRuleContext();
|
||||
result = visit(rctx);
|
||||
}
|
||||
else {
|
||||
result = visitTerminal(ctx, ((ParseTree.TerminalNode<? extends Token>)c).getSymbol());
|
||||
if ( c instanceof ParseTree.ErrorNode ) {
|
||||
result = visitErrorNode((ParseTree.ErrorNode<? extends Token>)c);
|
||||
}
|
||||
else {
|
||||
result = visitTerminal((ParseTree.TerminalNode<? extends Token>)c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public T visitTerminal(ParserRuleContext<? extends Token> ctx, Token symbol) {
|
||||
return null;
|
||||
}
|
||||
public T visitTerminal(ParseTree.TerminalNode<? extends Token> node) { return null; }
|
||||
public T visitErrorNode(ParseTree.ErrorNode<? extends Token> node) { return null; }
|
||||
}
|
||||
|
|
|
@ -37,8 +37,12 @@ public class ParseTreeWalker {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <Symbol extends Token> void walk(ParseTreeListener<Symbol> listener, ParseTree t) {
|
||||
if ( t instanceof ParseTree.TerminalNode) {
|
||||
visitTerminal(listener, (ParseTree.TerminalNode<Symbol>) t);
|
||||
if ( t instanceof ParseTree.ErrorNode ) {
|
||||
listener.visitErrorNode((ParseTree.ErrorNode<Symbol>)t);
|
||||
return;
|
||||
}
|
||||
else if ( t instanceof ParseTree.TerminalNode) {
|
||||
listener.visitTerminal((ParseTree.TerminalNode<Symbol>)t);
|
||||
return;
|
||||
}
|
||||
ParseTree.RuleNode r = (ParseTree.RuleNode)t;
|
||||
|
@ -50,18 +54,6 @@ public class ParseTreeWalker {
|
|||
exitRule(listener, r);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <Symbol extends Token> void visitTerminal(ParseTreeListener<Symbol> listener,
|
||||
ParseTree.TerminalNode<Symbol> t)
|
||||
{
|
||||
ParseTree.RuleNode r = (ParseTree.RuleNode)t.getParent();
|
||||
ParserRuleContext<Symbol> ctx = null;
|
||||
if ( r != null && r.getRuleContext() instanceof ParserRuleContext<?> ) {
|
||||
ctx = (ParserRuleContext<Symbol>)r.getRuleContext();
|
||||
}
|
||||
listener.visitTerminal(ctx, t.getSymbol());
|
||||
}
|
||||
|
||||
/** The discovery of a rule node, involves sending two events:
|
||||
* the generic discoverRule and a RuleContext-specific event.
|
||||
* First we trigger the generic and then the rule specific.
|
||||
|
|
|
@ -102,7 +102,7 @@ public class Trees {
|
|||
String ruleName = recog.getRuleNames()[ruleIndex];
|
||||
return ruleName;
|
||||
}
|
||||
else if ( t instanceof ParseTree.ErrorNodeImpl ) {
|
||||
else if ( t instanceof ParseTree.ErrorNode) {
|
||||
return t.toString();
|
||||
}
|
||||
else if ( t instanceof ParseTree.TerminalNode) {
|
||||
|
|
|
@ -129,7 +129,7 @@ public class TreePostScriptGenerator {
|
|||
// for debugging, turn this on to see boundingbox of nodes
|
||||
//doc.rect(box.x, box.y, box.width, box.height);
|
||||
// make error nodes from parse tree red by default
|
||||
if ( t instanceof ParseTree.ErrorNodeImpl ) {
|
||||
if ( t instanceof ParseTree.ErrorNode) {
|
||||
doc.highlight(box.x, box.y, box.width, box.height);
|
||||
}
|
||||
double x = box.x+nodeWidthPadding;
|
||||
|
|
|
@ -183,10 +183,10 @@ public class TreeViewer extends JComponent {
|
|||
Rectangle2D.Double box = getBoundsOfNode(tree);
|
||||
// draw the box in the background
|
||||
if ( isHighlighted(tree) || boxColor!=null ||
|
||||
tree instanceof ParseTree.ErrorNodeImpl )
|
||||
tree instanceof ParseTree.ErrorNode)
|
||||
{
|
||||
if ( isHighlighted(tree) ) g.setColor(highlightedBoxColor);
|
||||
else if ( tree instanceof ParseTree.ErrorNodeImpl ) g.setColor(LIGHT_RED);
|
||||
else if ( tree instanceof ParseTree.ErrorNode) g.setColor(LIGHT_RED);
|
||||
else g.setColor(boxColor);
|
||||
g.fillRoundRect((int) box.x, (int) box.y, (int) box.width - 1,
|
||||
(int) box.height - 1, arcSize, arcSize);
|
||||
|
|
|
@ -44,6 +44,7 @@ BaseListenerFile(file, header) ::= <<
|
|||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
public class <file.grammarName>BaseListener implements <file.grammarName>Listener {
|
||||
<file.listenerNames:{lname |
|
||||
|
@ -52,7 +53,8 @@ public class <file.grammarName>BaseListener implements <file.grammarName>Listene
|
|||
|
||||
@Override public void enterEveryRule(ParserRuleContext\<<InputSymbolType()>\> ctx) { }
|
||||
@Override public void exitEveryRule(ParserRuleContext\<<InputSymbolType()>\> ctx) { }
|
||||
@Override public void visitTerminal(ParserRuleContext\<<InputSymbolType()>\> ctx, <InputSymbolType()> symbol) { }
|
||||
@Override public void visitTerminal(ParseTree.TerminalNode\<<InputSymbolType()>\> node) { }
|
||||
@Override public void visitErrorNode(ParseTree.ErrorNode\<<InputSymbolType()>\> node) { }
|
||||
}
|
||||
>>
|
||||
|
||||
|
@ -106,7 +108,6 @@ import org.antlr.v4.runtime.ParserRuleContext;
|
|||
public class <file.grammarName>BaseVisitor\<T> extends ParseTreeVisitor\<T> implements <file.grammarName>Visitor\<T> {
|
||||
<file.visitorNames:{lname |
|
||||
@Override public T visit<lname; format="cap">(<file.parserName>.<lname; format="cap">Context ctx) { return visitChildren(ctx); \}}; separator="\n">
|
||||
@Override public T visitTerminal(ParserRuleContext\<? extends Token> ctx, Token symbol) { return null; }
|
||||
}
|
||||
>>
|
||||
|
||||
|
|
Loading…
Reference in New Issue