Parser.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <AK/HashTable.h>
  28. #include <AK/NonnullRefPtr.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibJS/AST.h>
  31. #include <LibJS/Lexer.h>
  32. #include <stdio.h>
  33. namespace JS {
  34. enum class Associativity {
  35. Left,
  36. Right
  37. };
  38. struct FunctionNodeParseOptions {
  39. enum {
  40. CheckForFunctionAndName = 1 << 0,
  41. AllowSuperPropertyLookup = 1 << 1,
  42. AllowSuperConstructorCall = 1 << 2,
  43. IsGetterFunction = 1 << 3,
  44. IsSetterFunction = 1 << 4,
  45. };
  46. };
  47. class Parser {
  48. public:
  49. explicit Parser(Lexer lexer);
  50. NonnullRefPtr<Program> parse_program();
  51. template<typename FunctionNodeType>
  52. NonnullRefPtr<FunctionNodeType> parse_function_node(u8 parse_options = FunctionNodeParseOptions::CheckForFunctionAndName);
  53. Vector<FunctionNode::Parameter> parse_function_parameters(int& function_length, u8 parse_options = 0);
  54. NonnullRefPtr<Declaration> parse_declaration();
  55. NonnullRefPtr<Statement> parse_statement();
  56. NonnullRefPtr<BlockStatement> parse_block_statement();
  57. NonnullRefPtr<BlockStatement> parse_block_statement(bool& is_strict);
  58. NonnullRefPtr<ReturnStatement> parse_return_statement();
  59. NonnullRefPtr<VariableDeclaration> parse_variable_declaration(bool with_semicolon = true);
  60. NonnullRefPtr<Statement> parse_for_statement();
  61. NonnullRefPtr<Statement> parse_for_in_of_statement(NonnullRefPtr<ASTNode> lhs);
  62. NonnullRefPtr<IfStatement> parse_if_statement();
  63. NonnullRefPtr<ThrowStatement> parse_throw_statement();
  64. NonnullRefPtr<TryStatement> parse_try_statement();
  65. NonnullRefPtr<CatchClause> parse_catch_clause();
  66. NonnullRefPtr<SwitchStatement> parse_switch_statement();
  67. NonnullRefPtr<SwitchCase> parse_switch_case();
  68. NonnullRefPtr<BreakStatement> parse_break_statement();
  69. NonnullRefPtr<ContinueStatement> parse_continue_statement();
  70. NonnullRefPtr<DoWhileStatement> parse_do_while_statement();
  71. NonnullRefPtr<WhileStatement> parse_while_statement();
  72. NonnullRefPtr<DebuggerStatement> parse_debugger_statement();
  73. NonnullRefPtr<ConditionalExpression> parse_conditional_expression(NonnullRefPtr<Expression> test);
  74. NonnullRefPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right, Vector<TokenType> forbidden = {});
  75. NonnullRefPtr<Expression> parse_primary_expression();
  76. NonnullRefPtr<Expression> parse_unary_prefixed_expression();
  77. NonnullRefPtr<RegExpLiteral> parse_regexp_literal();
  78. NonnullRefPtr<ObjectExpression> parse_object_expression();
  79. NonnullRefPtr<ArrayExpression> parse_array_expression();
  80. NonnullRefPtr<StringLiteral> parse_string_literal(Token token);
  81. NonnullRefPtr<TemplateLiteral> parse_template_literal(bool is_tagged);
  82. NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
  83. NonnullRefPtr<CallExpression> parse_call_expression(NonnullRefPtr<Expression>);
  84. NonnullRefPtr<NewExpression> parse_new_expression();
  85. RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);
  86. RefPtr<Statement> try_parse_labelled_statement();
  87. NonnullRefPtr<ClassDeclaration> parse_class_declaration();
  88. NonnullRefPtr<ClassExpression> parse_class_expression(bool expect_class_name);
  89. NonnullRefPtr<Expression> parse_property_key();
  90. NonnullRefPtr<AssignmentExpression> parse_assignment_expression(AssignmentOp, NonnullRefPtr<Expression> lhs, int min_precedence, Associativity);
  91. struct Error {
  92. String message;
  93. size_t line;
  94. size_t column;
  95. String to_string() const
  96. {
  97. if (line == 0 || column == 0)
  98. return message;
  99. return String::formatted("{} (line: {}, column: {})", message, line, column);
  100. }
  101. String source_location_hint(const StringView& source, const char spacer = ' ', const char indicator = '^') const
  102. {
  103. if (line == 0 || column == 0)
  104. return {};
  105. // We need to modify the source to match what the lexer considers one line - normalizing
  106. // line terminators to \n is easier than splitting using all different LT characters.
  107. String source_string { source };
  108. source_string.replace("\r\n", "\n");
  109. source_string.replace("\r", "\n");
  110. source_string.replace(LINE_SEPARATOR, "\n");
  111. source_string.replace(PARAGRAPH_SEPARATOR, "\n");
  112. StringBuilder builder;
  113. builder.append(source_string.split_view('\n', true)[line - 1]);
  114. builder.append('\n');
  115. for (size_t i = 0; i < column - 1; ++i)
  116. builder.append(spacer);
  117. builder.append(indicator);
  118. return builder.build();
  119. }
  120. };
  121. bool has_errors() const { return m_parser_state.m_errors.size(); }
  122. const Vector<Error>& errors() const { return m_parser_state.m_errors; }
  123. void print_errors() const
  124. {
  125. for (auto& error : m_parser_state.m_errors)
  126. fprintf(stderr, "SyntaxError: %s\n", error.to_string().characters());
  127. }
  128. private:
  129. friend class ScopePusher;
  130. Associativity operator_associativity(TokenType) const;
  131. bool match_expression() const;
  132. bool match_unary_prefixed_expression() const;
  133. bool match_secondary_expression(Vector<TokenType> forbidden = {}) const;
  134. bool match_statement() const;
  135. bool match_declaration() const;
  136. bool match_variable_declaration() const;
  137. bool match_identifier_name() const;
  138. bool match_property_key() const;
  139. bool match(TokenType type) const;
  140. bool done() const;
  141. void expected(const char* what);
  142. void syntax_error(const String& message, size_t line = 0, size_t column = 0);
  143. Token consume();
  144. Token consume(TokenType type);
  145. Token consume_and_validate_numeric_literal();
  146. void consume_or_insert_semicolon();
  147. void save_state();
  148. void load_state();
  149. enum class UseStrictDirectiveState {
  150. None,
  151. Looking,
  152. Found,
  153. };
  154. struct ParserState {
  155. Lexer m_lexer;
  156. Token m_current_token;
  157. Vector<Error> m_errors;
  158. Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes;
  159. Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes;
  160. Vector<NonnullRefPtrVector<FunctionDeclaration>> m_function_scopes;
  161. UseStrictDirectiveState m_use_strict_directive { UseStrictDirectiveState::None };
  162. HashTable<StringView> m_labels_in_scope;
  163. bool m_strict_mode { false };
  164. bool m_allow_super_property_lookup { false };
  165. bool m_allow_super_constructor_call { false };
  166. bool m_in_function_context { false };
  167. bool m_in_break_context { false };
  168. bool m_in_continue_context { false };
  169. explicit ParserState(Lexer);
  170. };
  171. ParserState m_parser_state;
  172. Vector<ParserState> m_saved_state;
  173. };
  174. }