Context aware address space parsing.

This Cl remoevs the address space names from the keyword list and
makes their parsing context dependant. The mechanism in the parser
is the same, we just change to looking for an `ident` in
`expect_address_space`.

`storage_buffer` is kept for now, this keeps the functionality the same.

Bug: tint:1621
Change-Id: I928a5472d8ac194b2bef2da56a224e9f4abb65a8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/96905
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Auto-Submit: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2022-07-22 16:09:55 +00:00 committed by Dawn LUCI CQ
parent 87254ff58e
commit a089376daf
8 changed files with 16 additions and 64 deletions

View File

@ -1127,9 +1127,6 @@ Token Lexer::check_keyword(const Source& source, std::string_view str) {
if (str == "for") { if (str == "for") {
return {Token::Type::kFor, source, "for"}; return {Token::Type::kFor, source, "for"};
} }
if (str == "function") {
return {Token::Type::kFunction, source, "function"};
}
if (str == "i32") { if (str == "i32") {
return {Token::Type::kI32, source, "i32"}; return {Token::Type::kI32, source, "i32"};
} }
@ -1175,9 +1172,6 @@ Token Lexer::check_keyword(const Source& source, std::string_view str) {
if (str == "override") { if (str == "override") {
return {Token::Type::kOverride, source, "override"}; return {Token::Type::kOverride, source, "override"};
} }
if (str == "private") {
return {Token::Type::kPrivate, source, "private"};
}
if (str == "ptr") { if (str == "ptr") {
return {Token::Type::kPtr, source, "ptr"}; return {Token::Type::kPtr, source, "ptr"};
} }
@ -1190,9 +1184,6 @@ Token Lexer::check_keyword(const Source& source, std::string_view str) {
if (str == "sampler_comparison") { if (str == "sampler_comparison") {
return {Token::Type::kComparisonSampler, source, "sampler_comparison"}; return {Token::Type::kComparisonSampler, source, "sampler_comparison"};
} }
if (str == "storage_buffer" || str == "storage") {
return {Token::Type::kStorage, source, "storage"};
}
if (str == "struct") { if (str == "struct") {
return {Token::Type::kStruct, source, "struct"}; return {Token::Type::kStruct, source, "struct"};
} }
@ -1259,9 +1250,6 @@ Token Lexer::check_keyword(const Source& source, std::string_view str) {
if (str == "u32") { if (str == "u32") {
return {Token::Type::kU32, source, "u32"}; return {Token::Type::kU32, source, "u32"};
} }
if (str == "uniform") {
return {Token::Type::kUniform, source, "uniform"};
}
if (str == "var") { if (str == "var") {
return {Token::Type::kVar, source, "var"}; return {Token::Type::kVar, source, "var"};
} }
@ -1277,9 +1265,6 @@ Token Lexer::check_keyword(const Source& source, std::string_view str) {
if (str == "while") { if (str == "while") {
return {Token::Type::kWhile, source, "while"}; return {Token::Type::kWhile, source, "while"};
} }
if (str == "workgroup") {
return {Token::Type::kWorkgroup, source, "workgroup"};
}
return {}; return {};
} }

View File

@ -941,7 +941,6 @@ INSTANTIATE_TEST_SUITE_P(
TokenData{"false", Token::Type::kFalse}, TokenData{"false", Token::Type::kFalse},
TokenData{"fn", Token::Type::kFn}, TokenData{"fn", Token::Type::kFn},
TokenData{"for", Token::Type::kFor}, TokenData{"for", Token::Type::kFor},
TokenData{"function", Token::Type::kFunction},
TokenData{"i32", Token::Type::kI32}, TokenData{"i32", Token::Type::kI32},
TokenData{"if", Token::Type::kIf}, TokenData{"if", Token::Type::kIf},
TokenData{"import", Token::Type::kImport}, TokenData{"import", Token::Type::kImport},
@ -957,13 +956,10 @@ INSTANTIATE_TEST_SUITE_P(
TokenData{"mat4x3", Token::Type::kMat4x3}, TokenData{"mat4x3", Token::Type::kMat4x3},
TokenData{"mat4x4", Token::Type::kMat4x4}, TokenData{"mat4x4", Token::Type::kMat4x4},
TokenData{"override", Token::Type::kOverride}, TokenData{"override", Token::Type::kOverride},
TokenData{"private", Token::Type::kPrivate},
TokenData{"ptr", Token::Type::kPtr}, TokenData{"ptr", Token::Type::kPtr},
TokenData{"return", Token::Type::kReturn}, TokenData{"return", Token::Type::kReturn},
TokenData{"sampler", Token::Type::kSampler}, TokenData{"sampler", Token::Type::kSampler},
TokenData{"sampler_comparison", Token::Type::kComparisonSampler}, TokenData{"sampler_comparison", Token::Type::kComparisonSampler},
TokenData{"storage", Token::Type::kStorage},
TokenData{"storage_buffer", Token::Type::kStorage},
TokenData{"struct", Token::Type::kStruct}, TokenData{"struct", Token::Type::kStruct},
TokenData{"switch", Token::Type::kSwitch}, TokenData{"switch", Token::Type::kSwitch},
TokenData{"texture_1d", Token::Type::kTextureSampled1d}, TokenData{"texture_1d", Token::Type::kTextureSampled1d},
@ -986,13 +982,11 @@ INSTANTIATE_TEST_SUITE_P(
TokenData{"true", Token::Type::kTrue}, TokenData{"true", Token::Type::kTrue},
TokenData{"type", Token::Type::kType}, TokenData{"type", Token::Type::kType},
TokenData{"u32", Token::Type::kU32}, TokenData{"u32", Token::Type::kU32},
TokenData{"uniform", Token::Type::kUniform},
TokenData{"var", Token::Type::kVar}, TokenData{"var", Token::Type::kVar},
TokenData{"vec2", Token::Type::kVec2}, TokenData{"vec2", Token::Type::kVec2},
TokenData{"vec3", Token::Type::kVec3}, TokenData{"vec3", Token::Type::kVec3},
TokenData{"vec4", Token::Type::kVec4}, TokenData{"vec4", Token::Type::kVec4},
TokenData{"while", Token::Type::kWhile}, TokenData{"while", Token::Type::kWhile}));
TokenData{"workgroup", Token::Type::kWorkgroup}));
} // namespace } // namespace
} // namespace tint::reader::wgsl } // namespace tint::reader::wgsl

View File

@ -1271,34 +1271,31 @@ Expect<const ast::Type*> ParserImpl::expect_type_decl_matrix(Token t) {
return builder_.ty.mat(make_source_range_from(t.source()), subtype, columns, rows); return builder_.ty.mat(make_source_range_from(t.source()), subtype, columns, rows);
} }
// storage_class
// : INPUT
// | OUTPUT
// | UNIFORM
// | WORKGROUP
// | STORAGE
// | PRIVATE
// | FUNCTION
Expect<ast::StorageClass> ParserImpl::expect_storage_class(std::string_view use) { Expect<ast::StorageClass> ParserImpl::expect_storage_class(std::string_view use) {
auto source = peek().source(); auto source = peek().source();
auto ident = expect_ident("storage class");
if (ident.errored) {
return Failure::kErrored;
}
if (match(Token::Type::kUniform)) { auto name = ident.value;
if (name == "uniform") {
return {ast::StorageClass::kUniform, source}; return {ast::StorageClass::kUniform, source};
} }
if (match(Token::Type::kWorkgroup)) { if (name == "workgroup") {
return {ast::StorageClass::kWorkgroup, source}; return {ast::StorageClass::kWorkgroup, source};
} }
if (match(Token::Type::kStorage)) { if (name == "storage" || name == "storage_buffer") {
return {ast::StorageClass::kStorage, source}; return {ast::StorageClass::kStorage, source};
} }
if (match(Token::Type::kPrivate)) { if (name == "private") {
return {ast::StorageClass::kPrivate, source}; return {ast::StorageClass::kPrivate, source};
} }
if (match(Token::Type::kFunction)) { if (name == "function") {
return {ast::StorageClass::kFunction, source}; return {ast::StorageClass::kFunction, source};
} }

View File

@ -56,10 +56,6 @@ TEST_F(ParserImplTest, StorageClass_NoMatch) {
EXPECT_EQ(sc.errored, true); EXPECT_EQ(sc.errored, true);
EXPECT_TRUE(p->has_error()); EXPECT_TRUE(p->has_error());
EXPECT_EQ(p->error(), "1:1: invalid storage class for test"); EXPECT_EQ(p->error(), "1:1: invalid storage class for test");
auto t = p->next();
EXPECT_TRUE(t.IsIdentifier());
EXPECT_EQ(t.to_str(), "not");
} }
} // namespace } // namespace

View File

@ -272,7 +272,7 @@ TEST_F(ParserImplTest, TypeDecl_Ptr_MissingStorageClass) {
EXPECT_FALSE(t.matched); EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr); ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:5: invalid storage class for ptr declaration"); ASSERT_EQ(p->error(), "1:5: expected identifier for storage class");
} }
TEST_F(ParserImplTest, TypeDecl_Ptr_MissingType) { TEST_F(ParserImplTest, TypeDecl_Ptr_MissingType) {
@ -302,7 +302,7 @@ TEST_F(ParserImplTest, TypeDecl_Ptr_MissingParams) {
EXPECT_FALSE(t.matched); EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr); ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:5: invalid storage class for ptr declaration"); ASSERT_EQ(p->error(), "1:5: expected identifier for storage class");
} }
TEST_F(ParserImplTest, TypeDecl_Ptr_BadStorageClass) { TEST_F(ParserImplTest, TypeDecl_Ptr_BadStorageClass) {

View File

@ -73,7 +73,7 @@ TEST_F(ParserImplTest, VariableQualifier_Empty) {
EXPECT_TRUE(p->has_error()); EXPECT_TRUE(p->has_error());
EXPECT_TRUE(sc.errored); EXPECT_TRUE(sc.errored);
EXPECT_FALSE(sc.matched); EXPECT_FALSE(sc.matched);
EXPECT_EQ(p->error(), "1:2: invalid storage class for variable declaration"); EXPECT_EQ(p->error(), "1:2: expected identifier for storage class");
} }
TEST_F(ParserImplTest, VariableQualifier_MissingLessThan) { TEST_F(ParserImplTest, VariableQualifier_MissingLessThan) {
@ -84,7 +84,7 @@ TEST_F(ParserImplTest, VariableQualifier_MissingLessThan) {
EXPECT_FALSE(sc.matched); EXPECT_FALSE(sc.matched);
auto t = p->next(); auto t = p->next();
ASSERT_TRUE(t.Is(Token::Type::kPrivate)); ASSERT_TRUE(t.Is(Token::Type::kIdentifier));
} }
TEST_F(ParserImplTest, VariableQualifier_MissingLessThan_AfterSC) { TEST_F(ParserImplTest, VariableQualifier_MissingLessThan_AfterSC) {
@ -95,7 +95,7 @@ TEST_F(ParserImplTest, VariableQualifier_MissingLessThan_AfterSC) {
EXPECT_FALSE(sc.matched); EXPECT_FALSE(sc.matched);
auto t = p->next(); auto t = p->next();
ASSERT_TRUE(t.Is(Token::Type::kPrivate)); ASSERT_TRUE(t.Is(Token::Type::kIdentifier));
} }
TEST_F(ParserImplTest, VariableQualifier_MissingGreaterThan) { TEST_F(ParserImplTest, VariableQualifier_MissingGreaterThan) {

View File

@ -167,8 +167,6 @@ std::string_view Token::TypeToName(Type type) {
return "fn"; return "fn";
case Token::Type::kFor: case Token::Type::kFor:
return "for"; return "for";
case Token::Type::kFunction:
return "function";
case Token::Type::kI32: case Token::Type::kI32:
return "i32"; return "i32";
case Token::Type::kIf: case Token::Type::kIf:
@ -199,8 +197,6 @@ std::string_view Token::TypeToName(Type type) {
return "mat4x4"; return "mat4x4";
case Token::Type::kOverride: case Token::Type::kOverride:
return "override"; return "override";
case Token::Type::kPrivate:
return "private";
case Token::Type::kPtr: case Token::Type::kPtr:
return "ptr"; return "ptr";
case Token::Type::kReturn: case Token::Type::kReturn:
@ -209,8 +205,6 @@ std::string_view Token::TypeToName(Type type) {
return "sampler"; return "sampler";
case Token::Type::kComparisonSampler: case Token::Type::kComparisonSampler:
return "sampler_comparison"; return "sampler_comparison";
case Token::Type::kStorage:
return "storage";
case Token::Type::kStruct: case Token::Type::kStruct:
return "struct"; return "struct";
case Token::Type::kSwitch: case Token::Type::kSwitch:
@ -255,8 +249,6 @@ std::string_view Token::TypeToName(Type type) {
return "type"; return "type";
case Token::Type::kU32: case Token::Type::kU32:
return "u32"; return "u32";
case Token::Type::kUniform:
return "uniform";
case Token::Type::kVar: case Token::Type::kVar:
return "var"; return "var";
case Token::Type::kVec2: case Token::Type::kVec2:
@ -267,8 +259,6 @@ std::string_view Token::TypeToName(Type type) {
return "vec4"; return "vec4";
case Token::Type::kWhile: case Token::Type::kWhile:
return "while"; return "while";
case Token::Type::kWorkgroup:
return "workgroup";
} }
return "<unknown>"; return "<unknown>";

View File

@ -177,8 +177,6 @@ class Token {
kFn, kFn,
// A 'for' // A 'for'
kFor, kFor,
/// A 'function'
kFunction,
/// A 'i32' /// A 'i32'
kI32, kI32,
/// A 'if' /// A 'if'
@ -209,8 +207,6 @@ class Token {
kMat4x4, kMat4x4,
/// A 'override' /// A 'override'
kOverride, kOverride,
/// A 'private'
kPrivate,
/// A 'ptr' /// A 'ptr'
kPtr, kPtr,
/// A 'return' /// A 'return'
@ -219,8 +215,6 @@ class Token {
kSampler, kSampler,
/// A 'sampler_comparison' /// A 'sampler_comparison'
kComparisonSampler, kComparisonSampler,
/// A 'storage'
kStorage,
/// A 'struct' /// A 'struct'
kStruct, kStruct,
/// A 'switch' /// A 'switch'
@ -265,8 +259,6 @@ class Token {
kType, kType,
/// A 'u32' /// A 'u32'
kU32, kU32,
/// A 'uniform'
kUniform,
/// A 'var' /// A 'var'
kVar, kVar,
/// A 'vec2' /// A 'vec2'
@ -277,8 +269,6 @@ class Token {
kVec4, kVec4,
/// A 'while' /// A 'while'
kWhile, kWhile,
/// A 'workgroup'
kWorkgroup,
}; };
/// Converts a token type to a name /// Converts a token type to a name