package bluej.parser.lexer;

import java.util.LinkedList;
import java.util.List;

import bluej.parser.JavaParser;
import bluej.parser.TokenStream;


| This is a token stream processor. It removes whitespace and comment tokens from the | stream, attaching comments as hidden tokens to the next suitable token. | | @author Davin McCall | public final class JavaTokenFilter implements TokenStream{ private TokenStream sourceStream; private LocatableToken lastComment; private LocatableToken cachedToken; private List<LocatableToken> buffer = new LinkedList<LocatableToken>(); private LinkedList<LocatableToken> recent = new LinkedList<>(); private JavaParser parser; public JavaTokenFilter(TokenStream source) { sourceStream = source; lastComment = null; } public JavaTokenFilter(TokenStream source, JavaParser parser) { this(source); this.parser = parser; } public LocatableToken nextToken() { LocatableToken rval; if (! buffer.isEmpty()) { if (cachedToken == null && buffer.size() == 1) { cachedToken = nextToken2(); } rval = buffer.remove(buffer.size() - 1); } else { if (cachedToken == null) { rval = nextToken2(); } else { rval = cachedToken; } cachedToken = nextToken2(); } recent.addLast(rval); if (recent.size() > 30) recent.removeFirst(); return rval; }
| Push a token on to the stream. The token will be returned by the next call | to nextToken(). | public void pushBack(LocatableToken token) { buffer.add(token); if (!recent.isEmpty() && token == recent.getLast()) recent.removeLast(); }
| Gets the most recent token returned by nextToken which has not been | pushed back using pushBack. | public LocatableToken getMostRecent() { return recent.isEmpty() ? null : recent.getLast(); }
| Look ahead a certain number of tokens (without actually consuming them). | @param distance The distance to look ahead (1 or greater). | public LocatableToken LA(int distance) { if (cachedToken != null) { buffer.add(0, (LocatableToken) cachedToken); cachedToken = null; } int numToAdd = distance - buffer.size(); while (numToAdd > 0){ buffer.add(0, nextToken2()); numToAdd--; } return buffer.get(buffer.size() - distance); } private LocatableToken nextToken2() { LocatableToken t = null; while (true){ t = (LocatableToken) sourceStream.nextToken(); int ttype = t.getType(); if (ttype == JavaTokenTypes.ML_COMMENT) { lastComment = t; if (parser != null) { parser.gotComment(t); } } else if (ttype == JavaTokenTypes.SL_COMMENT) { if (parser != null) { parser.gotComment(t); } } else { if (lastComment != null) { t.setHiddenBefore(lastComment); lastComment = null; } break; } } return t; } }

.   - JavaTokenFilter
.   JavaTokenFilter
.   JavaTokenFilter
.   nextToken
.   pushBack
.   getMostRecent
.   LA
.   nextToken2




176 neLoCode + 9 LoComm