diff --git a/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.cpp b/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.cpp new file mode 100644 index 0000000000..956ef1187c --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.cpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace JS::Bytecode { + +ErrorOr CodeGenerationError::to_string() const +{ + return String::formatted("CodeGenerationError in {}: {}", failing_node ? failing_node->class_name() : "", reason_literal); +} + +} diff --git a/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.h b/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.h index 3d448fc38c..036f08d61d 100644 --- a/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.h +++ b/Userland/Libraries/LibJS/Bytecode/CodeGenerationError.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -16,6 +17,7 @@ struct CodeGenerationError { ASTNode const* failing_node { nullptr }; StringView reason_literal; + ErrorOr to_string() const; DeprecatedString to_deprecated_string(); }; diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt index e7b0ccdceb..c08007f93c 100644 --- a/Userland/Libraries/LibJS/CMakeLists.txt +++ b/Userland/Libraries/LibJS/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES AST.cpp Bytecode/ASTCodegen.cpp Bytecode/BasicBlock.cpp + Bytecode/CodeGenerationError.cpp Bytecode/Executable.cpp Bytecode/Generator.cpp Bytecode/IdentifierTable.cpp diff --git a/Userland/Libraries/LibJS/ParserError.cpp b/Userland/Libraries/LibJS/ParserError.cpp index c790ca5d44..d9b0522102 100644 --- a/Userland/Libraries/LibJS/ParserError.cpp +++ b/Userland/Libraries/LibJS/ParserError.cpp @@ -12,6 +12,13 @@ namespace JS { +ErrorOr ParserError::to_string() const +{ + if (!position.has_value()) + return String::from_deprecated_string(message); + return String::formatted("{} (line: {}, column: {})", message, position.value().line, position.value().column); +} + DeprecatedString ParserError::to_deprecated_string() const { if (!position.has_value()) diff --git a/Userland/Libraries/LibJS/ParserError.h b/Userland/Libraries/LibJS/ParserError.h index 3f5eefec7c..f60928f46b 100644 --- a/Userland/Libraries/LibJS/ParserError.h +++ b/Userland/Libraries/LibJS/ParserError.h @@ -8,7 +8,9 @@ #pragma once #include +#include #include +#include #include namespace JS { @@ -17,6 +19,7 @@ struct ParserError { DeprecatedString message; Optional position; + ErrorOr to_string() const; DeprecatedString to_deprecated_string() const; DeprecatedString source_location_hint(StringView source, char const spacer = ' ', char const indicator = '^') const; };