Search in sources :

Example 1 with NewDocParser

use of nl.ramsolutions.sw.magik.parser.NewDocParser 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 NewDocParser

use of nl.ramsolutions.sw.magik.parser.NewDocParser in project magik-tools by StevenLooman.

the class NewDocTypeExistsTypeCheck method checkDefinitionLoops.

private void checkDefinitionLoops(final NewDocParser newDocParser, final String pakkage) {
    // Test @loop types.
    final ITypeKeeper typeKeeper = this.getTypeKeeper();
    final TypeParser typeParser = new TypeParser(typeKeeper);
    newDocParser.getLoopTypeNodes().entrySet().stream().filter(entry -> typeParser.parseTypeString(entry.getValue(), pakkage) == UndefinedType.INSTANCE).forEach(entry -> {
        final AstNode typeNode = entry.getKey();
        final String typeName = entry.getValue();
        final String message = String.format(MESSAGE, typeName);
        this.addIssue(typeNode, message);
    });
}
Also used : TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AstNode(com.sonar.sslr.api.AstNode) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) DefSlottedExemplarParser(nl.ramsolutions.sw.magik.analysis.definitions.DefSlottedExemplarParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) Map(java.util.Map) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) AstNode(com.sonar.sslr.api.AstNode)

Example 3 with NewDocParser

use of nl.ramsolutions.sw.magik.parser.NewDocParser in project magik-tools by StevenLooman.

the class NewDocTypeExistsTypeCheck method walkPostProcedureInvocation.

@Override
protected void walkPostProcedureInvocation(final AstNode node) {
    if (!DefSlottedExemplarParser.isDefSlottedExemplar(node)) {
        return;
    }
    final ITypeKeeper typeKeeper = this.getTypeKeeper();
    final TypeParser typeParser = new TypeParser(typeKeeper);
    // Get slot defintions.
    final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
    final NewDocParser newDocParser = new NewDocParser(statementNode);
    final Map<AstNode, String> slotTypeNodes = newDocParser.getSlotTypeNodes();
    final PackageNodeHelper helper = new PackageNodeHelper(node);
    final String currentPakkage = helper.getCurrentPackage();
    // Test slot types.
    slotTypeNodes.entrySet().stream().filter(entry -> {
        final AbstractType type = typeParser.parseTypeString(entry.getValue(), currentPakkage);
        return type == UndefinedType.INSTANCE;
    }).forEach(entry -> {
        final AstNode typeNode = entry.getKey();
        final String typeName = entry.getValue();
        final String message = String.format(MESSAGE, typeName);
        this.addIssue(typeNode, message);
    });
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AstNode(com.sonar.sslr.api.AstNode) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) DefSlottedExemplarParser(nl.ramsolutions.sw.magik.analysis.definitions.DefSlottedExemplarParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) Map(java.util.Map) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) AstNode(com.sonar.sslr.api.AstNode)

Example 4 with NewDocParser

use of nl.ramsolutions.sw.magik.parser.NewDocParser 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)

Example 5 with NewDocParser

use of nl.ramsolutions.sw.magik.parser.NewDocParser in project magik-tools by StevenLooman.

the class SemanticTokenWalker method walkCommentToken.

@SuppressWarnings("checkstyle:NestedIfDepth")
private void walkCommentToken(final Token token) {
    final String value = token.getOriginalValue();
    if (value.startsWith("##")) {
        final Set<SemanticToken.Modifier> docModifier = Set.of(SemanticToken.Modifier.DOCUMENTATION);
        final List<Token> docTokens = List.of(token);
        final NewDocParser newDocParser = new NewDocParser(docTokens);
        final AstNode newDocNode = newDocParser.getNewDocNode();
        // It is either a FUNCTION node or element nodes.
        final AstNode functionNode = newDocNode.getFirstChild(NewDocGrammar.FUNCTION);
        if (functionNode != null) {
            this.addSemanticToken(token, SemanticToken.Type.COMMENT, docModifier);
        }
        final List<AstNode> elementNodes = newDocNode.getChildren(NewDocGrammar.PARAM, NewDocGrammar.RETURN, NewDocGrammar.LOOP, NewDocGrammar.SLOT);
        elementNodes.forEach(elementNode -> {
            // DOC_START
            final AstNode docStartNode = elementNode.getFirstChild(NewDocGrammar.DOC_START);
            this.addSemanticToken(docStartNode, SemanticToken.Type.COMMENT, docModifier);
            // Element
            final AstNode keywordNode = elementNode.getChildren().get(1);
            this.addSemanticToken(keywordNode, SemanticToken.Type.KEYWORD, docModifier);
            // TYPE
            final AstNode typeNode = elementNode.getFirstChild(NewDocGrammar.TYPE);
            if (typeNode != null) {
                final List<AstNode> typeNodes = typeNode.getDescendants(NewDocGrammar.TYPE_NAME, NewDocGrammar.TYPE_CLONE, NewDocGrammar.TYPE_SELF);
                typeNodes.forEach(typeTypeNode -> {
                    final String identifier = typeTypeNode.getTokenValue();
                    final GlobalReference globalRef = identifier.indexOf(':') != -1 ? GlobalReference.of(identifier) : GlobalReference.of(this.currentPakkage, identifier);
                    if (typeTypeNode.is(NewDocGrammar.TYPE_CLONE, NewDocGrammar.TYPE_SELF)) {
                        final Set<SemanticToken.Modifier> constModifier = Set.of(SemanticToken.Modifier.DOCUMENTATION, SemanticToken.Modifier.READONLY);
                        this.addSemanticToken(typeTypeNode, SemanticToken.Type.CLASS, constModifier);
                    } else if (this.isKnownType(globalRef)) {
                        this.addSemanticToken(typeTypeNode, SemanticToken.Type.CLASS, docModifier);
                    }
                });
            }
            // NAME
            final AstNode nameNode = elementNode.getFirstChild(NewDocGrammar.NAME);
            if (nameNode != null && nameNode.getToken() != null) {
                final SemanticToken.Type type = NEW_DOC_ELEMENT_TYPE_MAPPING.get(elementNode.getType());
                this.addSemanticToken(nameNode, type, docModifier);
            }
            // DESCRIPTION
            final List<AstNode> descriptionNodes = elementNode.getChildren(NewDocGrammar.DESCRIPTION);
            descriptionNodes.forEach(descriptionNode -> this.addSemanticToken(descriptionNode, SemanticToken.Type.COMMENT, docModifier));
        });
    } else {
        this.addSemanticToken(token, SemanticToken.Type.COMMENT);
    }
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Token(com.sonar.sslr.api.Token) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

NewDocParser (nl.ramsolutions.sw.magik.parser.NewDocParser)13 AstNode (com.sonar.sslr.api.AstNode)12 PackageNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper)10 Map (java.util.Map)9 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)9 MagikGrammar (nl.ramsolutions.sw.magik.api.MagikGrammar)9 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)8 TypeParser (nl.ramsolutions.sw.magik.analysis.typing.TypeParser)8 UndefinedType (nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 DefSlottedExemplarParser (nl.ramsolutions.sw.magik.analysis.definitions.DefSlottedExemplarParser)6 Definition (nl.ramsolutions.sw.magik.analysis.definitions.Definition)5 SlottedExemplarDefinition (nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition)5 MagikTypedCheck (nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck)5 Token (com.sonar.sslr.api.Token)4 Objects (java.util.Objects)4 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)4 IOException (java.io.IOException)3 URI (java.net.URI)3