test + exception

This commit is contained in:
ChUrl
2020-12-11 17:38:31 +01:00
parent ca9ce5dd45
commit 4d411a3473
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package parser.grammar;
public class GrammarParseException extends RuntimeException {
public GrammarParseException(String message) {
super(message);
}
}

View File

@ -0,0 +1,86 @@
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;
import static org.assertj.core.api.Assertions.assertThat;
class ASTCompacterTest {
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 testRemoveEpsilon() 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("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
assertThat(ASTCompacter.removeEpsilon(tree, grammar)).isEqualTo(2);
System.out.println(tree);
}
@Test
void testCompact() 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("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
assertThat(ASTCompacter.compact(tree, grammar)).isEqualTo(14);
System.out.println(tree);
}
@Test
void testRemoveNullable() 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("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
ASTCompacter.removeEpsilon(tree, grammar);
assertThat(ASTCompacter.removeNullable(tree, grammar)).isEqualTo(2);
System.out.println(tree);
}
@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("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
ASTCompacter.clean(tree, grammar);
assertThat(tree.size()).isEqualTo(33);
}
}