Browse Source

Utility class to find a suitable AST node for a given location.

cl-refactor
chriseth 10 years ago
parent
commit
9524ca5e31
  1. 25
      libevmcore/SourceLocation.h
  2. 48
      libsolidity/ASTUtils.cpp
  3. 54
      libsolidity/ASTUtils.h

25
libevmcore/SourceLocation.h

@ -25,6 +25,7 @@
#include <memory>
#include <string>
#include <ostream>
#include <tuple>
namespace dev
{
@ -45,6 +46,9 @@ struct SourceLocation
bool operator==(SourceLocation const& _other) const { return start == _other.start && end == _other.end;}
bool operator!=(SourceLocation const& _other) const { return !operator==(_other); }
inline bool operator<(SourceLocation const& _other) const;
inline bool contains(SourceLocation const& _other) const;
inline bool intersects(SourceLocation const& _other) const;
bool isEmpty() const { return start == -1 && end == -1; }
@ -61,4 +65,25 @@ inline std::ostream& operator<<(std::ostream& _out, SourceLocation const& _locat
return _out << *_location.sourceName << "[" << _location.start << "," << _location.end << ")";
}
bool SourceLocation::operator<(SourceLocation const& _other) const
{
if (!!sourceName != !!_other.sourceName)
return int(!!sourceName) < int(!!_other.sourceName);
return make_tuple(*sourceName, start, end) < make_tuple(*_other.sourceName, _other.start, _other.end);
}
bool SourceLocation::contains(SourceLocation const& _other) const
{
if (isEmpty() || _other.isEmpty() || !sourceName || !_other.sourceName || *sourceName != *_other.sourceName)
return false;
return start <= _other.start && _other.end <= end;
}
bool SourceLocation::intersects(SourceLocation const& _other) const
{
if (isEmpty() || _other.isEmpty() || !sourceName || !_other.sourceName || *sourceName != *_other.sourceName)
return false;
return _other.start < end && start < _other.end;
}
}

48
libsolidity/ASTUtils.cpp

@ -0,0 +1,48 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2015
* Utilities to work with the AST.
*/
#include <libsolidity/ASTUtils.h>
using namespace std;
using namespace dev;
using namespace dev::solidity;
ASTNode const* LocationFinder::leastUpperBound()
{
m_bestMatch = nullptr;
for (ASTNode const* rootNode: m_rootNodes)
rootNode->accept(*this);
return m_bestMatch;
}
bool LocationFinder::visitNode(const ASTNode& _node)
{
if (_node.getLocation().contains(m_location))
{
m_bestMatch = &_node;
return true;
}
return false;
}

54
libsolidity/ASTUtils.h

@ -0,0 +1,54 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2015
* Utilities to work with the AST.
*/
#pragma once
#include <libevmcore/SourceLocation.h>
#include <libsolidity/ASTVisitor.h>
namespace dev
{
namespace solidity
{
class LocationFinder: private ASTConstVisitor
{
public:
LocationFinder(SourceLocation const& _location, std::vector<ASTNode const*> _rootNodes):
m_rootNodes(_rootNodes), m_location(_location)
{
}
/// @returns the "closest" (in the sense of most-leafward) AST node which is a descendant of
/// _node and whose source location contains _location.
ASTNode const* leastUpperBound();
private:
bool visitNode(ASTNode const& _node);
std::vector<ASTNode const*> m_rootNodes;
SourceLocation m_location;
ASTNode const* m_bestMatch = nullptr;
};
}
}
Loading…
Cancel
Save