summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Parser.h
diff options
context:
space:
mode:
authorStephan Unverwerth <s.unverwerth@gmx.de>2020-03-12 23:02:41 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-14 00:11:24 +0100
commit15d5b2d29e5b375a7b0601e7c456ac49868c606d (patch)
tree7c0d4da55a7565f5d3ca54eedd441b26f800f80a /Libraries/LibJS/Parser.h
parentf347dd5c5e261fc8c77917508e47680dbd166be5 (diff)
downloadserenity-15d5b2d29e5b375a7b0601e7c456ac49868c606d.zip
LibJS: Add operator precedence parsing
Obey precedence and associativity rules when parsing expressions with chained operators.
Diffstat (limited to 'Libraries/LibJS/Parser.h')
-rw-r--r--Libraries/LibJS/Parser.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/Libraries/LibJS/Parser.h b/Libraries/LibJS/Parser.h
index e979a1d7d9..314c636aec 100644
--- a/Libraries/LibJS/Parser.h
+++ b/Libraries/LibJS/Parser.h
@@ -31,6 +31,12 @@
#include <AK/NonnullOwnPtr.h>
namespace JS {
+
+enum class Associativity {
+ Left,
+ Right
+};
+
class Parser {
public:
explicit Parser(Lexer lexer);
@@ -44,15 +50,17 @@ public:
NonnullOwnPtr<VariableDeclaration> parse_variable_declaration();
NonnullOwnPtr<ForStatement> parse_for_statement();
- NonnullOwnPtr<Expression> parse_expression();
+ NonnullOwnPtr<Expression> parse_expression(int min_precedence, Associativity associate = Associativity::Right);
NonnullOwnPtr<Expression> parse_primary_expression();
NonnullOwnPtr<ObjectExpression> parse_object_expression();
- NonnullOwnPtr<Expression> parse_secondary_expression(NonnullOwnPtr<Expression>);
+ NonnullOwnPtr<Expression> parse_secondary_expression(NonnullOwnPtr<Expression>, int min_precedence, Associativity associate = Associativity::Right);
NonnullOwnPtr<CallExpression> parse_call_expression(NonnullOwnPtr<Expression>);
bool has_errors() const { return m_has_errors; }
private:
+ int operator_precedence(TokenType) const;
+ Associativity operator_associativity(TokenType) const;
bool match_expression() const;
bool match_secondary_expression() const;
bool match_statement() const;