Tokenizer stuff

This commit is contained in:
dfsek
2020-12-10 09:49:46 -07:00
parent 8f58ba17a8
commit 26228d2c71
5 changed files with 39 additions and 1 deletions
@@ -6,6 +6,9 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.List; import java.util.List;
/**
* Stream-like data structure that allows viewing future elements without consuming current.
*/
public class Lookahead { public class Lookahead {
private final List<Char> buffer = new GlueList<>(); private final List<Char> buffer = new GlueList<>();
private final Reader input; private final Reader input;
@@ -17,10 +20,20 @@ public class Lookahead {
this.input = r; this.input = r;
} }
/**
* Get the current character without consuming it.
*
* @return
*/
public Char current() { public Char current() {
return next(0); return next(0);
} }
/**
* Consume and return one character.
*
* @return Character that was consumed.
*/
public Char consume() { public Char consume() {
Char consumed = current(); Char consumed = current();
consume(1); consume(1);
@@ -28,7 +41,7 @@ public class Lookahead {
} }
/** /**
* Fetch and consume the next character. * Fetch the next character.
* *
* @return Next character * @return Next character
*/ */
@@ -69,6 +82,11 @@ public class Lookahead {
} else return buffer.get(ahead); } else return buffer.get(ahead);
} }
/**
* Consume an amount of characters
*
* @param amount Number of characters to consume
*/
public void consume(int amount) { public void consume(int amount) {
if(amount < 0) throw new IllegalArgumentException(); if(amount < 0) throw new IllegalArgumentException();
while(amount-- > 0) { while(amount-- > 0) {
@@ -0,0 +1,11 @@
package com.dfsek.terra.structure.v2.tokenizer;
public class Position {
private final int line;
private final int index;
public Position(int line, int index) {
this.line = line;
this.index = index;
}
}
@@ -0,0 +1,4 @@
package com.dfsek.terra.structure.v2.tokenizer;
public class Token {
}
@@ -7,5 +7,6 @@ public class Tokenizer {
public Tokenizer(String data) { public Tokenizer(String data) {
reader = new Lookahead(new StringReader(data)); reader = new Lookahead(new StringReader(data));
} }
} }
@@ -0,0 +1,4 @@
package com.dfsek.terra.structure.v2.tokenizer.exceptions;
public abstract class TokenizerException extends Exception {
}