
com.github.leeonky.dal.token.TokenStream Maven / Gradle / Ivy
package com.github.leeonky.dal.token;
import com.github.leeonky.dal.SyntaxException;
import com.github.leeonky.dal.ast.BracketNode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.Arrays.asList;
public class TokenStream {
private static final Set UNARY_OPERATORS_WITHOUT_INTENTION = new HashSet<>(asList("!"));
private static final Set UNARY_OPERATORS = new HashSet(asList("-")) {{
addAll(UNARY_OPERATORS_WITHOUT_INTENTION);
}};
private final List tokens = new ArrayList<>();
private int index = 0;
public Token pop() {
return tokens.get(index++);
}
public boolean hasTokens() {
return index < tokens.size();
}
public List allTokens() {
return new ArrayList<>(tokens);
}
public void appendToken(Token token) {
tokens.add(token);
}
public Token.Type currentType() {
return tokens.get(index).getType();
}
public boolean isCurrentSingleEvaluateNode() {
return currentType() == Token.Type.PROPERTY || currentType() == Token.Type.CONST_INDEX;
}
public int getPosition() {
return hasTokens() ? tokens.get(index).getPositionBegin() : (index > 0 ? tokens.get(index - 1).getPositionEnd() : 0);
}
public boolean isCurrentKeywordAndTake(String keyword) {
final Token token = tokens.get(index);
if (token.getType() == Token.Type.KEY_WORD && keyword.equals(token.getValue())) {
index++;
return true;
}
return false;
}
public boolean isCurrentBeginBracket() {
return currentType() == Token.Type.BEGIN_BRACKET;
}
public boolean isCurrentEndBracketAndTakeThenFinishBracket(BracketNode bracketNode) {
if (currentType() == Token.Type.END_BRACKET) {
if (bracketNode == null)
throw new SyntaxException(index, "missed begin bracket");
bracketNode.finishBracket();
index++;
return true;
}
return false;
}
public boolean isSingleUnaryOperator(boolean withoutIntention) {
return currentType() == Token.Type.OPERATOR &&
(withoutIntention ? UNARY_OPERATORS_WITHOUT_INTENTION : UNARY_OPERATORS)
.contains(tokens.get(index).getValue());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy