Fixup shift_expression parsing.

This CL fixes the shift_expression to call the higher level math
expression instead of multiplicative_expression. This allows addition
in shift expressions along with multiplicative expressions.

Bug: tint:1633
Change-Id: I29414bfa540bff612110d5ea16c5c89222a5eb6b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99861
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair 2022-08-22 14:55:04 +00:00 committed by Dawn LUCI CQ
parent ee25586aec
commit e76521153f
2 changed files with 28 additions and 4 deletions

View File

@ -679,6 +679,9 @@ Maybe<const ast::Variable*> ParserImpl::global_constant_decl(AttributeList& attr
// variable_decl
// : VAR variable_qualifier? optionally_typed_ident
//
// Note, the `( LESS_THAN address_space ( COMMA access_mode )? GREATER_THAN ) is pulled out into
// a `variable_qualifier` helper.
Maybe<ParserImpl::VarDeclInfo> ParserImpl::variable_decl() {
Source source;
if (!match(Token::Type::kVar, &source)) {
@ -995,7 +998,7 @@ Expect<ast::Access> ParserImpl::expect_access_mode(std::string_view use) {
}
// variable_qualifier
// : LESS_THAN storage_class (COMMA access_mode)? GREATER_THAN
// : LESS_THAN address_spaces (COMMA access_mode)? GREATER_THAN
Maybe<ParserImpl::VariableQualifier> ParserImpl::variable_qualifier() {
if (!peek_is(Token::Type::kLessThan)) {
return Failure::kNoMatch;
@ -2814,11 +2817,11 @@ Maybe<const ast::Expression*> ParserImpl::maybe_shift_expression() {
}
// shift_expression.post.unary_expression
// : multiplicative_expression.post.unary_expression?
// : math_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
// Note, add the `math_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) {
@ -2845,7 +2848,7 @@ Expect<const ast::Expression*> ParserImpl::expect_shift_expression_post_unary_ex
return create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
}
return expect_multiplicative_expression_post_unary_expression(lhs);
return expect_math_expression_post_unary_expression(lhs);
}
// relational_expression

View File

@ -164,6 +164,27 @@ TEST_F(ParserImplTest, ShiftExpression_PostUnary_Parses_ShiftRight) {
ASSERT_TRUE(rel->rhs->As<ast::BoolLiteralExpression>()->value);
}
TEST_F(ParserImplTest, ShiftExpression_PostUnary_Parses_Additive) {
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::kAdd, 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_PostUnary_Parses_Multiplicative) {
auto p = parser("a * b");
auto lhs = p->unary_expression();