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 + "'");
}
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;
}
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;
}
}
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)));
}
}
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;
}
Aggregations