From 73c99d15cdf5ef0589096d6d42025a21502fc8d9 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 2 May 2018 20:18:11 +0100 Subject: [PATCH 1/2] Remove useless helper expectAssignmentOperator in parser --- libsolidity/parsing/Parser.cpp | 16 ++-------------- libsolidity/parsing/Parser.h | 1 - 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index a9ee90160..411466cd9 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1194,7 +1194,8 @@ ASTPointer Parser::parseExpression( ASTPointer expression = parseBinaryExpression(4, _lookAheadIndexAccessStructure); if (Token::isAssignmentOp(m_scanner->currentToken())) { - Token::Value assignmentOperator = expectAssignmentOperator(); + Token::Value assignmentOperator = m_scanner->currentToken(); + m_scanner->next(); ASTPointer rightHandSide = parseExpression(); ASTNodeFactory nodeFactory(*this, expression); nodeFactory.setEndPositionFromNode(rightHandSide); @@ -1613,19 +1614,6 @@ string Parser::currentTokenName() return Token::name(token); } -Token::Value Parser::expectAssignmentOperator() -{ - Token::Value op = m_scanner->currentToken(); - if (!Token::isAssignmentOp(op)) - fatalParserError( - string("Expected assignment operator, got '") + - currentTokenName() + - string("'") - ); - m_scanner->next(); - return op; -} - ASTPointer Parser::expectIdentifierToken() { Token::Value id = m_scanner->currentToken(); diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h index c42542312..2679af9fd 100644 --- a/libsolidity/parsing/Parser.h +++ b/libsolidity/parsing/Parser.h @@ -166,7 +166,6 @@ private: ASTPointer expressionFromIndexAccessStructure(IndexAccessedPath const& _pathAndIndices); std::string currentTokenName(); - Token::Value expectAssignmentOperator(); ASTPointer expectIdentifierToken(); ASTPointer getLiteralAndAdvance(); ///@} From ed9f80690bde53e56c6ef5cb046fb20713f3f780 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 2 May 2018 20:42:26 +0100 Subject: [PATCH 2/2] Simplify expectIdentifierToken by using expectToken --- libsolidity/parsing/Parser.cpp | 21 ++----------------- libsolidity/parsing/Parser.h | 1 - libsolidity/parsing/ParserBase.cpp | 5 +++-- libsolidity/parsing/ParserBase.h | 2 +- test/libsolidity/SolidityParser.cpp | 2 +- test/libsolidity/StandardCompiler.cpp | 4 ++-- .../parsing/constant_is_keyword.sol | 2 +- .../syntaxTests/parsing/external_variable.sol | 2 +- ...ocation_specifiers_for_state_variables.sol | 2 +- .../missing_parameter_name_in_named_args.sol | 2 +- .../missing_variable_name_in_declaration.sol | 2 +- .../syntaxTests/parsing/payable_accessor.sol | 2 +- .../syntaxTests/parsing/var_array.sol | 2 +- 13 files changed, 16 insertions(+), 33 deletions(-) diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 411466cd9..37732a370 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1602,27 +1602,10 @@ ASTPointer Parser::createEmptyParameterList() return nodeFactory.createNode(vector>()); } -string Parser::currentTokenName() -{ - Token::Value token = m_scanner->currentToken(); - if (Token::isElementaryTypeName(token)) //for the sake of accuracy in reporting - { - ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - return elemTypeName.toString(); - } - else - return Token::name(token); -} - ASTPointer Parser::expectIdentifierToken() { - Token::Value id = m_scanner->currentToken(); - if (id != Token::Identifier) - fatalParserError( - string("Expected identifier, got '") + - currentTokenName() + - string("'") - ); + // do not advance on success + expectToken(Token::Identifier, false); return getLiteralAndAdvance(); } diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h index 2679af9fd..7f02d895b 100644 --- a/libsolidity/parsing/Parser.h +++ b/libsolidity/parsing/Parser.h @@ -165,7 +165,6 @@ private: /// @returns an expression parsed in look-ahead fashion from something like "a.b[8][2**70]". ASTPointer expressionFromIndexAccessStructure(IndexAccessedPath const& _pathAndIndices); - std::string currentTokenName(); ASTPointer expectIdentifierToken(); ASTPointer getLiteralAndAdvance(); ///@} diff --git a/libsolidity/parsing/ParserBase.cpp b/libsolidity/parsing/ParserBase.cpp index 5b83c5bdc..617a17799 100644 --- a/libsolidity/parsing/ParserBase.cpp +++ b/libsolidity/parsing/ParserBase.cpp @@ -63,7 +63,7 @@ Token::Value ParserBase::advance() return m_scanner->next(); } -void ParserBase::expectToken(Token::Value _value) +void ParserBase::expectToken(Token::Value _value, bool _advance) { Token::Value tok = m_scanner->currentToken(); if (tok != _value) @@ -98,7 +98,8 @@ void ParserBase::expectToken(Token::Value _value) string("'") ); } - m_scanner->next(); + if (_advance) + m_scanner->next(); } void ParserBase::increaseRecursionDepth() diff --git a/libsolidity/parsing/ParserBase.h b/libsolidity/parsing/ParserBase.h index fd0de0d10..b28e1b1bd 100644 --- a/libsolidity/parsing/ParserBase.h +++ b/libsolidity/parsing/ParserBase.h @@ -63,7 +63,7 @@ protected: ///@{ ///@name Helper functions /// If current token value is not _value, throw exception otherwise advance token. - void expectToken(Token::Value _value); + void expectToken(Token::Value _value, bool _advance = true); Token::Value currentToken() const; Token::Value peekNextToken() const; std::string currentLiteral() const; diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 1ebf80105..f428f8925 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -992,7 +992,7 @@ BOOST_AUTO_TEST_CASE(keyword_is_reserved) for (const auto& keyword: keywords) { auto text = std::string("contract ") + keyword + " {}"; - CHECK_PARSE_ERROR(text.c_str(), "Expected identifier"); + CHECK_PARSE_ERROR(text.c_str(), "Expected token Identifier got reserved keyword"); } } diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp index 74bf01b2f..f816905c6 100644 --- a/test/libsolidity/StandardCompiler.cpp +++ b/test/libsolidity/StandardCompiler.cpp @@ -326,8 +326,8 @@ BOOST_AUTO_TEST_CASE(compilation_error) { BOOST_CHECK_EQUAL( dev::jsonCompactPrint(error), - "{\"component\":\"general\",\"formattedMessage\":\"fileA:1:23: ParserError: Expected identifier, got 'RBrace'\\n" - "contract A { function }\\n ^\\n\",\"message\":\"Expected identifier, got 'RBrace'\"," + "{\"component\":\"general\",\"formattedMessage\":\"fileA:1:23: ParserError: Expected token Identifier got 'RBrace'\\n" + "contract A { function }\\n ^\\n\",\"message\":\"Expected token Identifier got 'RBrace'\"," "\"severity\":\"error\",\"sourceLocation\":{\"end\":22,\"file\":\"fileA\",\"start\":22},\"type\":\"ParserError\"}" ); } diff --git a/test/libsolidity/syntaxTests/parsing/constant_is_keyword.sol b/test/libsolidity/syntaxTests/parsing/constant_is_keyword.sol index 40e237d2d..59fe8518d 100644 --- a/test/libsolidity/syntaxTests/parsing/constant_is_keyword.sol +++ b/test/libsolidity/syntaxTests/parsing/constant_is_keyword.sol @@ -2,4 +2,4 @@ contract Foo { uint constant = 4; } // ---- -// ParserError: (30-30): Expected identifier, got 'Assign' +// ParserError: (30-30): Expected token Identifier got 'Assign' diff --git a/test/libsolidity/syntaxTests/parsing/external_variable.sol b/test/libsolidity/syntaxTests/parsing/external_variable.sol index 5188875f8..1d2e65e65 100644 --- a/test/libsolidity/syntaxTests/parsing/external_variable.sol +++ b/test/libsolidity/syntaxTests/parsing/external_variable.sol @@ -2,4 +2,4 @@ contract c { uint external x; } // ---- -// ParserError: (19-19): Expected identifier, got 'External' +// ParserError: (19-19): Expected token Identifier got 'External' diff --git a/test/libsolidity/syntaxTests/parsing/location_specifiers_for_state_variables.sol b/test/libsolidity/syntaxTests/parsing/location_specifiers_for_state_variables.sol index 1b525506c..0fc85177f 100644 --- a/test/libsolidity/syntaxTests/parsing/location_specifiers_for_state_variables.sol +++ b/test/libsolidity/syntaxTests/parsing/location_specifiers_for_state_variables.sol @@ -2,4 +2,4 @@ contract Foo { uint[] memory x; } // ---- -// ParserError: (23-23): Expected identifier, got 'Memory' +// ParserError: (23-23): Expected token Identifier got 'Memory' diff --git a/test/libsolidity/syntaxTests/parsing/missing_parameter_name_in_named_args.sol b/test/libsolidity/syntaxTests/parsing/missing_parameter_name_in_named_args.sol index 0606e2c7c..3604f3b21 100644 --- a/test/libsolidity/syntaxTests/parsing/missing_parameter_name_in_named_args.sol +++ b/test/libsolidity/syntaxTests/parsing/missing_parameter_name_in_named_args.sol @@ -3,4 +3,4 @@ contract test { function b() returns (uint r) { r = a({: 1, : 2, : 3}); } } // ---- -// ParserError: (143-143): Expected identifier, got 'Colon' +// ParserError: (143-143): Expected token Identifier got 'Colon' diff --git a/test/libsolidity/syntaxTests/parsing/missing_variable_name_in_declaration.sol b/test/libsolidity/syntaxTests/parsing/missing_variable_name_in_declaration.sol index fd3067e30..bb1d015bc 100644 --- a/test/libsolidity/syntaxTests/parsing/missing_variable_name_in_declaration.sol +++ b/test/libsolidity/syntaxTests/parsing/missing_variable_name_in_declaration.sol @@ -2,4 +2,4 @@ contract test { uint256 ; } // ---- -// ParserError: (28-28): Expected identifier, got 'Semicolon' +// ParserError: (28-28): Expected token Identifier got 'Semicolon' diff --git a/test/libsolidity/syntaxTests/parsing/payable_accessor.sol b/test/libsolidity/syntaxTests/parsing/payable_accessor.sol index a73108ad5..44b04afdf 100644 --- a/test/libsolidity/syntaxTests/parsing/payable_accessor.sol +++ b/test/libsolidity/syntaxTests/parsing/payable_accessor.sol @@ -2,4 +2,4 @@ contract test { uint payable x; } // ---- -// ParserError: (22-22): Expected identifier, got 'Payable' +// ParserError: (22-22): Expected token Identifier got 'Payable' diff --git a/test/libsolidity/syntaxTests/parsing/var_array.sol b/test/libsolidity/syntaxTests/parsing/var_array.sol index 60f6dc281..86fc4fcbf 100644 --- a/test/libsolidity/syntaxTests/parsing/var_array.sol +++ b/test/libsolidity/syntaxTests/parsing/var_array.sol @@ -2,4 +2,4 @@ contract Foo { function f() { var[] a; } } // ---- -// ParserError: (34-34): Expected identifier, got 'LBrack' +// ParserError: (34-34): Expected token Identifier got 'LBrack'