43 lines
861 B
Java
43 lines
861 B
Java
package frontend.lexer;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class TokenStream {
|
|
private ArrayList<Token> tokens;
|
|
private int currentIndex;
|
|
|
|
public TokenStream(ArrayList<Token> tokens) {
|
|
this.tokens = tokens;
|
|
}
|
|
|
|
public Token read() {
|
|
if (currentIndex < tokens.size()) {
|
|
return tokens.get(currentIndex++);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void recall() {
|
|
if (currentIndex > 0) {
|
|
currentIndex--;
|
|
}
|
|
}
|
|
|
|
public int getCurrentIndex() {
|
|
return currentIndex;
|
|
}
|
|
|
|
public void resetIndex(int index) {
|
|
currentIndex = index;
|
|
}
|
|
|
|
public Token peek(int step) {
|
|
if (currentIndex + step < tokens.size()) {
|
|
return tokens.get(currentIndex + step);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|