Search in sources :

Example 1 with BSLParserRuleContext

use of com.github._1c_syntax.bsl.parser.BSLParserRuleContext in project bsl-language-server by 1c-syntax.

the class CognitiveComplexityComputer method flattenExpression.

private List<Token> flattenExpression(BSLParser.ExpressionContext ctx) {
    ignoredContexts.add(ctx);
    List<Token> result = new ArrayList<>();
    final List<Tree> children = Trees.getChildren(ctx);
    for (Tree tree : children) {
        if (!(tree instanceof BSLParserRuleContext)) {
            continue;
        }
        BSLParserRuleContext parserRule = ((BSLParserRuleContext) tree);
        if (parserRule instanceof BSLParser.MemberContext) {
            flattenMember(result, (BSLParser.MemberContext) parserRule);
        } else if (parserRule instanceof BSLParser.OperationContext) {
            flattenOperation(result, (BSLParser.OperationContext) parserRule);
        }
    }
    return result;
}
Also used : BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) ArrayList(java.util.ArrayList) Tree(org.antlr.v4.runtime.tree.Tree) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token)

Example 2 with BSLParserRuleContext

use of com.github._1c_syntax.bsl.parser.BSLParserRuleContext in project bsl-language-server by 1c-syntax.

the class CyclomaticComplexityComputer method flattenExpression.

private static List<Token> flattenExpression(BSLParser.ExpressionContext ctx) {
    List<Token> result = new ArrayList<>();
    final List<Tree> children = Trees.getChildren(ctx);
    for (Tree tree : children) {
        if (!(tree instanceof BSLParserRuleContext)) {
            continue;
        }
        BSLParserRuleContext parserRule = ((BSLParserRuleContext) tree);
        if (parserRule instanceof BSLParser.MemberContext) {
            flattenMember(result, (BSLParser.MemberContext) parserRule);
        } else if (parserRule instanceof BSLParser.OperationContext) {
            flattenOperation(result, (BSLParser.OperationContext) parserRule);
        }
    }
    return result;
}
Also used : BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) ArrayList(java.util.ArrayList) Tree(org.antlr.v4.runtime.tree.Tree) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token)

Example 3 with BSLParserRuleContext

use of com.github._1c_syntax.bsl.parser.BSLParserRuleContext in project bsl-language-server by 1c-syntax.

the class CreateQueryInCycleDiagnostic method visitAccessCall.

@Override
public ParseTree visitAccessCall(BSLParser.AccessCallContext ctx) {
    if (!EXECUTE_CALL_PATTERN.matcher(ctx.methodCall().methodName().getText()).matches()) {
        return super.visitAccessCall(ctx);
    }
    if (!currentScope.codeFlowInCycle()) {
        return super.visitAccessCall(ctx);
    }
    String variableName = null;
    BSLParserRuleContext errorContext = null;
    BSLParserRuleContext parent = ctx.getParent();
    if (parent instanceof BSLParser.CallStatementContext) {
        errorContext = parent;
        variableName = getVariableNameFromCallStatementContext((BSLParser.CallStatementContext) parent);
    } else if (parent instanceof BSLParser.ModifierContext) {
        BSLParser.ModifierContext callModifier = (BSLParser.ModifierContext) parent;
        errorContext = callModifier.getParent();
        variableName = getVariableNameFromModifierContext(callModifier);
    }
    Optional<VariableDefinition> variableDefinition = currentScope.getVariableByName(variableName);
    BSLParserRuleContext finalErrorContext = errorContext;
    if (finalErrorContext != null) {
        variableDefinition.ifPresent((VariableDefinition definition) -> {
            if (definition.types.contains(QUERY_BUILDER_TYPE) || definition.types.contains(REPORT_BUILDER_TYPE) || definition.types.contains(QUERY_TYPE)) {
                diagnosticStorage.addDiagnostic(finalErrorContext);
            }
        });
    }
    return super.visitAccessCall(ctx);
}
Also used : BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) ToString(lombok.ToString)

Example 4 with BSLParserRuleContext

use of com.github._1c_syntax.bsl.parser.BSLParserRuleContext in project bsl-language-server by 1c-syntax.

the class SymbolTree method getMethodSymbol.

/**
 * Попытка поиска символа метода по узлу дерева разбора.
 * <p>
 * Implementation note - Поиск осуществляется по месту определения метода (declaration).
 *
 * @param ctx узел дерева разбора документа.
 * @return найденный символ метода.
 */
public Optional<MethodSymbol> getMethodSymbol(BSLParserRuleContext ctx) {
    BSLParserRuleContext subNameNode;
    if (Trees.nodeContainsErrors(ctx)) {
        subNameNode = ctx;
    } else if (ctx instanceof BSLParser.SubContext) {
        if (((BSLParser.SubContext) ctx).function() == null) {
            subNameNode = ((BSLParser.SubContext) ctx).procedure().procDeclaration().subName();
        } else {
            subNameNode = ((BSLParser.SubContext) ctx).function().funcDeclaration().subName();
        }
    } else {
        subNameNode = ctx;
    }
    Range subNameRange = Ranges.create(subNameNode);
    return getMethods().stream().filter(methodSymbol -> methodSymbol.getSubNameRange().equals(subNameRange)).findAny();
}
Also used : List(java.util.List) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) Getter(lombok.Getter) BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) Optional(java.util.Optional) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range) Trees(com.github._1c_syntax.bsl.languageserver.utils.Trees) Collectors(java.util.stream.Collectors) Value(lombok.Value) ArrayList(java.util.ArrayList) BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range)

Example 5 with BSLParserRuleContext

use of com.github._1c_syntax.bsl.parser.BSLParserRuleContext in project bsl-language-server by 1c-syntax.

the class SymbolTree method getVariableSymbol.

/**
 * Попытка поиска символа переменной по узлу дерева разбора.
 * <p>
 * Implementation note Поиск осуществляется по месту определения переменной (declaration).
 *
 * @param ctx узел дерева разбора документа.
 * @return найденный символ переменной.
 */
public Optional<VariableSymbol> getVariableSymbol(BSLParserRuleContext ctx) {
    BSLParserRuleContext varNameNode;
    if (Trees.nodeContainsErrors(ctx)) {
        varNameNode = ctx;
    } else if (ctx instanceof BSLParser.ModuleVarDeclarationContext) {
        varNameNode = ((BSLParser.ModuleVarDeclarationContext) ctx).var_name();
    } else if (ctx instanceof BSLParser.SubVarDeclarationContext) {
        varNameNode = ((BSLParser.SubVarDeclarationContext) ctx).var_name();
    } else {
        varNameNode = ctx;
    }
    Range variableNameRange = Ranges.create(varNameNode);
    return getVariables().stream().filter(variableSymbol -> variableSymbol.getVariableNameRange().equals(variableNameRange)).findAny();
}
Also used : List(java.util.List) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) Getter(lombok.Getter) BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) Optional(java.util.Optional) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range) Trees(com.github._1c_syntax.bsl.languageserver.utils.Trees) Collectors(java.util.stream.Collectors) Value(lombok.Value) ArrayList(java.util.ArrayList) BSLParserRuleContext(com.github._1c_syntax.bsl.parser.BSLParserRuleContext) BSLParser(com.github._1c_syntax.bsl.parser.BSLParser) Range(org.eclipse.lsp4j.Range)

Aggregations

BSLParserRuleContext (com.github._1c_syntax.bsl.parser.BSLParserRuleContext)11 BSLParser (com.github._1c_syntax.bsl.parser.BSLParser)8 ArrayList (java.util.ArrayList)7 Token (org.antlr.v4.runtime.Token)5 Trees (com.github._1c_syntax.bsl.languageserver.utils.Trees)4 Range (org.eclipse.lsp4j.Range)4 Ranges (com.github._1c_syntax.bsl.languageserver.utils.Ranges)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 DiagnosticMetadata (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata)2 DiagnosticSeverity (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity)2 DiagnosticTag (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag)2 DiagnosticType (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType)2 BSLLexer (com.github._1c_syntax.bsl.parser.BSLLexer)2 Optional (java.util.Optional)2 Getter (lombok.Getter)2 Value (lombok.Value)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 Tree (org.antlr.v4.runtime.tree.Tree)2