Serenity Operating System
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
27#pragma once
28
29#include "AST.h"
30#include "Lexer.h"
31#include <AK/NonnullRefPtr.h>
32
33namespace JS {
34
35enum class Associativity {
36 Left,
37 Right
38};
39
40class Parser {
41public:
42 explicit Parser(Lexer lexer);
43
44 NonnullRefPtr<Program> parse_program();
45
46 template<typename FunctionNodeType>
47 NonnullRefPtr<FunctionNodeType> parse_function_node();
48
49 NonnullRefPtr<Statement> parse_statement();
50 NonnullRefPtr<BlockStatement> parse_block_statement();
51 NonnullRefPtr<ReturnStatement> parse_return_statement();
52 NonnullRefPtr<VariableDeclaration> parse_variable_declaration();
53 NonnullRefPtr<ForStatement> parse_for_statement();
54 NonnullRefPtr<IfStatement> parse_if_statement();
55 NonnullRefPtr<ThrowStatement> parse_throw_statement();
56 NonnullRefPtr<TryStatement> parse_try_statement();
57 NonnullRefPtr<CatchClause> parse_catch_clause();
58 NonnullRefPtr<SwitchStatement> parse_switch_statement();
59 NonnullRefPtr<SwitchCase> parse_switch_case();
60 NonnullRefPtr<BreakStatement> parse_break_statement();
61 NonnullRefPtr<ContinueStatement> parse_continue_statement();
62 NonnullRefPtr<DoWhileStatement> parse_do_while_statement();
63 NonnullRefPtr<ConditionalExpression> parse_conditional_expression(NonnullRefPtr<Expression> test);
64
65 NonnullRefPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
66 NonnullRefPtr<Expression> parse_primary_expression();
67 NonnullRefPtr<Expression> parse_unary_prefixed_expression();
68 NonnullRefPtr<ObjectExpression> parse_object_expression();
69 NonnullRefPtr<ArrayExpression> parse_array_expression();
70 NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
71 NonnullRefPtr<CallExpression> parse_call_expression(NonnullRefPtr<Expression>);
72 NonnullRefPtr<NewExpression> parse_new_expression();
73 RefPtr<FunctionExpression> try_parse_arrow_function_expression(bool expect_parens);
74
75 bool has_errors() const { return m_parser_state.m_has_errors; }
76
77private:
78 int operator_precedence(TokenType) const;
79 Associativity operator_associativity(TokenType) const;
80 bool match_expression() const;
81 bool match_unary_prefixed_expression() const;
82 bool match_secondary_expression() const;
83 bool match_statement() const;
84 bool match_variable_declaration() const;
85 bool match(TokenType type) const;
86 bool done() const;
87 void expected(const char* what);
88 Token consume();
89 Token consume(TokenType type);
90 void save_state();
91 void load_state();
92
93 struct ParserState {
94 Lexer m_lexer;
95 Token m_current_token;
96 bool m_has_errors = false;
97
98 explicit ParserState(Lexer);
99 };
100
101 ParserState m_parser_state;
102 Optional<ParserState> m_saved_state;
103};
104}