Skip to content

Commit

Permalink
Add basic error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlt committed Mar 12, 2024
1 parent 600428b commit b17a4f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
43 changes: 30 additions & 13 deletions libyul/AsmParser.cpp
Expand Up @@ -178,7 +178,7 @@ void Parser::fetchDebugDataFromComment()
}
else if (match[1] == "@debug.set")
{
if (auto parseResult = parseDebugDataAttributeOperationComment(commentLiteral, m_scanner->currentCommentLocation()))
if (auto parseResult = parseDebugDataAttributeOperationComment(match[1], commentLiteral, m_scanner->currentCommentLocation()))
{
commentLiteral = parseResult->first;
if (parseResult->second.has_value())
Expand All @@ -189,7 +189,7 @@ void Parser::fetchDebugDataFromComment()
}
else if (match[1] == "@debug.merge")
{
if (auto parseResult = parseDebugDataAttributeOperationComment(commentLiteral, m_scanner->currentCommentLocation()))
if (auto parseResult = parseDebugDataAttributeOperationComment(match[1], commentLiteral, m_scanner->currentCommentLocation()))
{
commentLiteral = parseResult->first;
if (parseResult->second.has_value())
Expand All @@ -200,11 +200,11 @@ void Parser::fetchDebugDataFromComment()
}
else if (match[1] == "@debug.patch")
{
if (auto parseResult = parseDebugDataAttributeOperationComment(commentLiteral, m_scanner->currentCommentLocation()))
if (auto parseResult = parseDebugDataAttributeOperationComment(match[1], commentLiteral, m_scanner->currentCommentLocation()))
{
commentLiteral = parseResult->first;
if (parseResult->second.has_value())
applyDebugDataAttributePatch(parseResult->second.value());
applyDebugDataAttributePatch(parseResult->second.value(), m_scanner->currentCommentLocation());
}
else
break;
Expand All @@ -218,8 +218,9 @@ void Parser::fetchDebugDataFromComment()
}

std::optional<std::pair<std::string_view, std::optional<Json>>> Parser::parseDebugDataAttributeOperationComment(
std::string const& _command,
std::string_view _arguments,
langutil::SourceLocation const&
langutil::SourceLocation const& _location
)
{
std::optional<Json> jsonData;
Expand All @@ -233,25 +234,41 @@ std::optional<std::pair<std::string_view, std::optional<Json>>> Parser::parseDeb
{
jsonData = Json::parse(_arguments.substr(0, e.byte - 1), nullptr, true);
}
catch(nlohmann::json::parse_error&)
catch(nlohmann::json::parse_error& ee)
{
m_errorReporter.syntaxError(
5721_error,
_location,
_command + ": Could not parse debug data: " + removeNlohmannInternalErrorIdentifier(ee.what())
);
jsonData.reset();
}
_arguments = _arguments.substr(e.byte - 1);
}
return {{_arguments, jsonData}};
}

void Parser::applyDebugDataAttributePatch(Json const& _jsonPatch)
void Parser::applyDebugDataAttributePatch(Json const& _jsonPatch, langutil::SourceLocation const& _location)
{
if (_jsonPatch.is_object())
try
{
Json array = Json::array();
array.push_back(_jsonPatch);
m_currentDebugDataAttributes = m_currentDebugDataAttributes.patch(array);
if (_jsonPatch.is_object())
{
Json array = Json::array();
array.push_back(_jsonPatch);
m_currentDebugDataAttributes = m_currentDebugDataAttributes.patch(array);
}
else
m_currentDebugDataAttributes = m_currentDebugDataAttributes.patch(_jsonPatch);
}
catch(nlohmann::json::parse_error& ee)
{
m_errorReporter.syntaxError(
9426_error,
_location,
"@debug.patch: Could not parse debug data: " + removeNlohmannInternalErrorIdentifier(ee.what())
);
}
else
m_currentDebugDataAttributes = m_currentDebugDataAttributes.patch(_jsonPatch);
}

std::optional<std::pair<std::string_view, SourceLocation>> Parser::parseSrcComment(
Expand Down
3 changes: 2 additions & 1 deletion libyul/AsmParser.h
Expand Up @@ -118,11 +118,12 @@ class Parser: public langutil::ParserBase
);

std::optional<std::pair<std::string_view, std::optional<Json>>> parseDebugDataAttributeOperationComment(
std::string const& _command,
std::string_view _arguments,
langutil::SourceLocation const& _commentLocation
);

void applyDebugDataAttributePatch(Json const& _jsonPatch);
void applyDebugDataAttributePatch(Json const& _jsonPatch, langutil::SourceLocation const& _location);

/// Creates a DebugData object with the correct source location set.
langutil::DebugData::ConstPtr createDebugData() const;
Expand Down

0 comments on commit b17a4f7

Please sign in to comment.