mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
liblangutil: SourceLocation: adds (shared) pointer to underlying CharStream source, eliminating sourceName
Also, adapted affecting code to those changes.
This commit is contained in:
@@ -105,10 +105,10 @@ void ParserBase::decreaseRecursionDepth()
|
||||
|
||||
void ParserBase::parserError(string const& _description)
|
||||
{
|
||||
m_errorReporter.parserError(SourceLocation(position(), endPosition(), sourceName()), _description);
|
||||
m_errorReporter.parserError(SourceLocation(position(), endPosition(), source()), _description);
|
||||
}
|
||||
|
||||
void ParserBase::fatalParserError(string const& _description)
|
||||
{
|
||||
m_errorReporter.fatalParserError(SourceLocation(position(), endPosition(), sourceName()), _description);
|
||||
m_errorReporter.fatalParserError(SourceLocation(position(), endPosition(), source()), _description);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
explicit ParserBase(ErrorReporter& errorReporter): m_errorReporter(errorReporter) {}
|
||||
|
||||
std::shared_ptr<std::string const> const& sourceName() const;
|
||||
CharStream const* charStream() const { return &m_scanner->charStream(); }
|
||||
std::shared_ptr<CharStream> source() const { return m_scanner->charStream(); }
|
||||
|
||||
protected:
|
||||
/// Utility class that creates an error and throws an exception if the
|
||||
|
||||
+29
-22
@@ -169,15 +169,22 @@ private:
|
||||
|
||||
void Scanner::reset(CharStream _source, string _sourceName)
|
||||
{
|
||||
m_source = std::move(_source);
|
||||
m_source = make_shared<CharStream>(std::move(_source));
|
||||
m_sourceName = make_shared<string const>(std::move(_sourceName));
|
||||
reset();
|
||||
}
|
||||
|
||||
void Scanner::reset(std::shared_ptr<CharStream> _source)
|
||||
{
|
||||
solAssert(_source.get() != nullptr, "You MUST provide a CharStream when resetting.");
|
||||
m_source = _source;
|
||||
reset();
|
||||
}
|
||||
|
||||
void Scanner::reset()
|
||||
{
|
||||
m_source.reset();
|
||||
m_char = m_source.get();
|
||||
m_source->reset();
|
||||
m_char = m_source->get();
|
||||
skipWhitespace();
|
||||
scanToken();
|
||||
next();
|
||||
@@ -296,13 +303,13 @@ Token Scanner::scanSingleLineDocComment()
|
||||
{
|
||||
// check if next line is also a documentation comment
|
||||
skipWhitespace();
|
||||
if (!m_source.isPastEndOfInput(3) &&
|
||||
m_source.get(0) == '/' &&
|
||||
m_source.get(1) == '/' &&
|
||||
m_source.get(2) == '/')
|
||||
if (!m_source->isPastEndOfInput(3) &&
|
||||
m_source->get(0) == '/' &&
|
||||
m_source->get(1) == '/' &&
|
||||
m_source->get(2) == '/')
|
||||
{
|
||||
addCommentLiteralChar('\n');
|
||||
m_char = m_source.advanceAndGet(3);
|
||||
m_char = m_source->advanceAndGet(3);
|
||||
}
|
||||
else
|
||||
break; // next line is not a documentation comment, we are done
|
||||
@@ -355,20 +362,20 @@ Token Scanner::scanMultiLineDocComment()
|
||||
if (isLineTerminator(m_char))
|
||||
{
|
||||
skipWhitespace();
|
||||
if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) == '*')
|
||||
if (!m_source->isPastEndOfInput(1) && m_source->get(0) == '*' && m_source->get(1) == '*')
|
||||
{ // it is unknown if this leads to the end of the comment
|
||||
addCommentLiteralChar('*');
|
||||
advance();
|
||||
}
|
||||
else if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) != '/')
|
||||
else if (!m_source->isPastEndOfInput(1) && m_source->get(0) == '*' && m_source->get(1) != '/')
|
||||
{ // skip first '*' in subsequent lines
|
||||
if (charsAdded)
|
||||
addCommentLiteralChar('\n');
|
||||
m_char = m_source.advanceAndGet(2);
|
||||
m_char = m_source->advanceAndGet(2);
|
||||
}
|
||||
else if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) == '/')
|
||||
else if (!m_source->isPastEndOfInput(1) && m_source->get(0) == '*' && m_source->get(1) == '/')
|
||||
{ // if after newline the comment ends, don't insert the newline
|
||||
m_char = m_source.advanceAndGet(2);
|
||||
m_char = m_source->advanceAndGet(2);
|
||||
endFound = true;
|
||||
break;
|
||||
}
|
||||
@@ -376,9 +383,9 @@ Token Scanner::scanMultiLineDocComment()
|
||||
addCommentLiteralChar('\n');
|
||||
}
|
||||
|
||||
if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) == '/')
|
||||
if (!m_source->isPastEndOfInput(1) && m_source->get(0) == '*' && m_source->get(1) == '/')
|
||||
{
|
||||
m_char = m_source.advanceAndGet(2);
|
||||
m_char = m_source->advanceAndGet(2);
|
||||
endFound = true;
|
||||
break;
|
||||
}
|
||||
@@ -715,11 +722,11 @@ bool Scanner::isUnicodeLinebreak()
|
||||
if (0x0a <= m_char && m_char <= 0x0d)
|
||||
// line feed, vertical tab, form feed, carriage return
|
||||
return true;
|
||||
else if (!m_source.isPastEndOfInput(1) && uint8_t(m_source.get(0)) == 0xc2 && uint8_t(m_source.get(1)) == 0x85)
|
||||
else if (!m_source->isPastEndOfInput(1) && uint8_t(m_source->get(0)) == 0xc2 && uint8_t(m_source->get(1)) == 0x85)
|
||||
// NEL - U+0085, C2 85 in utf8
|
||||
return true;
|
||||
else if (!m_source.isPastEndOfInput(2) && uint8_t(m_source.get(0)) == 0xe2 && uint8_t(m_source.get(1)) == 0x80 && (
|
||||
uint8_t(m_source.get(2)) == 0xa8 || uint8_t(m_source.get(2)) == 0xa9
|
||||
else if (!m_source->isPastEndOfInput(2) && uint8_t(m_source->get(0)) == 0xe2 && uint8_t(m_source->get(1)) == 0x80 && (
|
||||
uint8_t(m_source->get(2)) == 0xa8 || uint8_t(m_source->get(2)) == 0xa9
|
||||
))
|
||||
// LS - U+2028, E2 80 A8 in utf8
|
||||
// PS - U+2029, E2 80 A9 in utf8
|
||||
@@ -783,7 +790,7 @@ void Scanner::scanDecimalDigits()
|
||||
|
||||
// May continue with decimal digit or underscore for grouping.
|
||||
do addLiteralCharAndAdvance();
|
||||
while (!m_source.isPastEndOfInput() && (isDecimalDigit(m_char) || m_char == '_'));
|
||||
while (!m_source->isPastEndOfInput() && (isDecimalDigit(m_char) || m_char == '_'));
|
||||
|
||||
// Defer further validation of underscore to SyntaxChecker.
|
||||
}
|
||||
@@ -829,7 +836,7 @@ Token Scanner::scanNumber(char _charSeen)
|
||||
scanDecimalDigits(); // optional
|
||||
if (m_char == '.')
|
||||
{
|
||||
if (!m_source.isPastEndOfInput(1) && m_source.get(1) == '_')
|
||||
if (!m_source->isPastEndOfInput(1) && m_source->get(1) == '_')
|
||||
{
|
||||
// Assume the input may be a floating point number with leading '_' in fraction part.
|
||||
// Recover by consuming it all but returning `Illegal` right away.
|
||||
@@ -837,7 +844,7 @@ Token Scanner::scanNumber(char _charSeen)
|
||||
addLiteralCharAndAdvance(); // '_'
|
||||
scanDecimalDigits();
|
||||
}
|
||||
if (m_source.isPastEndOfInput() || !isDecimalDigit(m_source.get(1)))
|
||||
if (m_source->isPastEndOfInput() || !isDecimalDigit(m_source->get(1)))
|
||||
{
|
||||
// A '.' has to be followed by a number.
|
||||
literal.complete();
|
||||
@@ -854,7 +861,7 @@ Token Scanner::scanNumber(char _charSeen)
|
||||
solAssert(kind != HEX, "'e'/'E' must be scanned as part of the hex number");
|
||||
if (kind != DECIMAL)
|
||||
return setError(ScannerError::IllegalExponent);
|
||||
else if (!m_source.isPastEndOfInput(1) && m_source.get(1) == '_')
|
||||
else if (!m_source->isPastEndOfInput(1) && m_source->get(1) == '_')
|
||||
{
|
||||
// Recover from wrongly placed underscore as delimiter in literal with scientific
|
||||
// notation by consuming until the end.
|
||||
|
||||
+13
-12
@@ -90,14 +90,16 @@ class Scanner
|
||||
{
|
||||
friend class LiteralScope;
|
||||
public:
|
||||
explicit Scanner(std::shared_ptr<CharStream> _source) { reset(std::move(_source)); }
|
||||
explicit Scanner(CharStream _source = CharStream(), std::string _sourceName = "") { reset(std::move(_source), std::move(_sourceName)); }
|
||||
|
||||
std::string source() const { return m_source.source(); }
|
||||
std::string source() const { return m_source->source(); }
|
||||
|
||||
CharStream const& charStream() const noexcept { return m_source; }
|
||||
std::shared_ptr<CharStream> charStream() noexcept { return m_source; }
|
||||
|
||||
/// Resets the scanner as if newly constructed with _source and _sourceName as input.
|
||||
void reset(CharStream _source, std::string _sourceName);
|
||||
void reset(std::shared_ptr<CharStream> _source);
|
||||
/// Resets scanner to the start of input.
|
||||
void reset();
|
||||
|
||||
@@ -154,14 +156,13 @@ public:
|
||||
///@name Error printing helper functions
|
||||
/// Functions that help pretty-printing parse errors
|
||||
/// Do only use in error cases, they are quite expensive.
|
||||
std::string lineAtPosition(int _position) const { return m_source.lineAtPosition(_position); }
|
||||
std::tuple<int, int> translatePositionToLineColumn(int _position) const { return m_source.translatePositionToLineColumn(_position); }
|
||||
std::string lineAtPosition(int _position) const { return m_source->lineAtPosition(_position); }
|
||||
std::tuple<int, int> translatePositionToLineColumn(int _position) const { return m_source->translatePositionToLineColumn(_position); }
|
||||
std::string sourceAt(SourceLocation const& _location) const
|
||||
{
|
||||
solAssert(!_location.isEmpty(), "");
|
||||
solAssert(m_sourceName && _location.sourceName, "");
|
||||
solAssert(*m_sourceName == *_location.sourceName, "");
|
||||
return m_source.source().substr(_location.start, _location.end - _location.start);
|
||||
solAssert(m_source.get() == _location.source.get(), "CharStream memory locations must match.");
|
||||
return m_source->source().substr(_location.start, _location.end - _location.start);
|
||||
}
|
||||
///@}
|
||||
|
||||
@@ -190,8 +191,8 @@ private:
|
||||
void addUnicodeAsUTF8(unsigned codepoint);
|
||||
///@}
|
||||
|
||||
bool advance() { m_char = m_source.advanceAndGet(); return !m_source.isPastEndOfInput(); }
|
||||
void rollback(int _amount) { m_char = m_source.rollback(_amount); }
|
||||
bool advance() { m_char = m_source->advanceAndGet(); return !m_source->isPastEndOfInput(); }
|
||||
void rollback(int _amount) { m_char = m_source->rollback(_amount); }
|
||||
|
||||
inline Token selectErrorToken(ScannerError _err) { advance(); return setError(_err); }
|
||||
inline Token selectToken(Token _tok) { advance(); return _tok; }
|
||||
@@ -231,8 +232,8 @@ private:
|
||||
bool isUnicodeLinebreak();
|
||||
|
||||
/// Return the current source position.
|
||||
int sourcePos() const { return m_source.position(); }
|
||||
bool isSourcePastEndOfInput() const { return m_source.isPastEndOfInput(); }
|
||||
int sourcePos() const { return m_source->position(); }
|
||||
bool isSourcePastEndOfInput() const { return m_source->isPastEndOfInput(); }
|
||||
|
||||
TokenDesc m_skippedComment; // desc for current skipped comment
|
||||
TokenDesc m_nextSkippedComment; // desc for next skipped comment
|
||||
@@ -240,7 +241,7 @@ private:
|
||||
TokenDesc m_currentToken; // desc for current token (as returned by Next())
|
||||
TokenDesc m_nextToken; // desc for next token (one token look-ahead)
|
||||
|
||||
CharStream m_source;
|
||||
std::shared_ptr<CharStream> m_source;
|
||||
std::shared_ptr<std::string const> m_sourceName;
|
||||
|
||||
/// one character look-ahead, equals 0 at end of input
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <libdevcore/Common.h> // defines noexcept macro for MSVC
|
||||
#include <liblangutil/CharStream.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
@@ -37,13 +38,13 @@ namespace langutil
|
||||
*/
|
||||
struct SourceLocation
|
||||
{
|
||||
SourceLocation(): start(-1), end(-1) { }
|
||||
SourceLocation(int _start, int _end, std::shared_ptr<std::string const> _sourceName):
|
||||
start(_start), end(_end), sourceName(_sourceName) { }
|
||||
SourceLocation(): start(-1), end(-1), source{nullptr} { }
|
||||
SourceLocation(int _start, int _end, std::shared_ptr<CharStream> _source):
|
||||
start(_start), end(_end), source{std::move(_source)} { }
|
||||
SourceLocation(SourceLocation&& _other) noexcept:
|
||||
start(_other.start),
|
||||
end(_other.end),
|
||||
sourceName(std::move(_other.sourceName))
|
||||
source{std::move(_other.source)}
|
||||
{}
|
||||
SourceLocation(SourceLocation const&) = default;
|
||||
SourceLocation& operator=(SourceLocation const&) = default;
|
||||
@@ -51,14 +52,15 @@ struct SourceLocation
|
||||
{
|
||||
start = _other.start;
|
||||
end = _other.end;
|
||||
sourceName = std::move(_other.sourceName);
|
||||
source = std::move(_other.source);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(SourceLocation const& _other) const
|
||||
{
|
||||
return start == _other.start && end == _other.end &&
|
||||
((!sourceName && !_other.sourceName) || (sourceName && _other.sourceName && *sourceName == *_other.sourceName));
|
||||
((!source.get() && !_other.source.get()) ||
|
||||
(source.get() && _other.source.get() && source->name() == _other.source->name()));
|
||||
}
|
||||
bool operator!=(SourceLocation const& _other) const { return !operator==(_other); }
|
||||
inline bool operator<(SourceLocation const& _other) const;
|
||||
@@ -69,7 +71,7 @@ struct SourceLocation
|
||||
|
||||
int start;
|
||||
int end;
|
||||
std::shared_ptr<std::string const> sourceName;
|
||||
std::shared_ptr<CharStream> source;
|
||||
};
|
||||
|
||||
/// Stream output for Location (used e.g. in boost exceptions).
|
||||
@@ -77,27 +79,33 @@ inline std::ostream& operator<<(std::ostream& _out, SourceLocation const& _locat
|
||||
{
|
||||
if (_location.isEmpty())
|
||||
return _out << "NO_LOCATION_SPECIFIED";
|
||||
return _out << *_location.sourceName << "[" << _location.start << "," << _location.end << ")";
|
||||
|
||||
if (_location.source)
|
||||
_out << _location.source->name();
|
||||
|
||||
_out << "[" << _location.start << "," << _location.end << ")";
|
||||
|
||||
return _out;
|
||||
}
|
||||
|
||||
bool SourceLocation::operator<(SourceLocation const& _other) const
|
||||
{
|
||||
if (!sourceName || !_other.sourceName)
|
||||
return std::make_tuple(int(!!sourceName), start, end) < std::make_tuple(int(!!_other.sourceName), _other.start, _other.end);
|
||||
if (!source|| !_other.source)
|
||||
return std::make_tuple(int(!!source), start, end) < std::make_tuple(int(!!_other.source), _other.start, _other.end);
|
||||
else
|
||||
return std::make_tuple(*sourceName, start, end) < std::make_tuple(*_other.sourceName, _other.start, _other.end);
|
||||
return std::make_tuple(source->name(), start, end) < std::make_tuple(_other.source->name(), _other.start, _other.end);
|
||||
}
|
||||
|
||||
bool SourceLocation::contains(SourceLocation const& _other) const
|
||||
{
|
||||
if (isEmpty() || _other.isEmpty() || ((!sourceName || !_other.sourceName || *sourceName != *_other.sourceName) && (sourceName || _other.sourceName)))
|
||||
if (isEmpty() || _other.isEmpty() || ((!source || !_other.source || source->name() != _other.source->name()) && (source || _other.source)))
|
||||
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) && (sourceName || _other.sourceName)))
|
||||
if (isEmpty() || _other.isEmpty() || ((!source || !_other.source || source->name() != _other.source->name()) && (source || _other.source)))
|
||||
return false;
|
||||
return _other.start < end && start < _other.end;
|
||||
}
|
||||
|
||||
@@ -30,9 +30,9 @@ using namespace langutil;
|
||||
|
||||
void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location)
|
||||
{
|
||||
if (!_location || !_location->sourceName)
|
||||
if (!_location || !_location->source)
|
||||
return; // Nothing we can print here
|
||||
auto const& scanner = m_scannerFromSourceName(*_location->sourceName);
|
||||
auto const& scanner = m_scannerFromSourceName(_location->source->name());
|
||||
int startLine;
|
||||
int startColumn;
|
||||
tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start);
|
||||
@@ -89,13 +89,13 @@ void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _locati
|
||||
|
||||
void SourceReferenceFormatter::printSourceName(SourceLocation const* _location)
|
||||
{
|
||||
if (!_location || !_location->sourceName)
|
||||
if (!_location || !_location->source)
|
||||
return; // Nothing we can print here
|
||||
auto const& scanner = m_scannerFromSourceName(*_location->sourceName);
|
||||
auto const& scanner = m_scannerFromSourceName(_location->source->name());
|
||||
int startLine;
|
||||
int startColumn;
|
||||
tie(startLine, startColumn) = scanner.translatePositionToLineColumn(_location->start);
|
||||
m_stream << *_location->sourceName << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
|
||||
m_stream << _location->source->name() << ":" << (startLine + 1) << ":" << (startColumn + 1) << ": ";
|
||||
}
|
||||
|
||||
void SourceReferenceFormatter::printExceptionInformation(
|
||||
|
||||
Reference in New Issue
Block a user