Remove the Dialect type from the Parser.
Let's use the same parser. Dialect differences are checked in a separate
validation pass.
RELNOTES: None.
PiperOrigin-RevId: 167280201
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java b/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java
index 2ba51d7..2f8b36b 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/BuildFileAST.java
@@ -13,10 +13,6 @@
// limitations under the License.
package com.google.devtools.build.lib.syntax;
-import static com.google.devtools.build.lib.syntax.Parser.Dialect.BUILD;
-import static com.google.devtools.build.lib.syntax.Parser.Dialect.SKYLARK;
-
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
@@ -124,8 +120,7 @@
* Collects all load statements. Returns a pair with a boolean saying if there were errors and the
* imports that could be resolved.
*/
- @VisibleForTesting
- static Pair<Boolean, ImmutableList<SkylarkImport>> fetchLoads(
+ private static Pair<Boolean, ImmutableList<SkylarkImport>> fetchLoads(
List<Statement> statements, EventHandler eventHandler) {
ImmutableList.Builder<SkylarkImport> imports = ImmutableList.builder();
boolean error = false;
@@ -272,13 +267,15 @@
public static BuildFileAST parseBuildFile(ParserInputSource input,
List<Statement> preludeStatements,
EventHandler eventHandler) {
- Parser.ParseResult result = Parser.parseFile(input, eventHandler, BUILD);
- return create(preludeStatements, result, /*contentHashCode=*/ null, eventHandler);
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler);
+ return create(preludeStatements, result, /*contentHashCode=*/ null, eventHandler)
+ .validateBuildFile(eventHandler);
}
public static BuildFileAST parseBuildFile(ParserInputSource input, EventHandler eventHandler) {
- Parser.ParseResult result = Parser.parseFile(input, eventHandler, BUILD);
- return create(ImmutableList.<Statement>of(), result, /*contentHashCode=*/ null, eventHandler);
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler);
+ return create(ImmutableList.<Statement>of(), result, /*contentHashCode=*/ null, eventHandler)
+ .validateBuildFile(eventHandler);
}
/**
@@ -295,14 +292,14 @@
public static BuildFileAST parseSkylarkFile(Path file, long fileSize, EventHandler eventHandler)
throws IOException {
ParserInputSource input = ParserInputSource.create(file, fileSize);
- Parser.ParseResult result = Parser.parseFile(input, eventHandler, SKYLARK);
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler);
return create(
ImmutableList.of(), result,
HashCode.fromBytes(file.getDigest()).toString(), eventHandler);
}
public static BuildFileAST parseSkylarkFile(ParserInputSource input, EventHandler eventHandler) {
- Parser.ParseResult result = Parser.parseFile(input, eventHandler, SKYLARK);
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler);
return create(ImmutableList.<Statement>of(), result, /*contentHashCode=*/ null, eventHandler);
}
@@ -315,7 +312,7 @@
*/
public static BuildFileAST parseSkylarkFileWithoutImports(
ParserInputSource input, EventHandler eventHandler) {
- ParseResult result = Parser.parseFile(input, eventHandler, SKYLARK);
+ ParseResult result = Parser.parseFile(input, eventHandler);
return new BuildFileAST(
ImmutableList.<Statement>builder()
.addAll(ImmutableList.<Statement>of())
@@ -341,20 +338,28 @@
return new BuildFileAST(statements, true, contentHashCode, getLocation(), comments, imports);
}
- private static BuildFileAST parseString(
- Parser.Dialect dialect, EventHandler eventHandler, String... content) {
+ /**
+ * Run static checks for a BUILD file.
+ *
+ * @return a new AST (or the same), with the containsErrors flag updated.
+ */
+ public BuildFileAST validateBuildFile(EventHandler eventHandler) {
+ boolean valid = ValidationEnvironment.checkBuildSyntax(statements, eventHandler);
+ if (valid || containsErrors) {
+ return this;
+ }
+ return new BuildFileAST(statements, true, contentHashCode, getLocation(), comments, imports);
+ }
+
+ public static BuildFileAST parseString(EventHandler eventHandler, String... content) {
String str = Joiner.on("\n").join(content);
ParserInputSource input = ParserInputSource.create(str, PathFragment.EMPTY_FRAGMENT);
- Parser.ParseResult result = Parser.parseFile(input, eventHandler, dialect);
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler);
return create(ImmutableList.of(), result, null, eventHandler);
}
public static BuildFileAST parseBuildString(EventHandler eventHandler, String... content) {
- return parseString(BUILD, eventHandler, content);
- }
-
- public static BuildFileAST parseSkylarkString(EventHandler eventHandler, String... content) {
- return parseString(SKYLARK, eventHandler, content);
+ return parseString(eventHandler, content).validateBuildFile(eventHandler);
}
/**
@@ -363,7 +368,7 @@
* @return true if the input file is syntactically valid
*/
public static boolean checkSyntax(ParserInputSource input, EventHandler eventHandler) {
- Parser.ParseResult result = Parser.parseFile(input, eventHandler, BUILD);
+ Parser.ParseResult result = Parser.parseFile(input, eventHandler);
return !result.containsErrors;
}
@@ -402,7 +407,7 @@
*/
public static BuildFileAST parseAndValidateSkylarkString(Environment env, String[] input)
throws EvalException {
- BuildFileAST ast = parseSkylarkString(env.getEventHandler(), input);
+ BuildFileAST ast = parseString(env.getEventHandler(), input);
ValidationEnvironment.validateAst(env, ast.getStatements());
return ast;
}
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 e948b84..6edf737 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
@@ -201,20 +201,14 @@
*
* @param input the input to parse
* @param eventHandler a reporter for parsing errors
- * @param dialect may restrict the parser to Build-language features
* @see BuildFileAST#parseBuildString
* @see BuildFileAST#parseSkylarkString
*/
- public static ParseResult parseFile(
- ParserInputSource input, EventHandler eventHandler, Dialect dialect) {
+ public static ParseResult parseFile(ParserInputSource input, EventHandler eventHandler) {
Lexer lexer = new Lexer(input, eventHandler);
Parser parser = new Parser(lexer, eventHandler);
List<Statement> statements = parser.parseFileInput();
boolean errors = parser.errorsCount > 0 || lexer.containsErrors();
- // TODO(laurentlb): Remove dialect argument.
- if (dialect == Dialect.BUILD) {
- errors |= !ValidationEnvironment.checkBuildSyntax(statements, eventHandler);
- }
return new ParseResult(
statements, parser.comments, locationFromStatements(lexer, statements), errors);
}