make stuff final
This commit is contained in:
@ -37,14 +37,14 @@ public final class StupsCompiler {
|
|||||||
try {
|
try {
|
||||||
// Relativer Pfad
|
// 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));
|
lexer = new StupsLexer(CharStreams.fromPath(programPath));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Absoluter Pfad
|
// Absoluter Pfad
|
||||||
|
|
||||||
Path programPath = Paths.get(filename);
|
final Path programPath = Paths.get(filename);
|
||||||
lexer = new StupsLexer(CharStreams.fromPath(programPath));
|
lexer = new StupsLexer(CharStreams.fromPath(programPath));
|
||||||
} catch (IOException ee) {
|
} catch (IOException ee) {
|
||||||
System.out.println("Das Programm konnte nicht gelesen werden.");
|
System.out.println("Das Programm konnte nicht gelesen werden.");
|
||||||
@ -52,18 +52,18 @@ public final class StupsCompiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Grammar grammar;
|
final Grammar grammar;
|
||||||
try {
|
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);
|
grammar = Grammar.fromFile(grammarFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Die Grammatik konnte nicht geöffnet werden.");
|
System.out.println("Die Grammatik konnte nicht geöffnet werden.");
|
||||||
return;
|
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);
|
tree.postprocess(grammar);
|
||||||
TypeChecker.validate(tree);
|
TypeChecker.validate(tree);
|
||||||
|
|
||||||
|
|||||||
@ -29,14 +29,14 @@ public class StupsParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static StupsParser fromGrammar(Grammar grammar) {
|
public static StupsParser fromGrammar(Grammar grammar) {
|
||||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
|
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
|
||||||
return new StupsParser(analyzer.getTable());
|
return new StupsParser(analyzer.getTable());
|
||||||
}
|
}
|
||||||
|
|
||||||
public AST parse(List<? extends Token> token, Vocabulary voc) {
|
public AST parse(List<? extends Token> token, Vocabulary voc) {
|
||||||
ASTNode root = new ASTNode(this.parsetable.getStartSymbol(), 0);
|
final ASTNode root = new ASTNode(this.parsetable.getStartSymbol(), 0);
|
||||||
AST tree = new AST(root);
|
final AST tree = new AST(root);
|
||||||
Deque<ASTNode> stack = new ArrayDeque<>();
|
final Deque<ASTNode> stack = new ArrayDeque<>();
|
||||||
stack.push(root);
|
stack.push(root);
|
||||||
|
|
||||||
int inputPosition = 0;
|
int inputPosition = 0;
|
||||||
@ -92,16 +92,16 @@ public class StupsParser {
|
|||||||
// Hier wird auch der AST aufgebaut
|
// Hier wird auch der AST aufgebaut
|
||||||
|
|
||||||
log("Used: " + top + " -> " + prod);
|
log("Used: " + top + " -> " + prod);
|
||||||
ASTNode pop = stack.pop();
|
final ASTNode pop = stack.pop();
|
||||||
|
|
||||||
final String[] split = prod.split(" ");
|
final String[] split = prod.split(" ");
|
||||||
|
|
||||||
for (int i = split.length - 1; i >= 0; i--) {
|
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()) {
|
if (inputPosition + i < token.size()) {
|
||||||
// Die Schleife geht in der Eingabe weiter
|
// 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
|
// Die Token mit semantischem Inhalt auswählen
|
||||||
if ("IDENTIFIER".equals(split[i]) || split[i].endsWith("_LIT")) {
|
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) {
|
private void printSourceLine(int line, List<? extends Token> token) {
|
||||||
Optional<String> srcLine = token.stream()
|
final Optional<String> srcLine = token.stream()
|
||||||
.filter(tok -> tok.getLine() == line)
|
.filter(tok -> tok.getLine() == line)
|
||||||
.map(Token::getText)
|
.map(Token::getText)
|
||||||
.reduce((s1, s2) -> s1 + " " + s2);
|
.reduce((s1, s2) -> s1 + " " + s2);
|
||||||
|
|
||||||
srcLine.ifPresent(s -> System.out.println(" :: " + s));
|
srcLine.ifPresent(s -> System.out.println(" :: " + s));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -91,11 +91,11 @@ public class GrammarAnalyzer {
|
|||||||
// ...and epsilon is in all of first(Y1) ... first(Yi-1).
|
// ...and epsilon is in all of first(Y1) ... first(Yi-1).
|
||||||
|
|
||||||
// Because a != epsilon
|
// Because a != epsilon
|
||||||
Set<String> firstYiNoEps = firstOut.get(split[i]).stream()
|
final Set<String> firstYiNoEps = firstOut.get(split[i]).stream()
|
||||||
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
|
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
boolean changeNow = firstOut.get(leftside).addAll(firstYiNoEps);
|
final boolean changeNow = firstOut.get(leftside).addAll(firstYiNoEps);
|
||||||
change = change || changeNow;
|
change = change || changeNow;
|
||||||
|
|
||||||
logIfTrue(changeNow, "First: Added " + firstYiNoEps + " to " + leftside + " (All before are nullable)");
|
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)) {
|
if (i == split.length - 1 && allNullable.test(split)) {
|
||||||
// 2. (b) If epsilon is in first(Y1) ... first(Yk), then add epsilon to first(X).
|
// 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;
|
change = change || changeNow;
|
||||||
|
|
||||||
logIfTrue(changeNow, "First: Added " + this.grammar.getEpsilonSymbol() + " to " + leftside + " (All are nullable)");
|
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())) {
|
if (rightside.equals(this.grammar.getEpsilonSymbol())) {
|
||||||
// 3. If X -> epsilon is a production, then add epsilon to first(X).
|
// 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;
|
change = change || changeNow;
|
||||||
|
|
||||||
logIfTrue(changeNow, "First: Added " + this.grammar.getEpsilonSymbol() + " to " + leftside + " (X -> EPS exists)");
|
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()))
|
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
|
||||||
.collect(Collectors.toSet());
|
.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;
|
change = change || changeNow;
|
||||||
|
|
||||||
logIfTrue(changeNow, "Follow: Added " + firstXkNoEps + " to " + split[i - 1] + " (Dazwischen nullable)");
|
logIfTrue(changeNow, "Follow: Added " + firstXkNoEps + " to " + split[i - 1] + " (Dazwischen nullable)");
|
||||||
@ -190,7 +190,7 @@ public class GrammarAnalyzer {
|
|||||||
|
|
||||||
if (this.allNullable(sub)) {
|
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;
|
change = change || changeNow;
|
||||||
|
|
||||||
logIfTrue(changeNow, "Follow: Added " + leftside + " to " + split[i - 1] + " (Dahinter nullable)");
|
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])) {
|
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).
|
// 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;
|
change = change || changeNow;
|
||||||
|
|
||||||
logIfTrue(changeNow, "Follow: Added " + followOut.get(leftside) + " to " + split[split.length - 1] + " (Ende der Regel)");
|
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() {
|
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:");
|
log("Parsetable Aufstellen:");
|
||||||
|
|
||||||
@ -231,7 +231,7 @@ public class GrammarAnalyzer {
|
|||||||
for (String sym : firstRightside) {
|
for (String sym : firstRightside) {
|
||||||
// 1. For each terminal t in first(a), add A -> a to table[A, t]
|
// 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 + ")");
|
log("Add " + rightside + " to cell (" + leftside + ", " + sym + ") (" + sym + " in first of " + rightside + ")");
|
||||||
logNullable("Overwritten " + prev + "!\n", prev);
|
logNullable("Overwritten " + prev + "!\n", prev);
|
||||||
@ -249,7 +249,7 @@ public class GrammarAnalyzer {
|
|||||||
for (String sym : followLeftside) {
|
for (String sym : followLeftside) {
|
||||||
// ...for each terminal b in follow(A), add A -> a to table[A, b].
|
// ...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 + ")");
|
log("Add " + rightside + " to cell (" + leftside + ", " + sym + ") (" + sym + " in follow of " + leftside + ")");
|
||||||
logNullable("Overwritten " + prev + "!\n", prev);
|
logNullable("Overwritten " + prev + "!\n", prev);
|
||||||
@ -258,7 +258,7 @@ public class GrammarAnalyzer {
|
|||||||
if (followLeftside.contains("$")) {
|
if (followLeftside.contains("$")) {
|
||||||
// If epsilon is in first(a) and $ is in follow(A), add A -> a to table[A, $].
|
// 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 + ")");
|
log("Add " + rightside + " to cell (" + leftside + ", $) (epsilon in first of " + rightside + " and $ in follow of " + leftside + ")");
|
||||||
logNullable("Overwritten " + prev + "!\n", prev);
|
logNullable("Overwritten " + prev + "!\n", prev);
|
||||||
@ -305,7 +305,7 @@ public class GrammarAnalyzer {
|
|||||||
if (this.allNullable(sub)) {
|
if (this.allNullable(sub)) {
|
||||||
// X1 ... Xi-1 are nullable, so first(X1 ... Xn) contains first(Xi)
|
// 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())) {
|
if (split.length == 1 && split[0].equals(this.grammar.getEpsilonSymbol())) {
|
||||||
// Stream collect has to be evaluated, doesn't work on empty stream
|
// Stream collect has to be evaluated, doesn't work on empty stream
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package lexer;
|
|||||||
|
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -18,8 +17,8 @@ class LexerTest {
|
|||||||
|
|
||||||
private Lexer initLexer(String program) {
|
private Lexer initLexer(String program) {
|
||||||
try {
|
try {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
return new StupsLexer(CharStreams.fromString(programCode));
|
return new StupsLexer(CharStreams.fromString(programCode));
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
ignore.printStackTrace();
|
ignore.printStackTrace();
|
||||||
@ -36,18 +35,18 @@ class LexerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEmptyFile() {
|
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();
|
assertThat(token).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testWhitespace() {
|
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",
|
assertThat(token).containsExactly("IDENTIFIER",
|
||||||
"IDENTIFIER",
|
"IDENTIFIER",
|
||||||
@ -57,9 +56,9 @@ class LexerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEmptyMain() {
|
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",
|
assertThat(token).containsExactly("CLASS",
|
||||||
"IDENTIFIER",
|
"IDENTIFIER",
|
||||||
@ -81,9 +80,9 @@ class LexerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGeneralWhile() {
|
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)
|
assertThat(token).hasSize(68)
|
||||||
.containsSequence("WHILE",
|
.containsSequence("WHILE",
|
||||||
@ -101,9 +100,9 @@ class LexerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGeneralComment() {
|
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)
|
assertThat(token).hasSize(21)
|
||||||
.doesNotContain("WHITESPACE")
|
.doesNotContain("WHITESPACE")
|
||||||
@ -124,9 +123,9 @@ class LexerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGeneralIfElse() {
|
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)
|
assertThat(token).hasSize(96)
|
||||||
.containsSequence("IF",
|
.containsSequence("IF",
|
||||||
|
|||||||
@ -18,8 +18,8 @@ class LexerStupsParserGrammarTest {
|
|||||||
|
|
||||||
private Lexer initLexer(String program) {
|
private Lexer initLexer(String program) {
|
||||||
try {
|
try {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
return new StupsLexer(CharStreams.fromString(programCode));
|
return new StupsLexer(CharStreams.fromString(programCode));
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
ignore.printStackTrace();
|
ignore.printStackTrace();
|
||||||
@ -30,80 +30,80 @@ class LexerStupsParserGrammarTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEmptyFile() throws URISyntaxException, IOException {
|
void testEmptyFile() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEmptyMain() throws URISyntaxException, IOException {
|
void testEmptyMain() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGeneralComment() throws URISyntaxException, IOException {
|
void testGeneralComment() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void tesMultiDecl() throws URISyntaxException, IOException {
|
void tesMultiDecl() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testDeclarationAssignment() throws URISyntaxException, IOException {
|
void testDeclarationAssignment() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testExpr() throws URISyntaxException, IOException {
|
void testExpr() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGeneralWhile() throws URISyntaxException, IOException {
|
void testGeneralWhile() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGeneralIfElse() throws URISyntaxException, IOException {
|
void testGeneralIfElse() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(path);
|
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();
|
assertThat(stupsParser.parse(lex.getAllTokens(), lex.getVocabulary())).isNotNull();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@ class ASTTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testOneNode() {
|
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);
|
System.out.println(tree);
|
||||||
|
|
||||||
assertThat(tree).hasToString("Wurzel\n");
|
assertThat(tree).hasToString("Wurzel\n");
|
||||||
@ -18,14 +18,14 @@ class ASTTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testThreeNodesBinary() {
|
void testThreeNodesBinary() {
|
||||||
ASTNode root = new ASTNode("Wurzel", 1);
|
final ASTNode root = new ASTNode("Wurzel", 1);
|
||||||
ASTNode childA = new ASTNode("A", 1);
|
final ASTNode childA = new ASTNode("A", 1);
|
||||||
ASTNode childB = new ASTNode("B", 1);
|
final ASTNode childB = new ASTNode("B", 1);
|
||||||
|
|
||||||
root.addChild(childA);
|
root.addChild(childA);
|
||||||
root.addChild(childB);
|
root.addChild(childB);
|
||||||
|
|
||||||
AST tree = new AST(root);
|
final AST tree = new AST(root);
|
||||||
System.out.println(tree);
|
System.out.println(tree);
|
||||||
|
|
||||||
assertThat(tree).hasToString("Wurzel\n├── A\n└── B\n");
|
assertThat(tree).hasToString("Wurzel\n├── A\n└── B\n");
|
||||||
@ -33,14 +33,14 @@ class ASTTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testThreeNodesLinear() {
|
void testThreeNodesLinear() {
|
||||||
ASTNode root = new ASTNode("Wurzel", 1);
|
final ASTNode root = new ASTNode("Wurzel", 1);
|
||||||
ASTNode childA = new ASTNode("A", 1);
|
final ASTNode childA = new ASTNode("A", 1);
|
||||||
ASTNode childB = new ASTNode("B", 1);
|
final ASTNode childB = new ASTNode("B", 1);
|
||||||
|
|
||||||
root.addChild(childA);
|
root.addChild(childA);
|
||||||
childA.addChild(childB);
|
childA.addChild(childB);
|
||||||
|
|
||||||
AST tree = new AST(root);
|
final AST tree = new AST(root);
|
||||||
System.out.println(tree);
|
System.out.println(tree);
|
||||||
|
|
||||||
assertThat(tree).hasToString("Wurzel\n└── A\n └── B\n");
|
assertThat(tree).hasToString("Wurzel\n└── A\n └── B\n");
|
||||||
|
|||||||
@ -23,9 +23,9 @@ class GrammarTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleGrammar0() throws IOException {
|
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;
|
assert grammar != null;
|
||||||
|
|
||||||
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
|
assertThat(grammar.getEpsilonSymbol()).isEqualTo("epsilon");
|
||||||
@ -39,9 +39,9 @@ class GrammarTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSimpleGrammar1() throws IOException {
|
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;
|
assert grammar != null;
|
||||||
|
|
||||||
System.out.println(grammar.getRules());
|
System.out.println(grammar.getRules());
|
||||||
|
|||||||
Reference in New Issue
Block a user