Skip to content

Commit 92f807c

Browse files
authored
Merge pull request #153 from eclipse/engineering-excellence
Make Preprocessor Parts Stateless to Simplify DI #24
2 parents 5d94264 + 4ee6895 commit 92f807c

File tree

13 files changed

+431
-314
lines changed

13 files changed

+431
-314
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ private ResultWithErrors<PreprocessedInput> parseDocument(
8888
final String document,
8989
final CobolSourceFormat format,
9090
final SemanticContext semanticContext) {
91-
return createDocumentParser(semanticContext).processLines(document, format);
91+
return createDocumentParser().processLines(document, semanticContext, format);
9292
}
9393

9494
private ResultWithErrors<List<CobolLine>> readLines(
9595
final String cobolCode, final CobolSourceFormat format, String documentURI) {
96-
return createLineReader(documentURI).processLines(cobolCode, format);
96+
return createLineReader().processLines(documentURI, cobolCode, format);
9797
}
9898

9999
private ResultWithErrors<List<CobolLine>> transformLines(
@@ -117,8 +117,8 @@ private CobolCommentEntriesMarker createCommentEntriesMarker() {
117117
return new CobolCommentEntriesMarkerImpl();
118118
}
119119

120-
private CobolSemanticParser createDocumentParser(SemanticContext semanticContext) {
121-
return new CobolSemanticParserImpl(semanticContext);
120+
private CobolSemanticParser createDocumentParser() {
121+
return new CobolSemanticParserImpl();
122122
}
123123

124124
private CobolDocumentCleaner createDocumentCleaner() {
@@ -137,8 +137,8 @@ private CobolLinesTransformation createContinuationLineProcessor() {
137137
return new ContinuationLineTransformation();
138138
}
139139

140-
private CobolLineReader createLineReader(String documentURI) {
141-
return new CobolLineReaderImpl(documentURI);
140+
private CobolLineReader createLineReader() {
141+
return new CobolLineReaderImpl();
142142
}
143143

144144
private CobolLineWriter createLineWriter() {

com.ca.lsp.cobol/lsp-core-cobol-parser/src/main/java/com/ca/lsp/core/cobol/preprocessor/sub/CobolLine.java

+45-228
Original file line numberDiff line numberDiff line change
@@ -15,162 +15,52 @@
1515

1616
import com.ca.lsp.core.cobol.params.CobolDialect;
1717
import com.ca.lsp.core.cobol.preprocessor.CobolSourceFormat;
18-
import com.ca.lsp.core.cobol.preprocessor.ProcessingConstants;
19-
import com.google.common.base.Strings;
20-
21-
import static com.ca.lsp.core.cobol.preprocessor.CobolSourceFormat.TANDEM;
18+
import com.ca.lsp.core.cobol.preprocessor.sub.util.CobolLineUtils;
19+
import lombok.Data;
20+
import lombok.NoArgsConstructor;
2221

22+
/**
23+
* This class represents a structure for a COBOL code line that is used for parsing. The exact line
24+
* structure depends on the format, see {@link CobolSourceFormat}
25+
*/
26+
@Data
27+
@NoArgsConstructor
2328
public class CobolLine {
24-
protected String commentArea = "";
25-
protected String commentAreaOriginal = "";
26-
protected String contentAreaA = "";
27-
protected String contentAreaAOriginal = "";
28-
protected String contentAreaB = "";
29-
protected String contentAreaBOriginal = "";
30-
protected CobolDialect dialect;
31-
protected CobolSourceFormat format;
32-
protected String indicatorArea = " ";
33-
protected String indicatorAreaOriginal = "";
34-
protected int number;
35-
protected CobolLine predecessor;
36-
protected String sequenceArea = "";
37-
protected String sequenceAreaOriginal = "";
38-
protected CobolLine successor;
39-
protected CobolLineTypeEnum type = CobolLineTypeEnum.NORMAL;
29+
private String commentArea = "";
30+
private String contentAreaA = "";
31+
private String contentAreaB = "";
32+
private CobolDialect dialect;
33+
private CobolSourceFormat format;
34+
private String indicatorArea = " ";
35+
private int number;
36+
private CobolLine predecessor;
37+
private String sequenceArea = "";
38+
private CobolLine successor;
39+
private CobolLineTypeEnum type = CobolLineTypeEnum.NORMAL;
4040

41-
// getter & setter
41+
/**
42+
* Create and return a blank sequence area depended on the type of the line
43+
*
44+
* @return an empty String or a String with white spaces
45+
*/
4246
public String getBlankSequenceArea() {
43-
return createBlankSequenceArea(format);
44-
}
45-
46-
public String getCommentArea() {
47-
return commentArea;
48-
}
49-
50-
public String getCommentAreaOriginal() {
51-
return commentAreaOriginal;
47+
return CobolLineUtils.createBlankSequenceArea(format);
5248
}
5349

50+
/**
51+
* Build and return a significant for syntax parsing content line
52+
*
53+
* @return a String with combined content areas
54+
*/
5455
public String getContentArea() {
5556
return contentAreaA + contentAreaB;
5657
}
5758

58-
public String getContentAreaA() {
59-
return contentAreaA;
60-
}
61-
62-
public String getContentAreaAOriginal() {
63-
return contentAreaAOriginal;
64-
}
65-
66-
public String getContentAreaB() {
67-
return contentAreaB;
68-
}
69-
70-
public String getContentAreaBOriginal() {
71-
return contentAreaBOriginal;
72-
}
73-
74-
public String getContentAreaOriginal() {
75-
return contentAreaAOriginal + contentAreaBOriginal;
76-
}
77-
78-
public CobolDialect getDialect() {
79-
return dialect;
80-
}
81-
82-
public CobolSourceFormat getFormat() {
83-
return format;
84-
}
85-
86-
public String getIndicatorArea() {
87-
return indicatorArea;
88-
}
89-
90-
public String getIndicatorAreaOriginal() {
91-
return indicatorAreaOriginal;
92-
}
93-
94-
public int getNumber() {
95-
return number;
96-
}
97-
98-
public CobolLine getPredecessor() {
99-
return predecessor;
100-
}
101-
102-
public String getSequenceArea() {
103-
return sequenceArea;
104-
}
105-
106-
public String getSequenceAreaOriginal() {
107-
return sequenceAreaOriginal;
108-
}
109-
110-
public CobolLine getSuccessor() {
111-
return successor;
112-
}
113-
114-
public CobolLineTypeEnum getType() {
115-
return type;
116-
}
117-
118-
public void setCommentArea(String commentArea) {
119-
this.commentArea = commentArea;
120-
}
121-
122-
public void setCommentAreaOriginal(String commentAreaOriginal) {
123-
this.commentAreaOriginal = commentAreaOriginal;
124-
}
125-
126-
public void setContentAreaA(String contentAreaA) {
127-
this.contentAreaA = contentAreaA;
128-
}
129-
130-
public void setContentAreaAOriginal(String contentAreaAOriginal) {
131-
this.contentAreaAOriginal = contentAreaAOriginal;
132-
}
133-
134-
public void setContentAreaB(String contentAreaB) {
135-
this.contentAreaB = contentAreaB;
136-
}
137-
138-
public void setContentAreaBOriginal(String contentAreaBOriginal) {
139-
this.contentAreaBOriginal = contentAreaBOriginal;
140-
}
141-
142-
public void setDialect(CobolDialect dialect) {
143-
this.dialect = dialect;
144-
}
145-
146-
public void setFormat(CobolSourceFormat format) {
147-
this.format = format;
148-
}
149-
150-
public void setIndicatorArea(String indicatorArea) {
151-
this.indicatorArea = indicatorArea;
152-
}
153-
154-
public void setIndicatorAreaOriginal(String indicatorAreaOriginal) {
155-
this.indicatorAreaOriginal = indicatorAreaOriginal;
156-
}
157-
158-
public void setNumber(int number) {
159-
this.number = number;
160-
}
161-
162-
public void setSequenceArea(String sequenceArea) {
163-
this.sequenceArea = sequenceArea;
164-
}
165-
166-
public void setSequenceAreaOriginal(String sequenceAreaOriginal) {
167-
this.sequenceAreaOriginal = sequenceAreaOriginal;
168-
}
169-
170-
public void setType(CobolLineTypeEnum type) {
171-
this.type = type;
172-
}
173-
59+
/**
60+
* Set previous line and bind this lines together
61+
*
62+
* @param predecessor - the previous line in a document
63+
*/
17464
public void setPredecessor(final CobolLine predecessor) {
17565
this.predecessor = predecessor;
17666

@@ -179,6 +69,11 @@ public void setPredecessor(final CobolLine predecessor) {
17969
}
18070
}
18171

72+
/**
73+
* Set following line and bind this lines together
74+
*
75+
* @param successor - the previous line in a document
76+
*/
18277
public void setSuccessor(final CobolLine successor) {
18378
this.successor = successor;
18479

@@ -187,91 +82,13 @@ public void setSuccessor(final CobolLine successor) {
18782
}
18883
}
18984

190-
public static CobolLine copyCobolLineWithContentArea(
191-
final String contentArea, final CobolLine line) {
192-
CobolLine cobolLine = copyCobolLine(line);
193-
cobolLine.setContentAreaA(extractContentAreaA(contentArea));
194-
cobolLine.setContentAreaB(extractContentAreaB(contentArea));
195-
return cobolLine;
196-
}
197-
19885
/**
199-
* @param indicatorArea
200-
* @param contentArea
201-
* @param line
202-
* @return new CobolLine
86+
* Serialize the line and combine the fields that are significant for parsing
87+
*
88+
* @return combination of string parts of the COBOL line
20389
*/
204-
public static CobolLine copyCobolLineWithIndicatorAndContentArea(
205-
final String indicatorArea, final String contentArea, final CobolLine line) {
206-
207-
CobolLine cobolLine = copyCobolLine(line);
208-
cobolLine.setIndicatorArea(indicatorArea);
209-
cobolLine.setContentAreaA(extractContentAreaA(contentArea));
210-
cobolLine.setContentAreaB(extractContentAreaB(contentArea));
211-
return cobolLine;
212-
}
213-
214-
public static CobolLine copyCobolLineWithIndicatorArea(
215-
final String indicatorArea, final CobolLine line) {
216-
CobolLine cobolLine = copyCobolLine(line);
217-
cobolLine.setIndicatorArea(indicatorArea);
218-
return cobolLine;
219-
}
220-
221-
public static CobolLine copyCobolLineWithEmptyContent(final CobolLine line) {
222-
CobolLine cobolLine = new CobolLine();
223-
cobolLine.setFormat(line.format);
224-
cobolLine.setDialect(line.dialect);
225-
cobolLine.setNumber(line.number);
226-
cobolLine.setType(line.type);
227-
cobolLine.setPredecessor(line.predecessor);
228-
cobolLine.setSuccessor(line.successor);
229-
return cobolLine;
230-
}
231-
232-
private static CobolLine copyCobolLine(final CobolLine line) {
233-
CobolLine cobolLine = new CobolLine();
234-
cobolLine.setSequenceArea(line.getSequenceArea());
235-
cobolLine.setSequenceAreaOriginal(line.getSequenceAreaOriginal());
236-
cobolLine.setIndicatorArea(line.indicatorArea);
237-
cobolLine.setIndicatorAreaOriginal(line.indicatorAreaOriginal);
238-
cobolLine.setContentAreaA(line.contentAreaA);
239-
cobolLine.setContentAreaAOriginal(line.contentAreaAOriginal);
240-
cobolLine.setContentAreaB(line.contentAreaB);
241-
cobolLine.setContentAreaBOriginal(line.contentAreaBOriginal);
242-
cobolLine.setCommentArea(line.commentArea);
243-
cobolLine.setCommentAreaOriginal(line.commentAreaOriginal);
244-
cobolLine.setFormat(line.format);
245-
cobolLine.setDialect(line.dialect);
246-
cobolLine.setNumber(line.number);
247-
cobolLine.setType(line.type);
248-
cobolLine.setPredecessor(line.predecessor);
249-
cobolLine.setSuccessor(line.successor);
250-
return cobolLine;
251-
}
252-
253-
public static String createBlankSequenceArea(final CobolSourceFormat format) {
254-
return TANDEM.equals(format) ? "" : Strings.repeat(ProcessingConstants.WS, 6);
255-
}
256-
257-
protected static String extractContentAreaA(final String contentArea) {
258-
return contentArea.length() > 4 ? contentArea.substring(0, 4) : contentArea;
259-
}
260-
261-
protected static String extractContentAreaB(final String contentArea) {
262-
return contentArea.length() > 4 ? contentArea.substring(4) : "";
263-
}
264-
265-
public String serialize() {
266-
return sequenceArea + indicatorArea + contentAreaA + contentAreaB + commentArea;
267-
}
268-
269-
public String serializeWithoutCommentArea() {
270-
return sequenceArea + indicatorArea + contentAreaA + contentAreaB;
271-
}
272-
27390
@Override
27491
public String toString() {
275-
return serialize();
92+
return sequenceArea + indicatorArea + contentAreaA + contentAreaB + commentArea;
27693
}
27794
}

com.ca.lsp.cobol/lsp-core-cobol-parser/src/main/java/com/ca/lsp/core/cobol/preprocessor/sub/document/CobolSemanticParser.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,28 @@
1616
import com.ca.lsp.core.cobol.model.PreprocessedInput;
1717
import com.ca.lsp.core.cobol.model.ResultWithErrors;
1818
import com.ca.lsp.core.cobol.preprocessor.CobolSourceFormat;
19+
import com.ca.lsp.core.cobol.semantics.SemanticContext;
1920

21+
import javax.annotation.Nonnull;
22+
23+
/**
24+
* Preprocessor which retrieves semantic elements definitions, such as variables, paragraphs and
25+
* copybooks
26+
*/
2027
public interface CobolSemanticParser {
2128

29+
/**
30+
* Fills-in the semantic context of the current document, including elements from the copybooks
31+
*
32+
* @param code - COBOL program text to analyse
33+
* @param semanticContext - semantic context of the currently processed document to be filled in.
34+
* @param format - the format of the processing document
35+
* @return a PreprocessedInput - text and its semantic context with syntax errors if found or an
36+
* empty list
37+
*/
38+
@Nonnull
2239
ResultWithErrors<PreprocessedInput> processLines(
23-
String code, CobolSourceFormat format);
40+
@Nonnull String code,
41+
@Nonnull SemanticContext semanticContext,
42+
@Nonnull CobolSourceFormat format);
2443
}

0 commit comments

Comments
 (0)