getObject -> Object::objectAt

This commit is contained in:
Daniel Lupu 2022-11-26 21:49:03 +02:00 committed by r0qs
parent 4581139afa
commit 9bd7f3553e
No known key found for this signature in database
GPG Key ID: 61503DBA6667276C
3 changed files with 35 additions and 35 deletions

View File

@ -33,6 +33,7 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <range/v3/algorithm/find_if.hpp>
#include <range/v3/view/transform.hpp>
using namespace std;
@ -174,3 +175,28 @@ vector<size_t> Object::pathToSubObject(YulString _qualifiedName) const
return path;
}
shared_ptr<Object> Object::objectAt(shared_ptr<Object> const& _object, string const& _qualifiedPath)
{
if (_qualifiedPath.empty() || _qualifiedPath == _object->name.str())
return _object;
if (!boost::algorithm::starts_with(_qualifiedPath, _object->name.str() + "."))
return nullptr;
string const subObjectPath = _qualifiedPath.substr(_object->name.str().length() + 1);
string const subObjectName = subObjectPath.substr(0, subObjectPath.find_first_of('.'));
auto subObjectIt = ranges::find_if(
_object->subObjects,
[&subObjectName](auto const& _subObject) { return _subObject->name.str() == subObjectName; }
);
if (subObjectIt == _object->subObjects.end())
return nullptr;
auto subObject = dynamic_pointer_cast<Object>(*subObjectIt);
yulAssert(subObject, "Assembly object <" + subObject->name.str() + "> does not contain code.");
return objectAt(subObject, subObjectPath);
}

View File

@ -31,6 +31,7 @@
#include <memory>
#include <set>
#include <string>
#include <limits>
#include <json/json.h>
@ -128,6 +129,13 @@ public:
/// @returns the name of the special metadata data object.
static std::string metadataName() { return ".metadata"; }
/// Recursively searches for an Object at @param _qualifiedPath within @param _object.
/// @returns a shared_ptr to the Object or a nullptr if it was not found.
static std::shared_ptr<Object> objectAt(
std::shared_ptr<Object> const& _object,
std::string const& _qualifiedPath
);
};
}

View File

@ -51,7 +51,6 @@
#include <boost/program_options.hpp>
#include <range/v3/action/sort.hpp>
#include <range/v3/algorithm/find_if.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/concat.hpp>
#include <range/v3/view/drop.hpp>
@ -88,39 +87,6 @@ public:
}.printErrorInformation(_errors);
}
/// Recursively searches for an object at @param _qualifiedPath within @param _object.
/// @returns the object at @param qualifiedPath or a nullptr if it was not found.
shared_ptr<Object> getObject(shared_ptr<Object> const& _object, string const& _qualifiedPath)
{
if (_qualifiedPath.empty() || _qualifiedPath == _object->name.str())
return _object;
if (!boost::algorithm::starts_with(_qualifiedPath, _object->name.str() + ".")) {
return nullptr;
}
string const subObjectPath = _qualifiedPath.substr(_object->name.str().length() + 1);
string const subObjectName = subObjectPath.substr(0, subObjectPath.find_first_of('.'));
auto subObjectIt = ranges::find_if(
_object->subObjects,
[&subObjectName](auto const& _subObject) { return _subObject->name.str() == subObjectName; }
);
if (subObjectIt == _object->subObjects.end()) {
return nullptr;
}
auto subObject = dynamic_pointer_cast<Object>(*subObjectIt);
yulAssert(
subObject != nullptr,
"Assembly object does not contain code."
);
return getObject(subObject, subObjectPath);
}
void parse(string const& _input, string const& _objectPath)
{
ErrorList errors;
@ -145,7 +111,7 @@ public:
throw runtime_error("Could not parse source.");
}
m_object = getObject(object, _objectPath);
m_object = Object::objectAt(object, _objectPath);
if (m_object == nullptr) {
throw runtime_error("Assembly object not found.");