LoadStatement: Keep the location of the path argument.
--
MOS_MIGRATED_REVID=102743954
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/LoadStatement.java b/src/main/java/com/google/devtools/build/lib/syntax/LoadStatement.java
index 8b9547a..b5e6650 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/LoadStatement.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/LoadStatement.java
@@ -30,7 +30,7 @@
private final ImmutableMap<Identifier, String> symbols;
private final ImmutableList<Identifier> cachedSymbols; // to save time
private final PathFragment importPath;
- private final String pathString;
+ private final StringLiteral pathString;
/**
* Constructs an import statement.
@@ -40,10 +40,10 @@
* If aliasing is used, the value differs from it's key's symbol#getName().
* Otherwise, both values are identical.
*/
- LoadStatement(String path, Map<Identifier, String> symbols) {
+ LoadStatement(StringLiteral path, Map<Identifier, String> symbols) {
this.symbols = ImmutableMap.copyOf(symbols);
this.cachedSymbols = ImmutableList.copyOf(symbols.keySet());
- this.importPath = new PathFragment(path + ".bzl");
+ this.importPath = new PathFragment(path.getValue() + ".bzl");
this.pathString = path;
}
@@ -87,7 +87,7 @@
@Override
void validate(ValidationEnvironment env) throws EvalException {
validatePath();
-
+
if (!importPath.isAbsolute() && importPath.segmentCount() > 1) {
throw new EvalException(getLocation(), String.format(PATH_ERROR_MSG, importPath));
}
@@ -96,6 +96,10 @@
}
}
+ public StringLiteral getPath() {
+ return pathString;
+ }
+
/**
* Throws an exception if the path argument to load() starts with more than one forward
* slash ('/')
@@ -103,9 +107,9 @@
public void validatePath() throws EvalException {
String error = null;
- if (pathString.isEmpty()) {
+ if (pathString.getValue().isEmpty()) {
error = "Path argument to load() must not be empty";
- } else if (pathString.startsWith("//")) {
+ } else if (pathString.getValue().startsWith("//")) {
error =
"First argument of load() is a path, not a label. "
+ "It should start with a single slash if it is an absolute path.";
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Parser.java b/src/main/java/com/google/devtools/build/lib/syntax/Parser.java
index 9efd28e..7d2311c 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Parser.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Parser.java
@@ -670,6 +670,24 @@
}
}
+ /**
+ * Parse a String literal value, e.g. "str".
+ */
+ private StringLiteral parseStringLiteral() {
+ Preconditions.checkState(token.kind == TokenKind.STRING);
+ int end = token.right;
+ char quoteChar = lexer.charAt(token.left);
+ StringLiteral literal =
+ setLocation(new StringLiteral((String) token.value, quoteChar), token.left, end);
+
+ nextToken();
+ if (token.kind == TokenKind.STRING) {
+ reportError(lexer.createLocation(end, token.left),
+ "Implicit string concatenation is forbidden, use the + operator");
+ }
+ return literal;
+ }
+
// primary ::= INTEGER
// | STRING
// | STRING '.' IDENTIFIER funcall_suffix
@@ -691,17 +709,7 @@
return literal;
}
case STRING: {
- String value = (String) token.value;
- int end = token.right;
- char quoteChar = lexer.charAt(start);
- nextToken();
- if (token.kind == TokenKind.STRING) {
- reportError(lexer.createLocation(end, token.left),
- "Implicit string concatenation is forbidden, use the + operator");
- }
- StringLiteral literal = new StringLiteral(value, quoteChar);
- setLocation(literal, start, end);
- return literal;
+ return parseStringLiteral();
}
case IDENTIFIER: {
Identifier ident = parseIdent();
@@ -1103,10 +1111,7 @@
return;
}
- Token pathToken = token;
- String path = (String) token.value;
-
- nextToken();
+ StringLiteral path = parseStringLiteral();
expect(TokenKind.COMMA);
Map<Identifier, String> symbols = new HashMap<>();
@@ -1130,7 +1135,7 @@
try {
stmt.validatePath();
} catch (EvalException e) {
- syntaxError(pathToken, e.getMessage());
+ reportError(path.getLocation(), e.getMessage());
}
list.add(setLocation(stmt, start, token.left));