Search in sources :

Example 36 with AstNode

use of com.sonar.sslr.api.AstNode 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 37 with AstNode

use of com.sonar.sslr.api.AstNode 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)

Example 38 with AstNode

use of com.sonar.sslr.api.AstNode 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 39 with AstNode

use of com.sonar.sslr.api.AstNode 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 40 with AstNode

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

the class Scope method getEndColumn.

/**
 * Get the end column of this scope.
 * @return End column.
 */
public int getEndColumn() {
    final AstNode parentNode = this.node.getParent();
    final List<Token> tokens = parentNode.getTokens();
    final Token lastToken = tokens.get(tokens.size() - 1);
    return lastToken.getColumn();
}
Also used : Token(com.sonar.sslr.api.Token) 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