From a0ac79764aa80592eadbccab371d27d8350dcaa8 Mon Sep 17 00:00:00 2001 From: Iurii Shchekochikhin Date: Fri, 17 May 2024 17:00:33 +0200 Subject: [PATCH] fix: alter go to support in experimental parser --- .../lsp/cobol/parser/hw/ParsingContext.java | 3 +- .../cobol/rules/procedure/ParagraphRule.java | 6 +++ .../divisions/procedure/AlterGoToTest.java | 48 +++++++++++++++++++ .../cobol/divisions/procedure/BrokenTest.java | 2 - 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/AlterGoToTest.java diff --git a/server/parser/src/main/java/org/eclipse/lsp/cobol/parser/hw/ParsingContext.java b/server/parser/src/main/java/org/eclipse/lsp/cobol/parser/hw/ParsingContext.java index 0496f76bf3..bdb0f40d76 100644 --- a/server/parser/src/main/java/org/eclipse/lsp/cobol/parser/hw/ParsingContext.java +++ b/server/parser/src/main/java/org/eclipse/lsp/cobol/parser/hw/ParsingContext.java @@ -173,7 +173,8 @@ public CstNode peek() { * @return true if we have a match */ public boolean matchSeq(String... lexemes) { - List tokens = lexer.peekSeq(lexemes.length, t -> t.getType() == TokenType.WHITESPACE); + List tokens = lexer.peekSeq(lexemes.length, + t -> t.getType() == TokenType.WHITESPACE || t.getType() == TokenType.NEW_LINE); if (tokens.size() != lexemes.length) { return false; } diff --git a/server/parser/src/main/java/org/eclipse/lsp/cobol/rules/procedure/ParagraphRule.java b/server/parser/src/main/java/org/eclipse/lsp/cobol/rules/procedure/ParagraphRule.java index d2ee51ac24..079c739601 100644 --- a/server/parser/src/main/java/org/eclipse/lsp/cobol/rules/procedure/ParagraphRule.java +++ b/server/parser/src/main/java/org/eclipse/lsp/cobol/rules/procedure/ParagraphRule.java @@ -58,6 +58,12 @@ public boolean tryMatch(ParsingContext ctx, CobolLanguage language) { if (!CobolLanguageUtils.isInAriaA(nameToken)) { return false; } + if (ctx.matchSeq(null, ".", "GO", "TO", ".")) { + return false; + } + if (ctx.matchSeq(null, ".", "GO", ".")) { + return false; + } return true; } } diff --git a/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/AlterGoToTest.java b/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/AlterGoToTest.java new file mode 100644 index 0000000000..807aca987b --- /dev/null +++ b/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/AlterGoToTest.java @@ -0,0 +1,48 @@ +/* + * 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 + * + */ +package org.eclipse.lsp.cobol.divisions.procedure; + +import org.eclipse.lsp.cobol.cst.ProgramUnit; +import org.eclipse.lsp.cobol.cst.procedure.Paragraph; +import org.eclipse.lsp.cobol.cst.procedure.ProcedureDivision; +import org.eclipse.lsp.cobol.parser.hw.CobolParser; +import org.eclipse.lsp.cobol.parser.hw.ParseResult; +import org.eclipse.lsp.cobol.parser.hw.ParserSettings; +import org.eclipse.lsp.cobol.parser.hw.lexer.CobolLexer; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** test alter go - to */ +public class AlterGoToTest { + @Test + void test() { + String source = " ID DIVISION. PROGRAM-ID. str1.\n" + + " PROCEDURE DIVISION.\n" + + " DISPLAY 'OUT'.\n" + + " PARAG2.\n" + + " GO TO.\n" + + " PARAG2.\n" + + " DISPLAY 'PARAG2'.\n"; + ParseResult parseResult = new CobolParser(new CobolLexer(source), new ParserSettings()).parse(); + assertTrue(parseResult.getDiagnostics().isEmpty()); + ProgramUnit pu = (ProgramUnit) parseResult.getSourceUnit().getChildren().get(1); + ProcedureDivision pd = (ProcedureDivision) pu.getChildren().get(1); + assertEquals(1, pd.getChildren().stream().filter(Paragraph.class::isInstance).count()); + assertEquals(source, parseResult.getSourceUnit().toText()); + } + +} diff --git a/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/BrokenTest.java b/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/BrokenTest.java index 6c06490d34..cb2daff2ac 100644 --- a/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/BrokenTest.java +++ b/server/parser/src/test/java/org/eclipse/lsp/cobol/divisions/procedure/BrokenTest.java @@ -10,8 +10,6 @@ * * Contributors: * Broadcom, Inc. - initial API and implementation - * DAF Trucks NV – implementation of DaCo COBOL statements - * and DAF development standards * */ package org.eclipse.lsp.cobol.divisions.procedure;