Lexer.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include "Token.h"
  28. #include <AK/HashMap.h>
  29. #include <AK/String.h>
  30. #include <AK/StringView.h>
  31. namespace JS {
  32. class Lexer {
  33. public:
  34. explicit Lexer(StringView source);
  35. Token next();
  36. private:
  37. void consume();
  38. void consume_exponent();
  39. bool is_eof() const;
  40. bool is_identifier_start() const;
  41. bool is_identifier_middle() const;
  42. bool is_line_comment_start() const;
  43. bool is_block_comment_start() const;
  44. bool is_block_comment_end() const;
  45. bool is_numeric_literal_start() const;
  46. bool match(char, char) const;
  47. bool match(char, char, char) const;
  48. bool match(char, char, char, char) const;
  49. bool slash_means_division() const;
  50. StringView m_source;
  51. size_t m_position { 0 };
  52. Token m_current_token;
  53. int m_current_char { 0 };
  54. size_t m_line_number { 1 };
  55. size_t m_line_column { 0 };
  56. bool m_regex_is_in_character_class { false };
  57. struct TemplateState {
  58. bool in_expr;
  59. u8 open_bracket_count;
  60. };
  61. Vector<TemplateState> m_template_states;
  62. static HashMap<String, 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. };
  67. }