From 08d735e6a91d327e1ddddb3d1b955422ad09d885 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 22 Aug 2022 19:23:24 +0000 Subject: [PATCH] Add missing relational_expression.post.unary_expression checks. This CL adds the missing checks and tests for `==` and `!=` into the `relational_expression.post.unary_expression` grammar elements. Bug: tint:1633 Change-Id: Ib5fbb42994ea12af133ca531989421fddc2393f2 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99940 Reviewed-by: Ben Clayton Commit-Queue: Dan Sinclair --- src/tint/reader/wgsl/parser_impl.cc | 4 ++ .../parser_impl_relational_expression_test.cc | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index 13a8b64f48..99abd60a0e 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -3174,6 +3174,10 @@ Expect ParserImpl::expect_relational_expression_post_una op = ast::BinaryOp::kLessThanEqual; } else if (t.Is(Token::Type::kGreaterThanEqual)) { op = ast::BinaryOp::kGreaterThanEqual; + } else if (t.Is(Token::Type::kEqualEqual)) { + op = ast::BinaryOp::kEqual; + } else if (t.Is(Token::Type::kNotEqual)) { + op = ast::BinaryOp::kNotEqual; } auto& next = peek(); diff --git a/src/tint/reader/wgsl/parser_impl_relational_expression_test.cc b/src/tint/reader/wgsl/parser_impl_relational_expression_test.cc index 1c47f879b6..59cddc6bf5 100644 --- a/src/tint/reader/wgsl/parser_impl_relational_expression_test.cc +++ b/src/tint/reader/wgsl/parser_impl_relational_expression_test.cc @@ -244,6 +244,56 @@ TEST_F(ParserImplTest, RelationalExpression_PostUnary_Parses_GreaterThanEqual) { ASSERT_TRUE(rel->rhs->As()->value); } +TEST_F(ParserImplTest, RelationalExpression_PostUnary_Parses_Equal) { + auto p = parser("a == true"); + auto lhs = p->unary_expression(); + auto e = p->expect_relational_expression_post_unary_expression(lhs.value); + EXPECT_FALSE(e.errored); + EXPECT_FALSE(p->has_error()) << p->error(); + ASSERT_NE(e.value, nullptr); + + EXPECT_EQ(e->source.range.begin.line, 1u); + EXPECT_EQ(e->source.range.begin.column, 3u); + EXPECT_EQ(e->source.range.end.line, 1u); + EXPECT_EQ(e->source.range.end.column, 5u); + + ASSERT_TRUE(e->Is()); + auto* rel = e->As(); + EXPECT_EQ(ast::BinaryOp::kEqual, rel->op); + + ASSERT_TRUE(rel->lhs->Is()); + auto* ident = rel->lhs->As(); + EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); + + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); +} + +TEST_F(ParserImplTest, RelationalExpression_PostUnary_Parses_NotEqual) { + auto p = parser("a != true"); + auto lhs = p->unary_expression(); + auto e = p->expect_relational_expression_post_unary_expression(lhs.value); + EXPECT_FALSE(e.errored); + EXPECT_FALSE(p->has_error()) << p->error(); + ASSERT_NE(e.value, nullptr); + + EXPECT_EQ(e->source.range.begin.line, 1u); + EXPECT_EQ(e->source.range.begin.column, 3u); + EXPECT_EQ(e->source.range.end.line, 1u); + EXPECT_EQ(e->source.range.end.column, 5u); + + ASSERT_TRUE(e->Is()); + auto* rel = e->As(); + EXPECT_EQ(ast::BinaryOp::kNotEqual, rel->op); + + ASSERT_TRUE(rel->lhs->Is()); + auto* ident = rel->lhs->As(); + EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a")); + + ASSERT_TRUE(rel->rhs->Is()); + ASSERT_TRUE(rel->rhs->As()->value); +} + TEST_F(ParserImplTest, RelationalExpression_PostUnary_InvalidRHS) { auto p = parser("true < if (a) {}"); auto lhs = p->unary_expression();