aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/cmdline
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2015-09-15 13:56:14 +0000
committerGravatar John Field <jfield@google.com>2015-09-15 20:27:47 +0000
commita6434361097c0ee18c706bf7a86a93324f68e284 (patch)
tree98a3109cb41b1f6cbcb5bae4f9452fdb81553ae4 /src/main/java/com/google/devtools/build/lib/cmdline
parent5d737d642623749c8672916548b7c7c85b2ca9e6 (diff)
Parse the label syntax "@//a:b" so that eventually we can make that the syntax that means "refer to the main repository".
There isn't an overarching plan for what we are going to do with the cmdline package, which seems to be separated from the .syntax one in all sorts of awkward ways. -- MOS_MIGRATED_REVID=103088960
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/LabelSyntaxException.java24
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java33
2 files changed, 40 insertions, 17 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/LabelSyntaxException.java b/src/main/java/com/google/devtools/build/lib/cmdline/LabelSyntaxException.java
new file mode 100644
index 0000000000..3c66959021
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/LabelSyntaxException.java
@@ -0,0 +1,24 @@
+// 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.cmdline;
+
+/**
+ * Thrown by the parsing methods to indicate a bad label.
+ */
+public class LabelSyntaxException extends Exception {
+ public LabelSyntaxException(String message) {
+ super(message);
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
index 8867c98d2c..9d0ef0edcf 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java
@@ -55,12 +55,12 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
.build(
new CacheLoader<String, RepositoryName> () {
@Override
- public RepositoryName load(String name) throws TargetParsingException {
+ public RepositoryName load(String name) throws LabelSyntaxException {
String errorMessage = validate(name);
if (errorMessage != null) {
errorMessage = "invalid repository name '"
+ StringUtilities.sanitizeControlChars(name) + "': " + errorMessage;
- throw new TargetParsingException(errorMessage);
+ throw new LabelSyntaxException(errorMessage);
}
return new RepositoryName(StringCanonicalizer.intern(name));
}
@@ -70,11 +70,11 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
* Makes sure that name is a valid repository name and creates a new RepositoryName using it.
* @throws TargetParsingException if the name is invalid.
*/
- public static RepositoryName create(String name) throws TargetParsingException {
+ public static RepositoryName create(String name) throws LabelSyntaxException {
try {
return repositoryNameCache.get(name);
} catch (ExecutionException e) {
- Throwables.propagateIfInstanceOf(e.getCause(), TargetParsingException.class);
+ Throwables.propagateIfInstanceOf(e.getCause(), LabelSyntaxException.class);
throw new IllegalStateException("Failed to create RepositoryName from " + name, e);
}
}
@@ -97,11 +97,6 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
return "workspace name must start with '@'";
}
- // "@" isn't a valid workspace name.
- if (name.length() == 1) {
- return "empty workspace name";
- }
-
// Check for any character outside of [/0-9A-Za-z_.-]. Try to evaluate the
// conditional quickly (by looking in decreasing order of character class
// likelihood).
@@ -174,11 +169,13 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
public static final String DEFAULT_REPOSITORY = "";
public static final RepositoryName DEFAULT_REPOSITORY_NAME;
+ public static final RepositoryName MAIN_REPOSITORY_NAME;
static {
try {
DEFAULT_REPOSITORY_NAME = RepositoryName.create(DEFAULT_REPOSITORY);
- } catch (TargetParsingException e) {
+ MAIN_REPOSITORY_NAME = RepositoryName.create("@");
+ } catch (LabelSyntaxException e) {
throw new IllegalStateException(e);
}
}
@@ -205,7 +202,7 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
throws IOException, ClassNotFoundException {
try {
packageId = new PackageIdentifier((String) in.readObject(), (PathFragment) in.readObject());
- } catch (TargetParsingException e) {
+ } catch (LabelSyntaxException e) {
throw new IOException("Error serializing package identifier: " + e.getMessage());
}
}
@@ -228,7 +225,7 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
public static PackageIdentifier createInDefaultRepo(PathFragment name) {
try {
return new PackageIdentifier(DEFAULT_REPOSITORY, name);
- } catch (TargetParsingException e) {
+ } catch (LabelSyntaxException e) {
throw new IllegalArgumentException("could not create package identifier for " + name
+ ": " + e.getMessage());
}
@@ -243,7 +240,7 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
/** The name of the package. Canonical (i.e. x.equals(y) <=> x==y). */
private final PathFragment pkgName;
- public PackageIdentifier(String repository, PathFragment pkgName) throws TargetParsingException {
+ public PackageIdentifier(String repository, PathFragment pkgName) throws LabelSyntaxException {
this(RepositoryName.create(repository), pkgName);
}
@@ -254,13 +251,15 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
this.pkgName = Canonicalizer.fragments().intern(pkgName.normalize());
}
- public static PackageIdentifier parse(String input) throws TargetParsingException {
+ public static PackageIdentifier parse(String input) throws LabelSyntaxException {
String repo;
String packageName;
int packageStartPos = input.indexOf("//");
- if (packageStartPos > 0) {
+ if (input.startsWith("@") && packageStartPos > 0) {
repo = input.substring(0, packageStartPos);
packageName = input.substring(packageStartPos + 2);
+ } else if (input.startsWith("@")) {
+ throw new LabelSyntaxException("invalid package name '" + input + "'");
} else if (packageStartPos == 0) {
repo = PackageIdentifier.DEFAULT_REPOSITORY;
packageName = input.substring(2);
@@ -271,12 +270,12 @@ public final class PackageIdentifier implements Comparable<PackageIdentifier>, S
String error = RepositoryName.validate(repo);
if (error != null) {
- throw new TargetParsingException(error);
+ throw new LabelSyntaxException(error);
}
error = LabelValidator.validatePackageName(packageName);
if (error != null) {
- throw new TargetParsingException(error);
+ throw new LabelSyntaxException(error);
}
return new PackageIdentifier(repo, new PathFragment(packageName));