Przeglądaj źródła

Fix handling of closing scopes in FindNextof (#977)

Jon Meow 4 lat temu
rodzic
commit
97ce1f5449

+ 2 - 2
toolchain/lexer/token_kind.h

@@ -64,12 +64,12 @@ class TokenKind {
   // The token kind must be an opening symbol.
   [[nodiscard]] auto GetClosingSymbol() const -> TokenKind;
 
-  // Test whether this kind of token is an closing symbol for a group.
+  // Test whether this kind of token is a closing symbol for a group.
   [[nodiscard]] auto IsClosingSymbol() const -> bool;
 
   // Returns the associated opening symbol for a closing symbol.
   //
-  // The token kind must be an closing symbol.
+  // The token kind must be a closing symbol.
   [[nodiscard]] auto GetOpeningSymbol() const -> TokenKind;
 
   // Test whether this kind of token is a keyword.

BIN
toolchain/parser/fuzzer_corpus/386740becc4c02670666928a4699c330b2fe1152


BIN
toolchain/parser/fuzzer_corpus/937ffb0f00790f35ae9b578df0bcfa40b06fd2e8


BIN
toolchain/parser/fuzzer_corpus/eddd593ed1b3ecde2f2b4ddf2ba4a4fbb83a6028


+ 16 - 0
toolchain/parser/parse_tree_test.cpp

@@ -1159,5 +1159,21 @@ TEST_F(ParseTreeTest, PrintingAsYAML) {
                              {"text", ""}}}));
 }
 
+TEST_F(ParseTreeTest, ParenMatchRegression) {
+  // A regression test that the search for the closing `)` doesn't end early on
+  // the closing `}` when it skips over the nested scope.
+  TokenizedBuffer tokens = GetTokenizedBuffer("var = (foo {})");
+  ParseTree tree = ParseTree::Parse(tokens, consumer);
+  EXPECT_TRUE(tree.HasErrors());
+  EXPECT_THAT(
+      tree, MatchParseTreeNodes(
+                {MatchVariableDeclaration(
+                     HasError, MatchVariableInitializer(
+                                   "=", MatchParenExpression(
+                                            HasError, MatchNameReference("foo"),
+                                            MatchParenExpressionEnd()))),
+                 MatchFileEnd()}));
+}
+
 }  // namespace
 }  // namespace Carbon

+ 2 - 0
toolchain/parser/parser_impl.cpp

@@ -347,6 +347,8 @@ auto ParseTree::Parser::FindNextOf(
     } else if (kind.IsOpeningSymbol()) {
       new_position =
           TokenizedBuffer::TokenIterator(tokens_.GetMatchedClosingToken(token));
+      // Advance past the closing token.
+      ++new_position;
     } else {
       ++new_position;
     }