Lexer.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include "Token.h"
  9. #include <AK/HashMap.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. namespace SQL::AST {
  13. class Lexer {
  14. public:
  15. explicit Lexer(StringView source);
  16. Token next();
  17. private:
  18. void consume(StringBuilder* = nullptr);
  19. bool consume_whitespace_and_comments();
  20. bool consume_numeric_literal(StringBuilder&);
  21. bool consume_string_literal(StringBuilder&);
  22. bool consume_quoted_identifier(StringBuilder&);
  23. bool consume_blob_literal(StringBuilder&);
  24. bool consume_exponent(StringBuilder&);
  25. bool consume_hexadecimal_number(StringBuilder&);
  26. bool match(char a, char b) const;
  27. bool is_identifier_start() const;
  28. bool is_identifier_middle() const;
  29. bool is_numeric_literal_start() const;
  30. bool is_string_literal_start() const;
  31. bool is_string_literal_end() const;
  32. bool is_quoted_identifier_start() const;
  33. bool is_quoted_identifier_end() const;
  34. bool is_blob_literal_start() const;
  35. bool is_line_comment_start() const;
  36. bool is_block_comment_start() const;
  37. bool is_block_comment_end() const;
  38. bool is_line_break() const;
  39. bool is_eof() const;
  40. static HashMap<String, TokenType> s_keywords;
  41. static HashMap<char, TokenType> s_one_char_tokens;
  42. static HashMap<String, TokenType> s_two_char_tokens;
  43. StringView m_source;
  44. size_t m_line_number { 1 };
  45. size_t m_line_column { 0 };
  46. char m_current_char { 0 };
  47. bool m_eof { false };
  48. size_t m_position { 0 };
  49. };
  50. }