Better error handling + other changes

This commit is contained in:
Astrash
2023-07-29 20:29:16 +10:00
parent 13300861ee
commit 772675639e
11 changed files with 363 additions and 308 deletions

View File

@@ -7,23 +7,80 @@
package structure;
import com.dfsek.terra.addons.terrascript.lexer.LookaheadStream;
import com.dfsek.terra.addons.terrascript.lexer.Char;
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
import org.junit.jupiter.api.Test;
import java.io.StringReader;
import com.dfsek.terra.addons.terrascript.lexer.LookaheadStream;
import static org.junit.jupiter.api.Assertions.*;
public class LookaheadStreamTest {
@Test
public void lookahead() {
LookaheadStream lookahead = new LookaheadStream(new StringReader("Test string..."));
String testString = "Test string...\nNew line";
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());
}
LookaheadStream lookahead = new LookaheadStream(testString);
Char first = new Char('T', new SourcePosition(1, 1));
Char second = new Char('e', new SourcePosition(1, 2));
Char third = new Char('s', new SourcePosition(1, 3));
Char space = new Char(' ', new SourcePosition(1, 5));
Char newline = new Char('\n', new SourcePosition(1, 15));
Char lineTwoColOne = new Char('N', new SourcePosition(2, 1));
String lineTwo = "New line";
assertTrue(lookahead.matchesString("Test", false));
assertTrue(lookahead.matchesString(testString, false));
assertFalse(lookahead.matchesString(testString + "asdf", false));
assertFalse(lookahead.matchesString("Foo", false));
assertEquals(first, lookahead.current());
assertEquals(first, lookahead.current());
assertEquals(new SourcePosition(1, 1), lookahead.getPosition());
assertEquals(new SourcePosition(1, 1), lookahead.getPosition());
assertEquals(second, lookahead.peek());
assertEquals(second, lookahead.peek());
assertEquals(first, lookahead.consume());
assertFalse(lookahead.matchesString(testString, false));
assertEquals(second, lookahead.current());
assertEquals(second, lookahead.consume());
assertEquals(third, lookahead.current());
assertTrue(lookahead.matchesString("st", true));
assertEquals(space, lookahead.current());
assertEquals(space, lookahead.consume());
assertTrue(lookahead.matchesString("string...", false));
assertTrue(lookahead.matchesString("string...", true));
assertFalse(lookahead.matchesString("string...", false));
assertEquals(newline, lookahead.current());
assertEquals(newline, lookahead.consume());
assertEquals(lineTwoColOne, lookahead.current());
assertTrue(lookahead.matchesString(lineTwo, false));
assertFalse(lookahead.matchesString(lineTwo + "asdf", false));
assertTrue(lookahead.matchesString(lineTwo, true));
assertEquals(new SourcePosition(2, 8), lookahead.getPosition());
assertDoesNotThrow(lookahead::consume);
assertEquals(new SourcePosition(2, 8), lookahead.getPosition());
assertDoesNotThrow(lookahead::consume);
assertEquals(new SourcePosition(2, 8), lookahead.getPosition());
}
}