summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Parser.h
diff options
context:
space:
mode:
authorHendi <hendi48@users.noreply.github.com>2021-07-04 03:15:52 +0200
committerLinus Groh <mail@linusgroh.de>2021-07-06 00:15:37 +0100
commit38fd980b0ce98130f5f62ab8c87531a2b367da5c (patch)
treea6297d970ed05b5653beeb90c5ee739150009fee /Userland/Libraries/LibJS/Parser.h
parent72f8d90dc5afb023eb2449511439fc2116783961 (diff)
downloadserenity-38fd980b0ce98130f5f62ab8c87531a2b367da5c.zip
LibJS: Improve function hoisting across blocks
The parser now keeps track of a scope chain so that it can hoist function declarations to the closest function scope.
Diffstat (limited to 'Userland/Libraries/LibJS/Parser.h')
-rw-r--r--Userland/Libraries/LibJS/Parser.h18
1 files changed, 17 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Parser.h b/Userland/Libraries/LibJS/Parser.h
index a53352c940..0e231a3dff 100644
--- a/Userland/Libraries/LibJS/Parser.h
+++ b/Userland/Libraries/LibJS/Parser.h
@@ -196,13 +196,29 @@ private:
[[nodiscard]] RulePosition push_start() { return { *this, position() }; }
+ struct Scope : public RefCounted<Scope> {
+ enum Type {
+ Function,
+ Block,
+ };
+
+ Type type;
+ RefPtr<Scope> parent;
+
+ NonnullRefPtrVector<FunctionDeclaration> function_declarations;
+ NonnullRefPtrVector<FunctionDeclaration> hoisted_function_declarations;
+
+ explicit Scope(Type, RefPtr<Scope>);
+ RefPtr<Scope> get_current_function_scope();
+ };
+
struct ParserState {
Lexer lexer;
Token current_token;
Vector<Error> errors;
Vector<NonnullRefPtrVector<VariableDeclaration>> var_scopes;
Vector<NonnullRefPtrVector<VariableDeclaration>> let_scopes;
- Vector<NonnullRefPtrVector<FunctionDeclaration>> function_scopes;
+ RefPtr<Scope> current_scope;
Vector<Vector<FunctionNode::Parameter>&> function_parameters;