reader/wgsl: Reserve ++ and -- tokens

Fixed: tint:971
Change-Id: I70732db7b22f7785866066cd7966eec630078de0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57761
Auto-Submit: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price 2021-07-13 13:34:15 +00:00 committed by Tint LUCI CQ
parent 8d33b8593c
commit ae23bd5548
7 changed files with 82 additions and 0 deletions

View File

@ -423,6 +423,10 @@ Token Lexer::try_punctuation() {
type = Token::Type::kArrow; type = Token::Type::kArrow;
pos_ += 2; pos_ += 2;
location_.column += 2; location_.column += 2;
} else if (matches(pos_, "--")) {
type = Token::Type::kMinusMinus;
pos_ += 2;
location_.column += 2;
} else if (matches(pos_, "-")) { } else if (matches(pos_, "-")) {
type = Token::Type::kMinus; type = Token::Type::kMinus;
pos_ += 1; pos_ += 1;
@ -431,6 +435,10 @@ Token Lexer::try_punctuation() {
type = Token::Type::kPeriod; type = Token::Type::kPeriod;
pos_ += 1; pos_ += 1;
location_.column += 1; location_.column += 1;
} else if (matches(pos_, "++")) {
type = Token::Type::kPlusPlus;
pos_ += 2;
location_.column += 2;
} else if (matches(pos_, "+")) { } else if (matches(pos_, "+")) {
type = Token::Type::kPlus; type = Token::Type::kPlus;
pos_ += 1; pos_ += 1;

View File

@ -387,8 +387,10 @@ INSTANTIATE_TEST_SUITE_P(
TokenData{"%", Token::Type::kMod}, TokenData{"%", Token::Type::kMod},
TokenData{"!=", Token::Type::kNotEqual}, TokenData{"!=", Token::Type::kNotEqual},
TokenData{"-", Token::Type::kMinus}, TokenData{"-", Token::Type::kMinus},
TokenData{"--", Token::Type::kMinusMinus},
TokenData{".", Token::Type::kPeriod}, TokenData{".", Token::Type::kPeriod},
TokenData{"+", Token::Type::kPlus}, TokenData{"+", Token::Type::kPlus},
TokenData{"++", Token::Type::kPlusPlus},
TokenData{"|", Token::Type::kOr}, TokenData{"|", Token::Type::kOr},
TokenData{"||", Token::Type::kOrOr}, TokenData{"||", Token::Type::kOrOr},
TokenData{"(", Token::Type::kParenLeft}, TokenData{"(", Token::Type::kParenLeft},

View File

@ -2223,6 +2223,15 @@ Maybe<ast::Expression*> ParserImpl::primary_expression() {
Maybe<ast::Expression*> ParserImpl::postfix_expression( Maybe<ast::Expression*> ParserImpl::postfix_expression(
ast::Expression* prefix) { ast::Expression* prefix) {
Source source; Source source;
if (match(Token::Type::kPlusPlus, &source) ||
match(Token::Type::kMinusMinus, &source)) {
add_error(source,
"postfix increment and decrement operators are reserved for a "
"future WGSL version");
return Failure::kErrored;
}
if (match(Token::Type::kBracketLeft, &source)) { if (match(Token::Type::kBracketLeft, &source)) {
return sync(Token::Type::kBracketRight, [&]() -> Maybe<ast::Expression*> { return sync(Token::Type::kBracketRight, [&]() -> Maybe<ast::Expression*> {
auto param = logical_or_expression(); auto param = logical_or_expression();
@ -2299,6 +2308,13 @@ Expect<ast::ExpressionList> ParserImpl::expect_argument_expression_list(
Maybe<ast::Expression*> ParserImpl::unary_expression() { Maybe<ast::Expression*> ParserImpl::unary_expression() {
auto t = peek(); auto t = peek();
if (match(Token::Type::kPlusPlus) || match(Token::Type::kMinusMinus)) {
add_error(t.source(),
"prefix increment and decrement operators are reserved for a "
"future WGSL version");
return Failure::kErrored;
}
ast::UnaryOp op; ast::UnaryOp op;
if (match(Token::Type::kMinus)) { if (match(Token::Type::kMinus)) {
op = ast::UnaryOp::kNegation; op = ast::UnaryOp::kNegation;

View File

@ -236,6 +236,30 @@ TEST_F(ParserImplTest, SingularExpression_Array_NestedArrayAccessor) {
EXPECT_EQ(index_expr->symbol(), p->builder().Symbols().Get("c")); EXPECT_EQ(index_expr->symbol(), p->builder().Symbols().Get("c"));
} }
TEST_F(ParserImplTest, SingularExpression_PostfixPlusPlus) {
auto p = parser("a++");
auto e = p->singular_expression();
EXPECT_FALSE(e.matched);
EXPECT_TRUE(e.errored);
EXPECT_EQ(e.value, nullptr);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(p->error(),
"1:2: postfix increment and decrement operators are reserved for a "
"future WGSL version");
}
TEST_F(ParserImplTest, SingularExpression_PostfixMinusMinus) {
auto p = parser("a--");
auto e = p->singular_expression();
EXPECT_FALSE(e.matched);
EXPECT_TRUE(e.errored);
EXPECT_EQ(e.value, nullptr);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(p->error(),
"1:2: postfix increment and decrement operators are reserved for a "
"future WGSL version");
}
} // namespace } // namespace
} // namespace wgsl } // namespace wgsl
} // namespace reader } // namespace reader

View File

@ -177,6 +177,30 @@ TEST_F(ParserImplTest, UnaryExpression_Tilde) {
EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 1); EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 1);
} }
TEST_F(ParserImplTest, UnaryExpression_PrefixPlusPlus) {
auto p = parser("++a");
auto e = p->unary_expression();
EXPECT_FALSE(e.matched);
EXPECT_TRUE(e.errored);
EXPECT_EQ(e.value, nullptr);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(p->error(),
"1:1: prefix increment and decrement operators are reserved for a "
"future WGSL version");
}
TEST_F(ParserImplTest, UnaryExpression_PrefixMinusMinus) {
auto p = parser("--a");
auto e = p->unary_expression();
EXPECT_FALSE(e.matched);
EXPECT_TRUE(e.errored);
EXPECT_EQ(e.value, nullptr);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(p->error(),
"1:1: prefix increment and decrement operators are reserved for a "
"future WGSL version");
}
} // namespace } // namespace
} // namespace wgsl } // namespace wgsl
} // namespace reader } // namespace reader

View File

@ -84,10 +84,14 @@ std::string Token::TypeToName(Type type) {
return "!="; return "!=";
case Token::Type::kMinus: case Token::Type::kMinus:
return "-"; return "-";
case Token::Type::kMinusMinus:
return "--";
case Token::Type::kPeriod: case Token::Type::kPeriod:
return "."; return ".";
case Token::Type::kPlus: case Token::Type::kPlus:
return "+"; return "+";
case Token::Type::kPlusPlus:
return "++";
case Token::Type::kOr: case Token::Type::kOr:
return "|"; return "|";
case Token::Type::kOrOr: case Token::Type::kOrOr:

View File

@ -90,12 +90,16 @@ class Token {
kMod, kMod,
/// A '-' /// A '-'
kMinus, kMinus,
/// A '--'
kMinusMinus,
/// A '!=' /// A '!='
kNotEqual, kNotEqual,
/// A '.' /// A '.'
kPeriod, kPeriod,
/// A '+' /// A '+'
kPlus, kPlus,
/// A '++'
kPlusPlus,
/// A '|' /// A '|'
kOr, kOr,
/// A '||' /// A '||'