Search in sources :

Example 1 with Definition

use of nl.ramsolutions.sw.magik.analysis.definitions.Definition in project magik-tools by StevenLooman.

the class MagikIndexer method handleDefinition.

private void handleDefinition(final MagikFile magikFile, final SlottedExemplarDefinition definition) {
    final AstNode node = definition.getNode();
    final GlobalReference globalRef = definition.getGlobalReference();
    final MagikType type = this.typeKeeper.getType(globalRef) instanceof SlottedType ? (MagikType) this.typeKeeper.getType(globalRef) : new SlottedType(globalRef);
    this.typeKeeper.addType(type);
    final NewDocParser docParser = new NewDocParser(node);
    final Map<String, String> slotTypes = docParser.getSlotTypes();
    // This needs a default value ("") due to https://bugs.openjdk.java.net/browse/JDK-8148463
    final Map<String, String> slots = definition.getSlots().stream().map(SlottedExemplarDefinition.Slot::getName).collect(Collectors.toMap(slotName -> slotName, slotName -> slotTypes.getOrDefault(slotName, "")));
    final List<String> parents = definition.getParents();
    final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:slotted_format_mixin"));
    this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
    final Path path = Paths.get(magikFile.getUri());
    this.indexedTypes.get(path).add(type);
    LOGGER.debug("Indexed type: {}", type);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) LoggerFactory(org.slf4j.LoggerFactory) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) AliasType(nl.ramsolutions.sw.magik.analysis.typing.types.AliasType) IndexedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.IndexedExemplarDefinition) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) EnumerationDefinition(nl.ramsolutions.sw.magik.analysis.definitions.EnumerationDefinition) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EnumSet(java.util.EnumSet) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) TypeAnnotationHandler(nl.ramsolutions.sw.magik.analysis.typing.TypeAnnotationHandler) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ParameterDefinition(nl.ramsolutions.sw.magik.analysis.definitions.ParameterDefinition) GlobalDefinition(nl.ramsolutions.sw.magik.analysis.definitions.GlobalDefinition) HashMap(java.util.HashMap) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) HashSet(java.util.HashSet) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Location(nl.ramsolutions.sw.magik.analysis.Location) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MagikCommentExtractor(nl.ramsolutions.sw.magik.parser.MagikCommentExtractor) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Token(com.sonar.sslr.api.Token) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Logger(org.slf4j.Logger) MagikFile(nl.ramsolutions.sw.magik.MagikFile) IOException(java.io.IOException) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) BinaryOperatorDefinition(nl.ramsolutions.sw.magik.analysis.definitions.BinaryOperatorDefinition) MixinDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MixinDefinition) IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) PackageDefinition(nl.ramsolutions.sw.magik.analysis.definitions.PackageDefinition) Paths(java.nio.file.Paths) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) BinaryOperator(nl.ramsolutions.sw.magik.analysis.typing.BinaryOperator) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) Path(java.nio.file.Path) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition)

Example 2 with Definition

use of nl.ramsolutions.sw.magik.analysis.definitions.Definition in project magik-tools by StevenLooman.

the class DuplicateMethodInFileCheck method walkPostMagik.

@Override
protected void walkPostMagik(final AstNode node) {
    // Test for duplicates.
    final Map<String, List<MethodDefinition>> definitions = this.methodDefinitions.stream().collect(Collectors.toMap(MethodDefinition::getName, List::of, (list1, list2) -> Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList())));
    definitions.entrySet().stream().filter(entry -> entry.getValue().size() > 1).flatMap(entry -> entry.getValue().stream()).forEach(definition -> {
        AstNode definitionNode = definition.getNode();
        this.addIssue(definitionNode, MESSAGE);
    });
}
Also used : HashSet(java.util.HashSet) AstNode(com.sonar.sslr.api.AstNode) List(java.util.List) MagikCheck(nl.ramsolutions.sw.magik.checks.MagikCheck) Stream(java.util.stream.Stream) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) Map(java.util.Map) Set(java.util.Set) MethodDefinitionParser(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinitionParser) Rule(org.sonar.check.Rule) Collectors(java.util.stream.Collectors) List(java.util.List) AstNode(com.sonar.sslr.api.AstNode)

Example 3 with Definition

use of nl.ramsolutions.sw.magik.analysis.definitions.Definition in project magik-tools by StevenLooman.

the class DuplicateMethodInFileCheck method walkPostMethodDefinition.

@Override
protected void walkPostMethodDefinition(final AstNode node) {
    final MethodDefinitionParser parser = new MethodDefinitionParser(node);
    final List<Definition> parsedDefinitions = parser.parseDefinitions();
    if (parsedDefinitions.isEmpty()) {
        // How does this happen?
        return;
    }
    final MethodDefinition methodDefinition = (MethodDefinition) parsedDefinitions.get(0);
    this.methodDefinitions.add(methodDefinition);
}
Also used : MethodDefinitionParser(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinitionParser) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition)

Example 4 with Definition

use of nl.ramsolutions.sw.magik.analysis.definitions.Definition in project magik-tools by StevenLooman.

the class ExemplarSlotCountCheck method walkPreProcedureInvocation.

@Override
protected void walkPreProcedureInvocation(final AstNode node) {
    if (!DefSlottedExemplarParser.isDefSlottedExemplar(node)) {
        return;
    }
    final DefSlottedExemplarParser parser = new DefSlottedExemplarParser(node);
    final List<Definition> parsedDefinitions = parser.parseDefinitions();
    final SlottedExemplarDefinition definition = (SlottedExemplarDefinition) parsedDefinitions.get(0);
    final int slotCount = definition.getSlots().size();
    if (slotCount > this.maxSlotCount) {
        final String message = String.format(MESSAGE, slotCount, this.maxSlotCount);
        this.addIssue(node, message);
    }
}
Also used : SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) DefSlottedExemplarParser(nl.ramsolutions.sw.magik.analysis.definitions.DefSlottedExemplarParser)

Example 5 with Definition

use of nl.ramsolutions.sw.magik.analysis.definitions.Definition in project magik-tools by StevenLooman.

the class NewDocCheck method walkPostProcedureInvocation.

@Override
protected void walkPostProcedureInvocation(AstNode node) {
    if (!DefSlottedExemplarParser.isDefSlottedExemplar(node)) {
        return;
    }
    // Get slot defintions.
    final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
    final NewDocParser newDocParser = new NewDocParser(statementNode);
    final Map<String, AstNode> docSlotNameNodes = newDocParser.getSlotNameNodes();
    final DefSlottedExemplarParser parser = new DefSlottedExemplarParser(node);
    final List<Definition> definitions = parser.parseDefinitions();
    final SlottedExemplarDefinition exemplarDefinition = (SlottedExemplarDefinition) definitions.get(0);
    final List<Slot> slots = exemplarDefinition.getSlots();
    final Map<String, Slot> slotNames = slots.stream().collect(Collectors.toMap(Slot::getName, slot -> slot));
    // Compare parameters.
    docSlotNameNodes.entrySet().stream().filter(entry -> !slotNames.containsKey(entry.getKey())).forEach(entry -> {
        final String docName = entry.getKey();
        final AstNode docNode = entry.getValue();
        final String message = String.format(MESSAGE_SLOT_UNKNOWN, docName);
        this.addIssue(docNode, message);
    });
    slotNames.entrySet().stream().filter(entry -> !docSlotNameNodes.containsKey(entry.getKey())).forEach(entry -> {
        final String docName = entry.getKey();
        final AstNode docNode = entry.getValue().getNode();
        final String message = String.format(MESSAGE_SLOT_MISSING, docName);
        this.addIssue(docNode, message);
    });
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) AstNode(com.sonar.sslr.api.AstNode) Slot(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition.Slot) MagikCheck(nl.ramsolutions.sw.magik.checks.MagikCheck) DefSlottedExemplarParser(nl.ramsolutions.sw.magik.analysis.definitions.DefSlottedExemplarParser) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) Collectors(java.util.stream.Collectors) List(java.util.List) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) Map(java.util.Map) ProcedureDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ProcedureDefinitionNodeHelper) Rule(org.sonar.check.Rule) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) Slot(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition.Slot) AstNode(com.sonar.sslr.api.AstNode) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) DefSlottedExemplarParser(nl.ramsolutions.sw.magik.analysis.definitions.DefSlottedExemplarParser)

Aggregations

Definition (nl.ramsolutions.sw.magik.analysis.definitions.Definition)7 AstNode (com.sonar.sslr.api.AstNode)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 MethodDefinition (nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition)5 SlottedExemplarDefinition (nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 Token (com.sonar.sslr.api.Token)3 IOException (java.io.IOException)3 URI (java.net.URI)3 Path (java.nio.file.Path)3 Paths (java.nio.file.Paths)3 Collections (java.util.Collections)3 EnumSet (java.util.EnumSet)3 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 Nullable (javax.annotation.Nullable)3