From 0e692ed436c33686973242464641c8bf829468e0 Mon Sep 17 00:00:00 2001 From: parrt Date: Wed, 10 Jun 2015 09:41:52 -0700 Subject: [PATCH 1/2] allow users to override the TreeLayoutAdaptor --- .../runtime/tree/gui/TreePostScriptGenerator.java | 8 +++++++- .../org/antlr/v4/runtime/tree/gui/TreeViewer.java | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java index df747ef44..fc7e83e25 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreePostScriptGenerator.java @@ -85,7 +85,7 @@ public class TreePostScriptGenerator { doc = new PostScriptDocument(fontName, fontSize); boolean compareNodeIdentities = true; this.treeLayout = - new TreeLayout(new TreeLayoutAdaptor(root), + new TreeLayout(getTreeLayoutAdaptor(root), new VariableExtentProvide(), new DefaultConfiguration(gapBetweenLevels, gapBetweenNodes, @@ -93,6 +93,12 @@ public class TreePostScriptGenerator { compareNodeIdentities); } + /** Get an adaptor for root that indicates how to walk ANTLR trees. + * Override to change the adapter from the default of {@link TreeLayoutAdaptor} */ + public TreeForTreeLayout getTreeLayoutAdaptor(Tree root) { + return new TreeLayoutAdaptor(root); + } + public String getPS() { // generate the edges and boxes (with text) generateEdges(getTree().getRoot()); diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java index 106a305f2..3b686b96e 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/gui/TreeViewer.java @@ -53,7 +53,11 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; import java.awt.*; -import java.awt.event.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; import java.awt.geom.CubicCurve2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; @@ -683,7 +687,7 @@ public class TreeViewer extends JComponent { if ( root!=null ) { boolean useIdentity = true; // compare node identity this.treeLayout = - new TreeLayout(new TreeLayoutAdaptor(root), + new TreeLayout(getTreeLayoutAdaptor(root), new TreeViewer.VariableExtentProvide(this), new DefaultConfiguration(gapBetweenLevels, gapBetweenNodes), @@ -697,6 +701,12 @@ public class TreeViewer extends JComponent { } } + /** Get an adaptor for root that indicates how to walk ANTLR trees. + * Override to change the adapter from the default of {@link TreeLayoutAdaptor} */ + public TreeForTreeLayout getTreeLayoutAdaptor(Tree root) { + return new TreeLayoutAdaptor(root); + } + public double getScale() { return scale; } From dc150f4aa27944c526e27b9ec51134b966a67865 Mon Sep 17 00:00:00 2001 From: parrt Date: Tue, 9 Jun 2015 13:55:29 -0700 Subject: [PATCH 2/2] refactor tokens.size() to be size() --- .../antlr/v4/runtime/BufferedTokenStream.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java b/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java index ba1417c68..914a6d099 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java +++ b/runtime/Java/src/org/antlr/v4/runtime/BufferedTokenStream.java @@ -133,11 +133,11 @@ public class BufferedTokenStream implements TokenStream { if (fetchedEOF) { // the last token in tokens is EOF. skip check if p indexes any // fetched token except the last. - skipEofCheck = p < tokens.size() - 1; + skipEofCheck = p < size() - 1; } else { // no EOF token in tokens. skip check if p indexes a fetched token. - skipEofCheck = p < tokens.size(); + skipEofCheck = p < size(); } } else { @@ -162,7 +162,7 @@ public class BufferedTokenStream implements TokenStream { */ protected boolean sync(int i) { assert i >= 0; - int n = i - tokens.size() + 1; // how many more elements we need? + int n = i - size() + 1; // how many more elements we need? //System.out.println("sync("+i+") needs "+n); if ( n > 0 ) { int fetched = fetch(n); @@ -184,7 +184,7 @@ public class BufferedTokenStream implements TokenStream { for (int i = 0; i < n; i++) { Token t = tokenSource.nextToken(); if ( t instanceof WritableToken ) { - ((WritableToken)t).setTokenIndex(tokens.size()); + ((WritableToken)t).setTokenIndex(size()); } tokens.add(t); if ( t.getType()==Token.EOF ) { @@ -198,8 +198,8 @@ public class BufferedTokenStream implements TokenStream { @Override public Token get(int i) { - if ( i < 0 || i >= tokens.size() ) { - throw new IndexOutOfBoundsException("token index "+i+" out of range 0.."+(tokens.size()-1)); + if ( i < 0 || i >= size() ) { + throw new IndexOutOfBoundsException("token index "+i+" out of range 0.."+(size()-1)); } return tokens.get(i); } @@ -209,7 +209,7 @@ public class BufferedTokenStream implements TokenStream { if ( start<0 || stop<0 ) return null; lazyInit(); List subset = new ArrayList(); - if ( stop>=tokens.size() ) stop = tokens.size()-1; + if ( stop>=size() ) stop = size()-1; for (int i = start; i <= stop; i++) { Token t = tokens.get(i); if ( t.getType()==Token.EOF ) break; @@ -235,9 +235,9 @@ public class BufferedTokenStream implements TokenStream { int i = p + k - 1; sync(i); - if ( i >= tokens.size() ) { // return EOF token + if ( i >= size() ) { // return EOF token // EOF must be last token - return tokens.get(tokens.size()-1); + return tokens.get(size()-1); } // if ( i>range ) range = i; return tokens.get(i); @@ -290,11 +290,11 @@ public class BufferedTokenStream implements TokenStream { */ public List getTokens(int start, int stop, Set types) { lazyInit(); - if ( start<0 || stop>=tokens.size() || - stop<0 || start>=tokens.size() ) + if ( start<0 || stop>=size() || + stop<0 || start>=size() ) { throw new IndexOutOfBoundsException("start "+start+" or stop "+stop+ - " not in 0.."+(tokens.size()-1)); + " not in 0.."+(size()-1)); } if ( start>stop ) return null; @@ -379,8 +379,8 @@ public class BufferedTokenStream implements TokenStream { */ public List getHiddenTokensToRight(int tokenIndex, int channel) { lazyInit(); - if ( tokenIndex<0 || tokenIndex>=tokens.size() ) { - throw new IndexOutOfBoundsException(tokenIndex+" not in 0.."+(tokens.size()-1)); + if ( tokenIndex<0 || tokenIndex>=size() ) { + throw new IndexOutOfBoundsException(tokenIndex+" not in 0.."+(size()-1)); } int nextOnChannel = @@ -408,8 +408,8 @@ public class BufferedTokenStream implements TokenStream { */ public List getHiddenTokensToLeft(int tokenIndex, int channel) { lazyInit(); - if ( tokenIndex<0 || tokenIndex>=tokens.size() ) { - throw new IndexOutOfBoundsException(tokenIndex+" not in 0.."+(tokens.size()-1)); + if ( tokenIndex<0 || tokenIndex>=size() ) { + throw new IndexOutOfBoundsException(tokenIndex+" not in 0.."+(size()-1)); } if (tokenIndex == 0) { @@ -468,7 +468,7 @@ public class BufferedTokenStream implements TokenStream { int stop = interval.b; if ( start<0 || stop<0 ) return ""; lazyInit(); - if ( stop>=tokens.size() ) stop = tokens.size()-1; + if ( stop>=size() ) stop = size()-1; StringBuilder buf = new StringBuilder(); for (int i = start; i <= stop; i++) {