diff options
author | chriseth <chris@ethereum.org> | 2018-05-09 17:06:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-09 17:06:23 +0800 |
commit | 54aa10ce367297a4b4f5ae3513dbe8c715e9fb88 (patch) | |
tree | ce5ad2cf32e2118ae92191ec648a1f522d1255c0 /libsolidity | |
parent | c79351efb05be08f3f1797c8cf6ff50988b74763 (diff) | |
parent | 840ed1e88a8d70bdbc541a1330654cb1e730e298 (diff) | |
download | dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.tar dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.tar.gz dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.tar.bz2 dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.tar.lz dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.tar.xz dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.tar.zst dexon-solidity-54aa10ce367297a4b4f5ae3513dbe8c715e9fb88.zip |
Merge pull request #4054 from ethereum/token-names
Improve parser error messages
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/inlineasm/AsmParser.cpp | 8 | ||||
-rw-r--r-- | libsolidity/parsing/Parser.cpp | 2 | ||||
-rw-r--r-- | libsolidity/parsing/ParserBase.cpp | 45 | ||||
-rw-r--r-- | libsolidity/parsing/Token.h | 11 |
4 files changed, 33 insertions, 33 deletions
diff --git a/libsolidity/inlineasm/AsmParser.cpp b/libsolidity/inlineasm/AsmParser.cpp index 7f618e07..d3b0808b 100644 --- a/libsolidity/inlineasm/AsmParser.cpp +++ b/libsolidity/inlineasm/AsmParser.cpp @@ -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) + diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 37732a37..49745e29 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -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."}); diff --git a/libsolidity/parsing/ParserBase.cpp b/libsolidity/parsing/ParserBase.cpp index 617a1779..d0c7a551 100644 --- a/libsolidity/parsing/ParserBase.cpp +++ b/libsolidity/parsing/ParserBase.cpp @@ -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(); diff --git a/libsolidity/parsing/Token.h b/libsolidity/parsing/Token.h index 805fbf5d..4d7a7bc6 100644 --- a/libsolidity/parsing/Token.h +++ b/libsolidity/parsing/Token.h @@ -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) |