use unmodifiable collection views
This commit is contained in:
@ -4,6 +4,7 @@ import parser.grammar.Grammar;
|
|||||||
import parser.grammar.GrammarAnalyzer;
|
import parser.grammar.GrammarAnalyzer;
|
||||||
|
|
||||||
import java.util.AbstractMap.SimpleEntry;
|
import java.util.AbstractMap.SimpleEntry;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Formatter;
|
import java.util.Formatter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -19,11 +20,11 @@ public class ParsingTable {
|
|||||||
|
|
||||||
public ParsingTable(Grammar grammar, Map<Entry<String, String>, String> parsetable) {
|
public ParsingTable(Grammar grammar, Map<Entry<String, String>, String> parsetable) {
|
||||||
this.grammar = grammar;
|
this.grammar = grammar;
|
||||||
this.parsetable = parsetable;
|
this.parsetable = Collections.unmodifiableMap(parsetable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParsingTable fromGrammar(Grammar grammar) {
|
public static ParsingTable fromGrammar(Grammar grammar) {
|
||||||
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
|
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
|
||||||
return analyzer.getTable();
|
return analyzer.getTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,16 +50,16 @@ public class ParsingTable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder output = new StringBuilder();
|
final StringBuilder output = new StringBuilder();
|
||||||
Formatter format = new Formatter(output);
|
final Formatter format = new Formatter(output);
|
||||||
|
|
||||||
List<String> inputSymbols = this.parsetable.keySet().stream()
|
final List<String> inputSymbols = this.parsetable.keySet().stream()
|
||||||
.map(Entry::getValue)
|
.map(Entry::getValue)
|
||||||
.distinct()
|
.distinct()
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
// Determine margins
|
// Determine margins (column-sizes)
|
||||||
Map<String, Integer> margins = new HashMap<>();
|
final Map<String, Integer> margins = new HashMap<>();
|
||||||
margins.put("NTERM", 0);
|
margins.put("NTERM", 0);
|
||||||
for (String terminal : inputSymbols) {
|
for (String terminal : inputSymbols) {
|
||||||
margins.put(terminal, 0);
|
margins.put(terminal, 0);
|
||||||
@ -69,17 +70,16 @@ public class ParsingTable {
|
|||||||
margins.put("NTERM", Math.max(margins.get("NTERM"), nonterminal.length()));
|
margins.put("NTERM", Math.max(margins.get("NTERM"), nonterminal.length()));
|
||||||
|
|
||||||
for (String terminal : inputSymbols) {
|
for (String terminal : inputSymbols) {
|
||||||
String prod = this.parsetable.get(new SimpleEntry<>(nonterminal, terminal));
|
final String prod = this.parsetable.get(new SimpleEntry<>(nonterminal, terminal));
|
||||||
|
|
||||||
int length;
|
final int length;
|
||||||
if (prod == null) {
|
if (prod == null) {
|
||||||
length = 0;
|
length = 0;
|
||||||
} else {
|
} else {
|
||||||
length = prod.length();
|
length = prod.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
margins.put(terminal, Math.max(margins.get(terminal), length));
|
margins.put(terminal, Math.max(margins.get(terminal), Math.max(length, terminal.length())));
|
||||||
margins.put(terminal, Math.max(margins.get(terminal), terminal.length()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ public class ParsingTable {
|
|||||||
format.format("%-Xs| ".replaceAll("X", String.valueOf(margins.get("NTERM"))), nonterminal);
|
format.format("%-Xs| ".replaceAll("X", String.valueOf(margins.get("NTERM"))), nonterminal);
|
||||||
|
|
||||||
for (String terminal : inputSymbols) {
|
for (String terminal : inputSymbols) {
|
||||||
String prod = this.parsetable.get(new SimpleEntry<>(nonterminal, terminal));
|
final String prod = this.parsetable.get(new SimpleEntry<>(nonterminal, terminal));
|
||||||
format.format("%-Xs ".replaceAll("X", String.valueOf(margins.get(terminal))), prod == null ? " ".repeat(9) : prod);
|
format.format("%-Xs ".replaceAll("X", String.valueOf(margins.get(terminal))), prod == null ? " ".repeat(9) : prod);
|
||||||
}
|
}
|
||||||
output.append("|\n");
|
output.append("|\n");
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@ -47,16 +48,18 @@ public class Grammar {
|
|||||||
Map<String, List<String>> valToValMappings,
|
Map<String, List<String>> valToValMappings,
|
||||||
Map<String, List<String>> delChildMappings,
|
Map<String, List<String>> delChildMappings,
|
||||||
Set<GrammarRule> rules) {
|
Set<GrammarRule> rules) {
|
||||||
this.terminals = terminals;
|
|
||||||
this.nonterminals = nonterminals;
|
this.terminals = Collections.unmodifiableSet(terminals);
|
||||||
|
this.nonterminals = Collections.unmodifiableSet(nonterminals);
|
||||||
|
this.rules = Collections.unmodifiableSet(rules);
|
||||||
this.startSymbol = startSymbol;
|
this.startSymbol = startSymbol;
|
||||||
this.epsilonSymbol = epsilonSymbol;
|
this.epsilonSymbol = epsilonSymbol;
|
||||||
this.actions = actions;
|
|
||||||
this.renameMappings = renameMappings;
|
this.actions = Collections.unmodifiableMap(actions);
|
||||||
this.nameToValMappings = nameToValMappings;
|
this.renameMappings = Collections.unmodifiableMap(renameMappings);
|
||||||
this.valToValMappings = valToValMappings;
|
this.nameToValMappings = Collections.unmodifiableMap(nameToValMappings);
|
||||||
this.delChildMappings = delChildMappings;
|
this.valToValMappings = Collections.unmodifiableMap(valToValMappings);
|
||||||
this.rules = rules;
|
this.delChildMappings = Collections.unmodifiableMap(delChildMappings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Grammar fromFile(Path path) throws IOException {
|
public static Grammar fromFile(Path path) throws IOException {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import parser.ast.AST;
|
|||||||
import parser.ast.ASTNode;
|
import parser.ast.ASTNode;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -17,50 +18,39 @@ public class TypeTable {
|
|||||||
private final Map<String, List<String>> methodArgumentTable;
|
private final Map<String, List<String>> methodArgumentTable;
|
||||||
|
|
||||||
public TypeTable(Map<String, String> symbolTable) {
|
public TypeTable(Map<String, String> symbolTable) {
|
||||||
this.symbolTable = symbolTable;
|
this.symbolTable = Collections.unmodifiableMap(symbolTable);
|
||||||
|
|
||||||
// Enthält die Return-Types der Operatoren
|
// Enthält die Return-Types der Operatoren
|
||||||
final Map<String, String> methodTable = new HashMap<>();
|
|
||||||
|
|
||||||
methodTable.put("ADD", "INTEGER_TYPE");
|
this.methodReturnTable = Map.ofEntries(Map.entry("ADD", "INTEGER_TYPE"),
|
||||||
methodTable.put("SUB", "INTEGER_TYPE");
|
Map.entry("SUB", "INTEGER_TYPE"),
|
||||||
methodTable.put("MUL", "INTEGER_TYPE");
|
Map.entry("MUL", "INTEGER_TYPE"),
|
||||||
methodTable.put("DIV", "INTEGER_TYPE");
|
Map.entry("DIV", "INTEGER_TYPE"),
|
||||||
methodTable.put("MOD", "INTEGER_TYPE");
|
Map.entry("MOD", "INTEGER_TYPE"),
|
||||||
|
Map.entry("NOT", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("AND", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("OR", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("LESS", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("LESS_EQUAL", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("GREATER", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("GREATER_EQUAL", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("EQUAL", "BOOLEAN_TYPE"),
|
||||||
|
Map.entry("NOT_EQUAL", "BOOLEAN_TYPE"));
|
||||||
|
|
||||||
methodTable.put("NOT", "BOOLEAN_TYPE");
|
this.methodArgumentTable = Map.ofEntries(Map.entry("ADD", Arrays.asList("INTEGER_TYPE")),
|
||||||
methodTable.put("AND", "BOOLEAN_TYPE");
|
Map.entry("SUB", Arrays.asList("INTEGER_TYPE")),
|
||||||
methodTable.put("OR", "BOOLEAN_TYPE");
|
Map.entry("MUL", Arrays.asList("INTEGER_TYPE")),
|
||||||
|
Map.entry("DIV", Arrays.asList("INTEGER_TYPE")),
|
||||||
methodTable.put("LESS", "BOOLEAN_TYPE");
|
Map.entry("MOD", Arrays.asList("INTEGER_TYPE")),
|
||||||
methodTable.put("LESS_EQUAL", "BOOLEAN_TYPE");
|
Map.entry("AND", Arrays.asList("BOOLEAN_TYPE")),
|
||||||
methodTable.put("GREATER", "BOOLEAN_TYPE");
|
Map.entry("OR", Arrays.asList("BOOLEAN_TYPE")),
|
||||||
methodTable.put("GREATER_EQUAL", "BOOLEAN_TYPE");
|
Map.entry("NOT", Arrays.asList("BOOLEAN_TYPE")),
|
||||||
methodTable.put("EQUAL", "BOOLEAN_TYPE");
|
Map.entry("LESS", Arrays.asList("INTEGER_TYPE")),
|
||||||
methodTable.put("NOT_EQUAL", "BOOLEAN_TYPE");
|
Map.entry("LESS_EQUAL", Arrays.asList("INTEGER_TYPE")),
|
||||||
|
Map.entry("GREATER", Arrays.asList("INTEGER_TYPE")),
|
||||||
this.methodReturnTable = methodTable;
|
Map.entry("GREATER_EQUAL", Arrays.asList("INTEGER_TYPE")),
|
||||||
|
Map.entry("EQUAL", Arrays.asList("INTEGER_TYPE", "BOOLEAN_TYPE", "STRING_TYPE")),
|
||||||
final Map<String, List<String>> argumentTable = new HashMap<>();
|
Map.entry("NOT_EQUAL", Arrays.asList("INTEGER_TYPE", "BOOLEAN_TYPE", "STRING_TYPE")));
|
||||||
|
|
||||||
argumentTable.put("ADD", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("SUB", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("MUL", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("DIV", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("MOD", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
|
|
||||||
argumentTable.put("AND", Arrays.asList("BOOLEAN_TYPE"));
|
|
||||||
argumentTable.put("OR", Arrays.asList("BOOLEAN_TYPE"));
|
|
||||||
argumentTable.put("NOT", Arrays.asList("BOOLEAN_TYPE"));
|
|
||||||
|
|
||||||
argumentTable.put("LESS", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("LESS_EQUAL", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("GREATER", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("GREATER_EQUAL", Arrays.asList("INTEGER_TYPE"));
|
|
||||||
argumentTable.put("EQUAL", Arrays.asList("INTEGER_TYPE", "BOOLEAN_TYPE", "STRING_TYPE"));
|
|
||||||
argumentTable.put("NOT_EQUAL", Arrays.asList("INTEGER_TYPE", "BOOLEAN_TYPE", "STRING_TYPE"));
|
|
||||||
|
|
||||||
this.methodArgumentTable = argumentTable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TypeTable fromAST(AST tree) {
|
public static TypeTable fromAST(AST tree) {
|
||||||
|
|||||||
Reference in New Issue
Block a user