mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:27:35 +00:00
js: Throw a regular SyntaxError for errors from the parser
This commit is contained in:
parent
33defef267
commit
4e0ed34d7e
1 changed files with 30 additions and 48 deletions
|
@ -269,6 +269,32 @@ bool write_to_file(const StringView& path)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool parse_and_run(JS::Interpreter& interpreter, const StringView& source)
|
||||||
|
{
|
||||||
|
auto parser = JS::Parser(JS::Lexer(source));
|
||||||
|
auto program = parser.parse_program();
|
||||||
|
|
||||||
|
if (s_dump_ast)
|
||||||
|
program->dump(0);
|
||||||
|
|
||||||
|
if (parser.has_errors()) {
|
||||||
|
auto error = parser.errors()[0];
|
||||||
|
interpreter.throw_exception<JS::SyntaxError>(error.to_string());
|
||||||
|
} else {
|
||||||
|
interpreter.run(*program);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (interpreter.exception()) {
|
||||||
|
printf("Uncaught exception: ");
|
||||||
|
print(interpreter.exception()->value());
|
||||||
|
interpreter.clear_exception();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (s_print_last_result)
|
||||||
|
print(interpreter.last_value());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
ReplObject::ReplObject()
|
ReplObject::ReplObject()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -335,17 +361,7 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
|
||||||
} else {
|
} else {
|
||||||
source = file_contents;
|
source = file_contents;
|
||||||
}
|
}
|
||||||
auto parser = JS::Parser(JS::Lexer(source));
|
parse_and_run(interpreter, source);
|
||||||
auto program = parser.parse_program();
|
|
||||||
if (s_dump_ast)
|
|
||||||
program->dump(0);
|
|
||||||
|
|
||||||
if (parser.has_errors())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
interpreter.run(*program);
|
|
||||||
if (s_print_last_result)
|
|
||||||
print(interpreter.last_value());
|
|
||||||
}
|
}
|
||||||
return JS::Value(true);
|
return JS::Value(true);
|
||||||
}
|
}
|
||||||
|
@ -357,24 +373,7 @@ void repl(JS::Interpreter& interpreter)
|
||||||
if (piece.is_empty())
|
if (piece.is_empty())
|
||||||
continue;
|
continue;
|
||||||
repl_statements.append(piece);
|
repl_statements.append(piece);
|
||||||
auto parser = JS::Parser(JS::Lexer(piece));
|
parse_and_run(interpreter, piece);
|
||||||
auto program = parser.parse_program();
|
|
||||||
if (s_dump_ast)
|
|
||||||
program->dump(0);
|
|
||||||
|
|
||||||
if (parser.has_errors()) {
|
|
||||||
printf("Parse error\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
interpreter.run(*program);
|
|
||||||
if (interpreter.exception()) {
|
|
||||||
printf("Uncaught exception: ");
|
|
||||||
print(interpreter.exception()->value());
|
|
||||||
interpreter.clear_exception();
|
|
||||||
} else {
|
|
||||||
print(interpreter.last_value());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,6 +490,7 @@ int main(int argc, char** argv)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (script_path == nullptr) {
|
if (script_path == nullptr) {
|
||||||
|
s_print_last_result = true;
|
||||||
interpreter = JS::Interpreter::create<ReplObject>();
|
interpreter = JS::Interpreter::create<ReplObject>();
|
||||||
ReplConsoleClient console_client(interpreter->console());
|
ReplConsoleClient console_client(interpreter->console());
|
||||||
interpreter->console().set_client(console_client);
|
interpreter->console().set_client(console_client);
|
||||||
|
@ -742,27 +742,9 @@ int main(int argc, char** argv)
|
||||||
} else {
|
} else {
|
||||||
source = file_contents;
|
source = file_contents;
|
||||||
}
|
}
|
||||||
auto parser = JS::Parser(JS::Lexer(source));
|
|
||||||
auto program = parser.parse_program();
|
|
||||||
|
|
||||||
if (s_dump_ast)
|
if (!parse_and_run(*interpreter, source))
|
||||||
program->dump(0);
|
|
||||||
|
|
||||||
if (parser.has_errors()) {
|
|
||||||
printf("Parse Error\n");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
|
|
||||||
auto result = interpreter->run(*program);
|
|
||||||
|
|
||||||
if (interpreter->exception()) {
|
|
||||||
printf("Uncaught exception: ");
|
|
||||||
print(interpreter->exception()->value());
|
|
||||||
interpreter->clear_exception();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (s_print_last_result)
|
|
||||||
print(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue