summaryrefslogtreecommitdiff
path: root/Userland/js.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-05-14 16:29:24 +0100
committerAndreas Kling <kling@serenityos.org>2020-05-15 09:53:52 +0200
commit4e0ed34d7ec2a8c6536924de28e16b3ab6d37524 (patch)
tree06bd8df167a08d2b634dfbd1c5a4519e705a84bc /Userland/js.cpp
parent33defef267d76b1a6ce8f6fdafed00228ecda8ba (diff)
downloadserenity-4e0ed34d7ec2a8c6536924de28e16b3ab6d37524.zip
js: Throw a regular SyntaxError for errors from the parser
Diffstat (limited to 'Userland/js.cpp')
-rw-r--r--Userland/js.cpp78
1 files changed, 30 insertions, 48 deletions
diff --git a/Userland/js.cpp b/Userland/js.cpp
index acfe17d87e..efee923e6f 100644
--- a/Userland/js.cpp
+++ b/Userland/js.cpp
@@ -269,6 +269,32 @@ bool write_to_file(const StringView& path)
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()
{
}
@@ -335,17 +361,7 @@ JS::Value ReplObject::load_file(JS::Interpreter& interpreter)
} else {
source = file_contents;
}
- auto parser = JS::Parser(JS::Lexer(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());
+ parse_and_run(interpreter, source);
}
return JS::Value(true);
}
@@ -357,24 +373,7 @@ void repl(JS::Interpreter& interpreter)
if (piece.is_empty())
continue;
repl_statements.append(piece);
- auto parser = JS::Parser(JS::Lexer(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());
- }
+ parse_and_run(interpreter, piece);
}
}
@@ -491,6 +490,7 @@ int main(int argc, char** argv)
};
if (script_path == nullptr) {
+ s_print_last_result = true;
interpreter = JS::Interpreter::create<ReplObject>();
ReplConsoleClient console_client(interpreter->console());
interpreter->console().set_client(console_client);
@@ -742,27 +742,9 @@ int main(int argc, char** argv)
} else {
source = file_contents;
}
- auto parser = JS::Parser(JS::Lexer(source));
- auto program = parser.parse_program();
-
- if (s_dump_ast)
- program->dump(0);
- if (parser.has_errors()) {
- printf("Parse Error\n");
+ if (!parse_and_run(*interpreter, source))
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;