[wgsl-reader] Parsing texture sampler types

Change-Id: I6a29d81deca40e89cf5b8ca95cf0373af3dd16b7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/28005
Commit-Queue: Tomek Ponitka <tommek@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 690cb9b..6add641 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -926,6 +926,7 @@
     "src/reader/wgsl/parser_impl_test.cc",
     "src/reader/wgsl/parser_impl_test_helper.cc",
     "src/reader/wgsl/parser_impl_test_helper.h",
+    "src/reader/wgsl/parser_impl_texture_sampler_types_test.cc",
     "src/reader/wgsl/parser_impl_type_alias_test.cc",
     "src/reader/wgsl/parser_impl_type_decl_test.cc",
     "src/reader/wgsl/parser_impl_unary_expression_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 2f755c2..c735b0d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -457,6 +457,7 @@
     reader/wgsl/parser_impl_test.cc
     reader/wgsl/parser_impl_test_helper.cc
     reader/wgsl/parser_impl_test_helper.h
+    reader/wgsl/parser_impl_texture_sampler_types_test.cc
     reader/wgsl/parser_impl_type_alias_test.cc
     reader/wgsl/parser_impl_type_decl_test.cc
     reader/wgsl/parser_impl_unary_expression_test.cc
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc
index 59dc553..e7a748b 100644
--- a/src/reader/wgsl/parser_impl.cc
+++ b/src/reader/wgsl/parser_impl.cc
@@ -577,6 +577,26 @@
   return std::make_unique<ast::Variable>(source, name, sc, type);
 }
 
+// texture_sampler_types
+//  : sampler_type
+//  | depth_texture_type
+//  | TODO: sampled_texture_type LESS_THAN type_decl GREATER_THAN
+//  | TODO: multisampled_texture_type LESS_THAN type_decl GREATER_THAN
+//  | TODO: storage_texture_type LESS_THAN image_storage_type GREATER_THAN
+ast::type::Type* ParserImpl::texture_sampler_types() {
+  auto* type = sampler_type();
+  if (type != nullptr) {
+    return type;
+  }
+
+  type = depth_texture_type();
+  if (type != nullptr) {
+    return type;
+  }
+
+  return nullptr;
+}
+
 // sampler_type
 //  : SAMPLER
 //  | SAMPLER_COMPARISON
diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h
index b13f264..d492438 100644
--- a/src/reader/wgsl/parser_impl.h
+++ b/src/reader/wgsl/parser_impl.h
@@ -180,6 +180,9 @@
   /// Parses a `function_decl` grammar element
   /// @returns the parsed function, nullptr otherwise
   std::unique_ptr<ast::Function> function_decl();
+  /// Parses a `texture_sampler_types` grammar element
+  /// @returns the parsed Type or nullptr if none matched.
+  ast::type::Type* texture_sampler_types();
   /// Parses a `sampler_type` grammar element
   /// @returns the parsed Type or nullptr if none matched.
   ast::type::Type* sampler_type();
diff --git a/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc
new file mode 100644
index 0000000..a1580e6
--- /dev/null
+++ b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc
@@ -0,0 +1,55 @@
+// 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 "gtest/gtest.h"
+#include "src/ast/type/sampled_texture_type.h"
+#include "src/ast/type/sampler_type.h"
+#include "src/reader/wgsl/parser_impl.h"
+#include "src/reader/wgsl/parser_impl_test_helper.h"
+
+namespace tint {
+namespace reader {
+namespace wgsl {
+namespace {
+
+TEST_F(ParserImplTest, TextureSamplerTypes_Invalid) {
+  auto* p = parser("1234");
+  auto* t = p->texture_sampler_types();
+  EXPECT_EQ(t, nullptr);
+  EXPECT_FALSE(p->has_error());
+}
+
+TEST_F(ParserImplTest, TextureSamplerTypes_Sampler) {
+  auto* p = parser("sampler");
+  auto* t = p->texture_sampler_types();
+  ASSERT_NE(t, nullptr);
+  ASSERT_TRUE(t->IsSampler());
+  ASSERT_FALSE(t->AsSampler()->IsComparison());
+  EXPECT_FALSE(p->has_error());
+}
+
+TEST_F(ParserImplTest, TextureSamplerTypes_DepthTexture) {
+  auto* p = parser("texture_depth_2d");
+  auto* t = p->texture_sampler_types();
+  ASSERT_NE(t, nullptr);
+  ASSERT_TRUE(t->IsTexture());
+  ASSERT_TRUE(t->AsTexture()->IsDepth());
+  EXPECT_EQ(t->AsTexture()->dim(), ast::type::TextureDimension::k2d);
+  EXPECT_FALSE(p->has_error());
+}
+
+}  // namespace
+}  // namespace wgsl
+}  // namespace reader
+}  // namespace tint