Skip to content

Commit

Permalink
feat: Normalize model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurkambay committed Nov 4, 2022
1 parent fe2c594 commit 22fb5b3
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private void analyzeEmbeddedCode(List<Node> syntaxTree, Map<Token, Locality> map
private List<SyntaxError> finalizeErrors(
@NonNull List<SyntaxError> errors, @NonNull Map<Token, Locality> mapping) {
return errors.stream()
.filter(c -> c.getOffendedToken() != null)
.filter(c -> c.getTokenIndex() != -1)
.map(convertError(mapping))
.filter(it -> it.getLocality() != null)
.collect(toList());
Expand All @@ -456,7 +456,7 @@ private List<SyntaxError> finalizeErrors(
private Function<SyntaxError, SyntaxError> convertError(@NonNull Map<Token, Locality> mapping) {
return err ->
err.toBuilder()
.locality(LocalityUtils.findPreviousVisibleLocality(err.getOffendedToken(), mapping))
.locality(LocalityUtils.findPreviousVisibleLocality(err.getTokenIndex(), mapping))
.suggestion(messageService.getMessage(err.getSuggestion()))
.errorSource(ErrorSource.PARSING)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/** This error listener registers syntax errors found by dialect parser. */
@Slf4j
Expand All @@ -53,7 +54,9 @@ public void syntaxError(
SyntaxError error =
SyntaxError.syntaxError()
.errorSource(ErrorSource.DIALECT)
.offendedToken((CommonToken) offendingSymbol)
.tokenIndex(Optional.ofNullable((CommonToken) offendingSymbol)
.map(CommonToken::getTokenIndex)
.orElse(-1))
.suggestion(msg)
.locality(
Locality.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package org.eclipse.lsp.cobol.core.model;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.antlr.v4.runtime.Token;
import org.eclipse.lsp.cobol.core.messages.MessageTemplate;

/**
Expand All @@ -33,7 +31,7 @@
public class SyntaxError {
Locality locality;
MessageTemplate messageTemplate;
@EqualsAndHashCode.Exclude Token offendedToken;
@Builder.Default int tokenIndex = -1;
String suggestion;
ErrorSeverity severity;
ErrorCode errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
*/
package org.eclipse.lsp.cobol.core.model.tree;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import lombok.Getter;
import lombok.ToString;
import org.eclipse.lsp.cobol.core.engine.symbols.CopyDefinition;
import org.eclipse.lsp.cobol.core.model.Locality;
import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository;
import org.eclipse.lsp4j.Location;

import java.util.HashMap;
Expand All @@ -36,9 +36,13 @@
public class RootNode extends Node {
private final Map<String, CopyDefinition> copyDefinitionMap = new HashMap<>();

public RootNode(Locality locality, CopybooksRepository copybooks) {
public RootNode() {
this(Locality.builder().build(), ImmutableMultimap.of());
}

public RootNode(Locality locality, Multimap<String, Location> copybookDefinitions) {
super(locality, ROOT);
createCopyBookDefinitionNode(copybooks.getDefinitions());
createCopyBookDefinitionNode(copybookDefinitions);
}

private void createCopyBookDefinitionNode(Multimap<String, Location> definition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.lsp.cobol.core.engine.symbols.CodeBlockReference;
import org.eclipse.lsp.cobol.core.engine.symbols.Context;
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolService;
import org.eclipse.lsp.cobol.core.messages.MessageService;
import org.eclipse.lsp.cobol.core.model.Locality;
import org.eclipse.lsp4j.Location;

Expand All @@ -30,15 +29,13 @@
@Getter
public class SectionNameNode extends Node implements Context {
private final String name;
@EqualsAndHashCode.Exclude @ToString.Exclude private final MessageService messageService;

@EqualsAndHashCode.Exclude @ToString.Exclude private final SymbolService symbolService;

public SectionNameNode(
Locality location, String name, MessageService messageService, SymbolService symbolService) {
Locality location, String name, SymbolService symbolService) {
super(location, NodeType.SECTION_NAME_NODE);
this.name = name.toUpperCase();
this.messageService = messageService;
this.symbolService = symbolService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.annotation.Nullable;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

Expand Down Expand Up @@ -79,21 +80,22 @@ public Function<Token, Locality> toLocality(String documentUri, @Nullable String
* Checks at most RANGE_LOOK_BACK_TOKENS previous tokens. For example, embedded languages may
* produce errors on the edge positions that don't belong to the mapping.
*
* @param token to find previous visible locality
* @param tokenIndex to find previous visible locality
* @param mapping A Map of Token to Locality for a document in analysis.
* @return Locality for a passed token or null
*/
public Locality findPreviousVisibleLocality(Token token, Map<Token, Locality> mapping) {
return mapping.computeIfAbsent(token, it -> lookBackLocality(it.getTokenIndex(), mapping));
public Locality findPreviousVisibleLocality(int tokenIndex, Map<Token, Locality> mapping) {
return Optional.ofNullable(lookBackLocality(tokenIndex, mapping))
.map(e -> mapping.computeIfAbsent(e.getKey(), (it) -> e.getValue()))
.orElse(null);
}

private Locality lookBackLocality(int index, Map<Token, Locality> mapping) {
private Map.Entry<Token, Locality> lookBackLocality(int index, Map<Token, Locality> mapping) {
if (index < 0) return null;
return mapping.entrySet().stream()
.filter(previousIndexes(index))
.filter(isNotHidden())
.max(Comparator.comparingInt(it -> it.getKey().getTokenIndex()))
.map(Map.Entry::getValue)
.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public List<Node> visitStartRule(StartRuleContext ctx) {
// we can skip the other nodes, but not the root
return ImmutableList.of(
retrieveRangeLocality(ctx, positions)
.map(it -> new RootNode(it, copybooks))
.map(it -> new RootNode(it, copybooks.getDefinitions()))
.map(
rootNode -> {
visitChildren(ctx).forEach(rootNode::addChild);
Expand All @@ -127,7 +127,7 @@ public List<Node> visitStartRule(StartRuleContext ctx) {
.orElseGet(
() -> {
LOG.warn("The root node for syntax tree was not constructed");
return new RootNode(Locality.builder().build(), copybooks);
return new RootNode(Locality.builder().build(), copybooks.getDefinitions());
}));
}

Expand Down Expand Up @@ -400,7 +400,7 @@ public List<Node> visitLocalStorageSection(LocalStorageSectionContext ctx) {
@Override
public List<Node> visitSectionName(SectionNameContext ctx) {
return addTreeNode(
ctx, locality -> new SectionNameNode(locality, ctx.getText(), messageService, symbolService));
ctx, locality -> new SectionNameNode(locality, ctx.getText(), symbolService));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/** This error listener registers syntax errors found by the COBOL parser. */
@Slf4j
Expand All @@ -45,7 +46,9 @@ public void syntaxError(
SyntaxError error =
SyntaxError.syntaxError()
.errorSource(ErrorSource.PARSING)
.offendedToken((CommonToken) offendingSymbol)
.tokenIndex(Optional.ofNullable((CommonToken) offendingSymbol)
.map(CommonToken::getTokenIndex)
.orElse(-1))
.suggestion(msg)
.severity(ErrorSeverity.ERROR)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolTable;
import org.eclipse.lsp.cobol.core.model.Locality;
import org.eclipse.lsp.cobol.core.model.tree.Node;
import org.eclipse.lsp.cobol.core.model.tree.RootNode;
import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository;
import org.eclipse.lsp4j.Diagnostic;

import java.util.HashMap;
Expand All @@ -36,6 +34,6 @@
@Builder(toBuilder = true)
public class AnalysisResult {
@Builder.Default Map<String, List<Diagnostic>> diagnostics = new HashMap<>();
@Builder.Default Node rootNode = new RootNode(Locality.builder().build(), new CopybooksRepository());
@Builder.Default Node rootNode = new RootNode();
@EqualsAndHashCode.Exclude @Builder.Default Map<String, SymbolTable> symbolTableMap = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package org.eclipse.lsp.cobol.core.model.tree;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import org.eclipse.lsp.cobol.core.model.Locality;
import org.eclipse.lsp.cobol.core.model.tree.variables.VariableDefinitionNode;
import org.eclipse.lsp.cobol.core.model.variables.SectionType;
import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand All @@ -30,11 +30,10 @@
/** Test {@link Node} */
class NodeTest {
private static final Locality LOCALITY = Locality.builder().build();
private static final CopybooksRepository COPYBOOK = new CopybooksRepository();

@Test
void getDepthFirstStream() {
Node rootNode = new RootNode(LOCALITY, COPYBOOK);
Node rootNode = new RootNode(LOCALITY, ImmutableMultimap.of());
Node firstProg = new ProgramNode(LOCALITY);
Node sectionNode = new SectionNode(LOCALITY, SectionType.WORKING_STORAGE);
Node definition = VariableDefinitionNode.builder().build();
Expand Down Expand Up @@ -63,7 +62,7 @@ void getDepthFirstStream() {

@Test
void getParentByType() {
Node rootNode = new RootNode(LOCALITY, COPYBOOK);
Node rootNode = new RootNode(LOCALITY, ImmutableMultimap.of());
Node program = new ProgramNode(LOCALITY);
rootNode.addChild(program);
Node nestedProgram = new ProgramNode(LOCALITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.lsp.cobol.core.model.tree.statements;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import org.eclipse.lsp.cobol.core.engine.processor.AstProcessor;
import org.eclipse.lsp.cobol.core.engine.processor.ProcessingContext;
import org.eclipse.lsp.cobol.core.engine.processor.ProcessingPhase;
Expand All @@ -27,7 +28,6 @@
import org.eclipse.lsp.cobol.core.model.tree.RemarksNode;
import org.eclipse.lsp.cobol.core.model.tree.RootNode;
import org.eclipse.lsp.cobol.core.model.tree.logic.ObsoleteNodeCheck;
import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand All @@ -40,7 +40,7 @@ class ObsoleteNodeTest {
@Test
public void testObsoleteNodeWarning() {
Locality locality = Locality.builder().build();
RootNode rootNode = new RootNode(locality, new CopybooksRepository());
RootNode rootNode = new RootNode(locality, ImmutableMultimap.of());
RemarksNode remarksNode = new RemarksNode(locality);
AstProcessor astProcessor = new AstProcessor();
List<SyntaxError> errors = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void testLocalityFindingPositive() {
positions.put(placeholderToken, notExpected);
positions.put(correctToken, expected);

Locality result = LocalityUtils.findPreviousVisibleLocality(correctToken, positions);
Locality result = LocalityUtils.findPreviousVisibleLocality(correctToken.getTokenIndex(), positions);
assertEquals(expected, result);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ void testLocalityFindingWithLookBack() {
positions.put(hiddenToken1, notExpected);
positions.put(hiddenToken2, notExpected);

Locality result = LocalityUtils.findPreviousVisibleLocality(tokenToCheck, positions);
Locality result = LocalityUtils.findPreviousVisibleLocality(tokenToCheck.getTokenIndex(), positions);
assertEquals(expected, result);
}

Expand Down Expand Up @@ -114,7 +114,7 @@ void testLocalityFindingTakesOnlyFiveAttempts() {
positions.put(hiddenToken4, notExpected);
positions.put(hiddenToken5, notExpected);

Locality result = LocalityUtils.findPreviousVisibleLocality(tokenToCheck, positions);
Locality result = LocalityUtils.findPreviousVisibleLocality(tokenToCheck.getTokenIndex(), positions);
assertNull(result);
}

Expand All @@ -140,7 +140,7 @@ void testLocalityFindingWithFarIndex() {
positions.put(correctToken, expected);
positions.put(hiddenToken, notExpected);

Locality result = LocalityUtils.findPreviousVisibleLocality(tokenToCheck, positions);
Locality result = LocalityUtils.findPreviousVisibleLocality(tokenToCheck.getTokenIndex(), positions);
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

Expand All @@ -68,7 +69,6 @@
import org.eclipse.lsp.cobol.core.model.tree.CopyNode;
import org.eclipse.lsp.cobol.core.model.tree.RootNode;
import org.eclipse.lsp.cobol.core.preprocessor.delegates.injector.ImplicitCodeUtils;
import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository;
import org.eclipse.lsp.cobol.domain.databus.api.DataBusBroker;
import org.eclipse.lsp.cobol.domain.databus.model.AnalysisFinishedEvent;
import org.eclipse.lsp.cobol.domain.databus.model.RunAnalysisEvent;
Expand Down Expand Up @@ -684,10 +684,10 @@ void testHover() {
void testAnalysisFinishedNotification() throws ExecutionException, InterruptedException {
AnalysisResult analysisResult =
AnalysisResult.builder()
.rootNode(new RootNode(Locality.builder().build(), new CopybooksRepository()))
.rootNode(new RootNode(Locality.builder().build(), ImmutableMultimap.of()))
.build();

RootNode rootNode = new RootNode(Locality.builder().build(), new CopybooksRepository());
RootNode rootNode = new RootNode(Locality.builder().build(), ImmutableMultimap.of());
analysisResult.getRootNode().addChild(rootNode);
CopyNode parent = new CopyNode(Locality.builder().uri(DOCUMENT_URI).build(), "PARENT");
CopyNode nested = new CopyNode(Locality.builder().uri(PARENT_CPY_URI).build(), "NESTED");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@
package org.eclipse.lsp.cobol.service.delegates.completions;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import org.eclipse.lsp.cobol.core.engine.symbols.SymbolService;
import org.eclipse.lsp.cobol.core.messages.MessageService;
import org.eclipse.lsp.cobol.core.model.Locality;
import org.eclipse.lsp.cobol.core.model.tree.*;
import org.eclipse.lsp.cobol.core.model.tree.variables.MnemonicNameNode;
import org.eclipse.lsp.cobol.core.model.tree.variables.VariableNode;
import org.eclipse.lsp.cobol.core.semantics.CopybooksRepository;
import org.eclipse.lsp.cobol.service.CobolDocumentModel;
import org.eclipse.lsp.cobol.service.delegates.validations.AnalysisResult;

import static org.mockito.Mockito.mock;

/** This class stores a model to assert the completion providers */
class MockCompletionModel {
static final AnalysisResult RESULT =
AnalysisResult.builder()
.rootNode(new RootNode(Locality.builder().build(), new CopybooksRepository()))
.rootNode(new RootNode(Locality.builder().build(), ImmutableMultimap.of()))
.build();
static final CobolDocumentModel MODEL = new CobolDocumentModel("some text", RESULT);
static final SymbolService SYMBOL_SERVICE = new SymbolService();
Expand All @@ -59,11 +56,11 @@ class MockCompletionModel {
name -> {
SectionNameNode nameNode =
new SectionNameNode(
Locality.builder().build(), name, mock(MessageService.class), SYMBOL_SERVICE);
Locality.builder().build(), name, SYMBOL_SERVICE);
SYMBOL_SERVICE.registerSectionNameNode(programNode, nameNode);
});

RootNode rootNode = new RootNode(Locality.builder().build(), new CopybooksRepository());
RootNode rootNode = new RootNode(Locality.builder().build(), ImmutableMultimap.of());
RESULT.getRootNode().addChild(rootNode);
ImmutableList.of("cpyU1", "CpyU2", "Not-cpyU")
.forEach(
Expand Down

0 comments on commit 22fb5b3

Please sign in to comment.