Lexer.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. Lexer(StringView source, bool log_errors)
  36. : Lexer(source)
  37. {
  38. m_log_errors = log_errors;
  39. }
  40. Token next();
  41. bool has_errors() const { return m_has_errors; }
  42. private:
  43. void consume();
  44. void consume_exponent();
  45. bool is_eof() const;
  46. bool is_identifier_start() const;
  47. bool is_identifier_middle() const;
  48. bool is_line_comment_start() const;
  49. bool is_block_comment_start() const;
  50. bool is_block_comment_end() const;
  51. bool is_numeric_literal_start() const;
  52. bool match(char, char) const;
  53. bool match(char, char, char) const;
  54. bool match(char, char, char, char) const;
  55. void syntax_error(const char*);
  56. StringView m_source;
  57. size_t m_position = 0;
  58. Token m_current_token;
  59. int m_current_char = 0;
  60. bool m_has_errors = false;
  61. size_t m_line_number = 1;
  62. size_t m_line_column = 1;
  63. bool m_log_errors = true;
  64. static HashMap<String, TokenType> s_keywords;
  65. static HashMap<String, TokenType> s_three_char_tokens;
  66. static HashMap<String, TokenType> s_two_char_tokens;
  67. static HashMap<char, TokenType> s_single_char_tokens;
  68. };
  69. }