mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #4054 from ethereum/token-names
Improve parser error messages
This commit is contained in:
@@ -276,7 +276,7 @@ assembly::Expression Parser::parseExpression()
|
||||
int args = instructionInfo(instr.instruction).args;
|
||||
if (args > 0 && currentToken() != Token::LParen)
|
||||
fatalParserError(string(
|
||||
"Expected token \"(\" (\"" +
|
||||
"Expected '(' (instruction \"" +
|
||||
instructionNames().at(instr.instruction) +
|
||||
"\" expects " +
|
||||
boost::lexical_cast<string>(args) +
|
||||
@@ -504,7 +504,7 @@ assembly::Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
|
||||
/// check for premature closing parentheses
|
||||
if (currentToken() == Token::RParen)
|
||||
fatalParserError(string(
|
||||
"Expected expression (\"" +
|
||||
"Expected expression (instruction \"" +
|
||||
instructionNames().at(instr) +
|
||||
"\" expects " +
|
||||
boost::lexical_cast<string>(args) +
|
||||
@@ -516,7 +516,7 @@ assembly::Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
|
||||
{
|
||||
if (currentToken() != Token::Comma)
|
||||
fatalParserError(string(
|
||||
"Expected comma (\"" +
|
||||
"Expected ',' (instruction \"" +
|
||||
instructionNames().at(instr) +
|
||||
"\" expects " +
|
||||
boost::lexical_cast<string>(args) +
|
||||
@@ -529,7 +529,7 @@ assembly::Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
|
||||
ret.location.end = endPosition();
|
||||
if (currentToken() == Token::Comma)
|
||||
fatalParserError(string(
|
||||
"Expected ')' (\"" +
|
||||
"Expected ')' (instruction \"" +
|
||||
instructionNames().at(instr) +
|
||||
"\" expects " +
|
||||
boost::lexical_cast<string>(args) +
|
||||
|
||||
@@ -528,7 +528,7 @@ ASTPointer<EnumDefinition> Parser::parseEnumDefinition()
|
||||
break;
|
||||
expectToken(Token::Comma);
|
||||
if (m_scanner->currentToken() != Token::Identifier)
|
||||
fatalParserError(string("Expected Identifier after ','"));
|
||||
fatalParserError(string("Expected identifier after ','"));
|
||||
}
|
||||
if (members.size() == 0)
|
||||
parserError({"enum with no members is not allowed."});
|
||||
|
||||
@@ -68,35 +68,24 @@ void ParserBase::expectToken(Token::Value _value, bool _advance)
|
||||
Token::Value tok = m_scanner->currentToken();
|
||||
if (tok != _value)
|
||||
{
|
||||
if (Token::isReservedKeyword(tok))
|
||||
auto tokenName = [this](Token::Value _token)
|
||||
{
|
||||
fatalParserError(
|
||||
string("Expected token ") +
|
||||
string(Token::name(_value)) +
|
||||
string(" got reserved keyword '") +
|
||||
string(Token::name(tok)) +
|
||||
string("'")
|
||||
);
|
||||
}
|
||||
else if (Token::isElementaryTypeName(tok)) //for the sake of accuracy in reporting
|
||||
{
|
||||
ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken();
|
||||
fatalParserError(
|
||||
string("Expected token ") +
|
||||
string(Token::name(_value)) +
|
||||
string(" got '") +
|
||||
elemTypeName.toString() +
|
||||
string("'")
|
||||
);
|
||||
}
|
||||
else
|
||||
fatalParserError(
|
||||
string("Expected token ") +
|
||||
string(Token::name(_value)) +
|
||||
string(" got '") +
|
||||
string(Token::name(m_scanner->currentToken())) +
|
||||
string("'")
|
||||
);
|
||||
if (_token == Token::Identifier)
|
||||
return string("identifier");
|
||||
else if (_token == Token::EOS)
|
||||
return string("end of source");
|
||||
else if (Token::isReservedKeyword(_token))
|
||||
return string("reserved keyword '") + Token::friendlyName(_token) + "'";
|
||||
else if (Token::isElementaryTypeName(_token)) //for the sake of accuracy in reporting
|
||||
{
|
||||
ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken();
|
||||
return string("'") + elemTypeName.toString() + "'";
|
||||
}
|
||||
else
|
||||
return string("'") + Token::friendlyName(_token) + "'";
|
||||
};
|
||||
|
||||
fatalParserError(string("Expected ") + tokenName(_value) + string(" but got ") + tokenName(tok));
|
||||
}
|
||||
if (_advance)
|
||||
m_scanner->next();
|
||||
|
||||
@@ -304,6 +304,17 @@ public:
|
||||
return m_string[tok];
|
||||
}
|
||||
|
||||
static std::string friendlyName(Value tok)
|
||||
{
|
||||
char const* ret = toString(tok);
|
||||
if (ret == nullptr)
|
||||
{
|
||||
ret = name(tok);
|
||||
solAssert(ret != nullptr, "");
|
||||
}
|
||||
return std::string(ret);
|
||||
}
|
||||
|
||||
// @returns the precedence > 0 for binary and compare
|
||||
// operators; returns 0 otherwise.
|
||||
static int precedence(Value tok)
|
||||
|
||||
Reference in New Issue
Block a user