Merge pull request #916 from parrt/master

small improvements for trees
This commit is contained in:
Terence Parr 2015-06-18 10:35:52 -07:00
commit fcae921e61
2 changed files with 13 additions and 3 deletions

View File

@ -107,6 +107,7 @@ public class Trees {
* @since 4.5.1
*/
public static String toStringTree(Tree t, TreeTextProvider nodeTextProvider) {
if ( t==null ) return "null";
String s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false);
if ( t.getChildCount()==0 ) return s;
StringBuilder buf = new StringBuilder();
@ -265,8 +266,9 @@ public class Trees {
if ( t instanceof ParserRuleContext ) {
ParserRuleContext r = (ParserRuleContext) t;
if ( startTokenIndex>=r.getStart().getTokenIndex() && // is range fully contained in t?
stopTokenIndex<=r.getStop().getTokenIndex() )
(r.getStop()==null || stopTokenIndex<=r.getStop().getTokenIndex()) )
{
// note: r.getStop()==null likely implies that we bailed out of parser and there's nothing to the right
return r;
}
}

View File

@ -34,6 +34,7 @@ import org.abego.treelayout.NodeExtentProvider;
import org.abego.treelayout.TreeForTreeLayout;
import org.abego.treelayout.TreeLayout;
import org.abego.treelayout.util.DefaultConfiguration;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.GraphicsSupport;
import org.antlr.v4.runtime.misc.JFileChooserConfirmOverwrite;
import org.antlr.v4.runtime.misc.Utils;
@ -196,11 +197,18 @@ public class TreeViewer extends JComponent {
protected void paintBox(Graphics g, Tree tree) {
Rectangle2D.Double box = getBoundsOfNode(tree);
// draw the box in the background
boolean ruleFailedAndMatchedNothing = false;
if ( tree instanceof ParserRuleContext ) {
ParserRuleContext ctx = (ParserRuleContext) tree;
ruleFailedAndMatchedNothing = ctx.exception != null &&
ctx.stop != null && ctx.stop.getTokenIndex() < ctx.start.getTokenIndex();
}
if ( isHighlighted(tree) || boxColor!=null ||
tree instanceof ErrorNode )
tree instanceof ErrorNode ||
ruleFailedAndMatchedNothing)
{
if ( isHighlighted(tree) ) g.setColor(highlightedBoxColor);
else if ( tree instanceof ErrorNode ) g.setColor(LIGHT_RED);
else if ( tree instanceof ErrorNode || ruleFailedAndMatchedNothing ) 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);