Skip to content

Commit

Permalink
generate toString() for symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterErwin committed Oct 24, 2023
1 parent 6debd57 commit d30f9df
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public ASTCDClass decorate(ASTCDClass symbolInput) {
.addCDMember(createDetermineFullName(scopeInterface))
.build();

// Only add a toString method, if not already present on the input CD
if (symbolClass.getCDMethodList().stream().noneMatch(x->x.getName().equals("toString") && x.getCDParameterList().isEmpty()))
symbolClass.addCDMember(createToString(symbolName));

// add only for scope spanning symbols
if (hasScope || hasInheritedScope) {
ASTCDAttribute spannedScopeAttribute = createSpannedScopeAttribute();
Expand Down Expand Up @@ -314,6 +318,15 @@ protected ASTCDMethod createDetermineFullName(String scopeInterface) {
return method;
}

protected ASTCDMethod createToString(String symbolName) {
ASTMCType stringType = getMCTypeFacade().createStringType();

ASTCDMethod method = getCDMethodFacade().createMethod(PUBLIC.build(), stringType, "toString");
this.replaceTemplate(EMPTY_BODY, method, new TemplateHookPoint(TEMPLATE_PATH + "ToString",
symbolName));
return method;
}

public boolean isSymbolTop() {
return isSymbolTop;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<#-- (c) https://github.com/MontiCore/monticore -->
${tc.signature("symbolName")}
return "${symbolName}{" +
"fullName='" + this.getFullName() + "'" +
", sourcePosition=" + this.getSourcePosition() +
'}';
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void testSymbolRuleAttributes() {

@Test
public void testMethods() {
assertEquals(20, symbolClassAutomaton.getCDMethodList().size());
assertEquals(21, symbolClassAutomaton.getCDMethodList().size());
}

@Test
Expand Down Expand Up @@ -559,7 +559,7 @@ public void testSpannedScopeAttributeStateSymbol() {

@Test
public void testMethodsStateSymbol() {
assertEquals(18, symbolClassState.getCDMethodList().size());
assertEquals(19, symbolClassState.getCDMethodList().size());

assertTrue(Log.getFindings().isEmpty());
}
Expand Down Expand Up @@ -655,7 +655,7 @@ public void testScopeRuleMethod() {
assertDeepEquals(String.class, method.getMCReturnType().getMCType());

assertTrue(method.isEmptyCDParameters());

assertTrue(Log.getFindings().isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public void testStateSymbol1() throws IOException {
assertEquals("ping",s1.getName());
assertEquals("",s1.getPackageName());
assertEquals("ping",s1.getFullName());
// Test the generated toString method()
assertEquals("StateSymbol{fullName='ping', sourcePosition=<0,0>}",s1.toString());
assertTrue(Log.getFindings().isEmpty());
}

Expand Down

0 comments on commit d30f9df

Please sign in to comment.