From 2fefe3b5497172a16094014b66e148b707bc7492 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 24 May 2017 22:55:29 +0100 Subject: Move Solidity specific methods from ParserBase to the Solidity Parser --- libsolidity/parsing/Parser.cpp | 56 ++++++++++++++++++++++++++++++++++++++ libsolidity/parsing/Parser.h | 4 +++ libsolidity/parsing/ParserBase.cpp | 56 -------------------------------------- libsolidity/parsing/ParserBase.h | 4 --- 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 72150a3e..470586cf 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1437,5 +1437,61 @@ ASTPointer Parser::createEmptyParameterList() return nodeFactory.createNode(vector>()); } +Token::Value Parser::expectAssignmentOperator() +{ + Token::Value op = m_scanner->currentToken(); + if (!Token::isAssignmentOp(op)) + { + if (Token::isElementaryTypeName(op)) //for the sake of accuracy in reporting + { + ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); + fatalParserError( + string("Expected assignment operator, got '") + + elemTypeName.toString() + + string("'") + ); + } + else + fatalParserError( + string("Expected assignment operator, got '") + + string(Token::name(m_scanner->currentToken())) + + string("'") + ); + } + m_scanner->next(); + return op; +} + +ASTPointer Parser::expectIdentifierToken() +{ + Token::Value id = m_scanner->currentToken(); + if (id != Token::Identifier) + { + if (Token::isElementaryTypeName(id)) //for the sake of accuracy in reporting + { + ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); + fatalParserError( + string("Expected identifier, got '") + + elemTypeName.toString() + + string("'") + ); + } + else + fatalParserError( + string("Expected identifier, got '") + + string(Token::name(id)) + + string("'") + ); + } + return getLiteralAndAdvance(); +} + +ASTPointer Parser::getLiteralAndAdvance() +{ + ASTPointer identifier = make_shared(m_scanner->currentLiteral()); + m_scanner->next(); + return identifier; +} + } } diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h index 282617ab..05e99566 100644 --- a/libsolidity/parsing/Parser.h +++ b/libsolidity/parsing/Parser.h @@ -154,6 +154,10 @@ private: std::vector> const& _path, std::vector, SourceLocation>> const& _indices ); + + Token::Value expectAssignmentOperator(); + ASTPointer expectIdentifierToken(); + ASTPointer getLiteralAndAdvance(); ///@} /// Creates an empty ParameterList at the current location (used if parameters can be omitted). diff --git a/libsolidity/parsing/ParserBase.cpp b/libsolidity/parsing/ParserBase.cpp index 87d47f4b..ac103bda 100644 --- a/libsolidity/parsing/ParserBase.cpp +++ b/libsolidity/parsing/ParserBase.cpp @@ -80,62 +80,6 @@ void ParserBase::expectToken(Token::Value _value) m_scanner->next(); } -Token::Value ParserBase::expectAssignmentOperator() -{ - Token::Value op = m_scanner->currentToken(); - if (!Token::isAssignmentOp(op)) - { - if (Token::isElementaryTypeName(op)) //for the sake of accuracy in reporting - { - ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - fatalParserError( - string("Expected assignment operator, got '") + - elemTypeName.toString() + - string("'") - ); - } - else - fatalParserError( - string("Expected assignment operator, got '") + - string(Token::name(m_scanner->currentToken())) + - string("'") - ); - } - m_scanner->next(); - return op; -} - -ASTPointer ParserBase::expectIdentifierToken() -{ - Token::Value id = m_scanner->currentToken(); - if (id != Token::Identifier) - { - if (Token::isElementaryTypeName(id)) //for the sake of accuracy in reporting - { - ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - fatalParserError( - string("Expected identifier, got '") + - elemTypeName.toString() + - string("'") - ); - } - else - fatalParserError( - string("Expected identifier, got '") + - string(Token::name(id)) + - string("'") - ); - } - return getLiteralAndAdvance(); -} - -ASTPointer ParserBase::getLiteralAndAdvance() -{ - ASTPointer identifier = make_shared(m_scanner->currentLiteral()); - m_scanner->next(); - return identifier; -} - void ParserBase::parserError(string const& _description) { auto err = make_shared(Error::Type::ParserError); diff --git a/libsolidity/parsing/ParserBase.h b/libsolidity/parsing/ParserBase.h index dfb7cab7..768b3bbf 100644 --- a/libsolidity/parsing/ParserBase.h +++ b/libsolidity/parsing/ParserBase.h @@ -25,7 +25,6 @@ #include #include #include -#include namespace dev { @@ -52,9 +51,6 @@ protected: ///@name Helper functions /// If current token value is not _value, throw exception otherwise advance token. void expectToken(Token::Value _value); - Token::Value expectAssignmentOperator(); - ASTPointer expectIdentifierToken(); - ASTPointer getLiteralAndAdvance(); ///@} /// Creates a @ref ParserError and annotates it with the current position and the -- cgit v1.2.3 From 3459052ae83ac08082855d41e21c297ac5f852b5 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 24 May 2017 23:03:15 +0100 Subject: Add scanner helpers to ParserBase --- libsolidity/parsing/ParserBase.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libsolidity/parsing/ParserBase.h b/libsolidity/parsing/ParserBase.h index 768b3bbf..18b39a5e 100644 --- a/libsolidity/parsing/ParserBase.h +++ b/libsolidity/parsing/ParserBase.h @@ -24,6 +24,7 @@ #include #include +#include #include namespace dev @@ -46,11 +47,14 @@ protected: /// End position of the current token int endPosition() const; - ///@{ ///@name Helper functions /// If current token value is not _value, throw exception otherwise advance token. void expectToken(Token::Value _value); + Token::Value currentToken() const { return m_scanner->currentToken(); } + Token::Value peekNextToken() const { return m_scanner->peekNextToken(); } + std::string currentLiteral() const { return m_scanner->currentLiteral(); } + Token::Value advance() { return m_scanner->next(); } ///@} /// Creates a @ref ParserError and annotates it with the current position and the -- cgit v1.2.3 From 9337003d5e623cd52c55001222b629543c859cd7 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 25 May 2017 00:13:51 +0100 Subject: Use the new ParserBase API in AsmParser --- libsolidity/inlineasm/AsmParser.cpp | 66 ++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/libsolidity/inlineasm/AsmParser.cpp b/libsolidity/inlineasm/AsmParser.cpp index 605d27be..80409c63 100644 --- a/libsolidity/inlineasm/AsmParser.cpp +++ b/libsolidity/inlineasm/AsmParser.cpp @@ -50,16 +50,16 @@ assembly::Block Parser::parseBlock() { assembly::Block block = createWithLocation(); expectToken(Token::LBrace); - while (m_scanner->currentToken() != Token::RBrace) + while (currentToken() != Token::RBrace) block.statements.emplace_back(parseStatement()); block.location.end = endPosition(); - m_scanner->next(); + advance(); return block; } assembly::Statement Parser::parseStatement() { - switch (m_scanner->currentToken()) + switch (currentToken()) { case Token::Let: return parseVariableDeclaration(); @@ -92,10 +92,10 @@ assembly::Statement Parser::parseStatement() if (m_julia) break; assembly::StackAssignment assignment = createWithLocation(); - m_scanner->next(); + advance(); expectToken(Token::Colon); assignment.variableName.location = location(); - assignment.variableName.name = m_scanner->currentLiteral(); + assignment.variableName.name = currentLiteral(); if (!m_julia && instructions().count(assignment.variableName.name)) fatalParserError("Identifier expected, got instruction name."); assignment.location.end = endPosition(); @@ -110,7 +110,7 @@ assembly::Statement Parser::parseStatement() // literal, // identifier (might turn into label or functional assignment) Statement statement(parseElementaryOperation(false)); - switch (m_scanner->currentToken()) + switch (currentToken()) { case Token::LParen: return parseCall(std::move(statement)); @@ -119,15 +119,15 @@ assembly::Statement Parser::parseStatement() if (statement.type() != typeid(assembly::Identifier)) fatalParserError("Label name / variable name must precede \":\"."); assembly::Identifier const& identifier = boost::get(statement); - m_scanner->next(); + advance(); // identifier:=: should be parsed as identifier: =: (i.e. a label), // while identifier:= (being followed by a non-colon) as identifier := (assignment). - if (m_scanner->currentToken() == Token::Assign && m_scanner->peekNextToken() != Token::Colon) + if (currentToken() == Token::Assign && peekNextToken() != Token::Colon) { assembly::Assignment assignment = createWithLocation(identifier.location); if (!m_julia && instructions().count(identifier.name)) fatalParserError("Cannot use instruction names for identifier names."); - m_scanner->next(); + advance(); assignment.variableName = identifier; assignment.value.reset(new Statement(parseExpression())); assignment.location.end = locationOf(*assignment.value).end; @@ -174,7 +174,7 @@ assembly::Case Parser::parseCase() assembly::Statement Parser::parseExpression() { Statement operation = parseElementaryOperation(true); - if (m_scanner->currentToken() == Token::LParen) + if (currentToken() == Token::LParen) return parseCall(std::move(operation)); else return operation; @@ -207,7 +207,7 @@ std::map const& Parser::instructions() assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher) { Statement ret; - switch (m_scanner->currentToken()) + switch (currentToken()) { case Token::Identifier: case Token::Return: @@ -215,14 +215,14 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher) case Token::Address: { string literal; - if (m_scanner->currentToken() == Token::Return) + if (currentToken() == Token::Return) literal = "return"; - else if (m_scanner->currentToken() == Token::Byte) + else if (currentToken() == Token::Byte) literal = "byte"; - else if (m_scanner->currentToken() == Token::Address) + else if (currentToken() == Token::Address) literal = "address"; else - literal = m_scanner->currentLiteral(); + literal = currentLiteral(); // first search the set of instructions. if (!m_julia && instructions().count(literal)) { @@ -237,7 +237,7 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher) } else ret = Identifier{location(), literal}; - m_scanner->next(); + advance(); break; } case Token::StringLiteral: @@ -246,7 +246,7 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher) case Token::FalseLiteral: { LiteralKind kind = LiteralKind::Number; - switch (m_scanner->currentToken()) + switch (currentToken()) { case Token::StringLiteral: kind = LiteralKind::String; @@ -265,10 +265,10 @@ assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher) Literal literal{ location(), kind, - m_scanner->currentLiteral(), + currentLiteral(), "" }; - m_scanner->next(); + advance(); if (m_julia) { expectToken(Token::Colon); @@ -297,7 +297,7 @@ assembly::VariableDeclaration Parser::parseVariableDeclaration() while (true) { varDecl.variables.emplace_back(parseTypedName()); - if (m_scanner->currentToken() == Token::Comma) + if (currentToken() == Token::Comma) expectToken(Token::Comma); else break; @@ -315,22 +315,22 @@ assembly::FunctionDefinition Parser::parseFunctionDefinition() expectToken(Token::Function); funDef.name = expectAsmIdentifier(); expectToken(Token::LParen); - while (m_scanner->currentToken() != Token::RParen) + while (currentToken() != Token::RParen) { funDef.arguments.emplace_back(parseTypedName()); - if (m_scanner->currentToken() == Token::RParen) + if (currentToken() == Token::RParen) break; expectToken(Token::Comma); } expectToken(Token::RParen); - if (m_scanner->currentToken() == Token::Sub) + if (currentToken() == Token::Sub) { expectToken(Token::Sub); expectToken(Token::GreaterThan); while (true) { funDef.returns.emplace_back(parseTypedName()); - if (m_scanner->currentToken() == Token::LBrace) + if (currentToken() == Token::LBrace) break; expectToken(Token::Comma); } @@ -359,7 +359,7 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction) for (unsigned i = 0; i < args; ++i) { /// check for premature closing parentheses - if (m_scanner->currentToken() == Token::RParen) + if (currentToken() == Token::RParen) fatalParserError(string( "Expected expression (" + instrInfo.name + @@ -371,7 +371,7 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction) ret.arguments.emplace_back(parseExpression()); if (i != args - 1) { - if (m_scanner->currentToken() != Token::Comma) + if (currentToken() != Token::Comma) fatalParserError(string( "Expected comma (" + instrInfo.name + @@ -380,11 +380,11 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction) " arguments)" )); else - m_scanner->next(); + advance(); } } ret.location.end = endPosition(); - if (m_scanner->currentToken() == Token::Comma) + if (currentToken() == Token::Comma) fatalParserError( string("Expected ')' (" + instrInfo.name + " expects " + boost::lexical_cast(args) + " arguments)") ); @@ -397,10 +397,10 @@ assembly::Statement Parser::parseCall(assembly::Statement&& _instruction) ret.functionName = std::move(boost::get(_instruction)); ret.location = ret.functionName.location; expectToken(Token::LParen); - while (m_scanner->currentToken() != Token::RParen) + while (currentToken() != Token::RParen) { ret.arguments.emplace_back(parseExpression()); - if (m_scanner->currentToken() == Token::RParen) + if (currentToken() == Token::RParen) break; expectToken(Token::Comma); } @@ -433,12 +433,12 @@ TypedName Parser::parseTypedName() string Parser::expectAsmIdentifier() { - string name = m_scanner->currentLiteral(); + string name = currentLiteral(); if (m_julia) { - if (m_scanner->currentToken() == Token::Bool) + if (currentToken() == Token::Bool) { - m_scanner->next(); + advance(); return name; } } -- cgit v1.2.3 From 48f58aa3f9b4d8962b2c6f1bd7967b119893a889 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 26 May 2017 21:00:08 +0100 Subject: Remove duplicate code --- libsolidity/parsing/Parser.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 470586cf..dca49360 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1442,21 +1442,20 @@ Token::Value Parser::expectAssignmentOperator() Token::Value op = m_scanner->currentToken(); if (!Token::isAssignmentOp(op)) { + string tokenName; if (Token::isElementaryTypeName(op)) //for the sake of accuracy in reporting { ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - fatalParserError( - string("Expected assignment operator, got '") + - elemTypeName.toString() + - string("'") - ); + tokenName = elemTypeName.toString(); } else - fatalParserError( - string("Expected assignment operator, got '") + - string(Token::name(m_scanner->currentToken())) + - string("'") - ); + tokenName = Token::name(m_scanner->currentToken()); + + fatalParserError( + string("Expected assignment operator, got '") + + tokenName + + string("'") + ); } m_scanner->next(); return op; @@ -1467,21 +1466,20 @@ ASTPointer Parser::expectIdentifierToken() Token::Value id = m_scanner->currentToken(); if (id != Token::Identifier) { + string tokenName; if (Token::isElementaryTypeName(id)) //for the sake of accuracy in reporting { ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - fatalParserError( - string("Expected identifier, got '") + - elemTypeName.toString() + - string("'") - ); + tokenName = elemTypeName.toString(); } else - fatalParserError( - string("Expected identifier, got '") + - string(Token::name(id)) + - string("'") - ); + tokenName = Token::name(id); + + fatalParserError( + string("Expected identifier, got '") + + tokenName + + string("'") + ); } return getLiteralAndAdvance(); } -- cgit v1.2.3 From 751c97ca19ed0bb69e67222a1edd61e7d227b30f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 26 May 2017 21:02:24 +0100 Subject: Pull out common code to a helper --- libsolidity/parsing/Parser.cpp | 38 ++++++++++++++------------------------ libsolidity/parsing/Parser.h | 1 + 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index dca49360..5b318b2b 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1437,26 +1437,27 @@ 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); +} + Token::Value Parser::expectAssignmentOperator() { Token::Value op = m_scanner->currentToken(); if (!Token::isAssignmentOp(op)) - { - string tokenName; - if (Token::isElementaryTypeName(op)) //for the sake of accuracy in reporting - { - ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - tokenName = elemTypeName.toString(); - } - else - tokenName = Token::name(m_scanner->currentToken()); - fatalParserError( string("Expected assignment operator, got '") + - tokenName + + currentTokenName() + string("'") ); - } m_scanner->next(); return op; } @@ -1465,22 +1466,11 @@ ASTPointer Parser::expectIdentifierToken() { Token::Value id = m_scanner->currentToken(); if (id != Token::Identifier) - { - string tokenName; - if (Token::isElementaryTypeName(id)) //for the sake of accuracy in reporting - { - ElementaryTypeNameToken elemTypeName = m_scanner->currentElementaryTypeNameToken(); - tokenName = elemTypeName.toString(); - } - else - tokenName = Token::name(id); - fatalParserError( string("Expected identifier, got '") + - tokenName + + currentTokenName() + string("'") ); - } return getLiteralAndAdvance(); } diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h index 05e99566..5ec3dbed 100644 --- a/libsolidity/parsing/Parser.h +++ b/libsolidity/parsing/Parser.h @@ -155,6 +155,7 @@ private: std::vector, SourceLocation>> const& _indices ); + std::string currentTokenName(); Token::Value expectAssignmentOperator(); ASTPointer expectIdentifierToken(); ASTPointer getLiteralAndAdvance(); -- cgit v1.2.3