diff --git a/server/engine/src/main/resources/resourceBundles/messages_en.properties b/server/engine/src/main/resources/resourceBundles/messages_en.properties index cb5c64b47b..f2f0b77e2d 100644 --- a/server/engine/src/main/resources/resourceBundles/messages_en.properties +++ b/server/engine/src/main/resources/resourceBundles/messages_en.properties @@ -39,6 +39,7 @@ parsers.alphaNumeric=Only alphanumerics are allowed for %s parsers.startsWith=String must starts with %s values parsers.stringLengthRange=String length must be between %s and %s parsers.allowedStringValues=Only allowed value(s): %s +parsers.notAllowedVariableName=Variable name %s is not allowed postprocessing.copybookHasErrors=Errors inside the copybook ErrorStrategy.endOfFile=Unexpected end of file ErrorStrategy.endOfLine=Unexpected end of line diff --git a/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/TestVariableNameNotAllowed.java b/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/TestVariableNameNotAllowed.java new file mode 100644 index 0000000000..74d7ecfd1c --- /dev/null +++ b/server/engine/src/test/java/org/eclipse/lsp/cobol/usecases/TestVariableNameNotAllowed.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024 Broadcom. + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Broadcom, Inc. - initial API and implementation + * DAF Trucks NV – implementation of DaCo COBOL statements + * and DAF development standards + * + */ +package org.eclipse.lsp.cobol.usecases; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.eclipse.lsp.cobol.common.error.ErrorSource; +import org.eclipse.lsp.cobol.test.engine.UseCaseEngine; +import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.DiagnosticSeverity; +import org.eclipse.lsp4j.Range; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** Tests the allowed name on a variable. */ +public class TestVariableNameNotAllowed { + public static final String TEXT = + " IDENTIFICATION DIVISION.\n" + + " PROGRAM-ID. TEST12.\n" + + " ENVIRONMENT DIVISION.\n" + + " DATA DIVISION.\n" + + " WORKING-STORAGE SECTION.\n" + + " 01 {$*%s|1} PIC 9(9) VALUE 0.\n" + + " PROCEDURE DIVISION."; + + @ParameterizedTest + @ValueSource(strings = {"INSERT", "BIT", "CONDITION", "COPY", "CURSOR"}) + void test(String input) { + UseCaseEngine.runTest( + String.format(TEXT, input), + ImmutableList.of(), + ImmutableMap.of( + "1", + new Diagnostic( + new Range(), + String.format("Variable name %s is not allowed", input), + DiagnosticSeverity.Error, + ErrorSource.PARSING.getText()))); + } +} diff --git a/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolDataDivisionParser.g4 b/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolDataDivisionParser.g4 index fc4e622abe..8e86586aca 100644 --- a/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolDataDivisionParser.g4 +++ b/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolDataDivisionParser.g4 @@ -204,7 +204,9 @@ dialectDescriptionEntry ; entryName - : (FILLER | { validateLength(_input.LT(1).getText(), "Variable name", 30);} dataName) + : (FILLER | { validateLength(_input.LT(1).getText(), "Variable name", 30); + validateAllowedVariableName(_input.LT(1).getText(), "INSERT", + "BIT", "CONDITION", "COPY", "CURSOR");} dataName) ; dataGroupUsageClause diff --git a/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolParser.g4 b/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolParser.g4 index ed27ea86e1..ca84fde563 100644 --- a/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolParser.g4 +++ b/server/parser/src/main/antlr4/org/eclipse/lsp/cobol/core/parser/CobolParser.g4 @@ -579,7 +579,9 @@ dialectDescriptionEntry ; entryName - : (FILLER | { validateLength(_input.LT(1).getText(), "Variable name", 30);} dataName) + : (FILLER | { validateLength(_input.LT(1).getText(), "Variable name", 30); + validateAllowedVariableName(_input.LT(1).getText(), "INSERT", + "BIT", "CONDITION", "COPY", "CURSOR");} dataName) ; dataGroupUsageClause diff --git a/server/parser/src/main/java/org/eclipse/lsp/cobol/core/MessageServiceParser.java b/server/parser/src/main/java/org/eclipse/lsp/cobol/core/MessageServiceParser.java index 964a9d03a2..968fe32979 100644 --- a/server/parser/src/main/java/org/eclipse/lsp/cobol/core/MessageServiceParser.java +++ b/server/parser/src/main/java/org/eclipse/lsp/cobol/core/MessageServiceParser.java @@ -18,6 +18,8 @@ package org.eclipse.lsp.cobol.core; import com.google.common.annotations.VisibleForTesting; + +import java.util.Arrays; import java.util.regex.Pattern; import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.TokenStream; @@ -116,6 +118,18 @@ protected void validateLength(String input, String objectType, Integer validLeng } } + /** + * Validate allowed variable names and throw an error if it is incorrect + * + * @param input string to check + * @param notAllowedValues arrays of not allowed variable names for Input parameter + */ + protected void validateAllowedVariableName(String input, String... notAllowedValues) { + if (input != null && Arrays.asList(notAllowedValues).contains(input.toUpperCase())) { + notifyError("parsers.notAllowedVariableName", input); + } + } + /** * Validate a string for alphanumeric characters and throw an error if it is incorrect *