diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-04-21 09:38:07 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-21 21:37:55 +0200 |
commit | a11f49f627da40e11843f419e8b9c2b01e5f7a04 (patch) | |
tree | 63a57920645cdcbd6de2055dcbe4783858287585 | |
parent | 912c9c93e9ec770a8e805357eb34fbde821619b3 (diff) | |
download | serenity-a11f49f627da40e11843f419e8b9c2b01e5f7a04.zip |
LibSQL: Add Parser::consume_if helper
The following is a common (and soon to be *very* common) expression:
if (match(token_type))
consume();
Using consume_if() makes this a bit simpler and makes it less likely to
forget to invoke consume() after the match().
-rw-r--r-- | Userland/Libraries/LibSQL/Parser.cpp | 40 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/Parser.h | 1 |
2 files changed, 20 insertions, 21 deletions
diff --git a/Userland/Libraries/LibSQL/Parser.cpp b/Userland/Libraries/LibSQL/Parser.cpp index f5e4f94896..fb25d1cddd 100644 --- a/Userland/Libraries/LibSQL/Parser.cpp +++ b/Userland/Libraries/LibSQL/Parser.cpp @@ -52,16 +52,13 @@ NonnullRefPtr<CreateTable> Parser::parse_create_table_statement() consume(TokenType::Create); bool is_temporary = false; - if (match(TokenType::Temp) || match(TokenType::Temporary)) { - consume(); + if (consume_if(TokenType::Temp) || consume_if(TokenType::Temporary)) is_temporary = true; - } consume(TokenType::Table); bool is_error_if_table_exists = true; - if (match(TokenType::If)) { - consume(TokenType::If); + if (consume_if(TokenType::If)) { consume(TokenType::Not); consume(TokenType::Exists); is_error_if_table_exists = false; @@ -71,8 +68,7 @@ NonnullRefPtr<CreateTable> Parser::parse_create_table_statement() String schema_name; String table_name; - if (match(TokenType::Period)) { - consume(); + if (consume_if(TokenType::Period)) { schema_name = move(schema_or_table_name); table_name = consume(TokenType::Identifier).value(); } else { @@ -107,8 +103,7 @@ NonnullRefPtr<DropTable> Parser::parse_drop_table_statement() consume(TokenType::Table); bool is_error_if_table_does_not_exist = true; - if (match(TokenType::If)) { - consume(TokenType::If); + if (consume_if(TokenType::If)) { consume(TokenType::Exists); is_error_if_table_does_not_exist = false; } @@ -117,8 +112,7 @@ NonnullRefPtr<DropTable> Parser::parse_drop_table_statement() String schema_name; String table_name; - if (match(TokenType::Period)) { - consume(); + if (consume_if(TokenType::Period)) { schema_name = move(schema_or_table_name); table_name = consume(TokenType::Identifier).value(); } else { @@ -151,14 +145,11 @@ NonnullRefPtr<TypeName> Parser::parse_type_name() auto name = consume(TokenType::Identifier).value(); NonnullRefPtrVector<SignedNumber> signed_numbers; - if (match(TokenType::ParenOpen)) { - consume(); + if (consume_if(TokenType::ParenOpen)) { signed_numbers.append(parse_signed_number()); - if (match(TokenType::Comma)) { - consume(); + if (consume_if(TokenType::Comma)) signed_numbers.append(parse_signed_number()); - } consume(TokenType::ParenClose); } @@ -171,12 +162,10 @@ NonnullRefPtr<SignedNumber> Parser::parse_signed_number() // https://sqlite.org/syntax/signed-number.html bool is_positive = true; - if (match(TokenType::Plus)) { - consume(); - } else if (match(TokenType::Minus)) { + if (consume_if(TokenType::Plus)) + is_positive = true; + else if (consume_if(TokenType::Minus)) is_positive = false; - consume(); - } if (match(TokenType::NumericLiteral)) { auto number = consume(TokenType::NumericLiteral).double_value(); @@ -202,6 +191,15 @@ Token Parser::consume(TokenType expected_type) return consume(); } +bool Parser::consume_if(TokenType expected_type) +{ + if (!match(expected_type)) + return false; + + consume(); + return true; +} + bool Parser::match(TokenType type) const { return m_parser_state.m_token.type() == type; diff --git a/Userland/Libraries/LibSQL/Parser.h b/Userland/Libraries/LibSQL/Parser.h index 379ddef1d5..26fb7d2a36 100644 --- a/Userland/Libraries/LibSQL/Parser.h +++ b/Userland/Libraries/LibSQL/Parser.h @@ -75,6 +75,7 @@ private: Token consume(); Token consume(TokenType type); + bool consume_if(TokenType type); bool match(TokenType type) const; void expected(StringView what); |