Lexer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. StringView source() const { return m_source; };
  17. StringView filename() const { return m_filename; };
  18. void disallow_html_comments() { m_allow_html_comments = false; };
  19. Token force_slash_as_regex();
  20. private:
  21. void consume();
  22. bool consume_exponent();
  23. bool consume_octal_number();
  24. bool consume_hexadecimal_number();
  25. bool consume_binary_number();
  26. bool consume_decimal_number();
  27. bool is_unicode_character() const;
  28. u32 current_code_point() const;
  29. bool is_eof() const;
  30. bool is_line_terminator() const;
  31. bool is_whitespace() const;
  32. Optional<u32> is_identifier_unicode_escape(size_t& identifier_length) const;
  33. Optional<u32> is_identifier_start(size_t& identifier_length) const;
  34. Optional<u32> is_identifier_middle(size_t& identifier_length) const;
  35. bool is_line_comment_start(bool line_has_token_yet) const;
  36. bool is_block_comment_start() const;
  37. bool is_block_comment_end() const;
  38. bool is_numeric_literal_start() const;
  39. bool match(char, char) const;
  40. bool match(char, char, char) const;
  41. bool match(char, char, char, char) const;
  42. template<typename Callback>
  43. bool match_numeric_literal_separator_followed_by(Callback) const;
  44. bool slash_means_division() const;
  45. TokenType consume_regex_literal();
  46. StringView m_source;
  47. size_t m_position { 0 };
  48. Token m_current_token;
  49. char m_current_char { 0 };
  50. bool m_eof { false };
  51. StringView m_filename;
  52. size_t m_line_number { 1 };
  53. size_t m_line_column { 0 };
  54. bool m_regex_is_in_character_class { false };
  55. struct TemplateState {
  56. bool in_expr;
  57. u8 open_bracket_count;
  58. };
  59. Vector<TemplateState> m_template_states;
  60. bool m_allow_html_comments { true };
  61. Optional<size_t> m_hit_invalid_unicode;
  62. static HashMap<FlyString, TokenType> s_keywords;
  63. static HashMap<String, TokenType> s_three_char_tokens;
  64. static HashMap<String, TokenType> s_two_char_tokens;
  65. static HashMap<char, TokenType> s_single_char_tokens;
  66. struct ParsedIdentifiers : public RefCounted<ParsedIdentifiers> {
  67. // Resolved identifiers must be kept alive for the duration of the parsing stage, otherwise
  68. // the only references to these strings are deleted by the Token destructor.
  69. HashTable<FlyString> identifiers;
  70. };
  71. RefPtr<ParsedIdentifiers> m_parsed_identifiers;
  72. };
  73. }