org.snapscript.parse.TokenScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.parse;
import static org.snapscript.parse.TokenType.BINARY;
import static org.snapscript.parse.TokenType.DECIMAL;
import static org.snapscript.parse.TokenType.HEXIDECIMAL;
import static org.snapscript.parse.TokenType.IDENTIFIER;
import static org.snapscript.parse.TokenType.LITERAL;
import static org.snapscript.parse.TokenType.QUALIFIER;
import static org.snapscript.parse.TokenType.SPACE;
import static org.snapscript.parse.TokenType.TEMPLATE;
import static org.snapscript.parse.TokenType.TEXT;
import static org.snapscript.parse.TokenType.TYPE;
import java.util.ArrayList;
import java.util.List;
public class TokenScanner implements TokenLexer {
private TokenIndexer indexer;
private List tokens;
private short[] masks;
private int count;
private int mark;
public TokenScanner(GrammarIndexer indexer, String resource, char[] original, char[] source, short[] lines, short[] types, int count) {
this.indexer = new TokenIndexer(indexer, resource, original, source, lines, types, count);
this.tokens = new ArrayList();
this.count = source.length;
}
@Override
public Token text() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & TEXT.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token template() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & TEMPLATE.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token type() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & TYPE.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token identifier() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & IDENTIFIER.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token qualifier() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & QUALIFIER.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token space() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & SPACE.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token literal(String text) {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & LITERAL.mask) != 0) {
Token token = tokens.get(mark);
Object value = token.getValue();
if (value == text) {
mark++;
return token;
}
}
}
return null;
}
@Override
public Token binary() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & BINARY.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token hexidecimal() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & HEXIDECIMAL.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Token decimal() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
if ((masks[mark] & DECIMAL.mask) != 0) {
return tokens.get(mark++);
}
}
return null;
}
@Override
public Line line(int position) {
int length = tokens.size();
int index = Math.min(length -1 , position);
Token token = tokens.get(index);
if(token != null) {
return token.getLine();
}
return null;
}
@Override
public int peek() {
if (masks == null) {
masks = indexer.index(tokens);
}
if (mark < masks.length) {
return masks[mark];
}
return 0;
}
@Override
public int reset(int position) {
int current = mark;
if (position <= count || position >= 0) {
mark = position;
}
return current;
}
@Override
public int count() {
return tokens.size();
}
@Override
public int mark() {
return mark;
}
}