Add new shift expression grammar rule.
This CL adds the `shift_expression.post.unary_expression` grammar rule into the WGSL parser. Bug: tint:1633 Change-Id: I833ddb05399afe8c792bd0b1abf3eda7f1d114e7 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99760 Commit-Queue: Dan Sinclair <dsinclair@chromium.org> Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
9d27ab7043
commit
97e0c71eb6
|
@ -2800,6 +2800,41 @@ Maybe<const ast::Expression*> ParserImpl::element_count_expression() {
|
|||
return math.value;
|
||||
}
|
||||
|
||||
// shift_expression.post.unary_expression
|
||||
// : multiplicative_expression.post.unary_expression?
|
||||
// | SHIFT_LEFT unary_expression
|
||||
// | SHIFT_RIGHT unary_expression
|
||||
//
|
||||
// Note, add the `multiplicative_expression.post.unary_expression` is added here to make
|
||||
// implementation simpler.
|
||||
Expect<const ast::Expression*> ParserImpl::expect_shift_expression_post_unary_expression(
|
||||
const ast::Expression* lhs) {
|
||||
auto& t = peek();
|
||||
if (match(Token::Type::kShiftLeft) || match(Token::Type::kShiftRight)) {
|
||||
std::string name;
|
||||
ast::BinaryOp op = ast::BinaryOp::kNone;
|
||||
if (t.Is(Token::Type::kShiftLeft)) {
|
||||
op = ast::BinaryOp::kShiftLeft;
|
||||
name = "<<";
|
||||
} else if (t.Is(Token::Type::kShiftRight)) {
|
||||
op = ast::BinaryOp::kShiftRight;
|
||||
name = ">>";
|
||||
}
|
||||
|
||||
auto rhs = unary_expression();
|
||||
if (rhs.errored) {
|
||||
return Failure::kErrored;
|
||||
}
|
||||
if (!rhs.matched) {
|
||||
return add_error(t,
|
||||
std::string("unable to parse right side of ") + name + " expression");
|
||||
}
|
||||
return create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
|
||||
}
|
||||
|
||||
return expect_multiplicative_expression_post_unary_expression(lhs);
|
||||
}
|
||||
|
||||
// singular_expression
|
||||
// : primary_expression postfix_expr
|
||||
Maybe<const ast::Expression*> ParserImpl::singular_expression() {
|
||||
|
|
|
@ -667,6 +667,11 @@ class ParserImpl {
|
|||
/// Parses an `element_count_expression` grammar element
|
||||
/// @returns the parsed expression or nullptr
|
||||
Maybe<const ast::Expression*> element_count_expression();
|
||||
/// Parses a `shift_expression.post.unary_expression` grammar element
|
||||
/// @param lhs the left side of the expression
|
||||
/// @returns the parsed expression or `lhs` if no match
|
||||
Expect<const ast::Expression*> expect_shift_expression_post_unary_expression(
|
||||
const ast::Expression* lhs);
|
||||
/// Parse the `additive_operator` grammar element
|
||||
/// @returns the parsed operator if successful
|
||||
Maybe<ast::BinaryOp> additive_operator();
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
namespace tint::reader::wgsl {
|
||||
namespace {
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftLeft) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_Parses_ShiftLeft) {
|
||||
auto p = parser("a << true");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_TRUE(e.matched);
|
||||
|
@ -42,7 +42,7 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftLeft) {
|
|||
ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_Parses_ShiftRight) {
|
||||
auto p = parser("a >> true");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_TRUE(e.matched);
|
||||
|
@ -67,7 +67,7 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) {
|
|||
ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceLeft) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidSpaceLeft) {
|
||||
auto p = parser("a < < true");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_TRUE(e.matched);
|
||||
|
@ -76,7 +76,7 @@ TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceLeft) {
|
|||
EXPECT_FALSE(e.value->Is<ast::BinaryExpression>());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceRight) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidSpaceRight) {
|
||||
auto p = parser("a > > true");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_TRUE(e.matched);
|
||||
|
@ -85,7 +85,7 @@ TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceRight) {
|
|||
EXPECT_FALSE(e.value->Is<ast::BinaryExpression>());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidLHS) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidLHS) {
|
||||
auto p = parser("if (a) {} << true");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_FALSE(e.matched);
|
||||
|
@ -94,7 +94,7 @@ TEST_F(ParserImplTest, ShiftExpression_InvalidLHS) {
|
|||
EXPECT_EQ(e.value, nullptr);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidRHS) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidRHS) {
|
||||
auto p = parser("true << if (a) {}");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_FALSE(e.matched);
|
||||
|
@ -104,7 +104,7 @@ TEST_F(ParserImplTest, ShiftExpression_InvalidRHS) {
|
|||
EXPECT_EQ(p->error(), "1:9: unable to parse right side of << expression");
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_NoOr_ReturnsLHS) {
|
||||
TEST_F(ParserImplTest, ShiftExpression_Orig_NoOr_ReturnsLHS) {
|
||||
auto p = parser("a true");
|
||||
auto e = p->shift_expression();
|
||||
EXPECT_TRUE(e.matched);
|
||||
|
@ -114,5 +114,114 @@ TEST_F(ParserImplTest, ShiftExpression_NoOr_ReturnsLHS) {
|
|||
ASSERT_TRUE(e->Is<ast::IdentifierExpression>());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftLeft) {
|
||||
auto p = parser("a << true");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_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<ast::BinaryExpression>());
|
||||
auto* rel = e->As<ast::BinaryExpression>();
|
||||
EXPECT_EQ(ast::BinaryOp::kShiftLeft, rel->op);
|
||||
|
||||
ASSERT_TRUE(rel->lhs->Is<ast::IdentifierExpression>());
|
||||
auto* ident = rel->lhs->As<ast::IdentifierExpression>();
|
||||
EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a"));
|
||||
|
||||
ASSERT_TRUE(rel->rhs->Is<ast::BoolLiteralExpression>());
|
||||
ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) {
|
||||
auto p = parser("a >> true");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_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<ast::BinaryExpression>());
|
||||
auto* rel = e->As<ast::BinaryExpression>();
|
||||
EXPECT_EQ(ast::BinaryOp::kShiftRight, rel->op);
|
||||
|
||||
ASSERT_TRUE(rel->lhs->Is<ast::IdentifierExpression>());
|
||||
auto* ident = rel->lhs->As<ast::IdentifierExpression>();
|
||||
EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a"));
|
||||
|
||||
ASSERT_TRUE(rel->rhs->Is<ast::BoolLiteralExpression>());
|
||||
ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_Parses_Multiplicative) {
|
||||
auto p = parser("a * b");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_expression_post_unary_expression(lhs.value);
|
||||
EXPECT_FALSE(e.errored);
|
||||
EXPECT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(e.value, nullptr);
|
||||
|
||||
ASSERT_TRUE(e->Is<ast::BinaryExpression>());
|
||||
auto* rel = e->As<ast::BinaryExpression>();
|
||||
EXPECT_EQ(ast::BinaryOp::kMultiply, rel->op);
|
||||
|
||||
ASSERT_TRUE(rel->lhs->Is<ast::IdentifierExpression>());
|
||||
auto* ident = rel->lhs->As<ast::IdentifierExpression>();
|
||||
EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("a"));
|
||||
|
||||
ASSERT_TRUE(rel->rhs->Is<ast::IdentifierExpression>());
|
||||
ident = rel->rhs->As<ast::IdentifierExpression>();
|
||||
EXPECT_EQ(ident->symbol, p->builder().Symbols().Get("b"));
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceLeft) {
|
||||
auto p = parser("a < < true");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_expression_post_unary_expression(lhs.value);
|
||||
EXPECT_FALSE(e.errored);
|
||||
ASSERT_NE(e.value, nullptr);
|
||||
EXPECT_FALSE(e.value->Is<ast::BinaryExpression>());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidSpaceRight) {
|
||||
auto p = parser("a > > true");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_expression_post_unary_expression(lhs.value);
|
||||
EXPECT_FALSE(e.errored);
|
||||
ASSERT_NE(e.value, nullptr);
|
||||
EXPECT_FALSE(e.value->Is<ast::BinaryExpression>());
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_InvalidRHS) {
|
||||
auto p = parser("a << if (a) {}");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_expression_post_unary_expression(lhs.value);
|
||||
EXPECT_TRUE(e.errored);
|
||||
EXPECT_TRUE(p->has_error());
|
||||
EXPECT_EQ(e.value, nullptr);
|
||||
EXPECT_EQ(p->error(), "1:3: unable to parse right side of << expression");
|
||||
}
|
||||
|
||||
TEST_F(ParserImplTest, ShiftExpression_NoOr_ReturnsLHS) {
|
||||
auto p = parser("a true");
|
||||
auto lhs = p->unary_expression();
|
||||
auto e = p->expect_shift_expression_post_unary_expression(lhs.value);
|
||||
EXPECT_FALSE(e.errored);
|
||||
EXPECT_FALSE(p->has_error()) << p->error();
|
||||
ASSERT_NE(e.value, nullptr);
|
||||
ASSERT_EQ(lhs.value, e.value);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace tint::reader::wgsl
|
||||
|
|
Loading…
Reference in New Issue