Skip to content

Make Preprocessor Parts Stateless to Simplify DI #24 #153

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

Merged
merged 5 commits into from
Jan 27, 2020
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 @@ -88,12 +88,12 @@ private ResultWithErrors<PreprocessedInput> parseDocument(
final String document,
final CobolSourceFormat format,
final SemanticContext semanticContext) {
return createDocumentParser(semanticContext).processLines(document, format);
return createDocumentParser().processLines(document, semanticContext, format);
}

private ResultWithErrors<List<CobolLine>> readLines(
final String cobolCode, final CobolSourceFormat format, String documentURI) {
return createLineReader(documentURI).processLines(cobolCode, format);
return createLineReader().processLines(documentURI, cobolCode, format);
}

private ResultWithErrors<List<CobolLine>> transformLines(
Expand All @@ -117,8 +117,8 @@ private CobolCommentEntriesMarker createCommentEntriesMarker() {
return new CobolCommentEntriesMarkerImpl();
}

private CobolSemanticParser createDocumentParser(SemanticContext semanticContext) {
return new CobolSemanticParserImpl(semanticContext);
private CobolSemanticParser createDocumentParser() {
return new CobolSemanticParserImpl();
}

private CobolDocumentCleaner createDocumentCleaner() {
Expand All @@ -137,8 +137,8 @@ private CobolLinesTransformation createContinuationLineProcessor() {
return new ContinuationLineTransformation();
}

private CobolLineReader createLineReader(String documentURI) {
return new CobolLineReaderImpl(documentURI);
private CobolLineReader createLineReader() {
return new CobolLineReaderImpl();
}

private CobolLineWriter createLineWriter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,162 +15,52 @@

import com.ca.lsp.core.cobol.params.CobolDialect;
import com.ca.lsp.core.cobol.preprocessor.CobolSourceFormat;
import com.ca.lsp.core.cobol.preprocessor.ProcessingConstants;
import com.google.common.base.Strings;

import static com.ca.lsp.core.cobol.preprocessor.CobolSourceFormat.TANDEM;
import com.ca.lsp.core.cobol.preprocessor.sub.util.CobolLineUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* This class represents a structure for a COBOL code line that is used for parsing. The exact line
* structure depends on the format, see {@link CobolSourceFormat}
*/
@Data
@NoArgsConstructor
public class CobolLine {
protected String commentArea = "";
protected String commentAreaOriginal = "";
protected String contentAreaA = "";
protected String contentAreaAOriginal = "";
protected String contentAreaB = "";
protected String contentAreaBOriginal = "";
protected CobolDialect dialect;
protected CobolSourceFormat format;
protected String indicatorArea = " ";
protected String indicatorAreaOriginal = "";
protected int number;
protected CobolLine predecessor;
protected String sequenceArea = "";
protected String sequenceAreaOriginal = "";
protected CobolLine successor;
protected CobolLineTypeEnum type = CobolLineTypeEnum.NORMAL;
private String commentArea = "";
private String contentAreaA = "";
private String contentAreaB = "";
private CobolDialect dialect;
private CobolSourceFormat format;
private String indicatorArea = " ";
private int number;
private CobolLine predecessor;
private String sequenceArea = "";
private CobolLine successor;
private CobolLineTypeEnum type = CobolLineTypeEnum.NORMAL;

// getter & setter
/**
* Create and return a blank sequence area depended on the type of the line
*
* @return an empty String or a String with white spaces
*/
public String getBlankSequenceArea() {
return createBlankSequenceArea(format);
}

public String getCommentArea() {
return commentArea;
}

public String getCommentAreaOriginal() {
return commentAreaOriginal;
return CobolLineUtils.createBlankSequenceArea(format);
}

/**
* Build and return a significant for syntax parsing content line
*
* @return a String with combined content areas
*/
public String getContentArea() {
return contentAreaA + contentAreaB;
}

public String getContentAreaA() {
return contentAreaA;
}

public String getContentAreaAOriginal() {
return contentAreaAOriginal;
}

public String getContentAreaB() {
return contentAreaB;
}

public String getContentAreaBOriginal() {
return contentAreaBOriginal;
}

public String getContentAreaOriginal() {
return contentAreaAOriginal + contentAreaBOriginal;
}

public CobolDialect getDialect() {
return dialect;
}

public CobolSourceFormat getFormat() {
return format;
}

public String getIndicatorArea() {
return indicatorArea;
}

public String getIndicatorAreaOriginal() {
return indicatorAreaOriginal;
}

public int getNumber() {
return number;
}

public CobolLine getPredecessor() {
return predecessor;
}

public String getSequenceArea() {
return sequenceArea;
}

public String getSequenceAreaOriginal() {
return sequenceAreaOriginal;
}

public CobolLine getSuccessor() {
return successor;
}

public CobolLineTypeEnum getType() {
return type;
}

public void setCommentArea(String commentArea) {
this.commentArea = commentArea;
}

public void setCommentAreaOriginal(String commentAreaOriginal) {
this.commentAreaOriginal = commentAreaOriginal;
}

public void setContentAreaA(String contentAreaA) {
this.contentAreaA = contentAreaA;
}

public void setContentAreaAOriginal(String contentAreaAOriginal) {
this.contentAreaAOriginal = contentAreaAOriginal;
}

public void setContentAreaB(String contentAreaB) {
this.contentAreaB = contentAreaB;
}

public void setContentAreaBOriginal(String contentAreaBOriginal) {
this.contentAreaBOriginal = contentAreaBOriginal;
}

public void setDialect(CobolDialect dialect) {
this.dialect = dialect;
}

public void setFormat(CobolSourceFormat format) {
this.format = format;
}

public void setIndicatorArea(String indicatorArea) {
this.indicatorArea = indicatorArea;
}

public void setIndicatorAreaOriginal(String indicatorAreaOriginal) {
this.indicatorAreaOriginal = indicatorAreaOriginal;
}

public void setNumber(int number) {
this.number = number;
}

public void setSequenceArea(String sequenceArea) {
this.sequenceArea = sequenceArea;
}

public void setSequenceAreaOriginal(String sequenceAreaOriginal) {
this.sequenceAreaOriginal = sequenceAreaOriginal;
}

public void setType(CobolLineTypeEnum type) {
this.type = type;
}

/**
* Set previous line and bind this lines together
*
* @param predecessor - the previous line in a document
*/
public void setPredecessor(final CobolLine predecessor) {
this.predecessor = predecessor;

Expand All @@ -179,6 +69,11 @@ public void setPredecessor(final CobolLine predecessor) {
}
}

/**
* Set following line and bind this lines together
*
* @param successor - the previous line in a document
*/
public void setSuccessor(final CobolLine successor) {
this.successor = successor;

Expand All @@ -187,91 +82,13 @@ public void setSuccessor(final CobolLine successor) {
}
}

public static CobolLine copyCobolLineWithContentArea(
final String contentArea, final CobolLine line) {
CobolLine cobolLine = copyCobolLine(line);
cobolLine.setContentAreaA(extractContentAreaA(contentArea));
cobolLine.setContentAreaB(extractContentAreaB(contentArea));
return cobolLine;
}

/**
* @param indicatorArea
* @param contentArea
* @param line
* @return new CobolLine
* Serialize the line and combine the fields that are significant for parsing
*
* @return combination of string parts of the COBOL line
*/
public static CobolLine copyCobolLineWithIndicatorAndContentArea(
final String indicatorArea, final String contentArea, final CobolLine line) {

CobolLine cobolLine = copyCobolLine(line);
cobolLine.setIndicatorArea(indicatorArea);
cobolLine.setContentAreaA(extractContentAreaA(contentArea));
cobolLine.setContentAreaB(extractContentAreaB(contentArea));
return cobolLine;
}

public static CobolLine copyCobolLineWithIndicatorArea(
final String indicatorArea, final CobolLine line) {
CobolLine cobolLine = copyCobolLine(line);
cobolLine.setIndicatorArea(indicatorArea);
return cobolLine;
}

public static CobolLine copyCobolLineWithEmptyContent(final CobolLine line) {
CobolLine cobolLine = new CobolLine();
cobolLine.setFormat(line.format);
cobolLine.setDialect(line.dialect);
cobolLine.setNumber(line.number);
cobolLine.setType(line.type);
cobolLine.setPredecessor(line.predecessor);
cobolLine.setSuccessor(line.successor);
return cobolLine;
}

private static CobolLine copyCobolLine(final CobolLine line) {
CobolLine cobolLine = new CobolLine();
cobolLine.setSequenceArea(line.getSequenceArea());
cobolLine.setSequenceAreaOriginal(line.getSequenceAreaOriginal());
cobolLine.setIndicatorArea(line.indicatorArea);
cobolLine.setIndicatorAreaOriginal(line.indicatorAreaOriginal);
cobolLine.setContentAreaA(line.contentAreaA);
cobolLine.setContentAreaAOriginal(line.contentAreaAOriginal);
cobolLine.setContentAreaB(line.contentAreaB);
cobolLine.setContentAreaBOriginal(line.contentAreaBOriginal);
cobolLine.setCommentArea(line.commentArea);
cobolLine.setCommentAreaOriginal(line.commentAreaOriginal);
cobolLine.setFormat(line.format);
cobolLine.setDialect(line.dialect);
cobolLine.setNumber(line.number);
cobolLine.setType(line.type);
cobolLine.setPredecessor(line.predecessor);
cobolLine.setSuccessor(line.successor);
return cobolLine;
}

public static String createBlankSequenceArea(final CobolSourceFormat format) {
return TANDEM.equals(format) ? "" : Strings.repeat(ProcessingConstants.WS, 6);
}

protected static String extractContentAreaA(final String contentArea) {
return contentArea.length() > 4 ? contentArea.substring(0, 4) : contentArea;
}

protected static String extractContentAreaB(final String contentArea) {
return contentArea.length() > 4 ? contentArea.substring(4) : "";
}

public String serialize() {
return sequenceArea + indicatorArea + contentAreaA + contentAreaB + commentArea;
}

public String serializeWithoutCommentArea() {
return sequenceArea + indicatorArea + contentAreaA + contentAreaB;
}

@Override
public String toString() {
return serialize();
return sequenceArea + indicatorArea + contentAreaA + contentAreaB + commentArea;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@
import com.ca.lsp.core.cobol.model.PreprocessedInput;
import com.ca.lsp.core.cobol.model.ResultWithErrors;
import com.ca.lsp.core.cobol.preprocessor.CobolSourceFormat;
import com.ca.lsp.core.cobol.semantics.SemanticContext;

import javax.annotation.Nonnull;

/**
* Preprocessor which retrieves semantic elements definitions, such as variables, paragraphs and
* copybooks
*/
public interface CobolSemanticParser {

/**
* Fills-in the semantic context of the current document, including elements from the copybooks
*
* @param code - COBOL program text to analyse
* @param semanticContext - semantic context of the currently processed document to be filled in.
* @param format - the format of the processing document
* @return a PreprocessedInput - text and its semantic context with syntax errors if found or an
* empty list
*/
@Nonnull
ResultWithErrors<PreprocessedInput> processLines(
String code, CobolSourceFormat format);
@Nonnull String code,
@Nonnull SemanticContext semanticContext,
@Nonnull CobolSourceFormat format);
}
Loading