Lexer.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 is_eof() const;
  25. bool is_line_terminator() const;
  26. bool is_identifier_start() const;
  27. bool is_identifier_middle() const;
  28. bool is_line_comment_start(bool line_has_token_yet) const;
  29. bool is_block_comment_start() const;
  30. bool is_block_comment_end() const;
  31. bool is_numeric_literal_start() const;
  32. bool match(char, char) const;
  33. bool match(char, char, char) const;
  34. bool match(char, char, char, char) const;
  35. bool slash_means_division() const;
  36. StringView m_source;
  37. size_t m_position { 0 };
  38. Token m_current_token;
  39. char m_current_char { 0 };
  40. bool m_eof { false };
  41. StringView m_filename;
  42. size_t m_line_number { 1 };
  43. size_t m_line_column { 0 };
  44. bool m_regex_is_in_character_class { false };
  45. struct TemplateState {
  46. bool in_expr;
  47. u8 open_bracket_count;
  48. };
  49. Vector<TemplateState> m_template_states;
  50. static HashMap<String, TokenType> s_keywords;
  51. static HashMap<String, TokenType> s_three_char_tokens;
  52. static HashMap<String, TokenType> s_two_char_tokens;
  53. static HashMap<char, TokenType> s_single_char_tokens;
  54. };
  55. }