Search in sources :

Example 1 with ExpressionNodeHelper

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

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

the class TypeDefParser method extractParents.

/**
 * Extract parents.
 * @param definitionNode Definition node.
 * @return List of parents.
 */
protected List<String> extractParents(@Nullable AstNode definitionNode) {
    if (definitionNode == null) {
        return Collections.emptyList();
    }
    final List<String> parents = new ArrayList<>();
    final ExpressionNodeHelper expressionHelper = new ExpressionNodeHelper(definitionNode);
    final String singleParent = expressionHelper.getConstant();
    final AstNode multiParentNode = AstQuery.getOnlyFromChain(definitionNode, MagikGrammar.ATOM, MagikGrammar.SIMPLE_VECTOR);
    if (singleParent != null) {
        final String parent = singleParent.startsWith(":") || singleParent.startsWith("@") ? singleParent.substring(1) : singleParent;
        parents.add(parent);
    } else if (multiParentNode != null) {
        for (final AstNode parentNode : multiParentNode.getChildren(MagikGrammar.EXPRESSION)) {
            final ExpressionNodeHelper parentExpressionHelper = new ExpressionNodeHelper(parentNode);
            final String parentStr = parentExpressionHelper.getConstant();
            Objects.requireNonNull(parentStr);
            final String parent = parentStr.startsWith(":") || parentStr.startsWith("@") ? parentStr.substring(1) : parentStr;
            parents.add(parent);
        }
    }
    return parents;
}
Also used : ArrayList(java.util.ArrayList) ExpressionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.ExpressionNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

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