Parser.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "AST.h"
  28. #include "Lexer.h"
  29. #include <AK/NonnullRefPtr.h>
  30. #include <stdio.h>
  31. namespace JS {
  32. enum class Associativity {
  33. Left,
  34. Right
  35. };
  36. class Parser {
  37. public:
  38. explicit Parser(Lexer lexer);
  39. NonnullRefPtr<Program> parse_program();
  40. template<typename FunctionNodeType>
  41. NonnullRefPtr<FunctionNodeType> parse_function_node(bool need_function_keyword = true);
  42. NonnullRefPtr<Statement> parse_statement();
  43. NonnullRefPtr<BlockStatement> parse_block_statement();
  44. NonnullRefPtr<ReturnStatement> parse_return_statement();
  45. NonnullRefPtr<VariableDeclaration> parse_variable_declaration();
  46. NonnullRefPtr<ForStatement> parse_for_statement();
  47. NonnullRefPtr<IfStatement> parse_if_statement();
  48. NonnullRefPtr<ThrowStatement> parse_throw_statement();
  49. NonnullRefPtr<TryStatement> parse_try_statement();
  50. NonnullRefPtr<CatchClause> parse_catch_clause();
  51. NonnullRefPtr<SwitchStatement> parse_switch_statement();
  52. NonnullRefPtr<SwitchCase> parse_switch_case();
  53. NonnullRefPtr<BreakStatement> parse_break_statement();
  54. NonnullRefPtr<ContinueStatement> parse_continue_statement();
  55. NonnullRefPtr<DoWhileStatement> parse_do_while_statement();
  56. NonnullRefPtr<WhileStatement> parse_while_statement();
  57. NonnullRefPtr<DebuggerStatement> parse_debugger_statement();
  58. NonnullRefPtr<ConditionalExpression> parse_conditional_expression(NonnullRefPtr<Expression> test);
  59. NonnullRefPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
  60. NonnullRefPtr<Expression> parse_primary_expression();
  61. NonnullRefPtr<Expression> parse_unary_prefixed_expression();
  62. NonnullRefPtr<ObjectExpression> parse_object_expression();
  63. NonnullRefPtr<ArrayExpression> parse_array_expression();
  64. NonnullRefPtr<TemplateLiteral> parse_template_literal(bool is_tagged);
  65. NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
  66. NonnullRefPtr<CallExpression> parse_call_expression(NonnullRefPtr<Expression>);
  67. NonnullRefPtr<NewExpression> parse_new_expression();
  68. RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);
  69. struct Error {
  70. String message;
  71. size_t line;
  72. size_t column;
  73. String to_string() const
  74. {
  75. if (line == 0 || column == 0)
  76. return message;
  77. return String::format("%s (line: %zu, column: %zu)", message.characters(), line, column);
  78. }
  79. };
  80. bool has_errors() const { return m_parser_state.m_errors.size(); }
  81. const Vector<Error>& errors() const { return m_parser_state.m_errors; }
  82. void print_errors() const
  83. {
  84. for (auto& error : m_parser_state.m_errors)
  85. fprintf(stderr, "SyntaxError: %s\n", error.to_string().characters());
  86. }
  87. private:
  88. friend class ScopePusher;
  89. int operator_precedence(TokenType) const;
  90. Associativity operator_associativity(TokenType) const;
  91. bool match_expression() const;
  92. bool match_unary_prefixed_expression() const;
  93. bool match_secondary_expression() const;
  94. bool match_statement() const;
  95. bool match_variable_declaration() const;
  96. bool match_identifier_name() const;
  97. bool match(TokenType type) const;
  98. bool done() const;
  99. void expected(const char* what);
  100. void syntax_error(const String& message, size_t line = 0, size_t column = 0);
  101. Token consume();
  102. Token consume(TokenType type);
  103. void consume_or_insert_semicolon();
  104. void save_state();
  105. void load_state();
  106. struct ParserState {
  107. Lexer m_lexer;
  108. Token m_current_token;
  109. Vector<Error> m_errors;
  110. Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes;
  111. Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes;
  112. explicit ParserState(Lexer);
  113. };
  114. ParserState m_parser_state;
  115. Vector<ParserState> m_saved_state;
  116. };
  117. }