Search in sources :

Example 31 with AstNode

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

the class PackageNodeHelper method getCurrentPackage.

/**
 * Get the current package from the current node.
 * @return Name of current package or "user" if none was found.
 */
public String getCurrentPackage() {
    // Find our parent node under MAGIK node.
    final AstNode magikNode = this.node.getFirstAncestor(MagikGrammar.MAGIK);
    AstNode topNode = this.node;
    while (topNode.getParent() != null && topNode.getParent() != magikNode) {
        topNode = topNode.getParent();
    }
    // Try to find via siblings.
    AstNode siblingNode = topNode.getPreviousSibling();
    while (siblingNode != null) {
        if (siblingNode.is(MagikGrammar.PACKAGE_SPECIFICATION)) {
            AstNode identifierNode = siblingNode.getFirstChild(MagikGrammar.IDENTIFIER);
            return identifierNode.getTokenValue();
        }
        siblingNode = siblingNode.getPreviousSibling();
    }
    // Default to package.
    return DEFAULT_PACKAGE;
}
Also used : AstNode(com.sonar.sslr.api.AstNode)

Example 32 with AstNode

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

the class ParameterNodeHelper method isOptionalParameter.

/**
 * Test if parameter is an {@code _optional} parameter.
 * @return True if optional, false otherwise.
 */
public boolean isOptionalParameter() {
    if (this.hasModifier(MagikKeyword.OPTIONAL)) {
        return true;
    }
    if (this.hasModifier(MagikKeyword.GATHER)) {
        return false;
    }
    final AstNode parametersNode = this.node.getParent();
    if (parametersNode.isNot(MagikGrammar.PARAMETERS)) {
        throw new IllegalStateException();
    }
    AstNode previousSiblingNode = this.node.getPreviousSibling();
    while (previousSiblingNode != null) {
        if (previousSiblingNode.is(MagikGrammar.PARAMETER)) {
            final ParameterNodeHelper helper = new ParameterNodeHelper(previousSiblingNode);
            if (helper.hasModifier(MagikKeyword.OPTIONAL)) {
                return true;
            }
        }
        previousSiblingNode = previousSiblingNode.getPreviousSibling();
    }
    return false;
}
Also used : AstNode(com.sonar.sslr.api.AstNode)

Example 33 with AstNode

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

the class DefineBinaryOperatorCaseParser method isBinaryOperatorCase.

/**
 * Test if node is a {@code define_binary_operator_case()}.
 * @param node Node to test
 * @return True if node is a {@code define_binary_operator_case()}, false otherwise.
 */
public static boolean isBinaryOperatorCase(final AstNode node) {
    if (!node.is(MagikGrammar.PROCEDURE_INVOCATION)) {
        return false;
    }
    final ProcedureInvocationNodeHelper helper = new ProcedureInvocationNodeHelper(node);
    if (!helper.isProcedureInvocationOf(DEFINE_BINARY_OPERATOR_CASE)) {
        return false;
    }
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
    final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
    if (argument0Node == null) {
        return false;
    }
    final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.IDENTIFIER);
    if (argument1Node == null) {
        return false;
    }
    final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.IDENTIFIER);
    if (argument2Node == null) {
        return false;
    }
    // Anything will do.
    final AstNode argument3Node = argumentsHelper.getArgument(3);
    if (argument3Node == null) {
        return false;
    }
    final String operatorSymbol = argument0Node.getTokenValue();
    return !operatorSymbol.isEmpty();
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) ProcedureInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ProcedureInvocationNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 34 with AstNode

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

the class DefineBinaryOperatorCaseParser method parseDefinitions.

/**
 * Parse defitions.
 * @return List of parsed definitions.
 */
public List<Definition> parseDefinitions() {
    // Some sanity.
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
    final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
    if (argument0Node == null) {
        throw new IllegalStateException();
    }
    final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.IDENTIFIER);
    if (argument1Node == null) {
        throw new IllegalStateException();
    }
    final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.IDENTIFIER);
    if (argument2Node == null) {
        throw new IllegalStateException();
    }
    // Anything will do.
    final AstNode argument3Node = argumentsHelper.getArgument(3);
    if (argument3Node == null) {
        throw new IllegalStateException();
    }
    final String operatorSymbol = argument0Node.getTokenValue();
    if (operatorSymbol.isEmpty()) {
        throw new IllegalStateException();
    }
    // Figure statement node.
    final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
    // Figure pakkage.
    final String pakkage = this.getCurrentPakkage();
    // Figure operator & lhs & rhs.
    final String operator = operatorSymbol.substring(1);
    final String lhs = argument1Node.getTokenValue();
    final String rhs = argument2Node.getTokenValue();
    final BinaryOperatorDefinition operatorDefinition = new BinaryOperatorDefinition(statementNode, pakkage, operator, lhs, rhs);
    return List.of(operatorDefinition);
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 35 with AstNode

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

the class DefineSharedConstantParser method isDefineSharedConstant.

/**
 * Test if node is a {@code define_shared_constant()}.
 * @param node Node to test
 * @return True if node is a {@code define_shared_variable()}, false otherwise.
 */
public static boolean isDefineSharedConstant(final AstNode node) {
    if (!node.is(MagikGrammar.METHOD_INVOCATION)) {
        return false;
    }
    final MethodInvocationNodeHelper helper = new MethodInvocationNodeHelper(node);
    if (!helper.isMethodInvocationOf(DEFINE_SHARED_CONSTANT)) {
        return false;
    }
    // Some sanity.
    final AstNode parentNode = node.getParent();
    final AstNode atomNode = parentNode.getFirstChild();
    if (atomNode.isNot(MagikGrammar.ATOM)) {
        return false;
    }
    // Assume this is an exemplar.
    final String exemplarName = atomNode.getTokenValue();
    if (exemplarName == null) {
        return false;
    }
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
    final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
    final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.SYMBOL);
    return argument0Node != null && argument2Node != null;
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AstNode (com.sonar.sslr.api.AstNode)491 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