From 202362f47db4f81df9dcee8d97a4fef295cf4579 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 24 Aug 2022 18:05:26 +0000 Subject: [PATCH] Convert to new expression grammar This CL moves all the grammar rules to use the new `maybe_expression` rules. All of the old `expression` rules are removed. Bug: tint:1633 Change-Id: I29637a4c6fcd200650a2b57011b6539c66be942a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99920 Reviewed-by: Ben Clayton Commit-Queue: Dan Sinclair Kokoro: Kokoro --- src/tint/BUILD.gn | 6 - src/tint/CMakeLists.txt | 6 - src/tint/reader/wgsl/parser_impl.cc | 460 +----------------- src/tint/reader/wgsl/parser_impl.h | 83 ---- .../parser_impl_additive_expression_test.cc | 74 --- .../wgsl/parser_impl_and_expression_test.cc | 75 --- .../parser_impl_equality_expression_test.cc | 100 ---- ...arser_impl_exclusive_or_expression_test.cc | 75 --- ...arser_impl_inclusive_or_expression_test.cc | 75 --- ...parser_impl_logical_and_expression_test.cc | 75 --- .../parser_impl_logical_or_expression_test.cc | 75 --- ...ser_impl_multiplicative_expression_test.cc | 94 ---- .../parser_impl_relational_expression_test.cc | 187 +++---- .../wgsl/parser_impl_shift_expression_test.cc | 99 +--- .../wgsl/parser_impl_unary_expression_test.cc | 4 +- 15 files changed, 78 insertions(+), 1410 deletions(-) delete mode 100644 src/tint/reader/wgsl/parser_impl_and_expression_test.cc delete mode 100644 src/tint/reader/wgsl/parser_impl_equality_expression_test.cc delete mode 100644 src/tint/reader/wgsl/parser_impl_exclusive_or_expression_test.cc delete mode 100644 src/tint/reader/wgsl/parser_impl_inclusive_or_expression_test.cc delete mode 100644 src/tint/reader/wgsl/parser_impl_logical_and_expression_test.cc delete mode 100644 src/tint/reader/wgsl/parser_impl_logical_or_expression_test.cc diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 8283b45827..d140e1e1c5 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -1350,7 +1350,6 @@ if (tint_build_unittests) { sources = [ "reader/wgsl/lexer_test.cc", "reader/wgsl/parser_impl_additive_expression_test.cc", - "reader/wgsl/parser_impl_and_expression_test.cc", "reader/wgsl/parser_impl_argument_expression_list_test.cc", "reader/wgsl/parser_impl_assignment_stmt_test.cc", "reader/wgsl/parser_impl_bitwise_expression_test.cc", @@ -1367,10 +1366,8 @@ if (tint_build_unittests) { "reader/wgsl/parser_impl_depth_texture_test.cc", "reader/wgsl/parser_impl_element_count_expression_test.cc", "reader/wgsl/parser_impl_enable_directive_test.cc", - "reader/wgsl/parser_impl_equality_expression_test.cc", "reader/wgsl/parser_impl_error_msg_test.cc", "reader/wgsl/parser_impl_error_resync_test.cc", - "reader/wgsl/parser_impl_exclusive_or_expression_test.cc", "reader/wgsl/parser_impl_expression_test.cc", "reader/wgsl/parser_impl_external_texture_test.cc", "reader/wgsl/parser_impl_for_stmt_test.cc", @@ -1382,11 +1379,8 @@ if (tint_build_unittests) { "reader/wgsl/parser_impl_global_decl_test.cc", "reader/wgsl/parser_impl_global_variable_decl_test.cc", "reader/wgsl/parser_impl_if_stmt_test.cc", - "reader/wgsl/parser_impl_inclusive_or_expression_test.cc", "reader/wgsl/parser_impl_increment_decrement_stmt_test.cc", "reader/wgsl/parser_impl_lhs_expression_test.cc", - "reader/wgsl/parser_impl_logical_and_expression_test.cc", - "reader/wgsl/parser_impl_logical_or_expression_test.cc", "reader/wgsl/parser_impl_loop_stmt_test.cc", "reader/wgsl/parser_impl_math_expression_test.cc", "reader/wgsl/parser_impl_multiplicative_expression_test.cc", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index 6703d6525f..5d77470d57 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -945,7 +945,6 @@ if(TINT_BUILD_TESTS) reader/wgsl/lexer_test.cc reader/wgsl/parser_test.cc reader/wgsl/parser_impl_additive_expression_test.cc - reader/wgsl/parser_impl_and_expression_test.cc reader/wgsl/parser_impl_argument_expression_list_test.cc reader/wgsl/parser_impl_assignment_stmt_test.cc reader/wgsl/parser_impl_bitwise_expression_test.cc @@ -962,10 +961,8 @@ if(TINT_BUILD_TESTS) reader/wgsl/parser_impl_depth_texture_test.cc reader/wgsl/parser_impl_element_count_expression_test.cc reader/wgsl/parser_impl_enable_directive_test.cc - reader/wgsl/parser_impl_equality_expression_test.cc reader/wgsl/parser_impl_error_msg_test.cc reader/wgsl/parser_impl_error_resync_test.cc - reader/wgsl/parser_impl_exclusive_or_expression_test.cc reader/wgsl/parser_impl_expression_test.cc reader/wgsl/parser_impl_external_texture_test.cc reader/wgsl/parser_impl_for_stmt_test.cc @@ -977,11 +974,8 @@ if(TINT_BUILD_TESTS) reader/wgsl/parser_impl_global_decl_test.cc reader/wgsl/parser_impl_global_variable_decl_test.cc reader/wgsl/parser_impl_if_stmt_test.cc - reader/wgsl/parser_impl_inclusive_or_expression_test.cc reader/wgsl/parser_impl_increment_decrement_stmt_test.cc reader/wgsl/parser_impl_lhs_expression_test.cc - reader/wgsl/parser_impl_logical_and_expression_test.cc - reader/wgsl/parser_impl_logical_or_expression_test.cc reader/wgsl/parser_impl_loop_stmt_test.cc reader/wgsl/parser_impl_math_expression_test.cc reader/wgsl/parser_impl_multiplicative_expression_test.cc diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index c49580cf06..f0c6a18a23 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -579,7 +579,7 @@ Maybe ParserImpl::global_variable_decl(AttributeList& attr const ast::Expression* initializer = nullptr; if (match(Token::Type::kEqual)) { - auto expr = expression(); + auto expr = maybe_expression(); if (expr.errored) { return Failure::kErrored; } @@ -639,7 +639,7 @@ Maybe ParserImpl::global_constant_decl(AttributeList& attr const ast::Expression* initializer = nullptr; if (has_initializer) { - auto expr = expression(); + auto expr = maybe_expression(); if (expr.errored) { return Failure::kErrored; } @@ -1426,7 +1426,7 @@ Maybe ParserImpl::static_assert_statement() { return Failure::kNoMatch; } - auto condition = expression(); + auto condition = maybe_expression(); if (condition.errored) { return Failure::kErrored; } @@ -1691,7 +1691,7 @@ Expect ParserImpl::expect_compound_statement() { // : PAREN_LEFT expression PAREN_RIGHT Expect ParserImpl::expect_paren_expression() { return expect_paren_block("", [&]() -> Expect { - auto expr = expression(); + auto expr = maybe_expression(); if (expr.errored) { return Failure::kErrored; } @@ -1895,7 +1895,7 @@ Maybe ParserImpl::return_statement() { return create(source, nullptr); } - auto expr = expression(); + auto expr = maybe_expression(); if (expr.errored) { return Failure::kErrored; } @@ -2018,7 +2018,7 @@ Maybe ParserImpl::if_statement() { return Failure::kNoMatch; } - auto condition = expression(); + auto condition = maybe_expression(); if (condition.errored) { return Failure::kErrored; } @@ -2086,7 +2086,7 @@ Maybe ParserImpl::switch_statement() { return Failure::kNoMatch; } - auto condition = expression(); + auto condition = maybe_expression(); if (condition.errored) { return Failure::kErrored; } @@ -2319,7 +2319,7 @@ Expect> ParserImpl::expect_for_header() { return Failure::kErrored; } - auto condition = expression(); + auto condition = maybe_expression(); if (condition.errored) { return Failure::kErrored; } @@ -2367,7 +2367,7 @@ Maybe ParserImpl::while_statement() { return Failure::kNoMatch; } - auto condition = expression(); + auto condition = maybe_expression(); if (condition.errored) { return Failure::kErrored; } @@ -2601,7 +2601,7 @@ Maybe ParserImpl::postfix_expression(const ast::Expressi while (continue_parsing()) { if (match(Token::Type::kBracketLeft, &source)) { auto res = sync(Token::Type::kBracketRight, [&]() -> Maybe { - auto param = expression(); + auto param = maybe_expression(); if (param.errored) { return Failure::kErrored; } @@ -2649,7 +2649,7 @@ Expect ParserImpl::expect_argument_expression_list( return expect_paren_block(use, [&]() -> Expect { ExpressionList ret; while (continue_parsing()) { - auto arg = expression(); + auto arg = maybe_expression(); if (arg.errored) { return Failure::kErrored; } else if (!arg.matched) { @@ -2913,12 +2913,13 @@ Expect ParserImpl::expect_shift_expression_post_unary_ex name = ">>"; } + auto& rhs_start = peek(); auto rhs = unary_expression(); if (rhs.errored) { return Failure::kErrored; } if (!rhs.matched) { - return add_error(t, + return add_error(rhs_start, std::string("unable to parse right side of ") + name + " expression"); } return create(t.source(), op, lhs, rhs.value); @@ -3044,7 +3045,7 @@ Maybe ParserImpl::maybe_expression() { } next(); - auto rhs = relational_expression(); + auto rhs = maybe_relational_expression(); if (rhs.errored) { return Failure::kErrored; } @@ -3130,437 +3131,6 @@ Maybe ParserImpl::unary_expression() { return create(t.source(), op, expr.value); } -// multiplicative_expr -// : -// | STAR unary_expression multiplicative_expr -// | FORWARD_SLASH unary_expression multiplicative_expr -// | MODULO unary_expression multiplicative_expr -Expect ParserImpl::expect_multiplicative_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kStar)) { - op = ast::BinaryOp::kMultiply; - } else if (peek_is(Token::Type::kForwardSlash)) { - op = ast::BinaryOp::kDivide; - } else if (peek_is(Token::Type::kMod)) { - op = ast::BinaryOp::kModulo; - } else { - return lhs; - } - - auto& t = next(); - - auto rhs = unary_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of " + std::string(t.to_name()) + - " expression"); - } - - lhs = create(t.source(), op, lhs, rhs.value); - } - return Failure::kErrored; -} - -// multiplicative_expression -// : unary_expression multiplicative_expr -Maybe ParserImpl::multiplicative_expression() { - auto lhs = unary_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_multiplicative_expr(lhs.value); -} - -// additive_expr -// : -// | PLUS multiplicative_expression additive_expr -// | MINUS multiplicative_expression additive_expr -Expect ParserImpl::expect_additive_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kPlus)) { - op = ast::BinaryOp::kAdd; - } else if (peek_is(Token::Type::kMinus)) { - op = ast::BinaryOp::kSubtract; - } else { - return lhs; - } - - auto& t = next(); - - auto rhs = multiplicative_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of + expression"); - } - - lhs = create(t.source(), op, lhs, rhs.value); - } - return Failure::kErrored; -} - -// additive_expression -// : multiplicative_expression additive_expr -Maybe ParserImpl::additive_expression() { - auto lhs = multiplicative_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_additive_expr(lhs.value); -} - -// shift_expr -// : -// | SHIFT_LEFT additive_expression shift_expr -// | SHIFT_RIGHT additive_expression shift_expr -Expect ParserImpl::expect_shift_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - auto* name = ""; - ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kShiftLeft)) { - op = ast::BinaryOp::kShiftLeft; - name = "<<"; - } else if (peek_is(Token::Type::kShiftRight)) { - op = ast::BinaryOp::kShiftRight; - name = ">>"; - } else { - return lhs; - } - - auto& t = next(); - auto rhs = additive_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), - std::string("unable to parse right side of ") + name + " expression"); - } - - return lhs = create(t.source(), op, lhs, rhs.value); - } - return Failure::kErrored; -} - -// shift_expression -// : additive_expression shift_expr -Maybe ParserImpl::shift_expression() { - auto lhs = additive_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_shift_expr(lhs.value); -} - -// relational_expr -// : -// | LESS_THAN shift_expression relational_expr -// | GREATER_THAN shift_expression relational_expr -// | LESS_THAN_EQUAL shift_expression relational_expr -// | GREATER_THAN_EQUAL shift_expression relational_expr -Expect ParserImpl::expect_relational_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kLessThan)) { - op = ast::BinaryOp::kLessThan; - } else if (peek_is(Token::Type::kGreaterThan)) { - op = ast::BinaryOp::kGreaterThan; - } else if (peek_is(Token::Type::kLessThanEqual)) { - op = ast::BinaryOp::kLessThanEqual; - } else if (peek_is(Token::Type::kGreaterThanEqual)) { - op = ast::BinaryOp::kGreaterThanEqual; - } else { - return lhs; - } - - auto& t = next(); - - auto rhs = shift_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of " + std::string(t.to_name()) + - " expression"); - } - - lhs = create(t.source(), op, lhs, rhs.value); - } - return Failure::kErrored; -} - -// relational_expression -// : shift_expression relational_expr -Maybe ParserImpl::relational_expression() { - auto lhs = shift_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_relational_expr(lhs.value); -} - -// equality_expr -// : -// | EQUAL_EQUAL relational_expression equality_expr -// | NOT_EQUAL relational_expression equality_expr -Expect ParserImpl::expect_equality_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - ast::BinaryOp op = ast::BinaryOp::kNone; - if (peek_is(Token::Type::kEqualEqual)) { - op = ast::BinaryOp::kEqual; - } else if (peek_is(Token::Type::kNotEqual)) { - op = ast::BinaryOp::kNotEqual; - } else { - return lhs; - } - - auto& t = next(); - - auto rhs = relational_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of " + std::string(t.to_name()) + - " expression"); - } - - lhs = create(t.source(), op, lhs, rhs.value); - } - return Failure::kErrored; -} - -// equality_expression -// : relational_expression equality_expr -Maybe ParserImpl::equality_expression() { - auto lhs = relational_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_equality_expr(lhs.value); -} - -// and_expr -// : -// | AND equality_expression and_expr -Expect ParserImpl::expect_and_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - if (!peek_is(Token::Type::kAnd)) { - return lhs; - } - - auto& t = next(); - - auto rhs = equality_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of & expression"); - } - - lhs = create(t.source(), ast::BinaryOp::kAnd, lhs, rhs.value); - } - return Failure::kErrored; -} - -// and_expression -// : equality_expression and_expr -Maybe ParserImpl::and_expression() { - auto lhs = equality_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_and_expr(lhs.value); -} - -// exclusive_or_expr -// : -// | XOR and_expression exclusive_or_expr -Expect ParserImpl::expect_exclusive_or_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - Source source; - if (!match(Token::Type::kXor, &source)) { - return lhs; - } - - auto rhs = and_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of ^ expression"); - } - - lhs = create(source, ast::BinaryOp::kXor, lhs, rhs.value); - } - return Failure::kErrored; -} - -// exclusive_or_expression -// : and_expression exclusive_or_expr -Maybe ParserImpl::exclusive_or_expression() { - auto lhs = and_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_exclusive_or_expr(lhs.value); -} - -// inclusive_or_expr -// : -// | OR exclusive_or_expression inclusive_or_expr -Expect ParserImpl::expect_inclusive_or_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - Source source; - if (!match(Token::Type::kOr, &source)) { - return lhs; - } - - auto rhs = exclusive_or_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of | expression"); - } - - lhs = create(source, ast::BinaryOp::kOr, lhs, rhs.value); - } - return Failure::kErrored; -} - -// inclusive_or_expression -// : exclusive_or_expression inclusive_or_expr -Maybe ParserImpl::inclusive_or_expression() { - auto lhs = exclusive_or_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_inclusive_or_expr(lhs.value); -} - -// logical_and_expr -// : -// | AND_AND inclusive_or_expression logical_and_expr -Expect ParserImpl::expect_logical_and_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - if (!peek_is(Token::Type::kAndAnd)) { - return lhs; - } - - auto& t = next(); - - auto rhs = inclusive_or_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of && expression"); - } - - lhs = create(t.source(), ast::BinaryOp::kLogicalAnd, lhs, rhs.value); - } - return Failure::kErrored; -} - -// logical_and_expression -// : inclusive_or_expression logical_and_expr -Maybe ParserImpl::logical_and_expression() { - auto lhs = inclusive_or_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_logical_and_expr(lhs.value); -} - -// logical_or_expr -// : -// | OR_OR logical_and_expression logical_or_expr -Expect ParserImpl::expect_logical_or_expr(const ast::Expression* lhs) { - while (continue_parsing()) { - Source source; - if (!match(Token::Type::kOrOr, &source)) { - return lhs; - } - - auto rhs = logical_and_expression(); - if (rhs.errored) { - return Failure::kErrored; - } - if (!rhs.matched) { - return add_error(peek(), "unable to parse right side of || expression"); - } - - lhs = create(source, ast::BinaryOp::kLogicalOr, lhs, rhs.value); - } - return Failure::kErrored; -} - -// logical_or_expression -// : logical_and_expression logical_or_expr -Maybe ParserImpl::logical_or_expression() { - auto lhs = logical_and_expression(); - if (lhs.errored) { - return Failure::kErrored; - } - if (!lhs.matched) { - return Failure::kNoMatch; - } - - return expect_logical_or_expr(lhs.value); -} - -// expression: -// : relational_expression -// | short_circuit_or_expression or_or relational_expression -// | short_circuit_and_expression and_and relational_expression -// | bitwise_expression -Maybe ParserImpl::expression() { - return logical_or_expression(); -} - // compound_assignment_operator // : plus_equal // | minus_equal @@ -3735,7 +3305,7 @@ Maybe ParserImpl::variable_updating_statement() { } } - auto rhs = expression(); + auto rhs = maybe_expression(); if (rhs.errored) { return Failure::kErrored; } diff --git a/src/tint/reader/wgsl/parser_impl.h b/src/tint/reader/wgsl/parser_impl.h index 44525c07d5..c733d02a3f 100644 --- a/src/tint/reader/wgsl/parser_impl.h +++ b/src/tint/reader/wgsl/parser_impl.h @@ -621,49 +621,9 @@ class ParserImpl { /// Parses a `unary_expression` grammar element /// @returns the parsed expression or nullptr Maybe unary_expression(); - /// Parses the recursive part of the `multiplicative_expression`, erroring on - /// parse failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_multiplicative_expr(const ast::Expression* lhs); - /// Parses the `multiplicative_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe multiplicative_expression(); - /// Parses the recursive part of the `additive_expression`, erroring on parse - /// failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_additive_expr(const ast::Expression* lhs); - /// Parses the `additive_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe additive_expression(); - /// Parses the recursive part of the `shift_expression`, erroring on parse - /// failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_shift_expr(const ast::Expression* lhs); - /// Parses the `shift_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe shift_expression(); - /// Parses the recursive part of the `relational_expression`, erroring on - /// parse failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_relational_expr(const ast::Expression* lhs); /// Parses the `expression` grammar rule /// @returns the parsed expression or nullptr Maybe maybe_expression(); - /// Parses the `relational_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe relational_expression(); - /// Parses the recursive part of the `equality_expression`, erroring on parse - /// failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_equality_expr(const ast::Expression* lhs); - /// Parses the `equality_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe equality_expression(); /// Parses the `bitwise_expression.post.unary_expression` grammar element /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr @@ -709,49 +669,6 @@ class ParserImpl { /// Parse the `additive_operator` grammar element /// @returns the parsed operator if successful Maybe additive_operator(); - /// Parses the recursive part of the `and_expression`, erroring on parse - /// failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_and_expr(const ast::Expression* lhs); - /// Parses the `and_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe and_expression(); - /// Parses the recursive part of the `exclusive_or_expression`, erroring on - /// parse failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_exclusive_or_expr(const ast::Expression* lhs); - /// Parses the `exclusive_or_expression` grammar elememnt - /// @returns the parsed expression or nullptr - Maybe exclusive_or_expression(); - /// Parses the recursive part of the `inclusive_or_expression`, erroring on - /// parse failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_inclusive_or_expr(const ast::Expression* lhs); - /// Parses the `inclusive_or_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe inclusive_or_expression(); - /// Parses the recursive part of the `logical_and_expression`, erroring on - /// parse failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_logical_and_expr(const ast::Expression* lhs); - /// Parses a `logical_and_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe logical_and_expression(); - /// Parses the recursive part of the `logical_or_expression`, erroring on - /// parse failure. - /// @param lhs the left side of the expression - /// @returns the parsed expression or nullptr - Expect expect_logical_or_expr(const ast::Expression* lhs); - /// Parses a `logical_or_expression` grammar element - /// @returns the parsed expression or nullptr - Maybe logical_or_expression(); - /// Parses an `expression` grammar element - /// @returns the parsed expression or nullptr - Maybe expression(); /// Parses a `compound_assignment_operator` grammar element /// @returns the parsed compound assignment operator Maybe compound_assignment_operator(); diff --git a/src/tint/reader/wgsl/parser_impl_additive_expression_test.cc b/src/tint/reader/wgsl/parser_impl_additive_expression_test.cc index 3259e273b3..971c6953bb 100644 --- a/src/tint/reader/wgsl/parser_impl_additive_expression_test.cc +++ b/src/tint/reader/wgsl/parser_impl_additive_expression_test.cc @@ -17,80 +17,6 @@ namespace tint::reader::wgsl { namespace { -TEST_F(ParserImplTest, AdditiveExpression_Orig_Parses_Plus) { - auto p = parser("a + true"); - auto e = p->additive_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kAdd, 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, AdditiveExpression_Orig_Parses_Minus) { - auto p = parser("a - true"); - auto e = p->additive_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kSubtract, 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, AdditiveExpression_Orig_InvalidLHS) { - auto p = parser("if (a) {} + true"); - auto e = p->additive_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, AdditiveExpression_Orig_InvalidRHS) { - auto p = parser("true + if (a) {}"); - auto e = p->additive_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:8: unable to parse right side of + expression"); -} - -TEST_F(ParserImplTest, AdditiveExpression_Orig_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->additive_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - TEST_F(ParserImplTest, AdditiveExpression_Parses_Plus) { auto p = parser("a + b"); auto lhs = p->unary_expression(); diff --git a/src/tint/reader/wgsl/parser_impl_and_expression_test.cc b/src/tint/reader/wgsl/parser_impl_and_expression_test.cc deleted file mode 100644 index fd90460124..0000000000 --- a/src/tint/reader/wgsl/parser_impl_and_expression_test.cc +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/tint/reader/wgsl/parser_impl_test_helper.h" - -namespace tint::reader::wgsl { -namespace { - -TEST_F(ParserImplTest, AndExpression_Parses) { - auto p = parser("a & true"); - auto e = p->and_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kAnd, rel->op); - - ASSERT_TRUE(rel->lhs->Is()); - auto* ident = rel->lhs->As(); - EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->As()->value); -} - -TEST_F(ParserImplTest, AndExpression_InvalidLHS) { - auto p = parser("if (a) {} & true"); - auto e = p->and_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, AndExpression_InvalidRHS) { - auto p = parser("true & if (a) {}"); - auto e = p->and_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:8: unable to parse right side of & expression"); -} - -TEST_F(ParserImplTest, AndExpression_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->and_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - -} // namespace -} // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_equality_expression_test.cc b/src/tint/reader/wgsl/parser_impl_equality_expression_test.cc deleted file mode 100644 index 158227d833..0000000000 --- a/src/tint/reader/wgsl/parser_impl_equality_expression_test.cc +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/tint/reader/wgsl/parser_impl_test_helper.h" - -namespace tint::reader::wgsl { -namespace { - -TEST_F(ParserImplTest, EqualityExpression_Parses_Equal) { - auto p = parser("a == true"); - auto e = p->equality_expression(); - EXPECT_TRUE(e.matched); - 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, EqualityExpression_Parses_NotEqual) { - auto p = parser("a != true"); - auto e = p->equality_expression(); - EXPECT_TRUE(e.matched); - 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, EqualityExpression_InvalidLHS) { - auto p = parser("if (a) {} == true"); - auto e = p->equality_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, EqualityExpression_InvalidRHS) { - auto p = parser("true == if (a) {}"); - auto e = p->equality_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:9: unable to parse right side of == expression"); -} - -TEST_F(ParserImplTest, EqualityExpression_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->equality_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - -} // namespace -} // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_exclusive_or_expression_test.cc b/src/tint/reader/wgsl/parser_impl_exclusive_or_expression_test.cc deleted file mode 100644 index 2994ae8eca..0000000000 --- a/src/tint/reader/wgsl/parser_impl_exclusive_or_expression_test.cc +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/tint/reader/wgsl/parser_impl_test_helper.h" - -namespace tint::reader::wgsl { -namespace { - -TEST_F(ParserImplTest, ExclusiveOrExpression_Parses) { - auto p = parser("a ^ true"); - auto e = p->exclusive_or_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kXor, 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, ExclusiveOrExpression_InvalidLHS) { - auto p = parser("if (a) {} ^ true"); - auto e = p->exclusive_or_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, ExclusiveOrExpression_InvalidRHS) { - auto p = parser("true ^ if (a) {}"); - auto e = p->exclusive_or_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - ASSERT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:8: unable to parse right side of ^ expression"); -} - -TEST_F(ParserImplTest, ExclusiveOrExpression_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->exclusive_or_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - -} // namespace -} // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_inclusive_or_expression_test.cc b/src/tint/reader/wgsl/parser_impl_inclusive_or_expression_test.cc deleted file mode 100644 index f534ff7991..0000000000 --- a/src/tint/reader/wgsl/parser_impl_inclusive_or_expression_test.cc +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/tint/reader/wgsl/parser_impl_test_helper.h" - -namespace tint::reader::wgsl { -namespace { - -TEST_F(ParserImplTest, InclusiveOrExpression_Parses) { - auto p = parser("a | true"); - auto e = p->inclusive_or_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kOr, 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, InclusiveOrExpression_InvalidLHS) { - auto p = parser("if (a) {} | true"); - auto e = p->inclusive_or_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, InclusiveOrExpression_InvalidRHS) { - auto p = parser("true | if (a) {}"); - auto e = p->inclusive_or_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:8: unable to parse right side of | expression"); -} - -TEST_F(ParserImplTest, InclusiveOrExpression_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->inclusive_or_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - -} // namespace -} // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_logical_and_expression_test.cc b/src/tint/reader/wgsl/parser_impl_logical_and_expression_test.cc deleted file mode 100644 index 8baadaf7cf..0000000000 --- a/src/tint/reader/wgsl/parser_impl_logical_and_expression_test.cc +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/tint/reader/wgsl/parser_impl_test_helper.h" - -namespace tint::reader::wgsl { -namespace { - -TEST_F(ParserImplTest, LogicalAndExpression_Parses) { - auto p = parser("a && true"); - auto e = p->logical_and_expression(); - EXPECT_TRUE(e.matched); - 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::kLogicalAnd, 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, LogicalAndExpression_InvalidLHS) { - auto p = parser("if (a) {} && true"); - auto e = p->logical_and_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, LogicalAndExpression_InvalidRHS) { - auto p = parser("true && if (a) {}"); - auto e = p->logical_and_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:9: unable to parse right side of && expression"); -} - -TEST_F(ParserImplTest, LogicalAndExpression_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->logical_and_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - -} // namespace -} // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_logical_or_expression_test.cc b/src/tint/reader/wgsl/parser_impl_logical_or_expression_test.cc deleted file mode 100644 index 943b059321..0000000000 --- a/src/tint/reader/wgsl/parser_impl_logical_or_expression_test.cc +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2020 The Tint Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "src/tint/reader/wgsl/parser_impl_test_helper.h" - -namespace tint::reader::wgsl { -namespace { - -TEST_F(ParserImplTest, LogicalOrExpression_Parses) { - auto p = parser("a || true"); - auto e = p->logical_or_expression(); - EXPECT_TRUE(e.matched); - 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::kLogicalOr, 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, LogicalOrExpression_InvalidLHS) { - auto p = parser("if (a) {} || true"); - auto e = p->logical_or_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, LogicalOrExpression_InvalidRHS) { - auto p = parser("true || if (a) {}"); - auto e = p->logical_or_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:9: unable to parse right side of || expression"); -} - -TEST_F(ParserImplTest, LogicalOrExpression_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->logical_or_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - -} // namespace -} // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_multiplicative_expression_test.cc b/src/tint/reader/wgsl/parser_impl_multiplicative_expression_test.cc index 618a0c083c..941747798f 100644 --- a/src/tint/reader/wgsl/parser_impl_multiplicative_expression_test.cc +++ b/src/tint/reader/wgsl/parser_impl_multiplicative_expression_test.cc @@ -17,100 +17,6 @@ namespace tint::reader::wgsl { namespace { -TEST_F(ParserImplTest, MultiplicativeExpression_Orig_Parses_Multiply) { - auto p = parser("a * true"); - auto e = p->multiplicative_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kMultiply, 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, MultiplicativeExpression_Orig_Parses_Divide) { - auto p = parser("a / true"); - auto e = p->multiplicative_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kDivide, 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, MultiplicativeExpression_Orig_Parses_Modulo) { - auto p = parser("a % true"); - auto e = p->multiplicative_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kModulo, 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, MultiplicativeExpression_Orig_InvalidLHS) { - auto p = parser("if (a) {} * true"); - auto e = p->multiplicative_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, MultiplicativeExpression_Orig_InvalidRHS) { - auto p = parser("true * if (a) {}"); - auto e = p->multiplicative_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_EQ(e.value, nullptr); - ASSERT_TRUE(p->has_error()); - EXPECT_EQ(p->error(), "1:8: unable to parse right side of * expression"); -} - -TEST_F(ParserImplTest, MultiplicativeExpression_Orig_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->multiplicative_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Multiply) { auto p = parser("a * b"); auto lhs = p->unary_expression(); 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 59cddc6bf5..0d0eabcc71 100644 --- a/src/tint/reader/wgsl/parser_impl_relational_expression_test.cc +++ b/src/tint/reader/wgsl/parser_impl_relational_expression_test.cc @@ -17,133 +17,6 @@ namespace tint::reader::wgsl { namespace { -TEST_F(ParserImplTest, RelationalExpression_Orig_Parses_LessThan) { - auto p = parser("a < true"); - auto e = p->relational_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kLessThan, rel->op); - - ASSERT_TRUE(rel->lhs->Is()); - auto* ident = rel->lhs->As(); - EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->As()->value); -} - -TEST_F(ParserImplTest, RelationalExpression_Orig_Parses_GreaterThan) { - auto p = parser("a > true"); - auto e = p->relational_expression(); - EXPECT_TRUE(e.matched); - 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, 4u); - - ASSERT_TRUE(e->Is()); - auto* rel = e->As(); - EXPECT_EQ(ast::BinaryOp::kGreaterThan, rel->op); - - ASSERT_TRUE(rel->lhs->Is()); - auto* ident = rel->lhs->As(); - EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->As()->value); -} - -TEST_F(ParserImplTest, RelationalExpression_Orig_Parses_LessThanEqual) { - auto p = parser("a <= true"); - auto e = p->relational_expression(); - EXPECT_TRUE(e.matched); - 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::kLessThanEqual, rel->op); - - ASSERT_TRUE(rel->lhs->Is()); - auto* ident = rel->lhs->As(); - EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->As()->value); -} - -TEST_F(ParserImplTest, RelationalExpression_Orig_Parses_GreaterThanEqual) { - auto p = parser("a >= true"); - auto e = p->relational_expression(); - EXPECT_TRUE(e.matched); - 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::kGreaterThanEqual, rel->op); - - ASSERT_TRUE(rel->lhs->Is()); - auto* ident = rel->lhs->As(); - EXPECT_EQ(ident->symbol, p->builder().Symbols().Register("a")); - - ASSERT_TRUE(rel->rhs->Is()); - ASSERT_TRUE(rel->rhs->As()->value); -} - -TEST_F(ParserImplTest, RelationalExpression_Orig_InvalidLHS) { - auto p = parser("if (a) {} < true"); - auto e = p->relational_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, RelationalExpression_Orig_InvalidRHS) { - auto p = parser("true < if (a) {}"); - auto e = p->relational_expression(); - ASSERT_TRUE(p->has_error()); - EXPECT_EQ(e.value, nullptr); - EXPECT_EQ(p->error(), "1:8: unable to parse right side of < expression"); -} - -TEST_F(ParserImplTest, RelationalExpression_Orig_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->relational_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - TEST_F(ParserImplTest, RelationalExpression_PostUnary_Parses_LessThan) { auto p = parser("a < true"); auto lhs = p->unary_expression(); @@ -352,5 +225,65 @@ TEST_F(ParserImplTest, RelationalExpression_InvalidRHS) { EXPECT_EQ(p->error(), "1:8: unable to parse right side of < expression"); } +TEST_F(ParserImplTest, RelationalExpression_Parses_Equal) { + auto p = parser("a == true"); + auto e = p->maybe_relational_expression(); + EXPECT_TRUE(e.matched); + 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_Parses_NotEqual) { + auto p = parser("a != true"); + auto e = p->maybe_relational_expression(); + EXPECT_TRUE(e.matched); + 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_Equal_InvalidRHS) { + auto p = parser("true == if (a) {}"); + auto e = p->maybe_relational_expression(); + EXPECT_FALSE(e.matched); + EXPECT_TRUE(e.errored); + EXPECT_EQ(e.value, nullptr); + EXPECT_TRUE(p->has_error()); + EXPECT_EQ(p->error(), "1:9: unable to parse right side of == expression"); +} + } // namespace } // namespace tint::reader::wgsl diff --git a/src/tint/reader/wgsl/parser_impl_shift_expression_test.cc b/src/tint/reader/wgsl/parser_impl_shift_expression_test.cc index d518004b75..2c8aef30a4 100644 --- a/src/tint/reader/wgsl/parser_impl_shift_expression_test.cc +++ b/src/tint/reader/wgsl/parser_impl_shift_expression_test.cc @@ -17,103 +17,6 @@ namespace tint::reader::wgsl { namespace { -TEST_F(ParserImplTest, ShiftExpression_Orig_Parses_ShiftLeft) { - auto p = parser("a << true"); - auto e = p->shift_expression(); - EXPECT_TRUE(e.matched); - 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::kShiftLeft, 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, ShiftExpression_Orig_Parses_ShiftRight) { - auto p = parser("a >> true"); - auto e = p->shift_expression(); - EXPECT_TRUE(e.matched); - 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::kShiftRight, 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, ShiftExpression_Orig_InvalidSpaceLeft) { - auto p = parser("a < < true"); - auto e = p->shift_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - ASSERT_NE(e.value, nullptr); - EXPECT_FALSE(e.value->Is()); -} - -TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidSpaceRight) { - auto p = parser("a > > true"); - auto e = p->shift_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - ASSERT_NE(e.value, nullptr); - EXPECT_FALSE(e.value->Is()); -} - -TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidLHS) { - auto p = parser("if (a) {} << true"); - auto e = p->shift_expression(); - EXPECT_FALSE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - EXPECT_EQ(e.value, nullptr); -} - -TEST_F(ParserImplTest, ShiftExpression_Orig_InvalidRHS) { - auto p = parser("true << if (a) {}"); - auto e = p->shift_expression(); - EXPECT_FALSE(e.matched); - EXPECT_TRUE(e.errored); - EXPECT_TRUE(p->has_error()); - EXPECT_EQ(e.value, nullptr); - EXPECT_EQ(p->error(), "1:9: unable to parse right side of << expression"); -} - -TEST_F(ParserImplTest, ShiftExpression_Orig_NoOr_ReturnsLHS) { - auto p = parser("a true"); - auto e = p->shift_expression(); - EXPECT_TRUE(e.matched); - EXPECT_FALSE(e.errored); - EXPECT_FALSE(p->has_error()) << p->error(); - ASSERT_NE(e.value, nullptr); - ASSERT_TRUE(e->Is()); -} - TEST_F(ParserImplTest, ShiftExpression_PostUnary_Parses_ShiftLeft) { auto p = parser("a << true"); auto lhs = p->unary_expression(); @@ -231,7 +134,7 @@ TEST_F(ParserImplTest, ShiftExpression_PostUnary_InvalidRHS) { 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"); + EXPECT_EQ(p->error(), "1:6: unable to parse right side of << expression"); } TEST_F(ParserImplTest, ShiftExpression_PostUnary_NoOr_ReturnsLHS) { diff --git a/src/tint/reader/wgsl/parser_impl_unary_expression_test.cc b/src/tint/reader/wgsl/parser_impl_unary_expression_test.cc index 184de66de5..29598d9dbe 100644 --- a/src/tint/reader/wgsl/parser_impl_unary_expression_test.cc +++ b/src/tint/reader/wgsl/parser_impl_unary_expression_test.cc @@ -86,7 +86,7 @@ TEST_F(ParserImplTest, UnaryExpression_Dereference) { TEST_F(ParserImplTest, UnaryExpression_AddressOf_Precedence) { auto p = parser("&x.y"); - auto e = p->logical_or_expression(); + auto e = p->unary_expression(); EXPECT_TRUE(e.matched); EXPECT_FALSE(e.errored); EXPECT_FALSE(p->has_error()) << p->error(); @@ -100,7 +100,7 @@ TEST_F(ParserImplTest, UnaryExpression_AddressOf_Precedence) { TEST_F(ParserImplTest, UnaryExpression_Dereference_Precedence) { auto p = parser("*x.y"); - auto e = p->logical_or_expression(); + auto e = p->unary_expression(); EXPECT_TRUE(e.matched); EXPECT_FALSE(e.errored); EXPECT_FALSE(p->has_error()) << p->error();