mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-06 07:46:13 +00:00
Initial terrascript 2 commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package lexer;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Lexer;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token.TokenType;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class LexerTest {
|
||||
|
||||
private static void tokenTypeTest(String input, TokenType type) {
|
||||
Lexer lexer = new Lexer(input);
|
||||
assertEquals(new Token(input, type, new SourcePosition(1, 1)), lexer.current());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeTest() {
|
||||
tokenTypeTest("identifier", TokenType.IDENTIFIER);
|
||||
tokenTypeTest("(", TokenType.OPEN_PAREN);
|
||||
tokenTypeTest(")", TokenType.CLOSE_PAREN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleTokensTest() {
|
||||
Lexer lexer = new Lexer("(3 + 2)");
|
||||
lexer.analyze().forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package lexer;
|
||||
|
||||
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 static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class LookaheadStreamTest {
|
||||
@Test
|
||||
public void lookahead() {
|
||||
String testString = "Test string...\nNew line";
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user