add test to try things out
This commit is contained in:
42
src/test/java/parser/GeneralTest.java
Normal file
42
src/test/java/parser/GeneralTest.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package parser;
|
||||||
|
|
||||||
|
import lexer.StupsLexer;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import parser.grammar.Grammar;
|
||||||
|
import util.ast.AST;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
class GeneralTest {
|
||||||
|
|
||||||
|
private Lexer initLexer(String program) {
|
||||||
|
try {
|
||||||
|
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
|
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
|
return new StupsLexer(CharStreams.fromString(programCode));
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
ignore.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testClean() throws URISyntaxException, IOException {
|
||||||
|
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
|
Grammar grammar = Grammar.fromFile(path);
|
||||||
|
LL1Parser parser = LL1Parser.fromGrammar(grammar);
|
||||||
|
|
||||||
|
Lexer lex = this.initLexer("General.stups");
|
||||||
|
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
|
||||||
|
ASTCompacter.clean(tree, grammar);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/test/resources/examplePrograms/General.stups
Normal file
6
src/test/resources/examplePrograms/General.stups
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class MyClass {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int a = (1 + 2) * 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user