Skip to content

Commit

Permalink
chore: Code refactor due to preparation for different COBOL dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurkambay authored and ishche committed Apr 19, 2024
1 parent 974adb2 commit 6d2c814
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.eclipse.lsp.cobol.common.model.tree.Node;
import org.eclipse.lsp.cobol.common.processor.ProcessorDescription;
import org.eclipse.lsp.cobol.core.engine.analysis.AnalysisContext;
import org.eclipse.lsp.cobol.core.engine.dialects.hp.HpDialect;
import org.eclipse.lsp.cobol.implicitDialects.cics.CICSDialect;
import org.eclipse.lsp.cobol.implicitDialects.sql.Db2SqlDialect;
import org.eclipse.lsp4j.Location;
Expand Down Expand Up @@ -140,7 +139,7 @@ public ResultWithErrors<DialectOutcome> processImplicitDialects(
* @param ctx a {@link AnalysisContext} class
* @param dialectProcessingContext is a DialectProcessingContext class with all needed data for
* dialect processing
* @return a error while extending the document
* @return an error while extending the document
*/
public List<SyntaxError> extendImplicitDialects(
AnalysisContext ctx, DialectProcessingContext dialectProcessingContext) {
Expand Down Expand Up @@ -263,7 +262,7 @@ private LinkedList<CobolDialect> sortDialects(List<String> dialects) {
* Returns dialect object by name
*
* @param dialectName is a dialect name
* @return a dialect is it's possible
* @return a dialect if it is possible
*/
public Optional<CobolDialect> getDialectByName(String dialectName) {
return Optional.ofNullable(dialectSuppliers.get(dialectName));
Expand Down Expand Up @@ -321,8 +320,6 @@ public boolean updateDialects(List<DialectRegistryItem> dialectRegistry) {
return dialect;
})
.orElse(null)));
changed.set(true);
dialectSuppliers.put("HP", new HpDialect(copybookService, messageService));
return changed.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.eclipse.lsp.cobol.common.CleanerPreprocessor;
import org.eclipse.lsp.cobol.common.SubroutineService;
import org.eclipse.lsp.cobol.common.copybook.CopybookService;
import org.eclipse.lsp.cobol.common.dialects.TrueDialectService;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.core.engine.analysis.AnalysisContext;
Expand Down Expand Up @@ -55,15 +56,16 @@ public TrueDialectServiceImpl(
DialectService dialectService,
AstProcessor astProcessor,
SymbolsRepository symbolsRepository,
CodeLayoutStore codeLayoutStore) {
CodeLayoutStore codeLayoutStore,
CopybookService copybookService) {
dialects = new HashMap<>();
dialects.put(CobolLanguageId.COBOL, new IbmTrueCobolDialect(grammarPreprocessor,
messageService, treeListener, subroutineService, cachingConfigurationService, dialectService,
astProcessor, symbolsRepository, codeLayoutStore));

dialects.put(CobolLanguageId.HP_COBOL, new HpTrueCobolDialect(grammarPreprocessor,
messageService, treeListener, subroutineService, cachingConfigurationService, dialectService,
astProcessor, symbolsRepository, codeLayoutStore));
astProcessor, symbolsRepository, codeLayoutStore, copybookService));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.core.engine.dialects.hp;
package org.eclipse.lsp.cobol.dialects.hp;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.core.engine.dialects.hp;
package org.eclipse.lsp.cobol.dialects.hp;

import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument;
import org.eclipse.lsp4j.Position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@
* Broadcom, Inc. - initial API and implementation
*
*/
package org.eclipse.lsp.cobol.core.engine.dialects.hp;
package org.eclipse.lsp.cobol.dialects.hp;

import com.google.common.collect.ImmutableList;
import org.eclipse.lsp.cobol.common.ResultWithErrors;
import lombok.RequiredArgsConstructor;
import org.eclipse.lsp.cobol.common.copybook.CopybookModel;
import org.eclipse.lsp.cobol.common.copybook.CopybookName;
import org.eclipse.lsp.cobol.common.copybook.CopybookService;
import org.eclipse.lsp.cobol.common.dialects.CobolDialect;
import org.eclipse.lsp.cobol.common.dialects.DialectOutcome;
import org.eclipse.lsp.cobol.common.dialects.DialectProcessingContext;
import org.eclipse.lsp.cobol.common.error.ErrorCodes;
import org.eclipse.lsp.cobol.common.error.ErrorSource;
import org.eclipse.lsp.cobol.common.error.SyntaxError;
import org.eclipse.lsp.cobol.common.mapping.ExtendedDocument;
import org.eclipse.lsp.cobol.common.mapping.ExtendedText;
import org.eclipse.lsp.cobol.common.mapping.MappedCharacter;
import org.eclipse.lsp.cobol.common.mapping.OriginalLocation;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.common.model.Locality;
import org.eclipse.lsp.cobol.common.model.tree.CopyNode;
import org.eclipse.lsp.cobol.common.pipeline.Stage;
import org.eclipse.lsp.cobol.common.pipeline.StageResult;
import org.eclipse.lsp.cobol.core.engine.analysis.AnalysisContext;
import org.eclipse.lsp4j.Location;

import java.util.HashSet;
Expand All @@ -41,48 +44,41 @@
import static org.eclipse.lsp.cobol.common.error.ErrorSeverity.ERROR;

/**
* HP Dialect support
* Resolving and inserting copybooks into the extended source stage
*/
public class HpDialect implements CobolDialect {
@RequiredArgsConstructor
class HpCopybookProcessingStage implements Stage<AnalysisContext, DialectOutcome, Void> {

private final CopybookService copybookService;
private final MessageService messageService;

public HpDialect(CopybookService copybookService, MessageService messageService) {
this.copybookService = copybookService;
this.messageService = messageService;
}

@Override
public String getName() {
return "HP";
}
private final CopybookService copybookService;

@Override
public List<SyntaxError> extend(DialectProcessingContext context) {
public StageResult<DialectOutcome> run(AnalysisContext context, StageResult<Void> prevStageResult) {
List<SyntaxError> errors = new LinkedList<>();

List<CopybookDescriptor> cbs = CopybookParser.parseAndCleanup(context.getExtendedDocument());
cbs.forEach(cb -> {
String currentUri = context.getExtendedDocument().getUri();
insertHpCopybook(context, cb, errors);
List<CopyNode> copybookNodes = insertHpCopybook(context.getDocumentUri(), context.getExtendedDocument(), cb, errors);
context.getDialectNodes().addAll(copybookNodes);
});
return errors;

DialectOutcome outcome = new DialectOutcome(context.getDialectNodes(), DialectProcessingContext.builder().build());
return new StageResult<>(outcome);
}

@Override
public ResultWithErrors<DialectOutcome> processText(DialectProcessingContext context) {
return new ResultWithErrors<>(new DialectOutcome(context.getDialectNodes(), context), ImmutableList.of());
public String getName() {
return "Copybook processing";
}

private void insertHpCopybook(DialectProcessingContext context, CopybookDescriptor descriptor, List<SyntaxError> errors) {
private List<CopyNode> insertHpCopybook(String programUri, ExtendedDocument extendedDocument, CopybookDescriptor descriptor, List<SyntaxError> errors) {
CopybookName copybookName = new CopybookName(descriptor.getName());
CopybookModel model = copybookService.resolve(copybookName.toCopybookId(context.getProgramDocumentUri()),
copybookName, context.getProgramDocumentUri(),
context.getExtendedDocument().getUri(), null)
CopybookModel model = copybookService.resolve(copybookName.toCopybookId(extendedDocument.getUri()),
copybookName, programUri,
extendedDocument.getUri(), null)
.unwrap(errors::addAll);

Location nameLocation = new Location(context.getExtendedDocument().getUri(), descriptor.getNameRange());
Location nameLocation = new Location(extendedDocument.getUri(), descriptor.getNameRange());
if (model.getUri() == null) {
errors.add(SyntaxError.syntaxError()
.errorSource(ErrorSource.DIALECT)
Expand All @@ -94,7 +90,7 @@ private void insertHpCopybook(DialectProcessingContext context, CopybookDescript
.errorCode(ErrorCodes.MISSING_COPYBOOK)
.location(new OriginalLocation(nameLocation, null))
.build());
return;
return ImmutableList.of();
}

String text = model.getContent();
Expand All @@ -111,15 +107,16 @@ private void insertHpCopybook(DialectProcessingContext context, CopybookDescript
}
);

context.getExtendedDocument().insertCopybook(descriptor.getStatementRange(), copybook);
extendedDocument.insertCopybook(descriptor.getStatementRange(), copybook);

Locality statementLocality = Locality.builder()
.copybookId(copybookName.toCopybookId(context.getProgramDocumentUri()).toString())
.copybookId(copybookName.toCopybookId(programUri).toString())
.range(descriptor.getStatementRange())
.uri(context.getExtendedDocument().getUri())
.uri(extendedDocument.getUri())
.build();

CopyNode copyNode = new CopyNode(statementLocality, nameLocation, descriptor.getName(), model.getUri());
context.getDialectNodes().add(copyNode);
return ImmutableList.of(copyNode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.eclipse.lsp.cobol.common.CleanerPreprocessor;
import org.eclipse.lsp.cobol.common.SubroutineService;
import org.eclipse.lsp.cobol.common.copybook.CopybookService;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.common.pipeline.Pipeline;
import org.eclipse.lsp.cobol.core.engine.analysis.AnalysisContext;
Expand Down Expand Up @@ -45,14 +46,15 @@ public HpTrueCobolDialect(GrammarPreprocessor grammarPreprocessor,
DialectService dialectService,
AstProcessor astProcessor,
SymbolsRepository symbolsRepository,
CodeLayoutStore codeLayoutStore) {
CodeLayoutStore codeLayoutStore,
CopybookService copybookService) {
preprocessor = new HpTextPreprocessor(messageService, codeLayoutStore);

pipeline = new Pipeline<>();
pipeline.add(new HpCleanupStage(preprocessor));
pipeline.add(new DialectCompilerDirectiveStage(dialectService));
pipeline.add(new CompilerDirectivesStage(messageService));
pipeline.add(new DialectProcessingStage(dialectService, preprocessor));
pipeline.add(new HpCopybookProcessingStage(messageService, copybookService));
pipeline.add(new PreprocessorStage(grammarPreprocessor, preprocessor));
pipeline.add(new ImplicitDialectProcessingStage(dialectService));
pipeline.add(new ParserStage(messageService, treeListener));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.lsp.cobol.common.*;
import org.eclipse.lsp.cobol.common.benchmark.BenchmarkService;
import org.eclipse.lsp.cobol.common.benchmark.BenchmarkSession;
import org.eclipse.lsp.cobol.common.copybook.CopybookService;
import org.eclipse.lsp.cobol.common.dialects.DialectOutcome;
import org.eclipse.lsp.cobol.common.dialects.DialectProcessingContext;
import org.eclipse.lsp.cobol.common.dialects.TrueDialectService;
Expand Down Expand Up @@ -78,6 +79,7 @@ class CobolLanguageEngineTest {
private final SymbolsRepository symbolsRepository = mock(SymbolsRepository.class);
private final CleanerPreprocessor preprocessor = mock(CleanerPreprocessor.class);
private final CodeLayoutStore store = mock(CodeLayoutStore.class);
private final CopybookService copybookService = mock(CopybookService.class);

@Test
void testLanguageEngineRun() {
Expand All @@ -91,7 +93,7 @@ void testLanguageEngineRun() {

TrueDialectService<AnalysisContext> trueDialectService = new TrueDialectServiceImpl(grammarPreprocessor, mockMessageService, treeListener, mock(SubroutineService.class),
null,
dialectService, astProcessor, symbolsRepository, store);
dialectService, astProcessor, symbolsRepository, store, copybookService);
CobolLanguageEngine engine =
new CobolLanguageEngine(trueDialectService,
mockMessageService,
Expand Down Expand Up @@ -169,7 +171,7 @@ void testLanguageEngineRunWhenNativeServerWithDialects() {

TrueDialectService<AnalysisContext> trueDialectService = new TrueDialectServiceImpl(grammarPreprocessor, mockMessageService, treeListener, mock(SubroutineService.class),
null,
dialectService, astProcessor, symbolsRepository, store);
dialectService, astProcessor, symbolsRepository, store, copybookService);
CobolLanguageEngine engine =
new CobolLanguageEngine(trueDialectService,
mockMessageService,
Expand Down

0 comments on commit 6d2c814

Please sign in to comment.