48 lines
No EOL
1.1 KiB
Java
48 lines
No EOL
1.1 KiB
Java
package dev.peerat.parser;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
public interface ElementBuilder{
|
|
|
|
void build(Builder builder) throws Exception;
|
|
|
|
public static class Builder{
|
|
|
|
private List<Token> tokens;
|
|
|
|
public Builder(){
|
|
this.tokens = new LinkedList<>();
|
|
this.tokens.add(new Token(1,1,"", TokenType.GROUP));
|
|
}
|
|
|
|
public void append(Token token){
|
|
if(token == null) throw new NullPointerException();
|
|
this.tokens.add(token);
|
|
}
|
|
|
|
public void append(String value){
|
|
Token last = tokens.get(tokens.size()-1);
|
|
append(new Token(last.getLineNumber(), last.getCharacterNumber()+last.getValue().length(), value, TokenType.GROUP));
|
|
}
|
|
|
|
public void build(BufferedWriter writer) throws Exception{
|
|
int character = 1;
|
|
int line = 1;
|
|
for(Token token : tokens){
|
|
while(line < token.getLineNumber()){
|
|
writer.write("\n");
|
|
line++;
|
|
}
|
|
while(character < token.getCharacterNumber()){
|
|
writer.write(" ");
|
|
character++;
|
|
}
|
|
|
|
writer.write(token.getValue());
|
|
character+=token.getValue().length();
|
|
}
|
|
}
|
|
}
|
|
} |