From 5a9c5b7d5028dd81944d7c7802db246076cfdd6f Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 28 Apr 2024 00:19:17 +0200 Subject: [PATCH] Better highlighting of keywords --- src/Parsers/ExpressionElementParsers.cpp | 38 +++++++++++------------ src/Parsers/ExpressionListParsers.cpp | 4 +-- src/Parsers/ParserAlterQuery.cpp | 14 ++++----- src/Parsers/ParserCreateQuery.cpp | 4 +-- src/Parsers/ParserDataType.cpp | 8 ++--- src/Parsers/ParserDictionary.cpp | 22 ++++++------- src/Parsers/ParserRenameQuery.cpp | 3 +- src/Parsers/ParserShowTablesQuery.cpp | 2 +- src/Parsers/ParserTablesInSelectQuery.cpp | 34 ++++++++++---------- src/Parsers/ParserTablesInSelectQuery.h | 2 -- 10 files changed, 64 insertions(+), 67 deletions(-) diff --git a/src/Parsers/ExpressionElementParsers.cpp b/src/Parsers/ExpressionElementParsers.cpp index d6593aa3365..de926e83024 100644 --- a/src/Parsers/ExpressionElementParsers.cpp +++ b/src/Parsers/ExpressionElementParsers.cpp @@ -2085,28 +2085,28 @@ bool ParserOrderByElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expect int direction = 1; - if (descending.ignore(pos) || desc.ignore(pos)) + if (descending.ignore(pos, expected) || desc.ignore(pos, expected)) direction = -1; else - ascending.ignore(pos) || asc.ignore(pos); + ascending.ignore(pos, expected) || asc.ignore(pos, expected); int nulls_direction = direction; bool nulls_direction_was_explicitly_specified = false; - if (nulls.ignore(pos)) + if (nulls.ignore(pos, expected)) { nulls_direction_was_explicitly_specified = true; - if (first.ignore(pos)) + if (first.ignore(pos, expected)) nulls_direction = -direction; - else if (last.ignore(pos)) + else if (last.ignore(pos, expected)) ; else return false; } ASTPtr locale_node; - if (collate.ignore(pos)) + if (collate.ignore(pos, expected)) { if (!collate_locale_parser.parse(pos, locale_node, expected)) return false; @@ -2117,16 +2117,16 @@ bool ParserOrderByElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expect ASTPtr fill_from; ASTPtr fill_to; ASTPtr fill_step; - if (with_fill.ignore(pos)) + if (with_fill.ignore(pos, expected)) { has_with_fill = true; - if (from.ignore(pos) && !exp_parser.parse(pos, fill_from, expected)) + if (from.ignore(pos, expected) && !exp_parser.parse(pos, fill_from, expected)) return false; - if (to.ignore(pos) && !exp_parser.parse(pos, fill_to, expected)) + if (to.ignore(pos, expected) && !exp_parser.parse(pos, fill_to, expected)) return false; - if (step.ignore(pos) && !exp_parser.parse(pos, fill_step, expected)) + if (step.ignore(pos, expected) && !exp_parser.parse(pos, fill_step, expected)) return false; } @@ -2254,27 +2254,27 @@ bool ParserTTLElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) DataDestinationType destination_type = DataDestinationType::DELETE; String destination_name; - if (s_to_disk.ignore(pos)) + if (s_to_disk.ignore(pos, expected)) { mode = TTLMode::MOVE; destination_type = DataDestinationType::DISK; } - else if (s_to_volume.ignore(pos)) + else if (s_to_volume.ignore(pos, expected)) { mode = TTLMode::MOVE; destination_type = DataDestinationType::VOLUME; } - else if (s_group_by.ignore(pos)) + else if (s_group_by.ignore(pos, expected)) { mode = TTLMode::GROUP_BY; } - else if (s_recompress.ignore(pos)) + else if (s_recompress.ignore(pos, expected)) { mode = TTLMode::RECOMPRESS; } else { - s_delete.ignore(pos); + s_delete.ignore(pos, expected); mode = TTLMode::DELETE; } @@ -2286,7 +2286,7 @@ bool ParserTTLElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (mode == TTLMode::MOVE) { - if (s_if_exists.ignore(pos)) + if (s_if_exists.ignore(pos, expected)) if_exists = true; ASTPtr ast_space_name; @@ -2300,7 +2300,7 @@ bool ParserTTLElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!parser_keys_list.parse(pos, group_by_key, expected)) return false; - if (s_set.ignore(pos)) + if (s_set.ignore(pos, expected)) { ParserList parser_assignment_list( std::make_unique(), std::make_unique(TokenType::Comma)); @@ -2309,14 +2309,14 @@ bool ParserTTLElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) return false; } } - else if (mode == TTLMode::DELETE && s_where.ignore(pos)) + else if (mode == TTLMode::DELETE && s_where.ignore(pos, expected)) { if (!parser_exp.parse(pos, where_expr, expected)) return false; } else if (mode == TTLMode::RECOMPRESS) { - if (!s_codec.ignore(pos)) + if (!s_codec.ignore(pos, expected)) return false; if (!parser_codec.parse(pos, recompression_codec, expected)) diff --git a/src/Parsers/ExpressionListParsers.cpp b/src/Parsers/ExpressionListParsers.cpp index 276b4e82074..007c2855a13 100644 --- a/src/Parsers/ExpressionListParsers.cpp +++ b/src/Parsers/ExpressionListParsers.cpp @@ -416,13 +416,13 @@ bool ParserKeyValuePair::parseImpl(Pos & pos, ASTPtr & node, Expected & expected ParserToken open(TokenType::OpeningRoundBracket); ParserToken close(TokenType::ClosingRoundBracket); - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!kv_pairs_list.parse(pos, value, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; with_brackets = true; diff --git a/src/Parsers/ParserAlterQuery.cpp b/src/Parsers/ParserAlterQuery.cpp index 1baff45113b..6f48f79d942 100644 --- a/src/Parsers/ParserAlterQuery.cpp +++ b/src/Parsers/ParserAlterQuery.cpp @@ -495,11 +495,11 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected command->type = ASTAlterCommand::MOVE_PARTITION; command->part = true; - if (s_to_disk.ignore(pos)) + if (s_to_disk.ignore(pos, expected)) command->move_destination_type = DataDestinationType::DISK; - else if (s_to_volume.ignore(pos)) + else if (s_to_volume.ignore(pos, expected)) command->move_destination_type = DataDestinationType::VOLUME; - else if (s_to_shard.ignore(pos)) + else if (s_to_shard.ignore(pos, expected)) { command->move_destination_type = DataDestinationType::SHARD; } @@ -519,11 +519,11 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected command->type = ASTAlterCommand::MOVE_PARTITION; - if (s_to_disk.ignore(pos)) + if (s_to_disk.ignore(pos, expected)) command->move_destination_type = DataDestinationType::DISK; - else if (s_to_volume.ignore(pos)) + else if (s_to_volume.ignore(pos, expected)) command->move_destination_type = DataDestinationType::VOLUME; - else if (s_to_table.ignore(pos)) + else if (s_to_table.ignore(pos, expected)) { if (!parseDatabaseAndTableName(pos, expected, command->to_database, command->to_table)) return false; @@ -584,7 +584,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected if (!parser_partition.parse(pos, command_partition, expected)) return false; - if (s_from.ignore(pos)) + if (s_from.ignore(pos, expected)) { if (!parseDatabaseAndTableName(pos, expected, command->from_database, command->from_table)) return false; diff --git a/src/Parsers/ParserCreateQuery.cpp b/src/Parsers/ParserCreateQuery.cpp index ff88b58760b..db773427d6f 100644 --- a/src/Parsers/ParserCreateQuery.cpp +++ b/src/Parsers/ParserCreateQuery.cpp @@ -66,13 +66,13 @@ bool ParserNestedTable::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!name_p.parse(pos, name, expected)) return false; - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!columns_p.parse(pos, columns, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; auto func = std::make_shared(); diff --git a/src/Parsers/ParserDataType.cpp b/src/Parsers/ParserDataType.cpp index fcf189e51f4..58eb00519d8 100644 --- a/src/Parsers/ParserDataType.cpp +++ b/src/Parsers/ParserDataType.cpp @@ -93,9 +93,9 @@ bool ParserDataType::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) else if (type_name_upper.find("INT") != std::string::npos) { /// Support SIGNED and UNSIGNED integer type modifiers for compatibility with MySQL - if (ParserKeyword(Keyword::SIGNED).ignore(pos)) + if (ParserKeyword(Keyword::SIGNED).ignore(pos, expected)) type_name_suffix = toStringView(Keyword::SIGNED); - else if (ParserKeyword(Keyword::UNSIGNED).ignore(pos)) + else if (ParserKeyword(Keyword::UNSIGNED).ignore(pos, expected)) type_name_suffix = toStringView(Keyword::UNSIGNED); else if (pos->type == TokenType::OpeningRoundBracket) { @@ -105,9 +105,9 @@ bool ParserDataType::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (pos->type != TokenType::ClosingRoundBracket) return false; ++pos; - if (ParserKeyword(Keyword::SIGNED).ignore(pos)) + if (ParserKeyword(Keyword::SIGNED).ignore(pos, expected)) type_name_suffix = toStringView(Keyword::SIGNED); - else if (ParserKeyword(Keyword::UNSIGNED).ignore(pos)) + else if (ParserKeyword(Keyword::UNSIGNED).ignore(pos, expected)) type_name_suffix = toStringView(Keyword::UNSIGNED); } diff --git a/src/Parsers/ParserDictionary.cpp b/src/Parsers/ParserDictionary.cpp index 539ce30e010..83a006231d9 100644 --- a/src/Parsers/ParserDictionary.cpp +++ b/src/Parsers/ParserDictionary.cpp @@ -188,7 +188,7 @@ bool ParserDictionary::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) ASTPtr ast_settings; /// Primary is required to be the first in dictionary definition - if (primary_key_keyword.ignore(pos)) + if (primary_key_keyword.ignore(pos, expected)) { bool was_open = false; @@ -208,13 +208,13 @@ bool ParserDictionary::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!ast_source && source_keyword.ignore(pos, expected)) { - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!key_value_pairs_p.parse(pos, ast_source, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; continue; @@ -222,13 +222,13 @@ bool ParserDictionary::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!ast_lifetime && lifetime_keyword.ignore(pos, expected)) { - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!lifetime_p.parse(pos, ast_lifetime, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; continue; @@ -236,13 +236,13 @@ bool ParserDictionary::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!ast_layout && layout_keyword.ignore(pos, expected)) { - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!layout_p.parse(pos, ast_layout, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; continue; @@ -250,13 +250,13 @@ bool ParserDictionary::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!ast_range && range_keyword.ignore(pos, expected)) { - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!range_p.parse(pos, ast_range, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; continue; @@ -264,13 +264,13 @@ bool ParserDictionary::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) if (!ast_settings && settings_keyword.ignore(pos, expected)) { - if (!open.ignore(pos)) + if (!open.ignore(pos, expected)) return false; if (!settings_p.parse(pos, ast_settings, expected)) return false; - if (!close.ignore(pos)) + if (!close.ignore(pos, expected)) return false; continue; diff --git a/src/Parsers/ParserRenameQuery.cpp b/src/Parsers/ParserRenameQuery.cpp index 6b652c27f6c..e07fd7d492e 100644 --- a/src/Parsers/ParserRenameQuery.cpp +++ b/src/Parsers/ParserRenameQuery.cpp @@ -72,8 +72,7 @@ bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) else return false; - const auto ignore_delim = [&] { return exchange ? s_and.ignore(pos) : s_to.ignore(pos); }; - + const auto ignore_delim = [&] { return exchange ? s_and.ignore(pos, expected) : s_to.ignore(pos, expected); }; ASTRenameQuery::Elements elements; diff --git a/src/Parsers/ParserShowTablesQuery.cpp b/src/Parsers/ParserShowTablesQuery.cpp index 68b89bd0551..cbe748ea019 100644 --- a/src/Parsers/ParserShowTablesQuery.cpp +++ b/src/Parsers/ParserShowTablesQuery.cpp @@ -161,7 +161,7 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec } else { - if (s_temporary.ignore(pos)) + if (s_temporary.ignore(pos, expected)) query->temporary = true; if (!s_tables.ignore(pos, expected)) diff --git a/src/Parsers/ParserTablesInSelectQuery.cpp b/src/Parsers/ParserTablesInSelectQuery.cpp index ab35141403a..b4d48ae67e9 100644 --- a/src/Parsers/ParserTablesInSelectQuery.cpp +++ b/src/Parsers/ParserTablesInSelectQuery.cpp @@ -109,17 +109,17 @@ bool ParserArrayJoin::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) } -void ParserTablesInSelectQueryElement::parseJoinStrictness(Pos & pos, ASTTableJoin & table_join) +static void parseJoinStrictness(IParser::Pos & pos, ASTTableJoin & table_join, Expected & expected) { - if (ParserKeyword(Keyword::ANY).ignore(pos)) + if (ParserKeyword(Keyword::ANY).ignore(pos, expected)) table_join.strictness = JoinStrictness::Any; - else if (ParserKeyword(Keyword::ALL).ignore(pos)) + else if (ParserKeyword(Keyword::ALL).ignore(pos, expected)) table_join.strictness = JoinStrictness::All; - else if (ParserKeyword(Keyword::ASOF).ignore(pos)) + else if (ParserKeyword(Keyword::ASOF).ignore(pos, expected)) table_join.strictness = JoinStrictness::Asof; - else if (ParserKeyword(Keyword::SEMI).ignore(pos)) + else if (ParserKeyword(Keyword::SEMI).ignore(pos, expected)) table_join.strictness = JoinStrictness::Semi; - else if (ParserKeyword(Keyword::ANTI).ignore(pos) || ParserKeyword(Keyword::ONLY).ignore(pos)) + else if (ParserKeyword(Keyword::ANTI).ignore(pos, expected) || ParserKeyword(Keyword::ONLY).ignore(pos, expected)) table_join.strictness = JoinStrictness::Anti; } @@ -146,41 +146,41 @@ bool ParserTablesInSelectQueryElement::parseImpl(Pos & pos, ASTPtr & node, Expec } else { - if (ParserKeyword(Keyword::GLOBAL).ignore(pos)) + if (ParserKeyword(Keyword::GLOBAL).ignore(pos, expected)) table_join->locality = JoinLocality::Global; - else if (ParserKeyword(Keyword::LOCAL).ignore(pos)) + else if (ParserKeyword(Keyword::LOCAL).ignore(pos, expected)) table_join->locality = JoinLocality::Local; table_join->strictness = JoinStrictness::Unspecified; /// Legacy: allow JOIN type before JOIN kind - parseJoinStrictness(pos, *table_join); + parseJoinStrictness(pos, *table_join, expected); bool no_kind = false; - if (ParserKeyword(Keyword::INNER).ignore(pos)) + if (ParserKeyword(Keyword::INNER).ignore(pos, expected)) table_join->kind = JoinKind::Inner; - else if (ParserKeyword(Keyword::LEFT).ignore(pos)) + else if (ParserKeyword(Keyword::LEFT).ignore(pos, expected)) table_join->kind = JoinKind::Left; - else if (ParserKeyword(Keyword::RIGHT).ignore(pos)) + else if (ParserKeyword(Keyword::RIGHT).ignore(pos, expected)) table_join->kind = JoinKind::Right; - else if (ParserKeyword(Keyword::FULL).ignore(pos)) + else if (ParserKeyword(Keyword::FULL).ignore(pos, expected)) table_join->kind = JoinKind::Full; - else if (ParserKeyword(Keyword::CROSS).ignore(pos)) + else if (ParserKeyword(Keyword::CROSS).ignore(pos, expected)) table_join->kind = JoinKind::Cross; - else if (ParserKeyword(Keyword::PASTE).ignore(pos)) + else if (ParserKeyword(Keyword::PASTE).ignore(pos, expected)) table_join->kind = JoinKind::Paste; else no_kind = true; /// Standard position: JOIN type after JOIN kind - parseJoinStrictness(pos, *table_join); + parseJoinStrictness(pos, *table_join, expected); /// Optional OUTER keyword for outer joins. if (table_join->kind == JoinKind::Left || table_join->kind == JoinKind::Right || table_join->kind == JoinKind::Full) { - ParserKeyword(Keyword::OUTER).ignore(pos); + ParserKeyword(Keyword::OUTER).ignore(pos, expected); } if (no_kind) diff --git a/src/Parsers/ParserTablesInSelectQuery.h b/src/Parsers/ParserTablesInSelectQuery.h index 428b1482663..f7151f5b4f6 100644 --- a/src/Parsers/ParserTablesInSelectQuery.h +++ b/src/Parsers/ParserTablesInSelectQuery.h @@ -38,8 +38,6 @@ protected: private: bool is_first; bool allow_alias_without_as_keyword; - - static void parseJoinStrictness(Pos & pos, ASTTableJoin & table_join); };