Search in sources :

Example 1 with ArgumentsNodeHelper

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

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

use of nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper 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)

Example 4 with ArgumentsNodeHelper

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

the class DefineSharedVariableParser method parseDefinitions.

/**
 * Parse defitions.
 * @return List of parsed definitions.
 */
public List<Definition> parseDefinitions() {
    // Some sanity.
    final AstNode parentNode = node.getParent();
    final AstNode atomNode = parentNode.getFirstChild();
    if (atomNode.isNot(MagikGrammar.ATOM)) {
        throw new IllegalStateException();
    }
    // Assume this is an exemplar.
    final String exemplarName = atomNode.getTokenValue();
    if (exemplarName == null) {
        throw new IllegalStateException();
    }
    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);
    if (argument0Node == null || argument2Node == null) {
        throw new IllegalStateException();
    }
    // Figure statement node.
    final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
    // Figure pakkage.
    final String pakkage = this.getCurrentPakkage();
    final String variableNameSymbol = argument0Node.getTokenValue();
    final String variableName = variableNameSymbol.substring(1);
    final String flavor = argument2Node.getTokenValue();
    final List<MethodDefinition> methodDefinitions = this.generateVariableMethods(statementNode, pakkage, exemplarName, variableName, flavor);
    return List.copyOf(methodDefinitions);
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 5 with ArgumentsNodeHelper

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

the class DefineSlotAccessParser method parseDefinitions.

/**
 * Parse defitions.
 * @return List of parsed definitions.
 */
public List<Definition> parseDefinitions() {
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
    // Some sanity.
    final AstNode parentNode = node.getParent();
    final AstNode atomNode = parentNode.getFirstChild();
    if (atomNode.isNot(MagikGrammar.ATOM)) {
        throw new IllegalStateException();
    }
    final String exemplarName = atomNode.getTokenValue();
    if (exemplarName == null) {
        throw new IllegalStateException();
    }
    final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
    final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.SYMBOL);
    final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.SYMBOL);
    if (argument0Node == null || argument1Node == null) {
        throw new IllegalStateException();
    }
    // Figure statement node.
    final AstNode statementNode = node.getFirstAncestor(MagikGrammar.STATEMENT);
    // Figure pakkage.
    final String pakkage = this.getCurrentPakkage();
    final String slotNameSymbol = argument0Node.getTokenValue();
    final String slotName = slotNameSymbol.substring(1);
    final String flag = argument1Node.getTokenValue();
    final String flavor = argument2Node != null ? argument2Node.getTokenValue() : // Default is public.
    FLAVOR_PUBLIC;
    final List<MethodDefinition> methodDefinitions = this.generateSlotMethods(statementNode, pakkage, exemplarName, slotName, flag, flavor);
    return List.copyOf(methodDefinitions);
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AstNode (com.sonar.sslr.api.AstNode)19 ArgumentsNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper)19 ProcedureInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.ProcedureInvocationNodeHelper)7 MethodInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)1 ExpressionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.ExpressionNodeHelper)1 SimpleVectorNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.SimpleVectorNodeHelper)1