Fixed wrong error message when using class definitions in Skylark / BUILD files.
--
MOS_MIGRATED_REVID=105585492
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 af53ef1..36c6b95 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
@@ -129,6 +129,14 @@
TokenKind.RPAREN,
TokenKind.SLASH);
+ /**
+ * Keywords that are forbidden in both Skylark and BUILD parsing modes.
+ *
+ * <p>(Mapping: token -> human-readable string description)
+ */
+ private static final ImmutableMap<TokenKind, String> ILLEGAL_BLOCK_KEYWORDS =
+ ImmutableMap.of(TokenKind.CLASS, "Class definition", TokenKind.TRY, "Try statement");
+
private Token token; // current lookahead token
private Token pushedToken = null; // used to implement LL(2)
@@ -1433,18 +1441,19 @@
return setLocation(new ReturnStatement(expression), start, expression);
}
- // block ::= ('if' | 'for' | 'class') expr ':' suite
+ // block ::= ('if' | 'for' | 'class' | 'try' | 'def') expr ':' suite
private void skipBlock() {
int start = token.left;
Token blockToken = token;
syncTo(EnumSet.of(TokenKind.COLON, TokenKind.EOF)); // skip over expression or name
if (parsingMode != PYTHON) {
+ String msg =
+ ILLEGAL_BLOCK_KEYWORDS.containsKey(blockToken.kind)
+ ? String.format("%ss are not supported.", ILLEGAL_BLOCK_KEYWORDS.get(blockToken.kind))
+ : "This is not supported in BUILD files. Move the block to a .bzl file and load it";
reportError(
lexer.createLocation(start, token.right),
- "syntax error at '"
- + blockToken
- + "': This is not supported in BUILD files. "
- + "Move the block to a .bzl file and load it");
+ String.format("syntax error at '%s': %s", blockToken, msg));
}
expect(TokenKind.COLON);
skipSuite();
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
index 95b87aa..d1ac350 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
@@ -1253,4 +1253,40 @@
" else: return a");
assertContainsEvent("syntax error at 'else'");
}
+
+ @Test
+ public void testTryStatementInBuild() throws Exception {
+ setFailFast(false);
+ parseFile("try: pass");
+ assertContainsEvent("syntax error at 'try': Try statements are not supported.");
+ }
+
+ @Test
+ public void testTryStatementInSkylark() throws Exception {
+ setFailFast(false);
+ parseFileForSkylark("try: pass");
+ assertContainsEvent("syntax error at 'try': Try statements are not supported.");
+ }
+
+ @Test
+ public void testClassDefinitionInBuild() throws Exception {
+ setFailFast(false);
+ parseFile("class test(object): pass");
+ assertContainsEvent("syntax error at 'class': Class definitions are not supported.");
+ }
+
+ @Test
+ public void testClassDefinitionInSkylark() throws Exception {
+ setFailFast(false);
+ parseFileForSkylark("class test(object): pass");
+ assertContainsEvent("syntax error at 'class': Class definitions are not supported.");
+ }
+
+ @Test
+ public void testDefInBuild() throws Exception {
+ setFailFast(false);
+ parseFile("def func(): pass");
+ assertContainsEvent("syntax error at 'def': This is not supported in BUILD files. "
+ + "Move the block to a .bzl file and load it");
+ }
}