Search in sources :

Example 1 with PackageNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper 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 2 with PackageNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper in project magik-tools by StevenLooman.

the class MethodReturnMatchesDocTypedCheck method extractMethodDocResult.

private ExpressionResult extractMethodDocResult(final AstNode node) {
    final PackageNodeHelper packageHelper = new PackageNodeHelper(node);
    final String currentPakkage = packageHelper.getCurrentPackage();
    final NewDocParser docParser = new NewDocParser(node);
    final ITypeKeeper typeKeeper = this.getTypeKeeper();
    final TypeParser typeParser = new TypeParser(typeKeeper);
    final List<AbstractType> docReturnTypes = docParser.getReturnTypes().stream().map(typeStr -> typeParser.parseTypeString(typeStr, currentPakkage)).collect(Collectors.toList());
    return new ExpressionResult(docReturnTypes);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) AstNode(com.sonar.sslr.api.AstNode) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) Collectors(java.util.stream.Collectors) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) Objects(java.util.Objects) LocalTypeReasoner(nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) List(java.util.List) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikTypedCheck(nl.ramsolutions.sw.magik.typedchecks.MagikTypedCheck) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)

Example 3 with PackageNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper in project magik-tools by StevenLooman.

the class NewDocTypeExistsTypeCheck method checkMethodProcedureDefinition.

private void checkMethodProcedureDefinition(final AstNode node) {
    final NewDocParser newDocParser = new NewDocParser(node);
    final PackageNodeHelper packageNodeHelper = new PackageNodeHelper(node);
    final String pakkage = packageNodeHelper.getCurrentPackage();
    this.checkDefinitionParameters(newDocParser, pakkage);
    this.checkDefinitionLoops(newDocParser, pakkage);
    this.checkDefinitionReturns(newDocParser, pakkage);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper)

Example 4 with PackageNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper in project magik-tools by StevenLooman.

the class LocalTypeReasoner method walkPostParameter.

@Override
protected void walkPostParameter(final AstNode node) {
    final AstNode identifierNode = node.getFirstChild(MagikGrammar.IDENTIFIER);
    final String identifier = identifierNode.getTokenValue();
    // Parse method/proc docs and extract parameter type.
    final AstNode definitionNode = node.getFirstAncestor(MagikGrammar.METHOD_DEFINITION, MagikGrammar.PROCEDURE_DEFINITION);
    final NewDocParser docParser = new NewDocParser(definitionNode);
    final Map<String, String> parameterTypes = docParser.getParameterTypes();
    final String parameterTypeStr = parameterTypes.get(identifier);
    final ExpressionResult result;
    final ParameterNodeHelper helper = new ParameterNodeHelper(node);
    if (helper.isGatherParameter()) {
        final AbstractType simpleVectorType = this.typeKeeper.getType(SW_SIMPLE_VECTOR);
        result = new ExpressionResult(simpleVectorType);
    } else if (parameterTypeStr != null && !parameterTypeStr.isBlank()) {
        final PackageNodeHelper packageHelper = new PackageNodeHelper(node);
        final String pakkage = packageHelper.getCurrentPackage();
        final AbstractType type = this.typeParser.parseTypeString(parameterTypeStr, pakkage);
        if (helper.isOptionalParameter()) {
            final AbstractType unsetType = this.typeKeeper.getType(SW_UNSET);
            final AbstractType optionalType = new CombinedType(type, unsetType);
            result = new ExpressionResult(optionalType);
        } else {
            result = new ExpressionResult(type);
        }
    } else {
        result = ExpressionResult.UNDEFINED;
    }
    this.setNodeType(node, result);
    final Scope scope = this.globalScope.getScopeForNode(node);
    Objects.requireNonNull(scope);
    final ScopeEntry scopeEntry = scope.getScopeEntry(identifier);
    this.currentScopeEntryNodes.put(scopeEntry, node);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) ParameterNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ParameterNodeHelper) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Example 5 with PackageNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper in project magik-tools by StevenLooman.

the class MagikIndexer method fillType.

private void fillType(final MagikType magikType, final MagikFile magikFile, final AstNode node, final String packageName, final Map<String, String> slots, final List<String> parents, @Nullable final AbstractType defaultParentType) {
    // Set location.
    final URI uri = magikFile.getUri();
    final Location location = new Location(uri, node);
    magikType.setLocation(location);
    // Set slots.
    magikType.clearSlots();
    slots.entrySet().forEach(entry -> {
        final String slotName = entry.getKey();
        final String slotTypeName = entry.getValue();
        final AbstractType slotType = this.typeParser.parseTypeString(slotTypeName, packageName);
        final Slot slot = magikType.addSlot(null, slotName);
        slot.setType(slotType);
    });
    // Parents.
    magikType.clearParents();
    final PackageNodeHelper packageHelper = new PackageNodeHelper(node);
    final String pakkageName = packageHelper.getCurrentPackage();
    parents.stream().forEach(parent -> {
        final GlobalReference parentGlobalRef = parent.indexOf(':') != -1 ? GlobalReference.of(parent) : GlobalReference.of(pakkageName, parent);
        final AbstractType parentType = this.typeKeeper.getType(parentGlobalRef);
        if (parentType == UndefinedType.INSTANCE) {
            LOGGER.warn("Parent type not found: {} from package: {}", parent, pakkageName);
        } else {
            magikType.addParent(parentType);
        }
    });
    // Default parent types ()
    boolean parentNonIntrinsicType = magikType.getParents().stream().anyMatch(parentType -> parentType instanceof SlottedType || parentType instanceof IndexedType);
    if (defaultParentType != null && !parentNonIntrinsicType) {
        magikType.addParent(defaultParentType);
    }
    // Type doc.
    final String typeDoc = MagikCommentExtractor.extractDocComments(node).map(Token::getValue).map(// Strip '##'
    line -> line.substring(2)).collect(Collectors.joining("\n"));
    magikType.setDoc(typeDoc);
}
Also used : 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) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Token(com.sonar.sslr.api.Token) URI(java.net.URI) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) Location(nl.ramsolutions.sw.magik.analysis.Location)

Aggregations

PackageNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper)6 AstNode (com.sonar.sslr.api.AstNode)5 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)5 NewDocParser (nl.ramsolutions.sw.magik.parser.NewDocParser)5 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)4 TypeParser (nl.ramsolutions.sw.magik.analysis.typing.TypeParser)3 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)3 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 MethodDefinitionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper)2 MethodInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)2 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)2 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)2 ScopeEntry (nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry)2 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)2 Token (com.sonar.sslr.api.Token)1 IOException (java.io.IOException)1 URI (java.net.URI)1