Parser.h 14 KB

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