Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

US910181 - Check the variable rules #2366

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Nurkambay marked this conversation as resolved.
Show resolved Hide resolved
postprocessing.copybookHasErrors=Errors inside the copybook
ErrorStrategy.endOfFile=Unexpected end of file
ErrorStrategy.endOfLine=Unexpected end of line
Expand Down
Original file line number Diff line number Diff line change
@@ -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())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Loading