Search in sources :

Example 6 with ArgumentsNodeHelper

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

the class DefEnumerationParser method parseDefinitions.

/**
 * Parse defitions.
 * @return List of parsed definitions.
 */
@Override
public List<Definition> parseDefinitions() {
    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();
    }
    // Figure statement node.
    final AstNode statementNode = this.node.getFirstAncestor(MagikGrammar.STATEMENT);
    // Figure pakkage.
    final String pakkage = this.getCurrentPakkage();
    // Figure name.
    final String name = argument0Node.getTokenValue().substring(1);
    // Figure parents.
    final List<String> parents = Collections.emptyList();
    final EnumerationDefinition enumerationDefinition = new EnumerationDefinition(statementNode, pakkage, name, parents);
    return List.of(enumerationDefinition);
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 7 with ArgumentsNodeHelper

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

the class DefPackageParser method parseDefinitions.

/**
 * Parse defitions.
 * @return List of parsed definitions.
 */
@SuppressWarnings("checkstyle:NestedIfDepth")
public List<Definition> parseDefinitions() {
    final AstNode argumentsNode = this.node.getFirstChild(MagikGrammar.ARGUMENTS);
    final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
    final AstNode argument0Node = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
    if (argument0Node == null) {
        throw new IllegalStateException();
    }
    // Figure statement node.
    final AstNode statementNode = this.node.getFirstAncestor(MagikGrammar.STATEMENT);
    // Figure package.
    final String pakkage = this.getCurrentPakkage();
    // Figure name.
    final String name = argument0Node.getTokenValue().substring(1);
    // Figure uses-list.
    final List<String> uses = new ArrayList<>();
    final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.SYMBOL);
    if (argument1Node != null && argument1Node.getTokenValue().equals(":uses")) {
        final AstNode argument2Node = argumentsHelper.getArgument(2, MagikGrammar.SIMPLE_VECTOR);
        if (argument2Node != null) {
            for (final AstNode expressionNode : argument2Node.getChildren(MagikGrammar.EXPRESSION)) {
                final ExpressionNodeHelper expressionHelper = new ExpressionNodeHelper(expressionNode);
                final String usesSymbol = expressionHelper.getConstant();
                if (usesSymbol != null) {
                    final String usesName = usesSymbol.substring(1);
                    uses.add(usesName);
                }
            }
        }
    } else {
        uses.add("sw");
    }
    final PackageDefinition packageDefinition = new PackageDefinition(statementNode, pakkage, name, uses);
    return List.of(packageDefinition);
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) ArrayList(java.util.ArrayList) ExpressionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ExpressionNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 8 with ArgumentsNodeHelper

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

the class ExemplarDefinitionVisitor method walkPreProcedureInvocation.

@Override
public void walkPreProcedureInvocation(final AstNode node) {
    final ProcedureInvocationNodeHelper invocationHelper = new ProcedureInvocationNodeHelper(node);
    if (!invocationHelper.isProcedureInvocationOf("def_slotted_exemplar") && !invocationHelper.isProcedureInvocationOf("def_indexed_exemplar") && !invocationHelper.isProcedureInvocationOf("def_enumeration") && !invocationHelper.isProcedureInvocationOf("def_enumeration_from")) {
        return;
    }
    final AstNode argumentsNode = node.getFirstChild(MagikGrammar.ARGUMENTS);
    final ArgumentsNodeHelper argumentsHelper = new ArgumentsNodeHelper(argumentsNode);
    final AstNode symbolNode = argumentsHelper.getArgument(0, MagikGrammar.SYMBOL);
    if (symbolNode == null) {
        return;
    }
    // exemplar found
    this.count++;
}
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 9 with ArgumentsNodeHelper

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

the class DefSlottedExemplarParser method parseDefinitions.

/**
 * Parse defitions.
 * @return List of parsed definitions.
 */
@Override
public List<Definition> parseDefinitions() {
    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.SIMPLE_VECTOR);
    if (argument1Node == null) {
        throw new IllegalStateException();
    }
    // Figure statement node.
    final AstNode statementNode = this.node.getFirstAncestor(MagikGrammar.STATEMENT);
    // Figure pakkage.
    final String pakkage = this.getCurrentPakkage();
    // Figure name.
    final String name = argument0Node.getTokenValue().substring(1);
    // Figure slots.
    final List<SlottedExemplarDefinition.Slot> slots = new ArrayList<>();
    final List<MethodDefinition> methodDefinitions = new ArrayList<>();
    for (final AstNode slotDefNode : argument1Node.getChildren(MagikGrammar.EXPRESSION)) {
        final SimpleVectorNodeHelper simpleVectorHelper = SimpleVectorNodeHelper.fromExpression(slotDefNode);
        final AstNode slotNameNode = simpleVectorHelper.getNth(0, MagikGrammar.SYMBOL);
        if (slotNameNode == null) {
            continue;
        }
        // Slot definitions.
        final String slotNameSymbol = slotNameNode.getTokenValue();
        final String slotName = slotNameSymbol.substring(1);
        final SlottedExemplarDefinition.Slot slot = new SlottedExemplarDefinition.Slot(slotDefNode, slotName);
        slots.add(slot);
        // Method definitions.
        final AstNode flagNode = simpleVectorHelper.getNth(2, MagikGrammar.SYMBOL);
        final AstNode flavorNode = simpleVectorHelper.getNth(3, MagikGrammar.SYMBOL);
        if (flagNode != null && flavorNode != null) {
            final String flag = flagNode.getTokenValue();
            final String flavor = flavorNode.getTokenValue();
            final List<MethodDefinition> slotMethodDefinitions = this.generateSlotMethods(slotDefNode, pakkage, name, slotName, flag, flavor);
            methodDefinitions.addAll(slotMethodDefinitions);
        }
    }
    // Parents.
    final AstNode argument2Node = argumentsHelper.getArgument(2);
    final List<String> parents = this.extractParents(argument2Node);
    final SlottedExemplarDefinition slottedExemplarDefinition = new SlottedExemplarDefinition(statementNode, pakkage, name, slots, parents);
    final List<Definition> definitions = new ArrayList<>();
    definitions.add(slottedExemplarDefinition);
    definitions.addAll(methodDefinitions);
    return definitions;
}
Also used : SimpleVectorNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.SimpleVectorNodeHelper) ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) ArrayList(java.util.ArrayList) AstNode(com.sonar.sslr.api.AstNode)

Example 10 with ArgumentsNodeHelper

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

the class DefSlottedExemplarParser method isDefSlottedExemplar.

/**
 * Test if node is a {@code def_slotted_exemplar()}.
 * @param node Node to test
 * @return True if node is a {@code def_slotted_exemplar()}, false otherwise.
 */
public static boolean isDefSlottedExemplar(final AstNode node) {
    if (!node.is(MagikGrammar.PROCEDURE_INVOCATION)) {
        return false;
    }
    final ProcedureInvocationNodeHelper helper = new ProcedureInvocationNodeHelper(node);
    if (!helper.isProcedureInvocationOf(DEF_SLOTTED_EXEMPLAR)) {
        return false;
    }
    // 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) {
        return false;
    }
    final AstNode argument1Node = argumentsHelper.getArgument(1, MagikGrammar.SIMPLE_VECTOR);
    return argument1Node != null;
}
Also used : ArgumentsNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper) ProcedureInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ProcedureInvocationNodeHelper) 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