Begin work on tokenizer

This commit is contained in:
dfsek
2020-12-10 01:26:39 -07:00
parent 75f39640b0
commit 8f58ba17a8
7 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
package com.dfsek.terra.structure.v2;
public interface Function {
void apply();
}

View File

@@ -0,0 +1,4 @@
package com.dfsek.terra.structure.v2;
public class Parser {
}

View File

@@ -0,0 +1,35 @@
package com.dfsek.terra.structure.v2.tokenizer;
public class Char {
private final char character;
private final int index;
private final int line;
public Char(char character, int index, int line) {
this.character = character;
this.index = index;
this.line = line;
}
public char getCharacter() {
return character;
}
public int getIndex() {
return index;
}
public int getLine() {
return line;
}
public boolean is(char... tests) {
for(char test : tests) {
if(test == character && test != '\0') {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,83 @@
package com.dfsek.terra.structure.v2.tokenizer;
import org.polydev.gaea.util.GlueList;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class Lookahead {
private final List<Char> buffer = new GlueList<>();
private final Reader input;
private int index = 0;
private int line = 0;
private boolean end = false;
public Lookahead(Reader r) {
this.input = r;
}
public Char current() {
return next(0);
}
public Char consume() {
Char consumed = current();
consume(1);
return consumed;
}
/**
* Fetch and consume the next character.
*
* @return Next character
*/
private Char fetch() {
try {
int c = input.read();
if(c == -1) return null;
if(c == '\n') {
line++;
index = 0;
}
index++;
return new Char((char) c, line, index);
} catch(IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Fetch a future character without consuming it.
*
* @param ahead Distance ahead to peek
* @return Character
*/
public Char next(int ahead) {
if(ahead < 0) throw new IllegalArgumentException();
while(buffer.size() <= ahead && !end) {
Char item = fetch();
if(item != null) {
buffer.add(item);
} else end = true;
}
if(ahead >= buffer.size()) {
return null;
} else return buffer.get(ahead);
}
public void consume(int amount) {
if(amount < 0) throw new IllegalArgumentException();
while(amount-- > 0) {
if(!buffer.isEmpty()) buffer.remove(0); // Remove top item from buffer.
else {
if(end) return;
Char item = fetch();
if(item == null) end = true;
}
}
}
}

View File

@@ -0,0 +1,11 @@
package com.dfsek.terra.structure.v2.tokenizer;
import java.io.StringReader;
public class Tokenizer {
private final Lookahead reader;
public Tokenizer(String data) {
reader = new Lookahead(new StringReader(data));
}
}

View File

@@ -0,0 +1,5 @@
package com.dfsek.terra.structure.v2.tokenizer;
public enum Tokens {
FUNCTION, SEPARATOR, ARGUMENT
}

View File

@@ -0,0 +1,20 @@
package structure;
import com.dfsek.terra.structure.v2.tokenizer.Lookahead;
import org.junit.jupiter.api.Test;
import java.io.StringReader;
public class LookaheadTest {
@Test
public void lookahead() {
Lookahead lookahead = new Lookahead(new StringReader("Test string..."));
for(int i = 0; lookahead.next(i) != null; i++) {
System.out.print(lookahead.next(i).getCharacter());
}
while(lookahead.next(0) != null) {
System.out.print(lookahead.consume().getCharacter());
}
}
}