71 lines
1.3 KiB
Java
71 lines
1.3 KiB
Java
package dev.peerat.parser;
|
|
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.Function;
|
|
|
|
public class TokenValidator{
|
|
|
|
private Token[] elements;
|
|
private int validated;
|
|
|
|
private Bag bag;
|
|
|
|
private TokenValidator(Bag bag){
|
|
this.bag = bag;
|
|
}
|
|
|
|
public TokenValidator(Token[] elements){
|
|
this.elements = elements;
|
|
this.bag = new Bag();
|
|
}
|
|
|
|
public int getTokenCount(){
|
|
return this.elements.length;
|
|
}
|
|
|
|
public int getValidatedTokenCount(){
|
|
return this.validated;
|
|
}
|
|
|
|
public boolean hasNext(){
|
|
return validated < elements.length;
|
|
}
|
|
|
|
public boolean validate(Function<Token, Boolean> action){
|
|
if(validated >= this.elements.length) return false;
|
|
if(action.apply(this.elements[validated])){
|
|
this.validated++;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean validate(Function<Token, Boolean> action, BiConsumer<Bag, Token> filler){
|
|
if(validate(action)){
|
|
filler.accept(bag, elements[validated-1]);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public TokenValidator branch(){
|
|
TokenValidator branch = new TokenValidator(bag);
|
|
branch.elements = this.elements;
|
|
branch.validated = this.validated;
|
|
return branch;
|
|
}
|
|
|
|
public void merge(TokenValidator branch){
|
|
this.validated = branch.validated;
|
|
this.bag = branch.bag;
|
|
}
|
|
|
|
public void setBag(Bag bag){
|
|
this.bag = bag;
|
|
}
|
|
|
|
public Bag getBag(){
|
|
return this.bag;
|
|
}
|
|
|
|
}
|