Search in sources :

Example 1 with SimpleVectorNodeHelper

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

Aggregations

AstNode (com.sonar.sslr.api.AstNode)1 ArrayList (java.util.ArrayList)1 ArgumentsNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.ArgumentsNodeHelper)1 SimpleVectorNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.SimpleVectorNodeHelper)1