From ae23bd554881a09a2f1ee89611ce90a9e687412f Mon Sep 17 00:00:00 2001 From: James Price Date: Tue, 13 Jul 2021 13:34:15 +0000 Subject: [PATCH] reader/wgsl: Reserve ++ and -- tokens Fixed: tint:971 Change-Id: I70732db7b22f7785866066cd7966eec630078de0 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57761 Auto-Submit: James Price Kokoro: Kokoro Reviewed-by: Ben Clayton Commit-Queue: James Price --- src/reader/wgsl/lexer.cc | 8 +++++++ src/reader/wgsl/lexer_test.cc | 2 ++ src/reader/wgsl/parser_impl.cc | 16 +++++++++++++ .../parser_impl_singular_expression_test.cc | 24 +++++++++++++++++++ .../wgsl/parser_impl_unary_expression_test.cc | 24 +++++++++++++++++++ src/reader/wgsl/token.cc | 4 ++++ src/reader/wgsl/token.h | 4 ++++ 7 files changed, 82 insertions(+) diff --git a/src/reader/wgsl/lexer.cc b/src/reader/wgsl/lexer.cc index 8c92c14dcc..dffed2e0ff 100644 --- a/src/reader/wgsl/lexer.cc +++ b/src/reader/wgsl/lexer.cc @@ -423,6 +423,10 @@ Token Lexer::try_punctuation() { type = Token::Type::kArrow; pos_ += 2; location_.column += 2; + } else if (matches(pos_, "--")) { + type = Token::Type::kMinusMinus; + pos_ += 2; + location_.column += 2; } else if (matches(pos_, "-")) { type = Token::Type::kMinus; pos_ += 1; @@ -431,6 +435,10 @@ Token Lexer::try_punctuation() { type = Token::Type::kPeriod; pos_ += 1; location_.column += 1; + } else if (matches(pos_, "++")) { + type = Token::Type::kPlusPlus; + pos_ += 2; + location_.column += 2; } else if (matches(pos_, "+")) { type = Token::Type::kPlus; pos_ += 1; diff --git a/src/reader/wgsl/lexer_test.cc b/src/reader/wgsl/lexer_test.cc index c7374f503f..52a6f1bd09 100644 --- a/src/reader/wgsl/lexer_test.cc +++ b/src/reader/wgsl/lexer_test.cc @@ -387,8 +387,10 @@ INSTANTIATE_TEST_SUITE_P( TokenData{"%", Token::Type::kMod}, TokenData{"!=", Token::Type::kNotEqual}, TokenData{"-", Token::Type::kMinus}, + TokenData{"--", Token::Type::kMinusMinus}, TokenData{".", Token::Type::kPeriod}, TokenData{"+", Token::Type::kPlus}, + TokenData{"++", Token::Type::kPlusPlus}, TokenData{"|", Token::Type::kOr}, TokenData{"||", Token::Type::kOrOr}, TokenData{"(", Token::Type::kParenLeft}, diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 7069756d01..1c8bfda35b 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -2223,6 +2223,15 @@ Maybe ParserImpl::primary_expression() { Maybe ParserImpl::postfix_expression( ast::Expression* prefix) { Source source; + + if (match(Token::Type::kPlusPlus, &source) || + match(Token::Type::kMinusMinus, &source)) { + add_error(source, + "postfix increment and decrement operators are reserved for a " + "future WGSL version"); + return Failure::kErrored; + } + if (match(Token::Type::kBracketLeft, &source)) { return sync(Token::Type::kBracketRight, [&]() -> Maybe { auto param = logical_or_expression(); @@ -2299,6 +2308,13 @@ Expect ParserImpl::expect_argument_expression_list( Maybe ParserImpl::unary_expression() { auto t = peek(); + if (match(Token::Type::kPlusPlus) || match(Token::Type::kMinusMinus)) { + add_error(t.source(), + "prefix increment and decrement operators are reserved for a " + "future WGSL version"); + return Failure::kErrored; + } + ast::UnaryOp op; if (match(Token::Type::kMinus)) { op = ast::UnaryOp::kNegation; diff --git a/src/reader/wgsl/parser_impl_singular_expression_test.cc b/src/reader/wgsl/parser_impl_singular_expression_test.cc index ee73c87924..bcac8abe8a 100644 --- a/src/reader/wgsl/parser_impl_singular_expression_test.cc +++ b/src/reader/wgsl/parser_impl_singular_expression_test.cc @@ -236,6 +236,30 @@ TEST_F(ParserImplTest, SingularExpression_Array_NestedArrayAccessor) { EXPECT_EQ(index_expr->symbol(), p->builder().Symbols().Get("c")); } +TEST_F(ParserImplTest, SingularExpression_PostfixPlusPlus) { + auto p = parser("a++"); + auto e = p->singular_expression(); + EXPECT_FALSE(e.matched); + EXPECT_TRUE(e.errored); + EXPECT_EQ(e.value, nullptr); + EXPECT_TRUE(p->has_error()); + EXPECT_EQ(p->error(), + "1:2: postfix increment and decrement operators are reserved for a " + "future WGSL version"); +} + +TEST_F(ParserImplTest, SingularExpression_PostfixMinusMinus) { + auto p = parser("a--"); + auto e = p->singular_expression(); + EXPECT_FALSE(e.matched); + EXPECT_TRUE(e.errored); + EXPECT_EQ(e.value, nullptr); + EXPECT_TRUE(p->has_error()); + EXPECT_EQ(p->error(), + "1:2: postfix increment and decrement operators are reserved for a " + "future WGSL version"); +} + } // namespace } // namespace wgsl } // namespace reader diff --git a/src/reader/wgsl/parser_impl_unary_expression_test.cc b/src/reader/wgsl/parser_impl_unary_expression_test.cc index 5bddbaf037..0724c55f27 100644 --- a/src/reader/wgsl/parser_impl_unary_expression_test.cc +++ b/src/reader/wgsl/parser_impl_unary_expression_test.cc @@ -177,6 +177,30 @@ TEST_F(ParserImplTest, UnaryExpression_Tilde) { EXPECT_EQ(init->literal()->As()->value(), 1); } +TEST_F(ParserImplTest, UnaryExpression_PrefixPlusPlus) { + auto p = parser("++a"); + auto e = p->unary_expression(); + EXPECT_FALSE(e.matched); + EXPECT_TRUE(e.errored); + EXPECT_EQ(e.value, nullptr); + EXPECT_TRUE(p->has_error()); + EXPECT_EQ(p->error(), + "1:1: prefix increment and decrement operators are reserved for a " + "future WGSL version"); +} + +TEST_F(ParserImplTest, UnaryExpression_PrefixMinusMinus) { + auto p = parser("--a"); + auto e = p->unary_expression(); + EXPECT_FALSE(e.matched); + EXPECT_TRUE(e.errored); + EXPECT_EQ(e.value, nullptr); + EXPECT_TRUE(p->has_error()); + EXPECT_EQ(p->error(), + "1:1: prefix increment and decrement operators are reserved for a " + "future WGSL version"); +} + } // namespace } // namespace wgsl } // namespace reader diff --git a/src/reader/wgsl/token.cc b/src/reader/wgsl/token.cc index 7616a68e0d..9b3e0660a9 100644 --- a/src/reader/wgsl/token.cc +++ b/src/reader/wgsl/token.cc @@ -84,10 +84,14 @@ std::string Token::TypeToName(Type type) { return "!="; case Token::Type::kMinus: return "-"; + case Token::Type::kMinusMinus: + return "--"; case Token::Type::kPeriod: return "."; case Token::Type::kPlus: return "+"; + case Token::Type::kPlusPlus: + return "++"; case Token::Type::kOr: return "|"; case Token::Type::kOrOr: diff --git a/src/reader/wgsl/token.h b/src/reader/wgsl/token.h index e94f5a05b1..467e6ac457 100644 --- a/src/reader/wgsl/token.h +++ b/src/reader/wgsl/token.h @@ -90,12 +90,16 @@ class Token { kMod, /// A '-' kMinus, + /// A '--' + kMinusMinus, /// A '!=' kNotEqual, /// A '.' kPeriod, /// A '+' kPlus, + /// A '++' + kPlusPlus, /// A '|' kOr, /// A '||'