Lexer.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  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 JS {
  12. class Lexer {
  13. public:
  14. explicit Lexer(StringView source, StringView filename = "(unknown)", size_t line_number = 1, size_t line_column = 0);
  15. Token next();
  16. const StringView& source() const { return m_source; };
  17. const StringView& filename() const { return m_filename; };
  18. private:
  19. void consume();
  20. bool consume_exponent();
  21. bool consume_octal_number();
  22. bool consume_hexadecimal_number();
  23. bool consume_binary_number();
  24. bool consume_decimal_number();
  25. bool is_eof() const;
  26. bool is_line_terminator() const;
  27. bool is_identifier_start() const;
  28. bool is_identifier_middle() const;
  29. bool is_line_comment_start(bool line_has_token_yet) const;
  30. bool is_block_comment_start() const;
  31. bool is_block_comment_end() const;
  32. bool is_numeric_literal_start() const;
  33. bool match(char, char) const;
  34. bool match(char, char, char) const;
  35. bool match(char, char, char, char) const;
  36. template<typename Callback>
  37. bool match_numeric_literal_separator_followed_by(Callback) const;
  38. bool slash_means_division() const;
  39. StringView m_source;
  40. size_t m_position { 0 };
  41. Token m_current_token;
  42. char m_current_char { 0 };
  43. bool m_eof { false };
  44. StringView m_filename;
  45. size_t m_line_number { 1 };
  46. size_t m_line_column { 0 };
  47. bool m_regex_is_in_character_class { false };
  48. struct TemplateState {
  49. bool in_expr;
  50. u8 open_bracket_count;
  51. };
  52. Vector<TemplateState> m_template_states;
  53. static HashMap<String, TokenType> s_keywords;
  54. static HashMap<String, TokenType> s_three_char_tokens;
  55. static HashMap<String, TokenType> s_two_char_tokens;
  56. static HashMap<char, TokenType> s_single_char_tokens;
  57. };
  58. }