Search in sources :

Example 1 with Expression

use of com.google.idea.blaze.base.lang.buildfile.psi.Expression in project intellij by bazelbuild.

the class BuildElementGenerator method createExpressionFromText.

public Expression createExpressionFromText(String text) {
    PsiFile dummyFile = createDummyFile(text);
    PsiElement element = dummyFile.getFirstChild();
    if (element instanceof Expression) {
        return (Expression) element;
    }
    throw new RuntimeException("Could not parse text as expression: '" + text + "'");
}
Also used : FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 2 with Expression

use of com.google.idea.blaze.base.lang.buildfile.psi.Expression in project intellij by bazelbuild.

the class GlobReference method resolveListContents.

private static List<String> resolveListContents(Expression expr) {
    if (expr == null) {
        return ImmutableList.of();
    }
    PsiElement rootElement = PsiUtils.getReferencedTargetValue(expr);
    if (!(rootElement instanceof ListLiteral)) {
        return ImmutableList.of();
    }
    Expression[] children = ((ListLiteral) rootElement).getElements();
    List<String> strings = Lists.newArrayListWithCapacity(children.length);
    for (Expression child : children) {
        if (child instanceof StringLiteral) {
            strings.add(((StringLiteral) child).getStringContents());
        }
    }
    return strings;
}
Also used : ListLiteral(com.google.idea.blaze.base.lang.buildfile.psi.ListLiteral) StringLiteral(com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral) GlobExpression(com.google.idea.blaze.base.lang.buildfile.psi.GlobExpression) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) PsiElement(com.intellij.psi.PsiElement)

Example 3 with Expression

use of com.google.idea.blaze.base.lang.buildfile.psi.Expression in project intellij by bazelbuild.

the class ResolveUtil method searchInScope.

/**
 * Walks up PSI tree of local file, checking PsiNamedElements
 */
public static void searchInScope(PsiElement originalElement, Processor<BuildElement> processor) {
    // TODO: Handle list comprehension (where variable is defined *later* in the code)
    boolean topLevelScope = true;
    PsiElement element = originalElement;
    while (!(element instanceof PsiFileSystemItem)) {
        PsiElement parent = element.getParent();
        if (parent instanceof BuildFile) {
            if (!((BuildFile) parent).searchSymbolsInScope(processor, topLevelScope ? element : null)) {
                return;
            }
        } else if (parent instanceof FunctionStatement) {
            topLevelScope = false;
            for (Parameter param : ((FunctionStatement) parent).getParameters()) {
                if (!processor.process(param)) {
                    return;
                }
            }
        } else if (parent instanceof ForStatement) {
            for (Expression expr : ((ForStatement) parent).getForLoopVariables()) {
                if (expr instanceof TargetExpression && !processor.process(expr)) {
                    return;
                }
            }
        } else if (parent instanceof StatementList) {
            if (!visitChildAssignmentStatements((BuildElement) parent, (Processor) processor)) {
                return;
            }
        }
        element = parent;
    }
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) FunctionStatement(com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) StatementList(com.google.idea.blaze.base.lang.buildfile.psi.StatementList) Parameter(com.google.idea.blaze.base.lang.buildfile.psi.Parameter) TargetExpression(com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) ForStatement(com.google.idea.blaze.base.lang.buildfile.psi.ForStatement) PsiElement(com.intellij.psi.PsiElement)

Example 4 with Expression

use of com.google.idea.blaze.base.lang.buildfile.psi.Expression in project intellij by bazelbuild.

the class BuiltInRuleAnnotator method visitFuncallExpression.

@Override
public void visitFuncallExpression(FuncallExpression node) {
    BuildLanguageSpec spec = BuildLanguageSpecProvider.getInstance().getLanguageSpec(node.getProject());
    if (spec == null) {
        return;
    }
    String ruleName = node.getFunctionName();
    RuleDefinition rule = spec.getRule(ruleName);
    if (rule == null) {
        return;
    }
    if (node.getReferencedElement() != null) {
        // this has been locally overridden, so don't attempt validation
        return;
    }
    Set<String> missingAttributes = new TreeSet<>(rule.mandatoryAttributes.keySet());
    for (Argument arg : node.getArguments()) {
        if (arg instanceof Argument.StarStar) {
            missingAttributes.clear();
            continue;
        }
        String name = arg.getName();
        if (name == null) {
            continue;
        }
        AttributeDefinition attribute = rule.getAttribute(name);
        if (attribute == null) {
            markError(arg, String.format("Unrecognized attribute '%s' for rule type '%s'", name, ruleName));
            continue;
        }
        missingAttributes.remove(name);
        Expression argValue = arg.getValue();
        if (argValue == null) {
            continue;
        }
        PsiElement rootElement = PsiUtils.getReferencedTargetValue(argValue);
        if (!BuildElementValidation.possiblyValidType(rootElement, attribute.type)) {
            markError(arg, String.format("Invalid value for attribute '%s'. Expected a value of type '%s'", name, attribute.type));
        }
    }
    if (!missingAttributes.isEmpty()) {
        markError(node, String.format("Target missing required attribute(s): %s", Joiner.on(',').join(missingAttributes)));
    }
}
Also used : Argument(com.google.idea.blaze.base.lang.buildfile.psi.Argument) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) TreeSet(java.util.TreeSet) AttributeDefinition(com.google.idea.blaze.base.lang.buildfile.language.semantics.AttributeDefinition) RuleDefinition(com.google.idea.blaze.base.lang.buildfile.language.semantics.RuleDefinition) BuildLanguageSpec(com.google.idea.blaze.base.lang.buildfile.language.semantics.BuildLanguageSpec) PsiElement(com.intellij.psi.PsiElement)

Example 5 with Expression

use of com.google.idea.blaze.base.lang.buildfile.psi.Expression in project intellij by bazelbuild.

the class BuildFileModifierImpl method createRule.

private PsiElement createRule(Project project, Kind ruleKind, String ruleName) {
    String text = Joiner.on("\n").join(ruleKind.toString() + "(", "    name = \"" + ruleName + "\"", ")");
    Expression expr = BuildElementGenerator.getInstance(project).createExpressionFromText(text);
    assert (expr instanceof FuncallExpression);
    return expr;
}
Also used : FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) Expression(com.google.idea.blaze.base.lang.buildfile.psi.Expression) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)

Aggregations

Expression (com.google.idea.blaze.base.lang.buildfile.psi.Expression)5 PsiElement (com.intellij.psi.PsiElement)4 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)3 AttributeDefinition (com.google.idea.blaze.base.lang.buildfile.language.semantics.AttributeDefinition)1 BuildLanguageSpec (com.google.idea.blaze.base.lang.buildfile.language.semantics.BuildLanguageSpec)1 RuleDefinition (com.google.idea.blaze.base.lang.buildfile.language.semantics.RuleDefinition)1 Argument (com.google.idea.blaze.base.lang.buildfile.psi.Argument)1 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)1 ForStatement (com.google.idea.blaze.base.lang.buildfile.psi.ForStatement)1 FunctionStatement (com.google.idea.blaze.base.lang.buildfile.psi.FunctionStatement)1 GlobExpression (com.google.idea.blaze.base.lang.buildfile.psi.GlobExpression)1 ListLiteral (com.google.idea.blaze.base.lang.buildfile.psi.ListLiteral)1 Parameter (com.google.idea.blaze.base.lang.buildfile.psi.Parameter)1 StatementList (com.google.idea.blaze.base.lang.buildfile.psi.StatementList)1 StringLiteral (com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral)1 TargetExpression (com.google.idea.blaze.base.lang.buildfile.psi.TargetExpression)1 PsiFile (com.intellij.psi.PsiFile)1 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)1 TreeSet (java.util.TreeSet)1