From 574e48b0b51bbf890702a0c0fbae799473945bdb Mon Sep 17 00:00:00 2001 From: RJ Catalano Date: Mon, 14 Dec 2015 17:40:35 -0600 Subject: [PATCH] Inline array declarations complete --- libsolidity/ast/AST.h | 22 ++++++++++++++++++++-- libsolidity/parsing/Parser.cpp | 22 ++++++++++++++++++++++ test/libsolidity/SolidityParser.cpp | 10 ++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 1217d9455..0c37dad39 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -1126,9 +1126,10 @@ private: ASTPointer m_rightHandSide; }; + /** - * Tuple or just parenthesized expression. - * Examples: (1, 2), (x,), (x), () + * Tuple, parenthesized expression, or bracketed expression. + * Examples: (1, 2), (x,), (x), (), [1, 2], * Individual components might be empty shared pointers (as in the second example). * The respective types in lvalue context are: 2-tuple, 2-tuple (with wildcard), type of x, 0-tuple * Not in lvalue context: 2-tuple, _1_-tuple, type of x, 0-tuple. @@ -1150,6 +1151,23 @@ private: std::vector> m_components; }; +class InlineArrayExpression: public Expression +{ +public: + InlineArrayExpression( + SourceLocation const& _location, + std::vector> const& _components + ): + Expression(_location), m_components(_components) {} + virtual void accept(ASTVisitor& _visitor) override; + virtual void accept(ASTConstVisitor& _visitor) const override; + + std::vector> const& components() const { return m_components; } + +private: + std::vector> m_components; +}; + /** * Operation involving a unary operator, pre- or postfix. * Examples: ++i, delete x or !true diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 2b886121a..50e4d29da 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1056,6 +1056,28 @@ ASTPointer Parser::parsePrimaryExpression() expectToken(Token::RParen); return nodeFactory.createNode(components); } + case Token::LBrack: + { + // Inline array expression + // Special cases: [] is empty tuple type, (x) is not a real tuple, (x,) is one-dimensional tuple + m_scanner->next(); + vector> components; + if (m_scanner->currentToken() != Token::RBrack) + while (true) + { + if (m_scanner->currentToken() != Token::Comma && m_scanner->currentToken() != Token::RBrack) + components.push_back(parseExpression()); + else + components.push_back(ASTPointer()); + if (m_scanner->currentToken() == Token::RBrack) + break; + else if (m_scanner->currentToken() == Token::Comma) + m_scanner->next(); + } + nodeFactory.markEndPosition(); + expectToken(Token::RBrack); + return nodeFactory.createNode(components); + } default: if (Token::isElementaryTypeName(token)) { diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index fd9076c3b..53ffb618b 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -1047,6 +1047,16 @@ BOOST_AUTO_TEST_CASE(using_for) BOOST_CHECK(successParse(text)); } +BOOST_AUTO_TEST_CASE(inline_array_declaration) +{ + char const* text = R"( + contract C { + uint[] x = [0, 1, 2, 3]; + } + )"; + BOOST_CHECK(successParse(text)); +} + BOOST_AUTO_TEST_SUITE_END() }