From cd2ad0f9e082d101c0e9664729a4d2632659361d Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Mon, 14 Sep 2015 17:14:41 +0000 Subject: Fix and test SyntaxTreeVisitor -- MOS_MIGRATED_REVID=103003770 --- .../build/lib/syntax/SyntaxTreeVisitor.java | 29 ++++++-- .../build/lib/syntax/SyntaxTreeVisitorTest.java | 82 ++++++++++++++++++++++ 2 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java 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 d625c24973..a19d5ca1c0 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 @@ -40,7 +40,7 @@ public class SyntaxTreeVisitor { visit(node.getValue()); } - public void visit(Parameter node) { + public void visit(Parameter node) { // leaf node (we need the function for overrides) } @@ -55,6 +55,9 @@ public class SyntaxTreeVisitor { } public void visit(FuncallExpression node) { + if (node.getObject() != null) { + visit(node.getObject()); + } visit(node.getFunction()); visitAll(node.getArguments()); } @@ -66,7 +69,7 @@ public class SyntaxTreeVisitor { visit(node.getElementExpression()); for (ListComprehension.Clause clause : node.getClauses()) { if (clause.getLValue() != null) { - visit(clause.getLValue().getExpression()); + visit(clause.getLValue()); } visit(clause.getExpression()); } @@ -79,6 +82,12 @@ public class SyntaxTreeVisitor { visit(node.getListExpression()); } + public void visit(ForStatement node) { + visit(node.getVariable().getExpression()); + visit(node.getCollection()); + visitAll(node.block()); + } + public void visit(LoadStatement node) { visitAll(node.getSymbols()); } @@ -93,8 +102,12 @@ public class SyntaxTreeVisitor { public void visit(StringLiteral node) { } + public void visit(LValue node) { + visit(node.getExpression()); + } + public void visit(AssignmentStatement node) { - visit(node.getLValue().getExpression()); + visit(node.getLValue()); visit(node.getExpression()); } @@ -114,10 +127,7 @@ public class SyntaxTreeVisitor { public void visit(FunctionDefStatement node) { visit(node.getIdent()); - List defaults = node.getSignature().getDefaultValues(); - if (defaults != null) { - visitAll(defaults); - } + visitAll(node.getParameters()); visitAll(node.getStatements()); } @@ -138,6 +148,11 @@ public class SyntaxTreeVisitor { visit(node.getExpression()); } + public void visit(DotExpression node) { + visit(node.getObj()); + visit(node.getField()); + } + public void visit(Comment node) { } diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java new file mode 100644 index 0000000000..72edba4765 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java @@ -0,0 +1,82 @@ +// Copyright 2015 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.syntax; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.devtools.build.lib.cmdline.PackageIdentifier; +import com.google.devtools.build.lib.packages.CachingPackageLocator; +import com.google.devtools.build.lib.testutil.Scratch; +import com.google.devtools.build.lib.vfs.Path; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + +/** Tests for @{code SyntaxTreeVisitor} */ +@RunWith(JUnit4.class) +public class SyntaxTreeVisitorTest { + + private Scratch scratch = new Scratch(); + + private class ScratchPathPackageLocator implements CachingPackageLocator { + @Override + public Path getBuildFileForPackage(PackageIdentifier packageName) { + return scratch.resolve(packageName.getPackageFragment()).getRelative("BUILD"); + } + } + + private CachingPackageLocator locator = new ScratchPathPackageLocator(); + + /** Parses the contents of the specified string and returns the AST. */ + private BuildFileAST parse(String... lines) throws IOException { + Path file = scratch.file("/a/build/file/BUILD", lines); + return BuildFileAST.parseSkylarkFile( + file, null /* reporter */, locator, null /*validationEnvironment*/); + } + + @Test + public void everyIdentifierAndParameterIsVisitedInOrder() throws IOException { + final List idents = new ArrayList<>(); + final List params = new ArrayList<>(); + + class IdentVisitor extends SyntaxTreeVisitor { + @Override + public void visit(Identifier node) { + idents.add(node.getName()); + } + + @Override + public void visit(Parameter node) { + params.add(node.toString()); + } + } + + BuildFileAST ast = + parse( + "a = b", + "def c(p1, p2=4, **p3):", + " for d in e: f(g)", + " return h + i.j()"); + IdentVisitor visitor = new IdentVisitor(); + ast.accept(visitor); + assertThat(idents).containsExactly("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").inOrder(); + assertThat(params).containsExactly("p1", "p2=4", "**p3").inOrder(); + } +} -- cgit v1.2.3