mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-04 06:46:21 +00:00
Working parser/tokenizer
This commit is contained in:
73
common/src/test/java/structure/ParserTest.java
Normal file
73
common/src/test/java/structure/ParserTest.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package structure;
|
||||
|
||||
import com.dfsek.terra.api.math.vector.Location;
|
||||
import com.dfsek.terra.api.platform.world.Chunk;
|
||||
import com.dfsek.terra.api.structures.parser.Argument;
|
||||
import com.dfsek.terra.api.structures.parser.Function;
|
||||
import com.dfsek.terra.api.structures.parser.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.parser.Parser;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ParserTest {
|
||||
@Test
|
||||
public void parse() throws IOException, ParseException {
|
||||
Parser parser = new Parser(IOUtils.toString(getClass().getResourceAsStream("/test.tesf")));
|
||||
|
||||
parser.addFunction("test", new FunctionBuilder<Test1>() {
|
||||
@Override
|
||||
public Test1 build(List<String> argumentList) throws ParseException {
|
||||
return new Test1(argumentList.get(0), Double.parseDouble(argumentList.get(1)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<?>> getArguments() {
|
||||
return Arrays.asList(id -> id, Double::parseDouble);
|
||||
}
|
||||
});
|
||||
|
||||
List<Function> functions = parser.parse();
|
||||
|
||||
for(Function f : functions) {
|
||||
System.out.println(f);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Test1 implements Function {
|
||||
private final String a;
|
||||
private final double b;
|
||||
|
||||
public Test1(String a, double b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public String getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public double getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Location location) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Location location, Chunk chunk) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,15 @@ public class TokenizerTest {
|
||||
@Test
|
||||
public void tokens() throws IOException, TokenizerException {
|
||||
Tokenizer tokenizer = new Tokenizer(IOUtils.toString(getClass().getResourceAsStream("/test.tesf")));
|
||||
// Actual run
|
||||
long l = System.nanoTime();
|
||||
|
||||
for(int i = 0; i < 100; i++) {
|
||||
|
||||
while(tokenizer.hasNext()) {
|
||||
Token t = tokenizer.fetch();
|
||||
if(t == null) break;
|
||||
System.out.println(t);
|
||||
|
||||
}
|
||||
|
||||
System.out.println((double) (System.nanoTime() - l) / 1000000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user