Skip to content

Commit

Permalink
improve Parser error message for chain colon inside parentheses (SAP#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrassau committed Sep 22, 2023
1 parent 634d823 commit ec8759c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2739,29 +2739,7 @@ public final boolean changesSyTFillOrTLeng() {

return false;
}
/** Returns true if the Command matches a hard-coded pattern or condition.
* This method can be used during development to search for examples in all sample code files. */
public final boolean matchesPattern() {
// useful snippets:
// - is this a certain ABAP command?
// return firstCodeTokenIsKeyword("ABAP_KEYWORD");
// - is a certain Token found anywhere?
// Token token = firstToken.getLastTokenDeep(true, TokenSearch.ASTERISK, "SEARCH_TEXT|ALTERNATIVE|...");
// return (token != null && ...);
// - was a certain cleanup rule used?
// return changeControl.wasRuleUsed(RuleID....);
// - is a certain system field modified with at least 2 reads on this system field in subsequent program flow?
// return changesSyField(ABAP.SyField.SUBRC) && SyFieldAnalyzer.getSyFieldReadersFor(ABAP.SyField.SUBRC, this).size() >= 2;
// - getCommandsRelatedToPatternMatch() can then return SyFieldAnalyzer.getSyFieldReadersFor(ABAP.SyField.SUBRC, this);

return this.getFirstToken().isAnyKeyword("WITH", "ENDWITH");
//return false;
}

public final ArrayList<Command> getCommandsRelatedToPatternMatch() {
return null;
}


public final boolean containsMethodCallInsideConstructorExp() {
Token token = firstToken;
while (token != null) {
Expand Down Expand Up @@ -2789,4 +2767,33 @@ public final boolean containsFunctionalMethodCallBetween(Token startToken, Token
}
return false;
}

public final boolean containsChainColonInsideParentheses() {
Token token = firstToken.getLastTokenDeep(true, TokenSearch.ASTERISK, ":");
return (token != null && token.getParent() != null);
}

/** Returns true if the Command matches a hard-coded pattern or condition.
* This method can be used during development to search for examples in all sample code files. */
public final boolean matchesPattern() {
// useful snippets:
// - is this a certain ABAP command?
// return firstCodeTokenIsKeyword("ABAP_KEYWORD");
// - is a certain Token found anywhere?
// Token token = firstToken.getLastTokenDeep(true, TokenSearch.ASTERISK, "SEARCH_TEXT|ALTERNATIVE|...");
// return (token != null && ...);
// - was a certain cleanup rule used?
// return changeControl.wasRuleUsed(RuleID....);
// - is a certain system field modified with at least 2 reads on this system field in subsequent program flow?
// return changesSyField(ABAP.SyField.SUBRC) && SyFieldAnalyzer.getSyFieldReadersFor(ABAP.SyField.SUBRC, this).size() >= 2;
// - getCommandsRelatedToPatternMatch() can then return SyFieldAnalyzer.getSyFieldReadersFor(ABAP.SyField.SUBRC, this);

return this.getFirstToken().isAnyKeyword("WITH", "ENDWITH");
//return false;
}

public final ArrayList<Command> getCommandsRelatedToPatternMatch() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,16 @@ public final void addNext(Token newToken) throws UnexpectedSyntaxException {
if (opensLevel && !newToken.closesLevel) {
addChild(newToken);
} else if (!opensLevel && newToken.closesLevel) {
if (parent == null)
throw new UnexpectedSyntaxException(this, "The Token '" + newToken.text + "' cannot be added to '" + text + "', because the latter has no parent Token.");
if (parent == null) {
String msg ;
if (parentCommand.containsChainColonInsideParentheses()) {
msg = "Chain colons inside parentheses or brackets are currently not supported by " + Program.PRODUCT_NAME + ". ";
msg += "Please rewrite this ABAP statement manually before running " + Program.PRODUCT_NAME + " on this code.";
} else {
msg = "The Token '" + newToken.text + "' cannot be added to '" + text + "', because the latter has no parent Token.";
}
throw new UnexpectedSyntaxException(this, msg);
}
parent.addSibling(newToken);
} else {
addSibling(newToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ void testAddNextErr() {
}
}

@Test
void testColonInsideParens() {
String codeText = "any_method( : 1 ), 2 ).";
try {
Code.parse(null, ParseParams.createForTest(codeText, ABAP.NEWEST_RELEASE));
fail();
} catch(ParseException ex) {
// expected case
assertTrue(ex.getMessage().indexOf("Chain colons inside parentheses") >= 0);
assertTrue(ex.getMessage().indexOf("not supported") >= 0);
assertTrue(ex.getMessage().indexOf("Please rewrite") >= 0);
}
}

@Test
void testGetLastTokenOfSequence() {
Command command = buildCommand("a = 1 + 2.");
Expand Down

0 comments on commit ec8759c

Please sign in to comment.