LibJS: Add to_string definitions to CodeGenerationError and ParserError

This commit is contained in:
Timothy Flynn 2023-02-16 11:03:49 -05:00 committed by Tim Flynn
parent f7458b3e17
commit 93ad25fbe5
Notes: sideshowbarker 2024-07-17 06:46:15 +09:00
5 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/AST.h>
#include <LibJS/Bytecode/CodeGenerationError.h>
namespace JS::Bytecode {
ErrorOr<String> CodeGenerationError::to_string() const
{
return String::formatted("CodeGenerationError in {}: {}", failing_node ? failing_node->class_name() : "<unknown node>", reason_literal);
}
}

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/Error.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibJS/Forward.h>
@ -16,6 +17,7 @@ struct CodeGenerationError {
ASTNode const* failing_node { nullptr };
StringView reason_literal;
ErrorOr<String> to_string() const;
DeprecatedString to_deprecated_string();
};

View file

@ -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

View file

@ -12,6 +12,13 @@
namespace JS {
ErrorOr<String> 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())

View file

@ -8,7 +8,9 @@
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/Error.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <LibJS/SourceRange.h>
namespace JS {
@ -17,6 +19,7 @@ struct ParserError {
DeprecatedString message;
Optional<Position> position;
ErrorOr<String> to_string() const;
DeprecatedString to_deprecated_string() const;
DeprecatedString source_location_hint(StringView source, char const spacer = ' ', char const indicator = '^') const;
};