wsgl parser: Use match() for trivial patterns
Replace uses of peek, test, return-or-next with match(). Reduces code. Bug: tint:282 Change-Id: I5e53601746e78efd007773cfea7109483c5e1630 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31726 Commit-Queue: dan sinclair <dsinclair@chromium.org> Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
parent
f7fc63b8e3
commit
5c58a06515
|
@ -315,10 +315,7 @@ std::unique_ptr<ast::Variable> ParserImpl::global_variable_decl() {
|
||||||
var = std::move(dv);
|
var = std::move(dv);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto t = peek();
|
if (match(Token::Type::kEqual)) {
|
||||||
if (t.IsEqual()) {
|
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
auto expr = const_expr();
|
auto expr = const_expr();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -335,12 +332,9 @@ std::unique_ptr<ast::Variable> ParserImpl::global_variable_decl() {
|
||||||
// global_constant_decl
|
// global_constant_decl
|
||||||
// : CONST variable_ident_decl EQUAL const_expr
|
// : CONST variable_ident_decl EQUAL const_expr
|
||||||
std::unique_ptr<ast::Variable> ParserImpl::global_constant_decl() {
|
std::unique_ptr<ast::Variable> ParserImpl::global_constant_decl() {
|
||||||
auto t = peek();
|
if (!match(Token::Type::kConst))
|
||||||
if (!t.IsConst())
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
auto decl = variable_ident_decl();
|
auto decl = variable_ident_decl();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -353,7 +347,7 @@ std::unique_ptr<ast::Variable> ParserImpl::global_constant_decl() {
|
||||||
decl.source, decl.name, ast::StorageClass::kNone, decl.type);
|
decl.source, decl.name, ast::StorageClass::kNone, decl.type);
|
||||||
var->set_is_const(true);
|
var->set_is_const(true);
|
||||||
|
|
||||||
t = next();
|
auto t = next();
|
||||||
if (!t.IsEqual()) {
|
if (!t.IsEqual()) {
|
||||||
add_error(t, "missing = for const declaration");
|
add_error(t, "missing = for const declaration");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -404,11 +398,8 @@ bool ParserImpl::variable_decoration_list(ast::VariableDecorationList& decos) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
decos.push_back(std::move(deco));
|
decos.push_back(std::move(deco));
|
||||||
|
|
||||||
t = peek();
|
if (!match(Token::Type::kComma))
|
||||||
if (!t.IsComma()) {
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
next(); // consume the peek
|
|
||||||
|
|
||||||
deco = variable_decoration();
|
deco = variable_decoration();
|
||||||
if (has_error()) {
|
if (has_error()) {
|
||||||
|
@ -549,12 +540,9 @@ std::unique_ptr<ast::VariableDecoration> ParserImpl::variable_decoration() {
|
||||||
// variable_decl
|
// variable_decl
|
||||||
// : VAR variable_storage_decoration? variable_ident_decl
|
// : VAR variable_storage_decoration? variable_ident_decl
|
||||||
std::unique_ptr<ast::Variable> ParserImpl::variable_decl() {
|
std::unique_ptr<ast::Variable> ParserImpl::variable_decl() {
|
||||||
auto t = peek();
|
if (!match(Token::Type::kVar))
|
||||||
if (!t.IsVar())
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
auto sc = variable_storage_decoration();
|
auto sc = variable_storage_decoration();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return {};
|
return {};
|
||||||
|
@ -1043,12 +1031,9 @@ ParserImpl::TypedIdentifier ParserImpl::variable_ident_decl() {
|
||||||
// variable_storage_decoration
|
// variable_storage_decoration
|
||||||
// : LESS_THAN storage_class GREATER_THAN
|
// : LESS_THAN storage_class GREATER_THAN
|
||||||
ast::StorageClass ParserImpl::variable_storage_decoration() {
|
ast::StorageClass ParserImpl::variable_storage_decoration() {
|
||||||
auto t = peek();
|
if (!match(Token::Type::kLessThan))
|
||||||
if (!t.IsLessThan())
|
|
||||||
return ast::StorageClass::kNone;
|
return ast::StorageClass::kNone;
|
||||||
|
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
auto sc = storage_class();
|
auto sc = storage_class();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return sc;
|
return sc;
|
||||||
|
@ -1057,7 +1042,7 @@ ast::StorageClass ParserImpl::variable_storage_decoration() {
|
||||||
return sc;
|
return sc;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = next();
|
auto t = next();
|
||||||
if (!t.IsGreaterThan()) {
|
if (!t.IsGreaterThan()) {
|
||||||
add_error(t, "missing > for variable decoration");
|
add_error(t, "missing > for variable decoration");
|
||||||
return ast::StorageClass::kNone;
|
return ast::StorageClass::kNone;
|
||||||
|
@ -1639,14 +1624,10 @@ std::unique_ptr<ast::StructMember> ParserImpl::struct_member() {
|
||||||
// struct_member_decoration ATTR_RIGHT
|
// struct_member_decoration ATTR_RIGHT
|
||||||
bool ParserImpl::struct_member_decoration_decl(
|
bool ParserImpl::struct_member_decoration_decl(
|
||||||
ast::StructMemberDecorationList& decos) {
|
ast::StructMemberDecorationList& decos) {
|
||||||
auto t = peek();
|
if (!match(Token::Type::kAttrLeft))
|
||||||
if (!t.IsAttrLeft()) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
next(); // Consume the peek
|
auto t = peek();
|
||||||
|
|
||||||
t = peek();
|
|
||||||
if (t.IsAttrRight()) {
|
if (t.IsAttrRight()) {
|
||||||
add_error(t, "empty struct member decoration found");
|
add_error(t, "empty struct member decoration found");
|
||||||
return false;
|
return false;
|
||||||
|
@ -1910,14 +1891,12 @@ ast::type::Type* ParserImpl::function_type_decl() {
|
||||||
// function_header
|
// function_header
|
||||||
// : FN IDENT PAREN_LEFT param_list PAREN_RIGHT ARROW function_type_decl
|
// : FN IDENT PAREN_LEFT param_list PAREN_RIGHT ARROW function_type_decl
|
||||||
std::unique_ptr<ast::Function> ParserImpl::function_header() {
|
std::unique_ptr<ast::Function> ParserImpl::function_header() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsFn())
|
|
||||||
|
if (!match(Token::Type::kFn))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto source = t.source();
|
auto t = next();
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
t = next();
|
|
||||||
if (!t.IsIdentifier()) {
|
if (!t.IsIdentifier()) {
|
||||||
add_error(t, "missing identifier for function");
|
add_error(t, "missing identifier for function");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2218,16 +2197,13 @@ std::unique_ptr<ast::Statement> ParserImpl::statement() {
|
||||||
// return_stmt
|
// return_stmt
|
||||||
// : RETURN logical_or_expression?
|
// : RETURN logical_or_expression?
|
||||||
std::unique_ptr<ast::ReturnStatement> ParserImpl::return_stmt() {
|
std::unique_ptr<ast::ReturnStatement> ParserImpl::return_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsReturn())
|
|
||||||
|
if (!match(Token::Type::kReturn))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto source = t.source();
|
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
std::unique_ptr<ast::Expression> expr = nullptr;
|
std::unique_ptr<ast::Expression> expr = nullptr;
|
||||||
t = peek();
|
if (!peek().IsSemicolon()) {
|
||||||
if (!t.IsSemicolon()) {
|
|
||||||
expr = logical_or_expression();
|
expr = logical_or_expression();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2281,9 +2257,7 @@ std::unique_ptr<ast::VariableDeclStatement> ParserImpl::variable_stmt() {
|
||||||
if (var == nullptr)
|
if (var == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
t = peek();
|
if (match(Token::Type::kEqual)) {
|
||||||
if (t.IsEqual()) {
|
|
||||||
next(); // Consume the peek
|
|
||||||
auto constructor = logical_or_expression();
|
auto constructor = logical_or_expression();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2301,12 +2275,10 @@ std::unique_ptr<ast::VariableDeclStatement> ParserImpl::variable_stmt() {
|
||||||
// if_stmt
|
// if_stmt
|
||||||
// : IF paren_rhs_stmt body_stmt elseif_stmt? else_stmt?
|
// : IF paren_rhs_stmt body_stmt elseif_stmt? else_stmt?
|
||||||
std::unique_ptr<ast::IfStatement> ParserImpl::if_stmt() {
|
std::unique_ptr<ast::IfStatement> ParserImpl::if_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsIf())
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
auto source = t.source();
|
if (!match(Token::Type::kIf))
|
||||||
next(); // Consume the peek
|
return nullptr;
|
||||||
|
|
||||||
auto condition = paren_rhs_stmt();
|
auto condition = paren_rhs_stmt();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
|
@ -2393,12 +2365,10 @@ std::unique_ptr<ast::ElseStatement> ParserImpl::else_stmt() {
|
||||||
// switch_stmt
|
// switch_stmt
|
||||||
// : SWITCH paren_rhs_stmt BRACKET_LEFT switch_body+ BRACKET_RIGHT
|
// : SWITCH paren_rhs_stmt BRACKET_LEFT switch_body+ BRACKET_RIGHT
|
||||||
std::unique_ptr<ast::SwitchStatement> ParserImpl::switch_stmt() {
|
std::unique_ptr<ast::SwitchStatement> ParserImpl::switch_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsSwitch())
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
auto source = t.source();
|
if (!match(Token::Type::kSwitch))
|
||||||
next(); // Consume the peek
|
return nullptr;
|
||||||
|
|
||||||
auto condition = paren_rhs_stmt();
|
auto condition = paren_rhs_stmt();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
|
@ -2408,7 +2378,7 @@ std::unique_ptr<ast::SwitchStatement> ParserImpl::switch_stmt() {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = next();
|
auto t = next();
|
||||||
if (!t.IsBraceLeft()) {
|
if (!t.IsBraceLeft()) {
|
||||||
add_error(t, "missing { for switch statement");
|
add_error(t, "missing { for switch statement");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2543,14 +2513,12 @@ std::unique_ptr<ast::BlockStatement> ParserImpl::case_body() {
|
||||||
// loop_stmt
|
// loop_stmt
|
||||||
// : LOOP BRACKET_LEFT statements continuing_stmt? BRACKET_RIGHT
|
// : LOOP BRACKET_LEFT statements continuing_stmt? BRACKET_RIGHT
|
||||||
std::unique_ptr<ast::LoopStatement> ParserImpl::loop_stmt() {
|
std::unique_ptr<ast::LoopStatement> ParserImpl::loop_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsLoop())
|
|
||||||
|
if (!match(Token::Type::kLoop))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto source = t.source();
|
auto t = next();
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
t = next();
|
|
||||||
if (!t.IsBraceLeft()) {
|
if (!t.IsBraceLeft()) {
|
||||||
add_error(t, "missing { for loop");
|
add_error(t, "missing { for loop");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2641,14 +2609,12 @@ std::unique_ptr<ForHeader> ParserImpl::for_header() {
|
||||||
// for_statement
|
// for_statement
|
||||||
// : FOR PAREN_LEFT for_header PAREN_RIGHT BRACE_LEFT statements BRACE_RIGHT
|
// : FOR PAREN_LEFT for_header PAREN_RIGHT BRACE_LEFT statements BRACE_RIGHT
|
||||||
std::unique_ptr<ast::Statement> ParserImpl::for_stmt() {
|
std::unique_ptr<ast::Statement> ParserImpl::for_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsFor())
|
|
||||||
|
if (!match(Token::Type::kFor))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto source = t.source();
|
auto t = next();
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
t = next();
|
|
||||||
if (!t.IsParenLeft()) {
|
if (!t.IsParenLeft()) {
|
||||||
add_error(t, "missing for loop (");
|
add_error(t, "missing for loop (");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2760,33 +2726,30 @@ std::unique_ptr<ast::CallStatement> ParserImpl::func_call_stmt() {
|
||||||
// break_stmt
|
// break_stmt
|
||||||
// : BREAK
|
// : BREAK
|
||||||
std::unique_ptr<ast::BreakStatement> ParserImpl::break_stmt() {
|
std::unique_ptr<ast::BreakStatement> ParserImpl::break_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsBreak())
|
|
||||||
|
if (!match(Token::Type::kBreak))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
next(); // Consume the peek
|
return std::make_unique<ast::BreakStatement>(source);
|
||||||
return std::make_unique<ast::BreakStatement>(t.source());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// continue_stmt
|
// continue_stmt
|
||||||
// : CONTINUE
|
// : CONTINUE
|
||||||
std::unique_ptr<ast::ContinueStatement> ParserImpl::continue_stmt() {
|
std::unique_ptr<ast::ContinueStatement> ParserImpl::continue_stmt() {
|
||||||
auto t = peek();
|
auto source = peek().source();
|
||||||
if (!t.IsContinue())
|
|
||||||
|
if (!match(Token::Type::kContinue))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
next(); // Consume the peek
|
return std::make_unique<ast::ContinueStatement>(source);
|
||||||
return std::make_unique<ast::ContinueStatement>(t.source());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// continuing_stmt
|
// continuing_stmt
|
||||||
// : CONTINUING body_stmt
|
// : CONTINUING body_stmt
|
||||||
std::unique_ptr<ast::BlockStatement> ParserImpl::continuing_stmt() {
|
std::unique_ptr<ast::BlockStatement> ParserImpl::continuing_stmt() {
|
||||||
auto t = peek();
|
if (!match(Token::Type::kContinuing))
|
||||||
if (!t.IsContinuing()) {
|
|
||||||
return std::make_unique<ast::BlockStatement>();
|
return std::make_unique<ast::BlockStatement>();
|
||||||
}
|
|
||||||
next(); // Consume the peek
|
|
||||||
|
|
||||||
return body_stmt();
|
return body_stmt();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue