Replaced ParserBase::position() and ParserBase::endPosition() with ParserBase::currentLocation().

It might be simpler to pass `SourceLocation` object instead of splitting it into `start` and `end`, and creating another SourceLocation object using the same `start` and `end` later.
This commit is contained in:
a3d4 2020-02-06 00:04:18 +01:00
parent 7fecab07a8
commit 4ec4d23886
6 changed files with 30 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -88,7 +88,7 @@ Block Parser::parseBlock()
expectToken(Token::LBrace); expectToken(Token::LBrace);
while (currentToken() != Token::RBrace) while (currentToken() != Token::RBrace)
block.statements.emplace_back(parseStatement()); block.statements.emplace_back(parseStatement());
block.location.end = endPosition(); block.location.end = currentLocation().end;
advance(); advance();
return block; return block;
} }
@ -151,7 +151,7 @@ Statement Parser::parseStatement()
{ {
Statement stmt{createWithLocation<Leave>()}; Statement stmt{createWithLocation<Leave>()};
if (!m_insideFunction) 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(); m_scanner->next();
return stmt; return stmt;
} }
@ -328,13 +328,13 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
YulString literal{currentLiteral()}; YulString literal{currentLiteral()};
if (m_dialect.builtin(literal)) if (m_dialect.builtin(literal))
{ {
Identifier identifier{location(), literal}; Identifier identifier{currentLocation(), literal};
advance(); advance();
expectToken(Token::LParen, false); expectToken(Token::LParen, false);
return FunctionCall{identifier.location, identifier, {}}; return FunctionCall{identifier.location, identifier, {}};
} }
else else
ret = Identifier{location(), literal}; ret = Identifier{currentLocation(), literal};
advance(); advance();
break; break;
} }
@ -363,7 +363,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
} }
Literal literal{ Literal literal{
location(), currentLocation(),
kind, kind,
YulString{currentLiteral()}, YulString{currentLiteral()},
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType
@ -372,7 +372,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
if (currentToken() == Token::Colon) if (currentToken() == Token::Colon)
{ {
expectToken(Token::Colon); expectToken(Token::Colon);
literal.location.end = endPosition(); literal.location.end = currentLocation().end;
literal.type = expectAsmIdentifier(); literal.type = expectAsmIdentifier();
} }
@ -415,7 +415,7 @@ FunctionDefinition Parser::parseFunctionDefinition()
if (m_currentForLoopComponent == ForLoopComponent::ForLoopPre) if (m_currentForLoopComponent == ForLoopComponent::ForLoopPre)
m_errorReporter.syntaxError( m_errorReporter.syntaxError(
location(), currentLocation(),
"Functions cannot be defined inside a for-loop init block." "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.arguments.emplace_back(parseExpression());
} }
} }
ret.location.end = endPosition(); ret.location.end = currentLocation().end;
expectToken(Token::RParen); expectToken(Token::RParen);
return ret; return ret;
} }
@ -494,7 +494,7 @@ TypedName Parser::parseTypedName()
if (currentToken() == Token::Colon) if (currentToken() == Token::Colon)
{ {
expectToken(Token::Colon); expectToken(Token::Colon);
typedName.location.end = endPosition(); typedName.location.end = currentLocation().end;
typedName.type = expectAsmIdentifier(); typedName.type = expectAsmIdentifier();
} }
else else
@ -530,13 +530,13 @@ void Parser::checkBreakContinuePosition(string const& _which)
switch (m_currentForLoopComponent) switch (m_currentForLoopComponent)
{ {
case ForLoopComponent::None: 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; break;
case ForLoopComponent::ForLoopPre: 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; break;
case ForLoopComponent::ForLoopPost: 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; break;
case ForLoopComponent::ForLoopBody: case ForLoopComponent::ForLoopBody:
break; break;

View File

@ -64,10 +64,9 @@ protected:
template <class T> T createWithLocation() const template <class T> T createWithLocation() const
{ {
T r; T r;
r.location = location(); r.location = currentLocation();
return r; return r;
} }
langutil::SourceLocation location() const { return {position(), endPosition(), m_scanner->charStream()}; }
Block parseBlock(); Block parseBlock();
Statement parseStatement(); Statement parseStatement();