Merge pull request #915 from parrt/master

Small refactoring for flexibility of subclasses (token stream, tree view)
This commit is contained in:
Terence Parr 2015-06-18 10:25:07 -07:00
commit 4c063ee769
3 changed files with 36 additions and 20 deletions

View File

@ -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<Token> subset = new ArrayList<Token>();
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<Token> getTokens(int start, int stop, Set<Integer> 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<Token> 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<Token> 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++) {

View File

@ -85,7 +85,7 @@ public class TreePostScriptGenerator {
doc = new PostScriptDocument(fontName, fontSize);
boolean compareNodeIdentities = true;
this.treeLayout =
new TreeLayout<Tree>(new TreeLayoutAdaptor(root),
new TreeLayout<Tree>(getTreeLayoutAdaptor(root),
new VariableExtentProvide(),
new DefaultConfiguration<Tree>(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<Tree> getTreeLayoutAdaptor(Tree root) {
return new TreeLayoutAdaptor(root);
}
public String getPS() {
// generate the edges and boxes (with text)
generateEdges(getTree().getRoot());

View File

@ -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<Tree>(new TreeLayoutAdaptor(root),
new TreeLayout<Tree>(getTreeLayoutAdaptor(root),
new TreeViewer.VariableExtentProvide(this),
new DefaultConfiguration<Tree>(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<Tree> getTreeLayoutAdaptor(Tree root) {
return new TreeLayoutAdaptor(root);
}
public double getScale() {
return scale;
}