Parser.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <LibSQL/AST/AST.h>
  11. #include <LibSQL/AST/Lexer.h>
  12. #include <LibSQL/AST/Token.h>
  13. namespace SQL::AST {
  14. namespace Limits {
  15. // https://www.sqlite.org/limits.html
  16. constexpr size_t maximum_expression_tree_depth = 1000;
  17. constexpr size_t maximum_subquery_depth = 100;
  18. }
  19. class Parser {
  20. struct Error {
  21. String message;
  22. SourcePosition position;
  23. String to_string() const
  24. {
  25. return String::formatted("{} (line: {}, column: {})", message, position.line, position.column);
  26. }
  27. };
  28. public:
  29. explicit Parser(Lexer lexer);
  30. NonnullRefPtr<Statement> next_statement();
  31. bool has_errors() const { return m_parser_state.m_errors.size(); }
  32. Vector<Error> const& errors() const { return m_parser_state.m_errors; }
  33. protected:
  34. NonnullRefPtr<Expression> parse_expression(); // Protected for unit testing.
  35. private:
  36. struct ParserState {
  37. explicit ParserState(Lexer);
  38. Lexer m_lexer;
  39. Token m_token;
  40. Vector<Error> m_errors;
  41. size_t m_current_expression_depth { 0 };
  42. size_t m_current_subquery_depth { 0 };
  43. };
  44. NonnullRefPtr<Statement> parse_statement();
  45. NonnullRefPtr<Statement> parse_statement_with_expression_list(RefPtr<CommonTableExpressionList>);
  46. NonnullRefPtr<CreateSchema> parse_create_schema_statement();
  47. NonnullRefPtr<CreateTable> parse_create_table_statement();
  48. NonnullRefPtr<AlterTable> parse_alter_table_statement();
  49. NonnullRefPtr<DropTable> parse_drop_table_statement();
  50. NonnullRefPtr<DescribeTable> parse_describe_table_statement();
  51. NonnullRefPtr<Insert> parse_insert_statement(RefPtr<CommonTableExpressionList>);
  52. NonnullRefPtr<Update> parse_update_statement(RefPtr<CommonTableExpressionList>);
  53. NonnullRefPtr<Delete> parse_delete_statement(RefPtr<CommonTableExpressionList>);
  54. NonnullRefPtr<Select> parse_select_statement(RefPtr<CommonTableExpressionList>);
  55. RefPtr<CommonTableExpressionList> parse_common_table_expression_list();
  56. NonnullRefPtr<Expression> parse_primary_expression();
  57. NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression> primary);
  58. bool match_secondary_expression() const;
  59. RefPtr<Expression> parse_literal_value_expression();
  60. RefPtr<Expression> parse_column_name_expression(String with_parsed_identifier = {}, bool with_parsed_period = false);
  61. RefPtr<Expression> parse_unary_operator_expression();
  62. RefPtr<Expression> parse_binary_operator_expression(NonnullRefPtr<Expression> lhs);
  63. RefPtr<Expression> parse_chained_expression();
  64. RefPtr<Expression> parse_cast_expression();
  65. RefPtr<Expression> parse_case_expression();
  66. RefPtr<Expression> parse_exists_expression(bool invert_expression, TokenType opening_token = TokenType::Exists);
  67. RefPtr<Expression> parse_collate_expression(NonnullRefPtr<Expression> expression);
  68. RefPtr<Expression> parse_is_expression(NonnullRefPtr<Expression> expression);
  69. RefPtr<Expression> parse_match_expression(NonnullRefPtr<Expression> lhs, bool invert_expression);
  70. RefPtr<Expression> parse_null_expression(NonnullRefPtr<Expression> expression, bool invert_expression);
  71. RefPtr<Expression> parse_between_expression(NonnullRefPtr<Expression> expression, bool invert_expression);
  72. RefPtr<Expression> parse_in_expression(NonnullRefPtr<Expression> expression, bool invert_expression);
  73. NonnullRefPtr<ColumnDefinition> parse_column_definition();
  74. NonnullRefPtr<TypeName> parse_type_name();
  75. NonnullRefPtr<SignedNumber> parse_signed_number();
  76. NonnullRefPtr<CommonTableExpression> parse_common_table_expression();
  77. NonnullRefPtr<QualifiedTableName> parse_qualified_table_name();
  78. NonnullRefPtr<ReturningClause> parse_returning_clause();
  79. NonnullRefPtr<ResultColumn> parse_result_column();
  80. NonnullRefPtr<TableOrSubquery> parse_table_or_subquery();
  81. NonnullRefPtr<OrderingTerm> parse_ordering_term();
  82. void parse_schema_and_table_name(String& schema_name, String& table_name);
  83. ConflictResolution parse_conflict_resolution();
  84. template<typename ParseCallback>
  85. void parse_comma_separated_list(bool surrounded_by_parentheses, ParseCallback&& parse_callback)
  86. {
  87. if (surrounded_by_parentheses)
  88. consume(TokenType::ParenOpen);
  89. while (!has_errors() && !match(TokenType::Eof)) {
  90. parse_callback();
  91. if (!match(TokenType::Comma))
  92. break;
  93. consume(TokenType::Comma);
  94. };
  95. if (surrounded_by_parentheses)
  96. consume(TokenType::ParenClose);
  97. }
  98. Token consume();
  99. Token consume(TokenType type);
  100. bool consume_if(TokenType type);
  101. bool match(TokenType type) const;
  102. void expected(StringView what);
  103. void syntax_error(String message);
  104. SourcePosition position() const;
  105. ParserState m_parser_state;
  106. };
  107. }