diff options
author | Laurent Le Brun <laurentlb@google.com> | 2016-10-07 16:31:03 +0000 |
---|---|---|
committer | John Cater <jcater@google.com> | 2016-10-07 17:47:15 +0000 |
commit | b566c7dbf940ba41dc996b1a86eb6ad46ad5a9da (patch) | |
tree | 13c9feea59bea2e346f8054ac02ef839a29b6343 | |
parent | 4f5e12d4978212380fc900b6a7b01e640b85a550 (diff) |
Remove support for "Python" parsing mode.
It was unused in Bazel.
--
MOS_MIGRATED_REVID=135483937
5 files changed, 21 insertions, 121 deletions
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 98ab05583e..497715adeb 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 @@ -253,12 +253,12 @@ public class BuildFileAST extends ASTNode { public static BuildFileAST parseBuildFile(ParserInputSource input, List<Statement> preludeStatements, EventHandler eventHandler) { - Parser.ParseResult result = Parser.parseFile(input, eventHandler, false); + Parser.ParseResult result = Parser.parseFile(input, eventHandler); return create(preludeStatements, result, /*contentHashCode=*/ null, eventHandler); } public static BuildFileAST parseBuildFile(ParserInputSource input, EventHandler eventHandler) { - Parser.ParseResult result = Parser.parseFile(input, eventHandler, false); + Parser.ParseResult result = Parser.parseFile(input, eventHandler); return create(ImmutableList.<Statement>of(), result, /*contentHashCode=*/ null, eventHandler); } @@ -322,7 +322,7 @@ public class BuildFileAST extends ASTNode { public static BuildFileAST parseBuildString(EventHandler eventHandler, String... content) { String str = Joiner.on("\n").join(content); ParserInputSource input = ParserInputSource.create(str, null); - Parser.ParseResult result = Parser.parseFile(input, eventHandler, false); + Parser.ParseResult result = Parser.parseFile(input, eventHandler); return create(ImmutableList.<Statement>of(), result, null, eventHandler); } @@ -339,9 +339,8 @@ public class BuildFileAST extends ASTNode { * * @return true if the input file is syntactically valid */ - public static boolean checkSyntax( - ParserInputSource input, EventHandler eventHandler, boolean parsePython) { - Parser.ParseResult result = Parser.parseFile(input, eventHandler, parsePython); + public static boolean checkSyntax(ParserInputSource input, EventHandler eventHandler) { + Parser.ParseResult result = Parser.parseFile(input, eventHandler); return !result.containsErrors; } diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java index 177c052c3f..a1dd6ea11a 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java @@ -934,7 +934,7 @@ public final class Environment implements Freezable { valid.validateAst(result.statements, eventHandler); statements = result.statements; } else { - statements = Parser.parseFile(input, eventHandler, /*parsePython=*/false).statements; + statements = Parser.parseFile(input, eventHandler).statements; } // Force the validation of imports BuildFileAST.fetchLoads(statements, eventHandler); diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java b/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java index 4c923d058a..d8b34be169 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java @@ -91,19 +91,16 @@ public final class Lexer { private boolean containsErrors; - private boolean parsePython; - /** - * Constructs a lexer which tokenizes the contents of the specified - * InputBuffer. Any errors during lexing are reported on "handler". + * Constructs a lexer which tokenizes the contents of the specified InputBuffer. Any errors during + * lexing are reported on "handler". */ - public Lexer(ParserInputSource input, EventHandler eventHandler, boolean parsePython, - LineNumberTable lineNumberTable) { + public Lexer( + ParserInputSource input, EventHandler eventHandler, LineNumberTable lineNumberTable) { this.buffer = input.getContent(); // Empirical measurements show roughly 1 token per 8 characters in buffer. this.tokens = Lists.newArrayListWithExpectedSize(buffer.length / 8); this.pos = 0; - this.parsePython = parsePython; this.eventHandler = eventHandler; this.locationInfo = new LocationInfo(input.getPath(), lineNumberTable); @@ -114,13 +111,7 @@ public final class Lexer { } public Lexer(ParserInputSource input, EventHandler eventHandler) { - this(input, eventHandler, /*parsePython=*/false, - LineNumberTable.create(input.getContent(), input.getPath())); - } - - public Lexer(ParserInputSource input, EventHandler eventHandler, boolean parsePython) { - this(input, eventHandler, parsePython, - LineNumberTable.create(input.getContent(), input.getPath())); + this(input, eventHandler, LineNumberTable.create(input.getContent(), input.getPath())); } /** @@ -822,12 +813,7 @@ public final class Lexer { } else if (Character.isJavaIdentifierStart(c) && c != '$') { addToken(identifierOrKeyword()); } else { - // Some characters in Python are not recognized in Blaze syntax (e.g. '!') - if (parsePython) { - addToken(new Token(TokenKind.ILLEGAL, pos - 1, pos, Character.toString(c))); - } else { - error("invalid character: '" + c + "'"); - } + error("invalid character: '" + c + "'"); } break; } // default 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 ffe9f02afb..73da09ba0b 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 @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.syntax; import static com.google.devtools.build.lib.syntax.Parser.ParsingMode.BUILD; -import static com.google.devtools.build.lib.syntax.Parser.ParsingMode.PYTHON; import static com.google.devtools.build.lib.syntax.Parser.ParsingMode.SKYLARK; import com.google.common.annotations.VisibleForTesting; @@ -82,8 +81,6 @@ public class Parser { BUILD, /** Used for parsing .bzl files */ SKYLARK, - /** Used for syntax checking, ignoring all Python blocks (e.g. def, class, try) */ - PYTHON, } private static final EnumSet<TokenKind> STATEMENT_TERMINATOR_SET = @@ -212,14 +209,12 @@ public class Parser { } /** - * Entry-point to parser that parses a build file with comments. All errors - * encountered during parsing are reported via "reporter". + * Entry-point to parser that parses a build file with comments. All errors encountered during + * parsing are reported via "reporter". */ - public static ParseResult parseFile( - ParserInputSource input, EventHandler eventHandler, boolean parsePython) { - Lexer lexer = new Lexer(input, eventHandler, parsePython); - ParsingMode parsingMode = parsePython ? PYTHON : BUILD; - Parser parser = new Parser(lexer, eventHandler, parsingMode); + public static ParseResult parseFile(ParserInputSource input, EventHandler eventHandler) { + Lexer lexer = new Lexer(input, eventHandler); + Parser parser = new Parser(lexer, eventHandler, BUILD); List<Statement> statements = parser.parseFileInput(); return new ParseResult(statements, parser.comments, locationFromStatements(lexer, statements), parser.errorsCount > 0 || lexer.containsErrors()); @@ -232,7 +227,7 @@ public class Parser { */ public static ParseResult parseFileForSkylark( ParserInputSource input, EventHandler eventHandler) { - Lexer lexer = new Lexer(input, eventHandler, false); + Lexer lexer = new Lexer(input, eventHandler); Parser parser = new Parser(lexer, eventHandler, SKYLARK); List<Statement> statements = parser.parseFileInput(); return new ParseResult( @@ -249,7 +244,7 @@ public class Parser { */ @VisibleForTesting public static Expression parseExpression(ParserInputSource input, EventHandler eventHandler) { - Lexer lexer = new Lexer(input, eventHandler, false); + Lexer lexer = new Lexer(input, eventHandler); Parser parser = new Parser(lexer, eventHandler, null); Expression result = parser.parseExpression(); while (parser.token.kind == TokenKind.NEWLINE) { @@ -345,7 +340,7 @@ public class Parser { TokenKind.TRY, TokenKind.WITH, TokenKind.WHILE, TokenKind.YIELD); private void checkForbiddenKeywords(Token token) { - if (parsingMode == PYTHON || !FORBIDDEN_KEYWORDS.contains(token.kind)) { + if (!FORBIDDEN_KEYWORDS.contains(token.kind)) { return; } String error; @@ -1435,7 +1430,7 @@ public class Parser { reportError( lexer.createLocation(blockToken.left, blockToken.right), "syntax error at 'else': not allowed here."); - } else if (parsingMode != PYTHON) { + } else { String msg = ILLEGAL_BLOCK_KEYWORDS.containsKey(blockToken.kind) ? String.format("%ss are not supported.", ILLEGAL_BLOCK_KEYWORDS.get(blockToken.kind)) 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 e4d8e3b0b0..36ecfe143c 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 @@ -20,7 +20,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.events.Location; @@ -58,14 +57,6 @@ public class ParserTest extends EvaluationTestCase { return parseFileWithComments(input).getStatements(); } - /** Parses a build code (not Skylark) with PythonProcessing enabled */ - private List<Statement> parseFileWithPython(String... input) { - return Parser.parseFile( - ParserInputSource.create(Joiner.on("\n").join(input), null), - getEventHandler(), - /*parsePython=*/true).statements; - } - /** Parses Skylark code */ private List<Statement> parseFileForSkylark(String... input) { return env.parseFile(input); @@ -902,64 +893,6 @@ public class ParserTest extends EvaluationTestCase { } @Test - public void testFunctionDefinitionIgnoredEvenWithUnsupportedKeyword() throws Exception { - // Parser skips over entire function definitions without reporting error, - // when parsePython is set to true. - List<Statement> stmts = parseFileWithPython( - "x = 1;", - "def foo(x, y, **z):", - " try:", - " x = 2", - " with: pass", - " return 2", - "x = 3"); - assertThat(stmts).hasSize(2); - } - - @Test - public void testFunctionDefinitionIgnored() throws Exception { - // Parser skips over entire function definitions without reporting error, - // when parsePython is set to true. - List<Statement> stmts = parseFileWithPython( - "x = 1;", - "def foo(x, y, **z):", - " # a comment", - " if true:", - " x = 2", - " foo(bar)", - " return z", - "x = 3"); - assertThat(stmts).hasSize(2); - - stmts = parseFileWithPython( - "x = 1;", - "def foo(x, y, **z): return x", - "x = 3"); - assertThat(stmts).hasSize(2); - } - - @Test - public void testMissingBlock() throws Exception { - setFailFast(false); - List<Statement> stmts = parseFileWithPython( - "x = 1;", - "def foo(x):", - "x = 2;\n"); - assertThat(stmts).hasSize(2); - assertContainsError("expected an indented block"); - } - - @Test - public void testInvalidDef() throws Exception { - setFailFast(false); - parseFileWithPython( - "x = 1;", - "def foo(x)", - "x = 2;\n"); - assertContainsError("syntax error at 'EOF'"); - } - - @Test public void testDefSingleLine() throws Exception { List<Statement> statements = parseFileForSkylark( "def foo(): x = 1; y = 2\n"); @@ -968,19 +901,6 @@ public class ParserTest extends EvaluationTestCase { } @Test - public void testSkipIfBlock() throws Exception { - // Skip over 'if' blocks, when parsePython is set - List<Statement> stmts = parseFileWithPython( - "x = 1;", - "if x == 1:", - " foo(x)", - "else:", - " bar(x)", - "x = 3;\n"); - assertThat(stmts).hasSize(2); - } - - @Test public void testPass() throws Exception { List<Statement> statements = parseFileForSkylark("pass\n"); assertThat(statements).isEmpty(); |