mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-25 21:40:08 +00:00
Merge branch 'structure-rewrite' into agnostic
# Conflicts: # common/src/test/java/structure/LookaheadTest.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/Function.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/Parser.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/Char.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/Lookahead.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/Position.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/Token.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/Tokenizer.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/Tokens.java # platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/structure/v2/tokenizer/exceptions/TokenizerException.java
This commit is contained in:
commit
1ce884d1c7
10 files changed
+201
No files matched your search
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package com.dfsek.terra.structure.v2;
|
||||||
|
|
||||||
|
public interface Function {
|
||||||
|
void apply();
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.dfsek.terra.structure.v2;
|
||||||
|
|
||||||
|
public class Parser {
|
||||||
|
}
|
||||||
+35
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream-like data structure that allows viewing future elements without consuming current.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current character without consuming it.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Char current() {
|
||||||
|
return next(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consume and return one character.
|
||||||
|
*
|
||||||
|
* @return Character that was consumed.
|
||||||
|
*/
|
||||||
|
public Char consume() {
|
||||||
|
Char consumed = current();
|
||||||
|
consume(1);
|
||||||
|
return consumed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consume an amount of characters
|
||||||
|
*
|
||||||
|
* @param amount Number of characters to consume
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
package com.dfsek.terra.structure.v2.tokenizer;
|
||||||
|
|
||||||
|
public class Token {
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
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));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package com.dfsek.terra.structure.v2.tokenizer;
|
||||||
|
|
||||||
|
public enum Tokens {
|
||||||
|
FUNCTION, SEPARATOR, ARGUMENT
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
package com.dfsek.terra.structure.v2.tokenizer.exceptions;
|
||||||
|
|
||||||
|
public abstract class TokenizerException extends Exception {
|
||||||
|
}
|
||||||
Reference in new issue
Block a user