Improve SyntaxTreeVisitor API.
RELNOTES: None.
PiperOrigin-RevId: 164481927
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java b/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java
index 2d2b50e..2ba51d7 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java
@@ -301,6 +301,11 @@
HashCode.fromBytes(file.getDigest()).toString(), eventHandler);
}
+ public static BuildFileAST parseSkylarkFile(ParserInputSource input, EventHandler eventHandler) {
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler, SKYLARK);
+ return create(ImmutableList.<Statement>of(), result, /*contentHashCode=*/ null, eventHandler);
+ }
+
/**
* Parse the specified non-build Skylark file but avoid the validation of the imports, returning
* its AST. All errors during scanning or parsing will be reported to the reporter.
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Parameter.java b/src/main/java/com/google/devtools/build/lib/syntax/Parameter.java
index 29e122c..e16fe58 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Parameter.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Parameter.java
@@ -199,6 +199,6 @@
@Override
public void accept(SyntaxTreeVisitor visitor) {
- visitor.visit(this);
+ visitor.visit((Parameter<Expression, Expression>) this);
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitor.java b/src/main/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitor.java
index 8229e39..c086fe3 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitor.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitor.java
@@ -39,8 +39,10 @@
visit(node.getValue());
}
- public void visit(@SuppressWarnings("unused") Parameter<?, ?> node) {
- // leaf node (we need the function for overrides)
+ public void visit(Parameter<Expression, Expression> node) {
+ if (node.getDefaultValue() != null) {
+ visit(node.getDefaultValue());
+ }
}
public void visit(BuildFileAST node) {
@@ -122,7 +124,11 @@
public void visit(FunctionDefStatement node) {
visit(node.getIdentifier());
- visitAll(node.getParameters());
+ // Do not use visitAll for the parameters, because we would lose the type information.
+ // Inside the AST, we know that Parameters are using Expressions.
+ for (Parameter<Expression, Expression> param : node.getParameters()) {
+ visit(param);
+ }
visitAll(node.getStatements());
}