diff --git a/monticore-generator/src/main/java/de/monticore/codegen/parser/ParserGeneratorHelper.java b/monticore-generator/src/main/java/de/monticore/codegen/parser/ParserGeneratorHelper.java index e5eae2a8a..a5c3018bb 100644 --- a/monticore-generator/src/main/java/de/monticore/codegen/parser/ParserGeneratorHelper.java +++ b/monticore-generator/src/main/java/de/monticore/codegen/parser/ParserGeneratorHelper.java @@ -146,7 +146,7 @@ public String getParserPackage() { /** * @return the name for a lexsymbol that should be used in an Antlr-File */ - public String getLexSymbolName(String constName) { + public String getOrComputeLexSymbolName(String constName) { Log.errorIfNull(constName); if (grammarInfo.getSplitRules().containsKey(constName)) { return grammarInfo.getSplitRules().get(constName); @@ -155,6 +155,24 @@ public String getLexSymbolName(String constName) { } } + /** + * Side effect free. + * @return the name for a lexsymbol that was used in an Antlr-File + */ + public Optional getCachedLexSymbolName(String constName) { + Log.errorIfNull(constName); + if (grammarInfo.getSplitRules().containsKey(constName)) { + return Optional.of(grammarInfo.getSplitRules().get(constName)); + } else { + // Optimally we would call Map#get directly on the LexNamer + // (which is only available with the next release) + if (grammarInfo.getLexNamer().getLexnames().contains(constName)) { + return Optional.of(grammarInfo.getLexNamer().getLexName(grammarSymbol, constName)); + } + return Optional.empty(); + } + } + /** * @return the name for a rule replacing a keyword */ diff --git a/monticore-generator/src/main/java/de/monticore/codegen/parser/antlr/Grammar2Antlr.java b/monticore-generator/src/main/java/de/monticore/codegen/parser/antlr/Grammar2Antlr.java index 8d990df69..829b0c60f 100644 --- a/monticore-generator/src/main/java/de/monticore/codegen/parser/antlr/Grammar2Antlr.java +++ b/monticore-generator/src/main/java/de/monticore/codegen/parser/antlr/Grammar2Antlr.java @@ -268,7 +268,7 @@ public void handle(ASTEnumProd ast) { int index = 0; for (ASTConstant c : ast.getConstantList()) { addToCodeSection(sep); - addToCodeSection("\n", "e_" + index++ + "=" + parserHelper.getLexSymbolName(c.getName())); + addToCodeSection("\n", "e_" + index++ + "=" + parserHelper.getOrComputeLexSymbolName(c.getName())); if (embeddedJavaCode) { String temp1 = ""; @@ -319,11 +319,11 @@ public void handle(ASTConstantGroup ast) { if (x.isPresentKeyConstant()) { addToCodeSection(createKeyPredicate(x.getKeyConstant().getStringList(), tmpName + label)); } else if (!grammarInfo.isKeyword(x.getName(), grammarEntry)) { - addToCodeSection(tmpName + label + parserHelper.getLexSymbolName(x.getName())); + addToCodeSection(tmpName + label + parserHelper.getOrComputeLexSymbolName(x.getName())); } else if (grammarInfo.getKeywordRules().contains(x.getName())) { addToCodeSection(tmpName + label + parserHelper.getKeyRuleName(x.getName())); } else { - addToCodeSection(tmpName + label + parserHelper.getLexSymbolName(x.getName())); + addToCodeSection(tmpName + label + parserHelper.getOrComputeLexSymbolName(x.getName())); } del = "|\n"; @@ -464,7 +464,7 @@ public void visit(ASTTerminal ast) { } else if (grammarInfo.isKeyword(ast.getName(), grammarEntry) && grammarInfo.getKeywordRules().contains(ast.getName())) { rulename = parserHelper.getKeyRuleName(ast.getName()); } else { - rulename = parserHelper.getLexSymbolName(ast.getName().intern()); + rulename = parserHelper.getOrComputeLexSymbolName(ast.getName().intern()); } // No actions in predicates @@ -487,7 +487,7 @@ public void visit(ASTTerminal ast) { if (grammarInfo.getKeywordRules().contains(replaceString)) { addToCodeSection(parserHelper.getKeyRuleName(replaceString)); } else { - addToCodeSection(parserHelper.getLexSymbolName(replaceString)); + addToCodeSection(parserHelper.getOrComputeLexSymbolName(replaceString)); } seperator = " | "; } @@ -568,7 +568,7 @@ void handleTerminal(String labelAssignment, String tmpVarName, Map - ${genHelper.getLexSymbolName(lexSymbol)} : '${lexSymbol}'; + ${genHelper.getOrComputeLexSymbolName(lexSymbol)} : '${lexSymbol}'; // Explicit token diff --git a/monticore-grammar/src/main/java/de/monticore/grammar/LexNamer.java b/monticore-grammar/src/main/java/de/monticore/grammar/LexNamer.java index 5cb5293f2..69f4c6a5d 100644 --- a/monticore-grammar/src/main/java/de/monticore/grammar/LexNamer.java +++ b/monticore-grammar/src/main/java/de/monticore/grammar/LexNamer.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.Set; import de.monticore.grammar.grammar._symboltable.MCGrammarSymbol; @@ -119,14 +120,30 @@ public static String createSimpleGoodName(String x) { } + /** + * Computes a new name for the lexical production or returns the previously + * computed one. + * + * @deprecated Use {@link LexNamer#getComputedLexName(String)} + * or {@link LexNamer#getOrComputeLexName(MCGrammarSymbol, String)} instead + */ + @Deprecated + public String getLexName(MCGrammarSymbol grammarSymbol, String sym) { + return this.getOrComputeLexName(grammarSymbol, sym); + } + + public Optional getComputedLexName(String sym) { + return Optional.ofNullable(this.usedLex.get(sym)); + } + /** * Returns Human-Readable, antlr conformed name for a lexsymbols nice names for common tokens * (change constructor to add tokenes) LEXi where i is number for unknown ones - * + * * @param sym lexer symbol * @return Human-Readable, antlr conformed name for a lexsymbols */ - public String getLexName(MCGrammarSymbol grammarSymbol, String sym) { + public String getOrComputeLexName(MCGrammarSymbol grammarSymbol, String sym) { if (usedLex.containsKey(sym)) { return usedLex.get(sym); } @@ -139,8 +156,17 @@ public String getLexName(MCGrammarSymbol grammarSymbol, String sym) { Log.debug("Using lexer symbol " + goodName + " for symbol '" + sym + "'", "LexNamer"); return goodName; } - + + @Deprecated public String getConstantName(String sym) { + return this.getOrComputeConstantName(sym); + } + + public Optional getComputedConstantName(String sym) { + return Optional.ofNullable(this.usedConstants.get(sym)); + } + + public String getOrComputeConstantName(String sym) { String s = sym.intern(); if (!usedConstants.containsKey(s)) { @@ -165,8 +191,11 @@ protected String convertKeyword(String key) { return key; } - public Set getLexnames() { - return usedLex.keySet(); + return this.usedLex.keySet(); + } + + public Set getUsedConstants() { + return this.usedConstants.keySet(); } }