From 4d411a3473fc24e4648493effed7b678e1bfd4c4 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Fri, 11 Dec 2020 17:38:31 +0100 Subject: [PATCH] test + exception --- .../parser/grammar/GrammarParseException.java | 8 ++ src/test/java/parser/ASTCompacterTest.java | 86 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/main/java/parser/grammar/GrammarParseException.java create mode 100644 src/test/java/parser/ASTCompacterTest.java diff --git a/src/main/java/parser/grammar/GrammarParseException.java b/src/main/java/parser/grammar/GrammarParseException.java new file mode 100644 index 0000000..1ad92b5 --- /dev/null +++ b/src/main/java/parser/grammar/GrammarParseException.java @@ -0,0 +1,8 @@ +package parser.grammar; + +public class GrammarParseException extends RuntimeException { + + public GrammarParseException(String message) { + super(message); + } +} diff --git a/src/test/java/parser/ASTCompacterTest.java b/src/test/java/parser/ASTCompacterTest.java new file mode 100644 index 0000000..52d72fb --- /dev/null +++ b/src/test/java/parser/ASTCompacterTest.java @@ -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); + } +}