diff options
author | chriseth <c@ethdev.com> | 2015-12-17 08:21:22 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-12-17 08:21:22 +0800 |
commit | eb11c7f43f2fdfe608716db8cf58775bc22ad440 (patch) | |
tree | f00e1a47c786cc4eaae63c8860a8652e22030779 | |
parent | 591a4f1ff44af20fce9bafebf0fa0607e2bdfcc0 (diff) | |
parent | ed1dd50acda058a9c0c5b9c43776fba3642e23e2 (diff) | |
download | dexon-solidity-eb11c7f43f2fdfe608716db8cf58775bc22ad440.tar.gz dexon-solidity-eb11c7f43f2fdfe608716db8cf58775bc22ad440.tar.zst dexon-solidity-eb11c7f43f2fdfe608716db8cf58775bc22ad440.zip |
Merge pull request #305 from VoR0220/inlineArrays
Parsing for Inline Arrays is passing
-rw-r--r-- | libsolidity/analysis/TypeChecker.cpp | 1 | ||||
-rw-r--r-- | libsolidity/ast/AST.h | 14 | ||||
-rw-r--r-- | libsolidity/parsing/Parser.cpp | 22 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 26 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 42 |
5 files changed, 94 insertions, 11 deletions
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 01de5eb0..9718bf75 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -780,6 +780,7 @@ bool TypeChecker::visit(Assignment const& _assignment) bool TypeChecker::visit(TupleExpression const& _tuple) { vector<ASTPointer<Expression>> const& components = _tuple.components(); + solAssert(!_tuple.isInlineArray(), "Tuple type not properly declared"); TypePointers types; if (_tuple.annotation().lValueRequested) { diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 75cb9ab2..1ba4f65b 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -1127,9 +1127,10 @@ private: ASTPointer<Expression> 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. @@ -1139,16 +1140,21 @@ class TupleExpression: public Expression public: TupleExpression( SourceLocation const& _location, - std::vector<ASTPointer<Expression>> const& _components + std::vector<ASTPointer<Expression>> const& _components, + bool _isArray ): - Expression(_location), m_components(_components) {} + Expression(_location), + m_components(_components), + m_isArray(_isArray) {} virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; std::vector<ASTPointer<Expression>> const& components() const { return m_components; } + bool isInlineArray() const { return m_isArray; } private: std::vector<ASTPointer<Expression>> m_components; + bool m_isArray; }; /** diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 2b886121..caf38b1d 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1035,27 +1035,35 @@ ASTPointer<Expression> Parser::parsePrimaryExpression() expression = nodeFactory.createNode<Identifier>(getLiteralAndAdvance()); break; case Token::LParen: + case Token::LBrack: { - // Tuple or parenthesized expression. - // Special cases: () is empty tuple type, (x) is not a real tuple, (x,) is one-dimensional tuple + // Tuple/parenthesized expression or inline array/bracketed expression. + // Special cases: ()/[] is empty tuple/array type, (x) is not a real tuple, + // (x,) is one-dimensional tuple, elements in arrays cannot be left out, only in tuples. m_scanner->next(); vector<ASTPointer<Expression>> components; - if (m_scanner->currentToken() != Token::RParen) + Token::Value oppositeToken = (token == Token::LParen ? Token::RParen : Token::RBrack); + bool isArray = (token == Token::LBrack); + + if (m_scanner->currentToken() != oppositeToken) while (true) { - if (m_scanner->currentToken() != Token::Comma && m_scanner->currentToken() != Token::RParen) + if (m_scanner->currentToken() != Token::Comma && m_scanner->currentToken() != oppositeToken) components.push_back(parseExpression()); + else if (isArray) + parserError("Expected expression (inline array elements cannot be omitted)."); else components.push_back(ASTPointer<Expression>()); - if (m_scanner->currentToken() == Token::RParen) + if (m_scanner->currentToken() == oppositeToken) break; else if (m_scanner->currentToken() == Token::Comma) m_scanner->next(); } nodeFactory.markEndPosition(); - expectToken(Token::RParen); - return nodeFactory.createNode<TupleExpression>(components); + expectToken(oppositeToken); + return nodeFactory.createNode<TupleExpression>(components, isArray); } + default: if (Token::isElementaryTypeName(token)) { diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 73a9b660..1242e801 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2743,6 +2743,32 @@ BOOST_AUTO_TEST_CASE(invalid_args_creating_memory_array) BOOST_CHECK(expectError(text) == Error::Type::TypeError); } +/*BOOST_AUTO_TEST_CASE(inline_array_declaration_and_passing) +{ + char const* text = R"( + contract C { + uint[] a; + function f() returns (uint, uint) { + a = [1,2,3]; + return (a[3], [3,4][0]); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(invalid_types_in_inline_array) +{ + char const* text = R"( + contract C { + function f() { + uint[] x = [45, "foo", true]; + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +}*/ + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index fd9076c3..e6c8a8d8 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -1047,6 +1047,48 @@ BOOST_AUTO_TEST_CASE(using_for) BOOST_CHECK(successParse(text)); } +BOOST_AUTO_TEST_CASE(inline_array_declaration) +{ + char const* text = R"( + contract c { + uint[] a; + function f() returns (uint, uint) { + a = [1,2,3]; + return (a[3], [2,3,4][0]); + } + } + )"; + BOOST_CHECK(successParse(text)); +} + + +BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_lvalue) +{ + char const* text = R"( + contract c { + uint[] a; + function f() returns (uint) { + a = [,2,3]; + return (a[0]); + } + } + )"; + BOOST_CHECK(!successParse(text)); +} + +BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_without_lvalue) +{ + char const* text = R"( + contract c { + uint[] a; + function f() returns (uint, uint) { + return ([3, ,4][0]); + } + } + )"; + BOOST_CHECK(!successParse(text)); +} + BOOST_AUTO_TEST_SUITE_END() } |