Lexer.h 2.8 KB

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