Working parser/tokenizer

This commit is contained in:
dfsek
2020-12-19 20:04:58 -07:00
parent 29e2746e72
commit f970838ecf
21 changed files with 263 additions and 161 deletions

View 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;
}
}
}

View File

@@ -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);
}
}