Skip to content

Commit b8a0939

Browse files
committed
Move compiler directives processing to line reader GH-116
Replace compiler directives removal with moving it to the content area. Change transformation order not to remove syntax errors of not fitting the fixed format.
1 parent 12f8c74 commit b8a0939

File tree

6 files changed

+103
-77
lines changed

6 files changed

+103
-77
lines changed

com.ca.lsp.cobol/lsp-core-cobol-parser/src/main/java/com/ca/lsp/core/cobol/preprocessor/impl/CobolPreprocessorImpl.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.ca.lsp.core.cobol.preprocessor.sub.line.rewriter.impl.CobolInlineCommentEntriesNormalizerImpl;
3535
import com.ca.lsp.core.cobol.preprocessor.sub.line.rewriter.impl.CobolLineIndicatorProcessorImpl;
3636
import com.ca.lsp.core.cobol.preprocessor.sub.line.transformer.CobolLinesTransformation;
37-
import com.ca.lsp.core.cobol.preprocessor.sub.line.transformer.CobolUnsupportedFeaturesIgnorerImpl;
3837
import com.ca.lsp.core.cobol.preprocessor.sub.line.transformer.ContinuationLineTransformation;
3938
import com.ca.lsp.core.cobol.preprocessor.sub.line.writer.CobolLineWriter;
4039
import com.ca.lsp.core.cobol.preprocessor.sub.line.writer.impl.CobolLineWriterImpl;
@@ -95,8 +94,7 @@ private List<CobolLine> readLines(
9594
}
9695

9796
private List<CobolLine> transformLines(List<CobolLine> lines, String documentURI) {
98-
List<CobolLine> transformedLines = createUnsupportedFeaturesProcessor().transformLines(lines);
99-
return createContinuationLineProcessor(documentURI).transformLines(transformedLines);
97+
return createContinuationLineProcessor(documentURI).transformLines(lines);
10098
}
10199

102100
/**
@@ -135,10 +133,6 @@ private CobolLineIndicatorProcessor createLineIndicatorProcessor() {
135133
return new CobolLineIndicatorProcessorImpl();
136134
}
137135

138-
private CobolLinesTransformation createUnsupportedFeaturesProcessor() {
139-
return new CobolUnsupportedFeaturesIgnorerImpl(listener);
140-
}
141-
142136
private CobolLinesTransformation createContinuationLineProcessor(String documentURI) {
143137
return new ContinuationLineTransformation(listener, documentURI);
144138
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2020 Broadcom.
3+
*
4+
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
5+
*
6+
* This program and the accompanying materials are made
7+
* available under the terms of the Eclipse Public License 2.0
8+
* which is available at https://www.eclipse.org/legal/epl-2.0/
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
*
12+
* Contributors:
13+
* Broadcom, Inc. - initial API and implementation
14+
*/
15+
16+
package com.ca.lsp.core.cobol.preprocessor.sub.line.reader;
17+
18+
public interface CobolLineReaderDelegate {
19+
String apply(String line);
20+
}

com.ca.lsp.cobol/lsp-core-cobol-parser/src/main/java/com/ca/lsp/core/cobol/preprocessor/sub/line/reader/impl/CobolLineReaderImpl.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.ca.lsp.core.cobol.preprocessor.sub.CobolLine;
2020
import com.ca.lsp.core.cobol.preprocessor.sub.CobolLineTypeEnum;
2121
import com.ca.lsp.core.cobol.preprocessor.sub.line.reader.CobolLineReader;
22+
import com.ca.lsp.core.cobol.preprocessor.sub.line.reader.CobolLineReaderDelegate;
2223

2324
import java.util.ArrayList;
2425
import java.util.List;
@@ -30,6 +31,7 @@
3031

3132
public class CobolLineReaderImpl implements CobolLineReader {
3233
private static final int INDICATOR_AREA_INDEX = 6;
34+
3335
private PreprocessorListener listener;
3436
private String documentURI;
3537

@@ -69,10 +71,12 @@ public CobolLine parseLine(
6971
final CobolSourceFormat format,
7072
final CobolParserParams params) {
7173
final Pattern pattern = format.getPattern();
72-
final Matcher matcher = pattern.matcher(line);
7374

7475
CobolLine cobolLine = new CobolLine();
7576

77+
line = getDelegate().apply(line);
78+
79+
final Matcher matcher = pattern.matcher(line);
7680
line = checkFormatCorrect(line, lineNumber, format, matcher);
7781

7882
if (line.length() > 0) {
@@ -150,6 +154,10 @@ private String checkFormatCorrect(
150154
return line;
151155
}
152156

157+
private CobolLineReaderDelegate getDelegate() {
158+
return new CompilerDirectivesTransformation();
159+
}
160+
153161
private void registerFormatError(
154162
int lineNumber, final CobolSourceFormat format, int charPosition, int errorLength) {
155163
if (listener == null) return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2020 Broadcom.
3+
*
4+
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
5+
*
6+
* This program and the accompanying materials are made
7+
* available under the terms of the Eclipse Public License 2.0
8+
* which is available at https://www.eclipse.org/legal/epl-2.0/
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
*
12+
* Contributors:
13+
* Broadcom, Inc. - initial API and implementation
14+
*/
15+
16+
package com.ca.lsp.core.cobol.preprocessor.sub.line.reader.impl;
17+
18+
import com.ca.lsp.core.cobol.preprocessor.sub.line.reader.CobolLineReaderDelegate;
19+
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
23+
public class CompilerDirectivesTransformation implements CobolLineReaderDelegate {
24+
private static final Pattern COMPILER_DIRECTIVE_LINE =
25+
Pattern.compile("(?i)(.{0,6} +|\\s*)(CBL|PROCESS) .+");
26+
27+
@Override
28+
public String apply(String line) {
29+
final Matcher compilerConstantMatcher = COMPILER_DIRECTIVE_LINE.matcher(line);
30+
if (!compilerConstantMatcher.matches()) {
31+
return line;
32+
}
33+
line = cutTooLongString(line);
34+
return moveContentFromCommentArea(line);
35+
}
36+
37+
private String cutTooLongString(String line) {
38+
if (line.length() == 80) {
39+
line = line.substring(0, 72).trim();
40+
}
41+
return line;
42+
}
43+
44+
private String moveContentFromCommentArea(String line) {
45+
int index = getLineContentStart(line);
46+
return " " + line.substring(index);
47+
}
48+
49+
private int getLineContentStart(String line) {
50+
int cbl = line.toUpperCase().indexOf("CBL");
51+
int process = line.toUpperCase().indexOf("PROCESS");
52+
int max = Math.max(cbl, process);
53+
return max == -1 ? 0 : max;
54+
}
55+
}

com.ca.lsp.cobol/lsp-core-cobol-parser/src/main/java/com/ca/lsp/core/cobol/preprocessor/sub/line/transformer/CobolUnsupportedFeaturesIgnorerImpl.java

-53
This file was deleted.

com.ca.lsp.cobol/lsp-service-cobol/src/test/java/com/ca/lsp/cobol/usecases/TestNoErrorOnCompilerDirectives.java

+18-16
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,21 @@ public TestNoErrorOnCompilerDirectives() {
4343
super(null);
4444
}
4545

46-
private static final String CBL_WITHOUT_NUMBERS =
47-
" CBL DIR1,DIR2,DIR3,DIR4,DIR5,DIR6,DIR7,DIR8\r\n";
46+
private static final String CBL_WITHOUT_NUMBERS = " cbl LIB,QUOTE,NODYNAM,TEST(SEP)\r\n";
4847

49-
private static final String CBL_WITH_NUMBER =
50-
"012345 CBL DIR1,DIR2,DIR3,DIR4,DIR5,DIR6,DIR7,DIR8\r\n";
48+
private static final String CBL_WITH_NUMBER = "012345 CBL DATA(24),RMODE(24),NODYNAM\r\n";
5149

52-
private static final String PROCESS_WITHOUT_NUMBER =
53-
"PROCESS DIR1,DIR2,DIR3,DIR4,DIR5,DIR6,DIR7,DIR8\r\n";
50+
private static final String PROCESS_WITHOUT_NUMBER = "PROCESS DATA(24),RMODE(24),NODYNAM\r\n";
5451

55-
private static final String PROCESS_WITH_NUMBER =
56-
"012345 PROCESS DIR1,DIR2,DIR3,DIR4,DIR5,DIR6,DIR7,DIR8\r\n";
52+
private static final String PROCESS_WITH_NUMBER = "012345 PROCESS DATA(24),RMODE(24),NODYNAM\r\n";
5753

58-
private static final String PROCESS_WITHOUT_NUMBER_TYPO =
59-
"PROESS DIR1,DIR2,DIR3,DIR4,DIR5,DIR6,DIR7,DIR8\r\n";
54+
private static final String PROCESS_WITHOUT_NUMBER_TYPO = "PROESS DATA(24),RMODE(24),NODYNAM\r\n";
55+
56+
private static final String CBL_WITH_LONG_LINE =
57+
" CBL LIB,QUOTE,NODYNAM,TEST(SEP) 00010003\r\n";
6058

6159
private static final String PROCESS_WITH_NUMBER_TYPO =
62-
"012345 PROESS DIR1,DIR2,DIR3,DIR4,DIR5,DIR6,DIR7,DIR8\r\n";
60+
"012345 PROESS DATA(24),RMODE(24),NODYNAM\r\n";
6361

6462
private static final String FOLLOWING_TEXT =
6563
"000000 Identification DIVISION. 23323232\r\n"
@@ -97,20 +95,24 @@ public void testProcessWithNumbers() {
9795
super.test();
9896
}
9997

100-
@Ignore("Feature is not yet supported")
98+
@Test
99+
public void testCblWithLongLine() {
100+
setText(CBL_WITH_LONG_LINE + FOLLOWING_TEXT);
101+
super.test();
102+
}
103+
101104
@Test
102105
public void testProcessWithoutNumbersWithTypo() {
103106
Range range = retrieveRange(analyzeForErrors(PROCESS_WITHOUT_NUMBER_TYPO + FOLLOWING_TEXT));
104107

105-
assertEquals(1, range.getStart().getLine());
108+
assertEquals(0, range.getStart().getLine());
106109
assertEquals(7, range.getStart().getCharacter());
107110
}
108111

109-
@Ignore("Feature is not yet supported")
110112
@Test
111113
public void testProcessWithNumbersWithTypo() {
112114
Range range = retrieveRange(analyzeForErrors(PROCESS_WITH_NUMBER_TYPO + FOLLOWING_TEXT));
113-
assertEquals(1, range.getStart().getLine());
115+
assertEquals(0, range.getStart().getLine());
114116
assertEquals(7, range.getStart().getCharacter());
115117
}
116118

@@ -119,7 +121,7 @@ public void testProcessWithNumbersWithTypo() {
119121
public void testLinesBeforeCblNotAllowed() {
120122
Range range = retrieveRange(analyzeForErrors(FOLLOWING_TEXT + CBL_WITH_NUMBER));
121123

122-
assertEquals(4, range.getStart().getLine());
124+
assertEquals(1, range.getStart().getLine());
123125
assertEquals(7, range.getStart().getCharacter());
124126
}
125127

0 commit comments

Comments
 (0)