Merge pull request #8251 from a3d4/refactor-currentlocation

Refactor currentlocation
This commit is contained in:
chriseth 2020-02-06 09:57:41 +01:00 committed by GitHub
commit a41f5e91f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 48 deletions

View File

@ -28,14 +28,9 @@ using namespace std;
using namespace solidity;
using namespace solidity::langutil;
int ParserBase::position() const
SourceLocation ParserBase::currentLocation() const
{
return m_scanner->currentLocation().start;
}
int ParserBase::endPosition() const
{
return m_scanner->currentLocation().end;
return m_scanner->currentLocation();
}
Token ParserBase::currentToken() const
@ -101,8 +96,8 @@ void ParserBase::expectTokenOrConsumeUntil(Token _value, string const& _currentN
Token tok = m_scanner->currentToken();
if (tok != _value)
{
int startPosition = position();
SourceLocation errorLoc = SourceLocation{startPosition, endPosition(), source()};
SourceLocation errorLoc = currentLocation();
int startPosition = errorLoc.start;
while (m_scanner->currentToken() != _value && m_scanner->currentToken() != Token::EOS)
m_scanner->next();
@ -150,7 +145,7 @@ void ParserBase::decreaseRecursionDepth()
void ParserBase::parserWarning(string const& _description)
{
m_errorReporter.warning(SourceLocation{position(), endPosition(), source()}, _description);
m_errorReporter.warning(currentLocation(), _description);
}
void ParserBase::parserError(SourceLocation const& _location, string const& _description)
@ -160,12 +155,12 @@ void ParserBase::parserError(SourceLocation const& _location, string const& _des
void ParserBase::parserError(string const& _description)
{
parserError(SourceLocation{position(), endPosition(), source()}, _description);
parserError(currentLocation(), _description);
}
void ParserBase::fatalParserError(string const& _description)
{
fatalParserError(SourceLocation{position(), endPosition(), source()}, _description);
fatalParserError(currentLocation(), _description);
}
void ParserBase::fatalParserError(SourceLocation const& _location, string const& _description)

View File

@ -62,10 +62,8 @@ protected:
ParserBase& m_parser;
};
/// Start position of the current token
int position() const;
/// End position of the current token
int endPosition() const;
/// Location of the current token
SourceLocation currentLocation() const;
///@{
///@name Helper functions

View File

@ -681,6 +681,7 @@ void Scanner::scanToken()
}
while (token == Token::Whitespace);
m_tokens[NextNext].location.end = sourcePos();
m_tokens[NextNext].location.source = m_source;
m_tokens[NextNext].token = token;
m_tokens[NextNext].extendedTokenInfo = make_tuple(m, n);
}

View File

@ -45,11 +45,11 @@ class Parser::ASTNodeFactory
{
public:
explicit ASTNodeFactory(Parser& _parser):
m_parser(_parser), m_location{_parser.position(), -1, _parser.source()} {}
m_parser(_parser), m_location{_parser.currentLocation().start, -1, _parser.currentLocation().source} {}
ASTNodeFactory(Parser& _parser, ASTPointer<ASTNode> const& _childNode):
m_parser(_parser), m_location{_childNode->location()} {}
void markEndPosition() { m_location.end = m_parser.endPosition(); }
void markEndPosition() { m_location.end = m_parser.currentLocation().end; }
void setLocation(SourceLocation const& _location) { m_location = _location; }
void setLocationEmpty() { m_location.end = m_location.start; }
/// Set the end position to the one of the given node.
@ -218,12 +218,12 @@ ASTPointer<ImportDirective> Parser::parseImportDirective()
while (true)
{
ASTPointer<ASTString> alias;
SourceLocation aliasLocation = SourceLocation{position(), endPosition(), source()};
SourceLocation aliasLocation = currentLocation();
ASTPointer<Identifier> id = parseIdentifier();
if (m_scanner->currentToken() == Token::As)
{
expectToken(Token::As);
aliasLocation = SourceLocation{position(), endPosition(), source()};
aliasLocation = currentLocation();
alias = expectIdentifierToken();
}
symbolAliases.emplace_back(ImportDirective::SymbolAlias{move(id), move(alias), aliasLocation});
@ -1189,7 +1189,7 @@ ASTPointer<Statement> Parser::parseStatement()
ASTPointer<InlineAssembly> Parser::parseInlineAssembly(ASTPointer<ASTString> const& _docString)
{
RecursionGuard recursionGuard(*this);
SourceLocation location{position(), -1, source()};
SourceLocation location = currentLocation();
expectToken(Token::Assembly);
yul::Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(m_evmVersion);
@ -2024,13 +2024,13 @@ Parser::IndexAccessedPath Parser::parseIndexAccessedPath()
ASTPointer<Expression> endIndex;
if (m_scanner->currentToken() != Token::RBrack)
endIndex = parseExpression();
indexLocation.end = endPosition();
indexLocation.end = currentLocation().end;
iap.indices.emplace_back(IndexAccessedPath::Index{index, {endIndex}, indexLocation});
expectToken(Token::RBrack);
}
else
{
indexLocation.end = endPosition();
indexLocation.end = currentLocation().end;
iap.indices.emplace_back(IndexAccessedPath::Index{index, {}, indexLocation});
expectToken(Token::RBrack);
}

View File

@ -88,7 +88,7 @@ Block Parser::parseBlock()
expectToken(Token::LBrace);
while (currentToken() != Token::RBrace)
block.statements.emplace_back(parseStatement());
block.location.end = endPosition();
block.location.end = currentLocation().end;
advance();
return block;
}
@ -151,7 +151,7 @@ Statement Parser::parseStatement()
{
Statement stmt{createWithLocation<Leave>()};
if (!m_insideFunction)
m_errorReporter.syntaxError(location(), "Keyword \"leave\" can only be used inside a function.");
m_errorReporter.syntaxError(currentLocation(), "Keyword \"leave\" can only be used inside a function.");
m_scanner->next();
return stmt;
}
@ -206,8 +206,8 @@ Statement Parser::parseStatement()
elementary = parseElementaryOperation();
}
Assignment assignment =
createWithLocation<Assignment>(std::get<Identifier>(elementary).location);
Assignment assignment;
assignment.location = std::get<Identifier>(elementary).location;
assignment.variableNames = std::move(variableNames);
expectToken(Token::AssemblyAssign);
@ -328,13 +328,13 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
YulString literal{currentLiteral()};
if (m_dialect.builtin(literal))
{
Identifier identifier{location(), literal};
Identifier identifier{currentLocation(), literal};
advance();
expectToken(Token::LParen, false);
return FunctionCall{identifier.location, identifier, {}};
}
else
ret = Identifier{location(), literal};
ret = Identifier{currentLocation(), literal};
advance();
break;
}
@ -363,7 +363,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
}
Literal literal{
location(),
currentLocation(),
kind,
YulString{currentLiteral()},
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType
@ -372,7 +372,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
if (currentToken() == Token::Colon)
{
expectToken(Token::Colon);
literal.location.end = endPosition();
literal.location.end = currentLocation().end;
literal.type = expectAsmIdentifier();
}
@ -415,7 +415,7 @@ FunctionDefinition Parser::parseFunctionDefinition()
if (m_currentForLoopComponent == ForLoopComponent::ForLoopPre)
m_errorReporter.syntaxError(
location(),
currentLocation(),
"Functions cannot be defined inside a for-loop init block."
);
@ -481,7 +481,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
ret.arguments.emplace_back(parseExpression());
}
}
ret.location.end = endPosition();
ret.location.end = currentLocation().end;
expectToken(Token::RParen);
return ret;
}
@ -494,7 +494,7 @@ TypedName Parser::parseTypedName()
if (currentToken() == Token::Colon)
{
expectToken(Token::Colon);
typedName.location.end = endPosition();
typedName.location.end = currentLocation().end;
typedName.type = expectAsmIdentifier();
}
else
@ -530,13 +530,13 @@ void Parser::checkBreakContinuePosition(string const& _which)
switch (m_currentForLoopComponent)
{
case ForLoopComponent::None:
m_errorReporter.syntaxError(location(), "Keyword \"" + _which + "\" needs to be inside a for-loop body.");
m_errorReporter.syntaxError(currentLocation(), "Keyword \"" + _which + "\" needs to be inside a for-loop body.");
break;
case ForLoopComponent::ForLoopPre:
m_errorReporter.syntaxError(location(), "Keyword \"" + _which + "\" in for-loop init block is not allowed.");
m_errorReporter.syntaxError(currentLocation(), "Keyword \"" + _which + "\" in for-loop init block is not allowed.");
break;
case ForLoopComponent::ForLoopPost:
m_errorReporter.syntaxError(location(), "Keyword \"" + _which + "\" in for-loop post block is not allowed.");
m_errorReporter.syntaxError(currentLocation(), "Keyword \"" + _which + "\" in for-loop post block is not allowed.");
break;
case ForLoopComponent::ForLoopBody:
break;

View File

@ -60,21 +60,13 @@ public:
protected:
using ElementaryOperation = std::variant<Literal, Identifier, FunctionCall>;
/// Creates an inline assembly node with the given source location.
template <class T> T createWithLocation(langutil::SourceLocation const& _loc = {}) const
/// Creates an inline assembly node with the current source location.
template <class T> T createWithLocation() const
{
T r;
r.location = _loc;
if (!r.location.hasText())
{
r.location.start = position();
r.location.end = endPosition();
}
if (!r.location.source)
r.location.source = m_scanner->charStream();
r.location = currentLocation();
return r;
}
langutil::SourceLocation location() const { return {position(), endPosition(), m_scanner->charStream()}; }
Block parseBlock();
Statement parseStatement();