aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2015-09-17 12:18:45 +0000
committerGravatar David Chen <dzc@google.com>2015-09-17 19:35:31 +0000
commitde27f9e46892f6dd95966185a7cf09e256dfe5ff (patch)
treea32d62f025d2d5b6132f33947ef4c26155893ab0 /src/main/java/com/google/devtools/build/lib/syntax
parent4e5037520e3067f9d8784e1c59f9545b96111cd4 (diff)
Create an abstraction for user defined data types in Skylark.
This will be used to remove Blaze-specific data types, FilesetEntry, Label and GlobList from the .syntax package. -- MOS_MIGRATED_REVID=103281375
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java9
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/FilesetEntry.java31
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/GlobList.java16
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/Printer.java29
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SkylarkValue.java41
5 files changed, 96 insertions, 30 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
index ca5f4684f1..6b6608c89d 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
@@ -128,11 +128,12 @@ public abstract class EvalUtils {
* dictionary key) according to the rules of the Build language.
*/
public static boolean isImmutable(Object o) {
- if (o instanceof Map<?, ?> || o instanceof BaseFunction
- || o instanceof FilesetEntry || o instanceof GlobList<?>) {
+ if (o instanceof Map<?, ?> || o instanceof BaseFunction) {
return false;
} else if (o instanceof List<?>) {
return isTuple((List<?>) o); // tuples are immutable, lists are not.
+ } else if (o instanceof SkylarkValue) {
+ return ((SkylarkValue) o).isImmutable();
} else {
return true; // string/int
}
@@ -282,14 +283,10 @@ public abstract class EvalUtils {
// from native SkylarkList tuple and list.
// TODO(bazel-team): refactor SkylarkList and use it everywhere.
return isTuple(c) ? "Tuple" : "List";
- } else if (GlobList.class.isAssignableFrom(c)) {
- return "glob list";
} else if (Map.class.isAssignableFrom(c)) {
return "dict";
} else if (BaseFunction.class.isAssignableFrom(c)) {
return "function";
- } else if (c.equals(FilesetEntry.class)) {
- return "FilesetEntry";
} else if (c.equals(SelectorValue.class)) {
return "select";
} else if (NestedSet.class.isAssignableFrom(c) || SkylarkNestedSet.class.isAssignableFrom(c)) {
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/FilesetEntry.java b/src/main/java/com/google/devtools/build/lib/syntax/FilesetEntry.java
index 4586b64f44..c876d6328f 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/FilesetEntry.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/FilesetEntry.java
@@ -30,7 +30,36 @@ import javax.annotation.Nullable;
/**
* FilesetEntry is a value object used to represent a "FilesetEntry" inside a "Fileset" BUILD rule.
*/
-public final class FilesetEntry {
+@SkylarkModule(
+ name = "FilesetEntry",
+ doc = "",
+ documented = false)
+public final class FilesetEntry implements SkylarkValue {
+
+ @Override
+ public boolean isImmutable() {
+ return false;
+ }
+
+ @Override
+ public void write(Appendable buffer, char quotationMark) {
+ Printer.append(buffer, "FilesetEntry(srcdir = ");
+ Printer.write(buffer, getSrcLabel().toString(), quotationMark);
+ Printer.append(buffer, ", files = ");
+ Printer.write(buffer, Printer.makeStringList(getFiles()), quotationMark);
+ Printer.append(buffer, ", excludes = ");
+ Printer.write(buffer, Printer.makeList(getExcludes()), quotationMark);
+ Printer.append(buffer, ", destdir = ");
+ Printer.write(buffer, getDestDir().getPathString(), quotationMark);
+ Printer.append(buffer, ", strip_prefix = ");
+ Printer.write(buffer, getStripPrefix(), quotationMark);
+ Printer.append(buffer, ", symlinks = ");
+ Printer.append(buffer, quotationMark);
+ Printer.append(buffer, getSymlinkBehavior().toString());
+ Printer.append(buffer, quotationMark);
+ Printer.append(buffer, ")");
+ }
+
/** SymlinkBehavior decides what to do when a source file of a FilesetEntry is a symlink. */
public enum SymlinkBehavior {
/** Just copies the symlink as-is. May result in dangling links. */
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java b/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java
index 82afd0104d..d468033b80 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/GlobList.java
@@ -33,7 +33,11 @@ import java.util.List;
*
* @param <E> the element this List contains (generally either String or Label)
*/
-public class GlobList<E> extends ForwardingList<E> {
+@SkylarkModule(
+ name = "glob list",
+ doc = "",
+ documented = false)
+public final class GlobList<E> extends ForwardingList<E> implements SkylarkValue {
/** Include/exclude criteria. */
private final ImmutableList<GlobCriteria> criteria;
@@ -119,4 +123,14 @@ public class GlobList<E> extends ForwardingList<E> {
protected ImmutableList<E> delegate() {
return matches;
}
+
+ @Override
+ public boolean isImmutable() {
+ return false;
+ }
+
+ @Override
+ public void write(Appendable buffer, char quotationMark) {
+ Printer.printList(buffer, this, false, quotationMark);
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
index 46ccc55763..2586306bf0 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
@@ -158,27 +158,12 @@ public final class Printer {
} else if (o instanceof Label) {
write(buffer, o.toString(), quotationMark);
- } else if (o instanceof FilesetEntry) {
- FilesetEntry entry = (FilesetEntry) o;
- append(buffer, "FilesetEntry(srcdir = ");
- write(buffer, entry.getSrcLabel().toString(), quotationMark);
- append(buffer, ", files = ");
- write(buffer, makeStringList(entry.getFiles()), quotationMark);
- append(buffer, ", excludes = ");
- write(buffer, makeList(entry.getExcludes()), quotationMark);
- append(buffer, ", destdir = ");
- write(buffer, entry.getDestDir().getPathString(), quotationMark);
- append(buffer, ", strip_prefix = ");
- write(buffer, entry.getStripPrefix(), quotationMark);
- append(buffer, ", symlinks = ");
- append(buffer, quotationMark);
- append(buffer, entry.getSymlinkBehavior().toString());
- append(buffer, quotationMark);
- append(buffer, ")");
-
} else if (o instanceof PathFragment) {
append(buffer, ((PathFragment) o).getPathString());
+ } else if (o instanceof SkylarkValue) {
+ ((SkylarkValue) o).write(buffer, quotationMark);
+
} else {
append(buffer, o.toString());
}
@@ -206,7 +191,7 @@ public final class Printer {
// Throughout this file, we transform IOException into AssertionError.
// During normal operations, we only use in-memory Appendable-s that
// cannot cause an IOException.
- private static Appendable append(Appendable buffer, char c) {
+ public static Appendable append(Appendable buffer, char c) {
try {
return buffer.append(c);
} catch (IOException e) {
@@ -214,7 +199,7 @@ public final class Printer {
}
}
- private static Appendable append(Appendable buffer, CharSequence s) {
+ public static Appendable append(Appendable buffer, CharSequence s) {
try {
return buffer.append(s);
} catch (IOException e) {
@@ -357,11 +342,11 @@ public final class Printer {
return listString(list, before, separator, after, singletonTerminator, SKYLARK_QUOTATION_MARK);
}
- private static List<?> makeList(Collection<?> list) {
+ public static List<?> makeList(Collection<?> list) {
return list == null ? Lists.newArrayList() : Lists.newArrayList(list);
}
- private static List<String> makeStringList(List<Label> labels) {
+ public static List<String> makeStringList(List<Label> labels) {
if (labels == null) {
return Collections.emptyList();
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkValue.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkValue.java
new file mode 100644
index 0000000000..b93b0cda18
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkValue.java
@@ -0,0 +1,41 @@
+// 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;
+
+/**
+ * Java objects that are also Skylark values.
+ *
+ * <p>This is used for extending the Skylark interpreted with domain-specific values.
+ */
+public interface SkylarkValue {
+
+ /**
+ * Returns if the value is immutable and thus suitable for being used as a dictionary key.
+ *
+ * <p>Immutability is deep, i.e. in order for a value to be immutable, all values it is composed
+ * of must be immutable, too.
+ */
+ boolean isImmutable();
+
+ /**
+ * Print an official representation of object x.
+ *
+ * <p>For regular data structures, the value should be parsable back into an equal data structure.
+ *
+ * @param buffer the buffer to append the representation to
+ * @param quotationMark The quote style (" or ') to be used
+ */
+ void write(Appendable buffer, char quotationMark);
+}