Parser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashTable.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibJS/AST.h>
  12. #include <LibJS/Lexer.h>
  13. #include <LibJS/SourceRange.h>
  14. #include <stdio.h>
  15. namespace JS {
  16. enum class Associativity {
  17. Left,
  18. Right
  19. };
  20. struct FunctionNodeParseOptions {
  21. enum {
  22. CheckForFunctionAndName = 1 << 0,
  23. AllowSuperPropertyLookup = 1 << 1,
  24. AllowSuperConstructorCall = 1 << 2,
  25. IsGetterFunction = 1 << 3,
  26. IsSetterFunction = 1 << 4,
  27. IsArrowFunction = 1 << 5,
  28. IsGeneratorFunction = 1 << 6,
  29. IsAsyncFunction = 1 << 7,
  30. };
  31. };
  32. class ScopePusher;
  33. class Parser {
  34. public:
  35. explicit Parser(Lexer lexer, Program::Type program_type = Program::Type::Script);
  36. NonnullRefPtr<Program> parse_program(bool starts_in_strict_mode = false);
  37. template<typename FunctionNodeType>
  38. NonnullRefPtr<FunctionNodeType> parse_function_node(u8 parse_options = FunctionNodeParseOptions::CheckForFunctionAndName);
  39. Vector<FunctionNode::Parameter> parse_formal_parameters(int& function_length, u8 parse_options = 0);
  40. enum class AllowDuplicates {
  41. Yes,
  42. No
  43. };
  44. enum class AllowMemberExpressions {
  45. Yes,
  46. No
  47. };
  48. RefPtr<BindingPattern> parse_binding_pattern(AllowDuplicates is_var_declaration = AllowDuplicates::No, AllowMemberExpressions allow_member_expressions = AllowMemberExpressions::No);
  49. struct PrimaryExpressionParseResult {
  50. NonnullRefPtr<Expression> result;
  51. bool should_continue_parsing_as_expression { true };
  52. };
  53. NonnullRefPtr<Declaration> parse_declaration();
  54. enum class AllowLabelledFunction {
  55. No,
  56. Yes
  57. };
  58. NonnullRefPtr<Statement> parse_statement(AllowLabelledFunction allow_labelled_function = AllowLabelledFunction::No);
  59. NonnullRefPtr<BlockStatement> parse_block_statement();
  60. NonnullRefPtr<FunctionBody> parse_function_body(Vector<FunctionDeclaration::Parameter> const& parameters, FunctionKind function_kind, bool& contains_direct_call_to_eval);
  61. NonnullRefPtr<ReturnStatement> parse_return_statement();
  62. NonnullRefPtr<VariableDeclaration> parse_variable_declaration(bool for_loop_variable_declaration = false);
  63. NonnullRefPtr<Statement> parse_for_statement();
  64. NonnullRefPtr<Statement> parse_for_in_of_statement(NonnullRefPtr<ASTNode> lhs);
  65. NonnullRefPtr<IfStatement> parse_if_statement();
  66. NonnullRefPtr<ThrowStatement> parse_throw_statement();
  67. NonnullRefPtr<TryStatement> parse_try_statement();
  68. NonnullRefPtr<CatchClause> parse_catch_clause();
  69. NonnullRefPtr<SwitchStatement> parse_switch_statement();
  70. NonnullRefPtr<SwitchCase> parse_switch_case();
  71. NonnullRefPtr<BreakStatement> parse_break_statement();
  72. NonnullRefPtr<ContinueStatement> parse_continue_statement();
  73. NonnullRefPtr<DoWhileStatement> parse_do_while_statement();
  74. NonnullRefPtr<WhileStatement> parse_while_statement();
  75. NonnullRefPtr<WithStatement> parse_with_statement();
  76. NonnullRefPtr<DebuggerStatement> parse_debugger_statement();
  77. NonnullRefPtr<ConditionalExpression> parse_conditional_expression(NonnullRefPtr<Expression> test);
  78. NonnullRefPtr<OptionalChain> parse_optional_chain(NonnullRefPtr<Expression> base);
  79. NonnullRefPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right, const Vector<TokenType>& forbidden = {});
  80. PrimaryExpressionParseResult parse_primary_expression();
  81. NonnullRefPtr<Expression> parse_unary_prefixed_expression();
  82. NonnullRefPtr<RegExpLiteral> parse_regexp_literal();
  83. NonnullRefPtr<ObjectExpression> parse_object_expression();
  84. NonnullRefPtr<ArrayExpression> parse_array_expression();
  85. NonnullRefPtr<StringLiteral> parse_string_literal(const Token& token, bool in_template_literal = false);
  86. NonnullRefPtr<TemplateLiteral> parse_template_literal(bool is_tagged);
  87. NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
  88. NonnullRefPtr<Expression> parse_call_expression(NonnullRefPtr<Expression>);
  89. NonnullRefPtr<NewExpression> parse_new_expression();
  90. NonnullRefPtr<ClassDeclaration> parse_class_declaration();
  91. NonnullRefPtr<ClassExpression> parse_class_expression(bool expect_class_name);
  92. NonnullRefPtr<YieldExpression> parse_yield_expression();
  93. NonnullRefPtr<AwaitExpression> parse_await_expression();
  94. NonnullRefPtr<Expression> parse_property_key();
  95. NonnullRefPtr<AssignmentExpression> parse_assignment_expression(AssignmentOp, NonnullRefPtr<Expression> lhs, int min_precedence, Associativity);
  96. NonnullRefPtr<Identifier> parse_identifier();
  97. NonnullRefPtr<ImportStatement> parse_import_statement(Program& program);
  98. NonnullRefPtr<ExportStatement> parse_export_statement(Program& program);
  99. RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);
  100. RefPtr<Statement> try_parse_labelled_statement(AllowLabelledFunction allow_function);
  101. RefPtr<MetaProperty> try_parse_new_target_expression();
  102. Vector<CallExpression::Argument> parse_arguments();
  103. struct Error {
  104. String message;
  105. Optional<Position> position;
  106. String to_string() const
  107. {
  108. if (!position.has_value())
  109. return message;
  110. return String::formatted("{} (line: {}, column: {})", message, position.value().line, position.value().column);
  111. }
  112. String source_location_hint(StringView source, const char spacer = ' ', const char indicator = '^') const
  113. {
  114. if (!position.has_value())
  115. return {};
  116. // We need to modify the source to match what the lexer considers one line - normalizing
  117. // line terminators to \n is easier than splitting using all different LT characters.
  118. String source_string = source.replace("\r\n", "\n").replace("\r", "\n").replace(LINE_SEPARATOR_STRING, "\n").replace(PARAGRAPH_SEPARATOR_STRING, "\n");
  119. StringBuilder builder;
  120. builder.append(source_string.split_view('\n', true)[position.value().line - 1]);
  121. builder.append('\n');
  122. for (size_t i = 0; i < position.value().column - 1; ++i)
  123. builder.append(spacer);
  124. builder.append(indicator);
  125. return builder.build();
  126. }
  127. };
  128. bool has_errors() const { return m_state.errors.size(); }
  129. const Vector<Error>& errors() const { return m_state.errors; }
  130. void print_errors(bool print_hint = true) const
  131. {
  132. for (auto& error : m_state.errors) {
  133. if (print_hint) {
  134. auto hint = error.source_location_hint(m_state.lexer.source());
  135. if (!hint.is_empty())
  136. warnln("{}", hint);
  137. }
  138. warnln("SyntaxError: {}", error.to_string());
  139. }
  140. }
  141. struct TokenMemoization {
  142. bool try_parse_arrow_function_expression_failed;
  143. };
  144. private:
  145. friend class ScopePusher;
  146. Associativity operator_associativity(TokenType) const;
  147. bool match_expression() const;
  148. bool match_unary_prefixed_expression() const;
  149. bool match_secondary_expression(const Vector<TokenType>& forbidden = {}) const;
  150. bool match_statement() const;
  151. bool match_export_or_import() const;
  152. bool match_declaration() const;
  153. bool try_match_let_declaration() const;
  154. bool match_variable_declaration() const;
  155. bool match_identifier() const;
  156. bool match_identifier_name() const;
  157. bool match_property_key() const;
  158. bool is_private_identifier_valid() const;
  159. bool match(TokenType type) const;
  160. bool done() const;
  161. void expected(const char* what);
  162. void syntax_error(const String& message, Optional<Position> = {});
  163. Token consume();
  164. Token consume_identifier();
  165. Token consume_identifier_reference();
  166. Token consume(TokenType type);
  167. Token consume_and_validate_numeric_literal();
  168. void consume_or_insert_semicolon();
  169. void save_state();
  170. void load_state();
  171. void discard_saved_state();
  172. Position position() const;
  173. RefPtr<BindingPattern> synthesize_binding_pattern(Expression const& expression);
  174. Token next_token() const;
  175. void check_identifier_name_for_assignment_validity(StringView, bool force_strict = false);
  176. bool try_parse_arrow_function_expression_failed_at_position(const Position&) const;
  177. void set_try_parse_arrow_function_expression_failed_at_position(const Position&, bool);
  178. bool match_invalid_escaped_keyword() const;
  179. bool parse_directive(ScopeNode& body);
  180. void parse_statement_list(ScopeNode& output_node, AllowLabelledFunction allow_labelled_functions = AllowLabelledFunction::No);
  181. struct RulePosition {
  182. AK_MAKE_NONCOPYABLE(RulePosition);
  183. AK_MAKE_NONMOVABLE(RulePosition);
  184. public:
  185. RulePosition(Parser& parser, Position position)
  186. : m_parser(parser)
  187. , m_position(position)
  188. {
  189. m_parser.m_rule_starts.append(position);
  190. }
  191. ~RulePosition()
  192. {
  193. auto last = m_parser.m_rule_starts.take_last();
  194. VERIFY(last.line == m_position.line);
  195. VERIFY(last.column == m_position.column);
  196. }
  197. const Position& position() const { return m_position; }
  198. private:
  199. Parser& m_parser;
  200. Position m_position;
  201. };
  202. [[nodiscard]] RulePosition push_start() { return { *this, position() }; }
  203. struct ParserState {
  204. Lexer lexer;
  205. Token current_token;
  206. Vector<Error> errors;
  207. ScopePusher* current_scope_pusher { nullptr };
  208. HashMap<StringView, Optional<Position>> labels_in_scope;
  209. HashTable<StringView>* referenced_private_names { nullptr };
  210. bool strict_mode { false };
  211. bool allow_super_property_lookup { false };
  212. bool allow_super_constructor_call { false };
  213. bool in_function_context { false };
  214. bool in_formal_parameter_context { false };
  215. bool in_generator_function_context { false };
  216. bool in_async_function_context { false };
  217. bool in_arrow_function_context { false };
  218. bool in_break_context { false };
  219. bool in_continue_context { false };
  220. bool string_legacy_octal_escape_sequence_in_scope { false };
  221. bool in_class_field_initializer { false };
  222. bool in_class_static_init_block { false };
  223. bool function_might_need_arguments_object { false };
  224. ParserState(Lexer, Program::Type);
  225. };
  226. class PositionKeyTraits {
  227. public:
  228. static int hash(const Position& position)
  229. {
  230. return int_hash(position.line) ^ int_hash(position.column);
  231. }
  232. static bool equals(const Position& a, const Position& b)
  233. {
  234. return a.column == b.column && a.line == b.line;
  235. }
  236. };
  237. Vector<Position> m_rule_starts;
  238. ParserState m_state;
  239. FlyString m_filename;
  240. Vector<ParserState> m_saved_state;
  241. HashMap<Position, TokenMemoization, PositionKeyTraits> m_token_memoizations;
  242. Program::Type m_program_type;
  243. };
  244. }