Skip to content

Commit

Permalink
Merge pull request #47 from MontiCore/fix-parsevisitor-lexnames
Browse files Browse the repository at this point in the history
#4388 Side-effect free Lexer generation
  • Loading branch information
luepges authored Dec 5, 2024
2 parents b098ef1 + dfcfa23 commit 5436630
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String> 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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 = " | ";
}
Expand Down Expand Up @@ -568,7 +568,7 @@ void handleTerminal(String labelAssignment, String tmpVarName, Map<String, Colle
if (grammarInfo.getKeywordRules().contains(replaceString)) {
addToCodeSection(parserHelper.getKeyRuleName(replaceString));
} else {
addToCodeSection(parserHelper.getLexSymbolName(replaceString));
addToCodeSection(parserHelper.getOrComputeLexSymbolName(replaceString));
}
seperator = " | ";
}
Expand All @@ -586,7 +586,7 @@ public void visit(ASTTokenTerminal ast) {
if (replacedAdditionalKeywords.containsKey(ast.getName())) {
handleTerminal(labelAssignment, tmpVarName, replacedAdditionalKeywords, ast.getName());
} else {
addToCodeSection(tmpVarName + labelAssignment + parserHelper.getLexSymbolName(ast.getTokenConstant().getString()));
addToCodeSection(tmpVarName + labelAssignment + parserHelper.getOrComputeLexSymbolName(ast.getTokenConstant().getString()));
}

addActionToCodeSection();
Expand Down Expand Up @@ -1056,7 +1056,7 @@ protected void addActionForKeyword(ASTTerminal keyword, ProdSymbol rule, boolean
addToCodeSection("(");
String rulename = "";
if (grammarInfo.isKeyword(keyword.getName(), grammarEntry)) {
rulename = parserHelper.getLexSymbolName(keyword.getName());
rulename = parserHelper.getOrComputeLexSymbolName(keyword.getName());
}

// No actions in predicates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ protected String getRuleName(String name) {
} else if (grammarInfo.isKeyword(name, parserGeneratorHelper.getGrammarSymbol()) && grammarInfo.getKeywordRules().contains(name)) {
return parserGeneratorHelper.getKeyRuleName(name);
} else {
return parserGeneratorHelper.getLexSymbolName(name.intern());
return parserGeneratorHelper.getCachedLexSymbolName(name.intern()).orElse("##no-usagename-for-rulename");
}
}

Expand Down Expand Up @@ -779,13 +779,16 @@ protected String getRuleName(ASTConstant constant) {
}
return parserGeneratorHelper.getKeyRuleName(constant.getKeyConstant().getString(0));
} else if (constant.isPresentTokenConstant()) {
return parserGeneratorHelper.getLexSymbolName(constant.getTokenConstant().getString());
return parserGeneratorHelper.getCachedLexSymbolName(constant.getTokenConstant().getString())
.orElse("##no-usagename-rulename-tc");
} else if (!grammarInfo.isKeyword(constant.getName(), parserGeneratorHelper.getGrammarSymbol())) {
return parserGeneratorHelper.getLexSymbolName(constant.getName());
return parserGeneratorHelper.getCachedLexSymbolName(constant.getName())
.orElse("##no-usagename-rulename-k");
} else if (grammarInfo.getKeywordRules().contains(constant.getName())) {
return parserGeneratorHelper.getKeyRuleName(constant.getName());
} else {
return parserGeneratorHelper.getLexSymbolName(constant.getName());
return parserGeneratorHelper.getCachedLexSymbolName(constant.getName())
.orElse("##no-usagename-rulename");
}
}

Expand Down
2 changes: 1 addition & 1 deletion monticore-generator/src/main/resources/parser/Lexer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ${tc.includeArgs("parser.LexerMember", [antlrGenerator, parserHelper.getGrammarS

// Implicit token
<#list genHelper.getLexSymbolsWithInherited() as lexSymbol>
${genHelper.getLexSymbolName(lexSymbol)} : '${lexSymbol}';
${genHelper.getOrComputeLexSymbolName(lexSymbol)} : '${lexSymbol}';
</#list>

// Explicit token
Expand Down
39 changes: 34 additions & 5 deletions monticore-grammar/src/main/java/de/monticore/grammar/LexNamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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);
}
Expand All @@ -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<String> getComputedConstantName(String sym) {
return Optional.ofNullable(this.usedConstants.get(sym));
}

public String getOrComputeConstantName(String sym) {
String s = sym.intern();

if (!usedConstants.containsKey(s)) {
Expand All @@ -165,8 +191,11 @@ protected String convertKeyword(String key) {
return key;
}


public Set<String> getLexnames() {
return usedLex.keySet();
return this.usedLex.keySet();
}

public Set<String> getUsedConstants() {
return this.usedConstants.keySet();
}
}

0 comments on commit 5436630

Please sign in to comment.