summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2020-06-01 17:08:34 +0300
committerAndreas Kling <kling@serenityos.org>2020-06-01 17:37:44 +0200
commit600fcd2d467e3b2ebb5bf9e237c4255f53d7e329 (patch)
tree09e471ff2c7ff7ae1f5007bbd41d0057ab9897e6 /Libraries
parent2fbc37befcf11423cfb8784692ec1563cbf00b8e (diff)
downloadserenity-600fcd2d467e3b2ebb5bf9e237c4255f53d7e329.zip
LibJS: Replace some parser assertions by syntax errors
When parsing JavaScript, we can get pretty much any sequnce of tokens, and we shouldn't crash if it's not something that we normally expect. Instead, emit syntax errors.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Parser.cpp25
1 files changed, 17 insertions, 8 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index f95a88c8f2..31807cad17 100644
--- a/Libraries/LibJS/Parser.cpp
+++ b/Libraries/LibJS/Parser.cpp
@@ -225,9 +225,12 @@ NonnullRefPtr<Program> Parser::parse_program()
consume();
}
}
- ASSERT(m_parser_state.m_var_scopes.size() == 1);
- program->add_variables(m_parser_state.m_var_scopes.last());
- program->add_variables(m_parser_state.m_let_scopes.last());
+ if (m_parser_state.m_var_scopes.size() == 1) {
+ program->add_variables(m_parser_state.m_var_scopes.last());
+ program->add_variables(m_parser_state.m_let_scopes.last());
+ } else {
+ syntax_error("Unclosed scope");
+ }
return program;
}
@@ -620,13 +623,19 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
properties.append(create_ast_node<ObjectProperty>(*property_name, function, property_type));
} else if (match(TokenType::Colon)) {
+ if (!property_name) {
+ syntax_error("Expected a property name");
+ skip_to_next_property();
+ continue;
+ }
consume();
- ASSERT(property_name);
properties.append(create_ast_node<ObjectProperty>(*property_name, parse_expression(2), property_type));
- } else {
- ASSERT(property_name);
- ASSERT(property_value);
+ } else if (property_name && property_value) {
properties.append(create_ast_node<ObjectProperty>(*property_name, *property_value, property_type));
+ } else {
+ syntax_error("Expected a property");
+ skip_to_next_property();
+ continue;
}
if (!match(TokenType::Comma))
@@ -1342,7 +1351,7 @@ NonnullRefPtr<Statement> Parser::parse_for_statement()
if (match_for_in_of())
return parse_for_in_of_statement(*init);
} else {
- ASSERT_NOT_REACHED();
+ syntax_error("Unexpected token in for loop");
}
}
consume(TokenType::Semicolon);