Skip to content

Commit

Permalink
Merge branch 'cleanup-tr-pattern-alt-blocks' into 'dev'
Browse files Browse the repository at this point in the history
DSTLGen - allow multiple patterns and remove parser calls

Closes #3576

See merge request monticore/monticore!902
  • Loading branch information
MisterErwin committed Jan 8, 2024
2 parents cd5a5cc + 9a74e73 commit bdb99be
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import de.monticore.grammar.grammar_withconcepts.Grammar_WithConceptsMill;
import de.monticore.grammar.grammar_withconcepts._ast.ASTAction;
import de.monticore.grammar.grammar_withconcepts._parser.Grammar_WithConceptsParser;
import de.monticore.prettyprint.IndentPrinter;
import de.monticore.types.MCTypeFacade;
import de.monticore.types.mcbasictypes._ast.ASTMCReturnType;
import de.monticore.types.mcbasictypes._ast.ASTMCType;
import de.monticore.types.prettyprint.MCBasicTypesFullPrettyPrinter;
import de.se_rwth.commons.Joiners;
import de.se_rwth.commons.Splitters;
import de.se_rwth.commons.StringTransformations;
Expand Down Expand Up @@ -77,10 +76,11 @@ public static synchronized ASTRuleFactory getInstance() {
* @return the ast rule as an ASTASTRule object
*/
ASTASTRule createAstNegationProdForConstant(MCGrammarSymbol grammarSymbol, String name) {

// astrule AST_${nameWithPrefix }_Constant_Neg
String nameWithPrefix = grammarSymbol.getName() + "_" + name;
final String tfAstRuleDecl = AST + nameWithPrefix + "_Constant_Neg = ;";
ASTASTRule tfAstRule = parseASTRule(tfAstRuleDecl);
ASTASTRule tfAstRule = GrammarMill.aSTRuleBuilder()
.setType(nameWithPrefix + "_Constant_Neg")
.build();

// create method getLhs and getRhs
String patternNameFirstToUpper = StringTransformations.capitalize(nameWithPrefix);
Expand All @@ -102,10 +102,11 @@ ASTASTRule createAstNegationProdForConstant(MCGrammarSymbol grammarSymbol, Strin
* @return the ast rule as an ASTASTRule object
*/
ASTASTRule createAstOptionalProdForConstant(MCGrammarSymbol grammarSymbol, String name) {

// astrule {nameWithPrefix}_Constant_Opt
String nameWithPrefix = grammarSymbol.getName() + "_" + name;
final String tfAstRuleDecl = AST + nameWithPrefix + "_Constant_Opt = ;";
ASTASTRule tfAstRule = parseASTRule(tfAstRuleDecl);
ASTASTRule tfAstRule = GrammarMill.aSTRuleBuilder()
.setType(nameWithPrefix + "_Constant_Opt")
.build();

// create method getLhs and getRhs
String patternNameFirstToUpper = StringTransformations.capitalize(nameWithPrefix);
Expand All @@ -120,11 +121,12 @@ ASTASTRule createAstOptionalProdForConstant(MCGrammarSymbol grammarSymbol, Strin

public ASTASTRule createAstPatternProd(ASTAbstractProd srcNode,MCGrammarSymbol grammarSymbol) {
final String name = srcNode.getName();
final String tfAstRuleDecl;
tfAstRuleDecl = AST + name + PATTERN_SUFFIX + " = ;";
// astrule ${name}_Pat
ASTASTRule tfAstRule = GrammarMill.aSTRuleBuilder()
.setType(name + PATTERN_SUFFIX)
.build();
final String grammarPackage = Joiners.DOT.join(grammarSymbol.getAstNode().getPackageList())+
"."+ grammarSymbol.getName().toLowerCase();
ASTASTRule tfAstRule = parseASTRule(tfAstRuleDecl);

// create method getLhs and getRhs
tfAstRule.getGrammarMethodList()
Expand All @@ -133,7 +135,7 @@ public ASTASTRule createAstPatternProd(ASTAbstractProd srcNode,MCGrammarSymbol g
.add(createASTGrammarMethod(AST_PREFIX + name + PATTERN_SUFFIX, GET_LHS, RETURN_THIS));
// create method _getTFElementType
final String methodBody;
if (grammarPackage != null && !grammarPackage.startsWith(".")) {
if (!grammarPackage.startsWith(".")) {
methodBody = "return " + grammarPackage + "._ast.AST" + name + CLASS_SUFFIX;
}
else {
Expand All @@ -157,13 +159,15 @@ public ASTASTRule createAstPatternProd(ASTAbstractProd srcNode,MCGrammarSymbol g
*/
public ASTASTRule createAstProd(ASTProd srcNode, ProductionType type, boolean overridden, MCGrammarSymbol grammarSymbol) {
final String name = srcNode.getName();
final String tfAstRuleDecl = AST + name + "_" + type.getNameString() + " = ;";
// astrule ${name}_${type.getNameString()}
ASTASTRule tfAstRule = GrammarMill.aSTRuleBuilder()
.setType(name + "_" + type.getNameString())
.build();
String grammarPackage = Joiners.DOT.join(grammarSymbol.getAstNode().getPackageList());
if(!grammarPackage.isEmpty()){
grammarPackage += ".";
}
grammarPackage += grammarSymbol.getName().toLowerCase();
ASTASTRule tfAstRule = parseASTRule(tfAstRuleDecl);

// create method _getTFElementType
tfAstRule.getGrammarMethodList().add(buildASTGrammarMethodReturnFQNClass("Class", "_getTFElementType", grammarPackage + "._ast.AST" + name));
Expand Down Expand Up @@ -260,7 +264,7 @@ public ASTASTRule createASTRule(ASTASTRule srcNode, MCGrammarSymbol grammarSymbo

for (ASTAdditionalAttribute attr : targetNode.getAdditionalAttributeList()) {
ASTMCType ref = attr.getMCType();
String typeName = ref.printType(new MCBasicTypesFullPrettyPrinter(new IndentPrinter()));
String typeName = ref.printType();
if (typeName.equals(NAME)|| grammarSymbol.getProdWithInherited(typeName).isPresent()){
attrsToBeRemoved.add(attr);
}
Expand All @@ -281,14 +285,16 @@ public ASTASTRule createASTRule(ASTASTRule srcNode, MCGrammarSymbol grammarSymbo
* creates an AST rule for external production belonging to the production for the given type
*
* @param srcNode the external production
* @param type the productiuon type
* @param type the production type
* @return the newly created AST rule
*/
public ASTASTRule createAstProd(ASTExternalProd srcNode, ProductionType type) {
final String name = srcNode.getName();
final String tfAstRuleDecl = AST + name + "_" + type.getNameString() + " = ;";

ASTASTRule tfAstRule = parseASTRule(tfAstRuleDecl);
// atsrule ${name}_${type.getNameString()}
ASTASTRule tfAstRule = GrammarMill.aSTRuleBuilder()
.setType(name + "_" + type.getNameString())
.build();

// create method _getTFElementType
tfAstRule.getGrammarMethodList().add(createASTGrammarMethod("Class", "_getTFElementType", "return null;"));
Expand All @@ -302,12 +308,12 @@ public ASTASTRule createAstProd(ASTExternalProd srcNode, ProductionType type) {
return tfAstRule;
}

public ASTASTRule createAstExternalProd(ASTExternalProd srcNode){
final String name = srcNode.getName();
final String tfAstRuleDecl = AST + "ITF" + name + " astextends de.monticore.tf.ast.ITFElement" + " ;";

ASTASTRule tfAstRule = parseASTRule(tfAstRuleDecl);
return tfAstRule;
public ASTASTRule createAstExternalProd(ASTExternalProd srcNode) {
// astrule ${scrNode.getName()}_Constant_Op astextends de.monticore.tf.ast.ITFElement
return GrammarMill.aSTRuleBuilder()
.setType(srcNode.getName() + "_Constant_Opt")
.addASTSuperClass(MCTypeFacade.getInstance().createQualifiedType("de.monticore.tf.ast.ITFElement"))
.build();
}


Expand All @@ -319,17 +325,6 @@ protected ASTMCReturnType buildQualifiedReturnType(String partsFQN) {
.build();
}

protected ASTASTRule parseASTRule(String tfAstElementProduction) {
Grammar_WithConceptsParser p = Grammar_WithConceptsMill.parser();
try {
return p.parse_StringASTRule(tfAstElementProduction).get();
}
catch (IOException e) {
throw new RuntimeException("0xF1000 Unable to create ASTRule for" + tfAstElementProduction);
}
}


protected ASTAction parseAction(String methodBody) {
Grammar_WithConceptsParser p = Grammar_WithConceptsMill.parser();
try {
Expand Down
Loading

0 comments on commit bdb99be

Please sign in to comment.