make stuff final

This commit is contained in:
ChUrl
2020-12-15 16:58:52 +01:00
parent 1738771d1d
commit 4721f80f9c
7 changed files with 85 additions and 86 deletions

View File

@ -37,14 +37,14 @@ public final class StupsCompiler {
try {
// Relativer Pfad
Path programPath = Paths.get(System.getProperty("user.dir") + "/" + filename);
final Path programPath = Paths.get(System.getProperty("user.dir") + "/" + filename);
lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException e) {
try {
// Absoluter Pfad
Path programPath = Paths.get(filename);
final Path programPath = Paths.get(filename);
lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException ee) {
System.out.println("Das Programm konnte nicht gelesen werden.");
@ -52,18 +52,18 @@ public final class StupsCompiler {
}
}
Grammar grammar;
final Grammar grammar;
try {
Path grammarFile = Paths.get(System.getProperty("user.dir") + "/stups.grammar");
final Path grammarFile = Paths.get(System.getProperty("user.dir") + "/stups.grammar");
grammar = Grammar.fromFile(grammarFile);
} catch (IOException e) {
System.out.println("Die Grammatik konnte nicht geöffnet werden.");
return;
}
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
AST tree = stupsParser.parse(lexer.getAllTokens(), lexer.getVocabulary());
final AST tree = stupsParser.parse(lexer.getAllTokens(), lexer.getVocabulary());
tree.postprocess(grammar);
TypeChecker.validate(tree);

View File

@ -29,14 +29,14 @@ public class StupsParser {
}
public static StupsParser fromGrammar(Grammar grammar) {
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
return new StupsParser(analyzer.getTable());
}
public AST parse(List<? extends Token> token, Vocabulary voc) {
ASTNode root = new ASTNode(this.parsetable.getStartSymbol(), 0);
AST tree = new AST(root);
Deque<ASTNode> stack = new ArrayDeque<>();
final ASTNode root = new ASTNode(this.parsetable.getStartSymbol(), 0);
final AST tree = new AST(root);
final Deque<ASTNode> stack = new ArrayDeque<>();
stack.push(root);
int inputPosition = 0;
@ -92,16 +92,16 @@ public class StupsParser {
// Hier wird auch der AST aufgebaut
log("Used: " + top + " -> " + prod);
ASTNode pop = stack.pop();
final ASTNode pop = stack.pop();
final String[] split = prod.split(" ");
for (int i = split.length - 1; i >= 0; i--) {
ASTNode node = new ASTNode(split[i], currentLine);
final ASTNode node = new ASTNode(split[i], currentLine);
if (inputPosition + i < token.size()) {
// Die Schleife geht in der Eingabe weiter
Token currentTok = token.get(inputPosition + i);
final Token currentTok = token.get(inputPosition + i);
// Die Token mit semantischem Inhalt auswählen
if ("IDENTIFIER".equals(split[i]) || split[i].endsWith("_LIT")) {
@ -124,10 +124,10 @@ public class StupsParser {
}
private void printSourceLine(int line, List<? extends Token> token) {
Optional<String> srcLine = token.stream()
.filter(tok -> tok.getLine() == line)
.map(Token::getText)
.reduce((s1, s2) -> s1 + " " + s2);
final Optional<String> srcLine = token.stream()
.filter(tok -> tok.getLine() == line)
.map(Token::getText)
.reduce((s1, s2) -> s1 + " " + s2);
srcLine.ifPresent(s -> System.out.println(" :: " + s));
}

View File

@ -91,11 +91,11 @@ public class GrammarAnalyzer {
// ...and epsilon is in all of first(Y1) ... first(Yi-1).
// Because a != epsilon
Set<String> firstYiNoEps = firstOut.get(split[i]).stream()
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
.collect(Collectors.toSet());
final Set<String> firstYiNoEps = firstOut.get(split[i]).stream()
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
.collect(Collectors.toSet());
boolean changeNow = firstOut.get(leftside).addAll(firstYiNoEps);
final boolean changeNow = firstOut.get(leftside).addAll(firstYiNoEps);
change = change || changeNow;
logIfTrue(changeNow, "First: Added " + firstYiNoEps + " to " + leftside + " (All before are nullable)");
@ -104,7 +104,7 @@ public class GrammarAnalyzer {
if (i == split.length - 1 && allNullable.test(split)) {
// 2. (b) If epsilon is in first(Y1) ... first(Yk), then add epsilon to first(X).
boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
final boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
change = change || changeNow;
logIfTrue(changeNow, "First: Added " + this.grammar.getEpsilonSymbol() + " to " + leftside + " (All are nullable)");
@ -115,7 +115,7 @@ public class GrammarAnalyzer {
if (rightside.equals(this.grammar.getEpsilonSymbol())) {
// 3. If X -> epsilon is a production, then add epsilon to first(X).
boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
final boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
change = change || changeNow;
logIfTrue(changeNow, "First: Added " + this.grammar.getEpsilonSymbol() + " to " + leftside + " (X -> EPS exists)");
@ -177,7 +177,7 @@ public class GrammarAnalyzer {
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
.collect(Collectors.toSet());
boolean changeNow = followOut.get(split[i - 1]).addAll(firstXkNoEps);
final boolean changeNow = followOut.get(split[i - 1]).addAll(firstXkNoEps);
change = change || changeNow;
logIfTrue(changeNow, "Follow: Added " + firstXkNoEps + " to " + split[i - 1] + " (Dazwischen nullable)");
@ -190,7 +190,7 @@ public class GrammarAnalyzer {
if (this.allNullable(sub)) {
boolean changeNow = followOut.get(split[i - 1]).addAll(followOut.get(leftside));
final boolean changeNow = followOut.get(split[i - 1]).addAll(followOut.get(leftside));
change = change || changeNow;
logIfTrue(changeNow, "Follow: Added " + leftside + " to " + split[i - 1] + " (Dahinter nullable)");
@ -200,7 +200,7 @@ public class GrammarAnalyzer {
if (this.grammar.getNonterminals().contains(split[split.length - 1])) {
// 3. (a) If there is a production A -> aB, then everything in follow(A) is in follow(B).
boolean changeNow = followOut.get(split[split.length - 1]).addAll(followOut.get(leftside));
final boolean changeNow = followOut.get(split[split.length - 1]).addAll(followOut.get(leftside));
change = change || changeNow;
logIfTrue(changeNow, "Follow: Added " + followOut.get(leftside) + " to " + split[split.length - 1] + " (Ende der Regel)");
@ -217,7 +217,7 @@ public class GrammarAnalyzer {
}
private ParsingTable initParseTable() {
Map<Map.Entry<String, String>, String> tableOut = new HashMap<>();
final Map<Map.Entry<String, String>, String> tableOut = new HashMap<>();
log("Parsetable Aufstellen:");
@ -231,7 +231,7 @@ public class GrammarAnalyzer {
for (String sym : firstRightside) {
// 1. For each terminal t in first(a), add A -> a to table[A, t]
String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
final String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
log("Add " + rightside + " to cell (" + leftside + ", " + sym + ") (" + sym + " in first of " + rightside + ")");
logNullable("Overwritten " + prev + "!\n", prev);
@ -249,7 +249,7 @@ public class GrammarAnalyzer {
for (String sym : followLeftside) {
// ...for each terminal b in follow(A), add A -> a to table[A, b].
String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
final String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
log("Add " + rightside + " to cell (" + leftside + ", " + sym + ") (" + sym + " in follow of " + leftside + ")");
logNullable("Overwritten " + prev + "!\n", prev);
@ -258,7 +258,7 @@ public class GrammarAnalyzer {
if (followLeftside.contains("$")) {
// If epsilon is in first(a) and $ is in follow(A), add A -> a to table[A, $].
String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, "$"), rightside);
final String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, "$"), rightside);
log("Add " + rightside + " to cell (" + leftside + ", $) (epsilon in first of " + rightside + " and $ in follow of " + leftside + ")");
logNullable("Overwritten " + prev + "!\n", prev);
@ -305,7 +305,7 @@ public class GrammarAnalyzer {
if (this.allNullable(sub)) {
// X1 ... Xi-1 are nullable, so first(X1 ... Xn) contains first(Xi)
Set<String> firstXiNoEps;
final Set<String> firstXiNoEps;
if (split.length == 1 && split[0].equals(this.grammar.getEpsilonSymbol())) {
// Stream collect has to be evaluated, doesn't work on empty stream

View File

@ -2,7 +2,6 @@ package lexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
@ -18,8 +17,8 @@ class LexerTest {
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);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
return new StupsLexer(CharStreams.fromString(programCode));
} catch (Exception ignore) {
ignore.printStackTrace();
@ -36,18 +35,18 @@ class LexerTest {
@Test
void testEmptyFile() {
Lexer lex = this.initLexer("EmptyFile.stups");
final Lexer lex = this.initLexer("EmptyFile.stups");
List<String> token = this.getSymbols(lex);
final List<String> token = this.getSymbols(lex);
assertThat(token).isEmpty();
}
@Test
void testWhitespace() {
Lexer lex = this.initLexer("Whitespace.stups");
final Lexer lex = this.initLexer("Whitespace.stups");
List<String> token = this.getSymbols(lex);
final List<String> token = this.getSymbols(lex);
assertThat(token).containsExactly("IDENTIFIER",
"IDENTIFIER",
@ -57,9 +56,9 @@ class LexerTest {
@Test
void testEmptyMain() {
Lexer lex = this.initLexer("EmptyMain.stups");
final Lexer lex = this.initLexer("EmptyMain.stups");
List<String> token = this.getSymbols(lex);
final List<String> token = this.getSymbols(lex);
assertThat(token).containsExactly("CLASS",
"IDENTIFIER",
@ -81,9 +80,9 @@ class LexerTest {
@Test
void testGeneralWhile() {
Lexer lex = this.initLexer("GeneralWhile.stups");
final Lexer lex = this.initLexer("GeneralWhile.stups");
List<String> token = this.getSymbols(lex);
final List<String> token = this.getSymbols(lex);
assertThat(token).hasSize(68)
.containsSequence("WHILE",
@ -101,9 +100,9 @@ class LexerTest {
@Test
void testGeneralComment() {
Lexer lex = this.initLexer("GeneralComment.stups");
final Lexer lex = this.initLexer("GeneralComment.stups");
List<String> token = this.getSymbols(lex);
final List<String> token = this.getSymbols(lex);
assertThat(token).hasSize(21)
.doesNotContain("WHITESPACE")
@ -124,9 +123,9 @@ class LexerTest {
@Test
void testGeneralIfElse() {
Lexer lex = this.initLexer("GeneralIfElse.stups");
final Lexer lex = this.initLexer("GeneralIfElse.stups");
List<String> token = this.getSymbols(lex);
final List<String> token = this.getSymbols(lex);
assertThat(token).hasSize(96)
.containsSequence("IF",

View File

@ -18,8 +18,8 @@ class LexerStupsParserGrammarTest {
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);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
return new StupsLexer(CharStreams.fromString(programCode));
} catch (Exception ignore) {
ignore.printStackTrace();
@ -30,80 +30,80 @@ class LexerStupsParserGrammarTest {
@Test
void testEmptyFile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyFile.stups");
final Lexer lex = this.initLexer("EmptyFile.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void testEmptyMain() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyMain.stups");
final Lexer lex = this.initLexer("EmptyMain.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void testGeneralComment() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralComment.stups");
final Lexer lex = this.initLexer("GeneralComment.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void tesMultiDecl() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("MultipleDeclarations.stups");
final Lexer lex = this.initLexer("MultipleDeclarations.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void testDeclarationAssignment() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("DeclarationAssignment.stups");
final Lexer lex = this.initLexer("DeclarationAssignment.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void testExpr() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("Expr.stups");
final Lexer lex = this.initLexer("Expr.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void testGeneralWhile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralWhile.stups");
final Lexer lex = this.initLexer("GeneralWhile.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}
@Test
void testGeneralIfElse() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
StupsParser stupsParser = StupsParser.fromGrammar(path);
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
final StupsParser stupsParser = StupsParser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralIfElse.stups");
final Lexer lex = this.initLexer("GeneralIfElse.stups");
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
}

View File

@ -8,9 +8,9 @@ class ASTTest {
@Test
void testOneNode() {
ASTNode root = new ASTNode("Wurzel", 1);
final ASTNode root = new ASTNode("Wurzel", 1);
AST tree = new AST(root);
final AST tree = new AST(root);
System.out.println(tree);
assertThat(tree).hasToString("Wurzel\n");
@ -18,14 +18,14 @@ class ASTTest {
@Test
void testThreeNodesBinary() {
ASTNode root = new ASTNode("Wurzel", 1);
ASTNode childA = new ASTNode("A", 1);
ASTNode childB = new ASTNode("B", 1);
final ASTNode root = new ASTNode("Wurzel", 1);
final ASTNode childA = new ASTNode("A", 1);
final ASTNode childB = new ASTNode("B", 1);
root.addChild(childA);
root.addChild(childB);
AST tree = new AST(root);
final AST tree = new AST(root);
System.out.println(tree);
assertThat(tree).hasToString("Wurzel\n├── A\n└── B\n");
@ -33,14 +33,14 @@ class ASTTest {
@Test
void testThreeNodesLinear() {
ASTNode root = new ASTNode("Wurzel", 1);
ASTNode childA = new ASTNode("A", 1);
ASTNode childB = new ASTNode("B", 1);
final ASTNode root = new ASTNode("Wurzel", 1);
final ASTNode childA = new ASTNode("A", 1);
final ASTNode childB = new ASTNode("B", 1);
root.addChild(childA);
childA.addChild(childB);
AST tree = new AST(root);
final AST tree = new AST(root);
System.out.println(tree);
assertThat(tree).hasToString("Wurzel\n└── A\n └── B\n");

View File

@ -23,9 +23,9 @@ class GrammarTest {
@Test
void testSimpleGrammar0() throws IOException {
Path path = this.getPath("SimpleGrammar0.grammar");
final Path path = this.getPath("SimpleGrammar0.grammar");
Grammar grammar = Grammar.fromFile(path);
final Grammar grammar = Grammar.fromFile(path);
assert grammar != null;
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
@ -39,9 +39,9 @@ class GrammarTest {
@Test
void testSimpleGrammar1() throws IOException {
Path path = this.getPath("SimpleGrammar1.grammar");
final Path path = this.getPath("SimpleGrammar1.grammar");
Grammar grammar = Grammar.fromFile(path);
final Grammar grammar = Grammar.fromFile(path);
assert grammar != null;
System.out.println(grammar.getRules());