Lexer.h 1.3 KB

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