org.unlaxer.TokenList Maven / Gradle / Ivy
package org.unlaxer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.unlaxer.Source.SourceKind;
public class TokenList implements List{
List tokens;
public TokenList(List tokens) {
super();
this.tokens = new ArrayList<>(tokens);
}
public TokenList(Token... tokens) {
super();
this.tokens = new ArrayList<>();
for (Token token : tokens) {
this.tokens.add(token);
}
}
public TokenList() {
super();
this.tokens = new ArrayList<>();
}
public static TokenList of(List tokens) {
return new TokenList(tokens);
}
public static TokenList of(Token... tokens) {
return new TokenList(tokens);
}
public void forEach(Consumer super Token> action) {
tokens.forEach(action);
}
public int size() {
return tokens.size();
}
public boolean isEmpty() {
return tokens.isEmpty();
}
public boolean contains(Object o) {
return tokens.contains(o);
}
public Iterator iterator() {
return tokens.iterator();
}
public Object[] toArray() {
return tokens.toArray();
}
public T[] toArray(T[] a) {
return tokens.toArray(a);
}
public boolean add(Token e) {
return tokens.add(e);
}
public boolean remove(Object o) {
return tokens.remove(o);
}
public boolean containsAll(Collection> c) {
return tokens.containsAll(c);
}
public boolean addAll(Collection extends Token> c) {
return tokens.addAll(c);
}
public boolean addAll(int index, Collection extends Token> c) {
return tokens.addAll(index, c);
}
public boolean removeAll(Collection> c) {
return tokens.removeAll(c);
}
public T[] toArray(IntFunction generator) {
return tokens.toArray(generator);
}
public boolean retainAll(Collection> c) {
return tokens.retainAll(c);
}
public void replaceAll(UnaryOperator operator) {
tokens.replaceAll(operator);
}
public void sort(Comparator super Token> c) {
tokens.sort(c);
}
public void clear() {
tokens.clear();
}
public boolean equals(Object o) {
return tokens.equals(o);
}
public int hashCode() {
return tokens.hashCode();
}
public Token get(TokenIndex index) {
return tokens.get(index.value());
}
public Token get(int index) {
return tokens.get(index);
}
public boolean removeIf(Predicate super Token> filter) {
return tokens.removeIf(filter);
}
public Token set(int index, Token element) {
return tokens.set(index, element);
}
public Token set(TokenIndex index, Token element) {
return tokens.set(index.value(), element);
}
public void add(int index, Token element) {
tokens.add(index, element);
}
public void add(TokenIndex index, Token element) {
tokens.add(index.value(), element);
}
public Token remove(int index) {
return tokens.remove(index);
}
public Token remove(TokenIndex index) {
return tokens.remove(index.value());
}
public int indexOf(Object o) {
return tokens.indexOf(o);
}
public int lastIndexOf(Object o) {
return tokens.lastIndexOf(o);
}
public ListIterator listIterator() {
return tokens.listIterator();
}
public ListIterator listIterator(int index) {
return tokens.listIterator(index);
}
public ListIterator listIterator(TokenIndex index) {
return tokens.listIterator(index.value());
}
public List subList(int fromIndex, int toIndex) {
return tokens.subList(fromIndex, toIndex);
}
public List subList(TokenIndex fromIndexInclusive , TokenIndex toIndexExclusive) {
return tokens.subList(fromIndexInclusive.value(), toIndexExclusive.value());
}
public Spliterator spliterator() {
return tokens.spliterator();
}
public Stream stream() {
return tokens.stream();
}
public Stream parallelStream() {
return tokens.parallelStream();
}
public CursorRange combinedCursorRange() {
return combinedCursorRange(this);
}
public static CursorRange combinedCursorRange(TokenList tokens) {
return combinedCursorRange(tokens.tokens);
}
public static CursorRange combinedCursorRange(List tokens) {
if(tokens.isEmpty()) {
return new CursorRange(new StartInclusiveCursorImpl(), new EndExclusiveCursorImpl().incrementPosition());
}
CursorRange first = tokens.get(0).source.cursorRange();
CursorRange last = tokens.get(tokens.size()-1).source.cursorRange();
return new CursorRange(
new StartInclusiveCursorImpl()
.setLineNumber(first.startIndexInclusive.getLineNumber())
.setPositionInLine(first.startIndexInclusive.getPositionInLine())
.setPosition(first.startIndexInclusive.getPosition()),
new EndExclusiveCursorImpl()
.setLineNumber(last.endIndexExclusive.getLineNumber())
.setPositionInLine(last.endIndexExclusive.getPositionInLine())
.setPosition(last.endIndexExclusive.getPosition())
);
}
public Source toSource(SourceKind sourceKind) {
return toSource(tokens , sourceKind);
}
public static Source toSource(TokenList tokens, SourceKind sourceKind) {
return toSource(tokens.tokens , sourceKind);
}
public static Source toSource(List tokens , SourceKind sourceKind) {
if(tokens.isEmpty()) {
return new StringSource("",sourceKind,new CodePointOffset(0));
}
Token firstToken = tokens.get(0);
Source root = firstToken.source.root();
String collect = tokens.stream()
.map(Token::getSource)
.map(Source::toString)
.collect(Collectors.joining());
Source source = firstToken.getSource();
CodePointOffset offsetFromRoot = source.offsetFromRoot();
if(sourceKind == SourceKind.subSource) {
return new StringSource(root , collect , offsetFromRoot);
}else {
return StringSource.create(collect, sourceKind );
}
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy