new test + testprograms

This commit is contained in:
ChUrl
2020-12-13 14:49:02 +01:00
parent 36241a2699
commit 1adfc5246b
4 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,86 @@
package parser.typechecker;
import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Test;
import parser.LL1Parser;
import parser.ast.AST;
import parser.grammar.Grammar;
import typechecker.SymbolAlreadyDefinedException;
import typechecker.SymbolTable;
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;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class SymbolTableTest {
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 testSingleSymbol() 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("SingleSymbol.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
tree.preprocess(grammar);
SymbolTable table = SymbolTable.fromAST(tree);
assertThat(table.getSymbolType("i")).isEqualTo("INTEGER_TYPE");
assertThat(table.getSymbolCount()).isEqualTo(1);
}
@Test
void testMultipleSymbol() 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("MultipleSymbol.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
tree.preprocess(grammar);
SymbolTable table = SymbolTable.fromAST(tree);
assertThat(table.getSymbolType("i")).isEqualTo("INTEGER_TYPE");
assertThat(table.getSymbolType("ii")).isEqualTo("INTEGER_TYPE");
assertThat(table.getSymbolType("b")).isEqualTo("BOOLEAN_TYPE");
assertThat(table.getSymbolType("bb")).isEqualTo("BOOLEAN_TYPE");
assertThat(table.getSymbolType("s")).isEqualTo("STRING_TYPE");
assertThat(table.getSymbolType("ss")).isEqualTo("STRING_TYPE");
assertThat(table.getSymbolCount()).isEqualTo(6);
}
@Test
void testExistingSymbol() 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("ExistingSymbol.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
tree.preprocess(grammar);
assertThatThrownBy(() -> SymbolTable.fromAST(tree)).isInstanceOf(SymbolAlreadyDefinedException.class);
}
}

View File

@ -0,0 +1,6 @@
class SingleSymbol {
public static void main(String[] args) {
int i = 0;
String i = "Hi";
}
}

View File

@ -0,0 +1,10 @@
class SingleSymbol {
public static void main(String[] args) {
int i = 0;
int ii = 0;
boolean b = true;
boolean bb = true;
String s = "Hi";
String ss = "Hi";
}
}

View File

@ -0,0 +1,5 @@
class SingleSymbol {
public static void main(String[] args) {
int i = 0;
}
}