Search in sources :

Example 11 with AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class LocalTypeReasoner method walkPostBody.

@Override
protected void walkPostBody(final AstNode node) {
    // Get result from upper EXPRESSION.
    final AstNode expressionNode = node.getFirstAncestor(MagikGrammar.EXPRESSION);
    if (expressionNode == null) {
        // Happens with a return, don't do anything.
        return;
    }
    ExpressionResult result = this.getNodeType(expressionNode);
    // BODYs don't always have to result in something.
    // Find STATEMENT -> RETURN/EMIT/LEAVE
    // TODO: what about _loop _if .. _then _leave _with _endif _leave _with _endloop
    AstNode resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.RETURN_STATEMENT);
    if (resultingNode == null) {
        resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.EMIT_STATEMENT);
    }
    if (resultingNode == null) {
        resultingNode = AstQuery.getFirstChildFromChain(node, MagikGrammar.STATEMENT, MagikGrammar.LEAVE_STATEMENT);
    }
    if (resultingNode == null) {
        // Result can also be an unset, as no resulting statement was found.
        // TODO: but... "_block _block _return 1 _endblock _endblock"
        final ExpressionResult emptyResult = new ExpressionResult();
        final AbstractType unsetType = this.typeKeeper.getType(SW_UNSET);
        result = new ExpressionResult(result, emptyResult, unsetType);
    }
    // Set parent EXPRESSION result.
    this.setNodeType(expressionNode, result);
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Example 12 with AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class LocalTypeReasoner method walkPostLoopbody.

@Override
protected void walkPostLoopbody(final AstNode node) {
    final AbstractType unsetType = this.typeKeeper.getType(SW_UNSET);
    // Get results.
    final AstNode multiValueExprNode = node.getFirstChild(MagikGrammar.TUPLE);
    final ExpressionResult result = this.getNodeType(multiValueExprNode);
    // Find related node to store at.
    final AstNode procMethodDefNode = node.getFirstAncestor(MagikGrammar.PROCEDURE_DEFINITION, MagikGrammar.METHOD_DEFINITION);
    // Save results.
    if (this.hasNodeType(procMethodDefNode)) {
        // Combine types.
        final ExpressionResult existingResult = this.getNodeType(procMethodDefNode);
        final ExpressionResult combinedResult = new ExpressionResult(existingResult, result, unsetType);
        this.setLoopbodyNodeType(procMethodDefNode, combinedResult);
    } else {
        this.setLoopbodyNodeType(procMethodDefNode, result);
    }
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Example 13 with AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class LocalTypeReasoner method assignAtom.

private void assignAtom(final AstNode node, final ExpressionResult result) {
    final AstNode atomNode = node.getParent();
    this.setNodeType(atomNode, result);
}
Also used : AstNode(com.sonar.sslr.api.AstNode)

Example 14 with AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class LocalTypeReasoner method walkPostEmitStatement.

@Override
protected void walkPostEmitStatement(final AstNode node) {
    // Get results.
    final AstNode tupleNode = node.getFirstChild(MagikGrammar.TUPLE);
    final ExpressionResult result = this.getNodeType(tupleNode);
    // Find related node.
    final AstNode bodyNode = node.getFirstAncestor(MagikGrammar.BODY);
    final AstNode expressionNode = bodyNode.getFirstAncestor(// for BLOCK etc
    MagikGrammar.EXPRESSION, // for METHOD_DEFINITION
    MagikGrammar.METHOD_DEFINITION, // for PROC_DEFINITION
    MagikGrammar.PROCEDURE_DEFINITION);
    // Save results.
    this.addNodeType(expressionNode, result);
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AstNode(com.sonar.sslr.api.AstNode)

Example 15 with AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class LocalTypeReasoner method walkPostSuper.

@Override
protected void walkPostSuper(final AstNode node) {
    // Determine which type we are.
    final AbstractType methodOwnerType = this.getMethodOwnerType(node);
    if (methodOwnerType == UndefinedType.INSTANCE) {
        LOGGER.debug("Unknown type for node: {}", node);
        return;
    }
    // Find specified super, if given.
    final AstNode identifierNode = node.getFirstChild(MagikGrammar.IDENTIFIER);
    final String identifier = identifierNode != null ? identifierNode.getTokenValue() : null;
    final AbstractType superType;
    if (identifier != null) {
        superType = methodOwnerType.getParents().stream().filter(parentType -> {
            final String fullTypeName = parentType.getFullName();
            final String typeName = fullTypeName.split(":")[1];
            return identifier.equals(typeName);
        }).findAny().orElse(null);
    } else {
        superType = methodOwnerType.getParents().stream().reduce(CombinedType::combine).orElse(null);
    }
    if (superType == null) {
        return;
    }
    final ExpressionResult result = new ExpressionResult(superType);
    this.assignAtom(node, result);
}
Also used : Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) AstNode(com.sonar.sslr.api.AstNode) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ArrayList(java.util.ArrayList) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) MagikKeyword(nl.ramsolutions.sw.magik.api.MagikKeyword) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Map(java.util.Map) LeaveStatementNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.LeaveStatementNodeHelper) URI(java.net.URI) Location(nl.ramsolutions.sw.magik.analysis.Location) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EnumSet(java.util.EnumSet) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Logger(org.slf4j.Logger) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) Collection(java.util.Collection) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) ParameterNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ParameterNodeHelper) AstWalker(nl.ramsolutions.sw.magik.analysis.AstWalker) Objects(java.util.Objects) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) List(java.util.List) ProcedureInstance(nl.ramsolutions.sw.magik.analysis.typing.types.ProcedureInstance) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AstNode (com.sonar.sslr.api.AstNode)492 Test (org.junit.Test)108 Test (org.junit.jupiter.api.Test)91 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)71 Token (com.sonar.sslr.api.Token)50 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)49 Token (org.sonar.plugins.python.api.tree.Token)46 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)41 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)40 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)39 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)39 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)38 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)38 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)38 Expression (org.sonar.plugins.python.api.tree.Expression)38 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)38 List (java.util.List)35 RuleTest (org.sonar.python.parser.RuleTest)34 ArrayList (java.util.ArrayList)33 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)33