changed a couple of small nuances, made an attempt at fixing the parsing in the inline arrays case (fails), and added test for inline arrays per Chriseth request

This commit is contained in:
RJ Catalano 2015-12-16 12:55:52 -06:00
parent caa6202f62
commit 0ba24a5289
3 changed files with 28 additions and 18 deletions

View File

@ -779,7 +779,6 @@ bool TypeChecker::visit(Assignment const& _assignment)
bool TypeChecker::visit(TupleExpression const& _tuple) bool TypeChecker::visit(TupleExpression const& _tuple)
{ {
vector<ASTPointer<Expression>> const& components = _tuple.components(); vector<ASTPointer<Expression>> const& components = _tuple.components();
solAssert(!_tuple.isInlineArray(), "Tuple type not properly declared"); solAssert(!_tuple.isInlineArray(), "Tuple type not properly declared");
TypePointers types; TypePointers types;
@ -793,10 +792,7 @@ bool TypeChecker::visit(TupleExpression const& _tuple)
} }
else else
types.push_back(TypePointer()); types.push_back(TypePointer());
if (_tuple.isInlineArray()) _tuple.annotation().type = make_shared<TupleType>(types);
_tuple.annotation().type = make_shared<ArrayType>(DataLocation::Storage, _tuple.annotation().type, types.size());
else
_tuple.annotation().type = make_shared<TupleType>(types);
// If some of the components are not LValues, the error is reported above. // If some of the components are not LValues, the error is reported above.
_tuple.annotation().isLValue = true; _tuple.annotation().isLValue = true;
} }
@ -806,10 +802,7 @@ bool TypeChecker::visit(TupleExpression const& _tuple)
{ {
// Outside of an lvalue-context, the only situation where a component can be empty is (x,). // Outside of an lvalue-context, the only situation where a component can be empty is (x,).
if (!components[i] && !(i == 1 && components.size() == 2)) if (!components[i] && !(i == 1 && components.size() == 2))
_tuple.isInlineArray() ? fatalTypeError(_tuple.location(), "Tuple component cannot be empty.");
fatalTypeError(_tuple.location(), "Array component cannot have empty cells.")
:
fatalTypeError(_tuple.location(), "Tuple component cannot be empty.");
else if (components[i]) else if (components[i])
{ {
components[i]->accept(*this); components[i]->accept(*this);

View File

@ -1043,9 +1043,10 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
m_scanner->next(); m_scanner->next();
vector<ASTPointer<Expression>> components; vector<ASTPointer<Expression>> components;
Token::Value oppositeToken = (token == Token::LParen ? Token::RParen : Token::RBrack); Token::Value oppositeToken = (token == Token::LParen ? Token::RParen : Token::RBrack);
bool isArray = (token == Token::LParen ? false : true); bool isArray = (token == Token::RBrace ? true : false);
if (isArray && (m_scanner->currentToken() == Token::Comma))
if (m_scanner->currentToken() != Token::RParen) fatalParserError("Expected value in array cell after '[' .");
if (m_scanner->currentToken() != oppositeToken)
while (true) while (true)
{ {
if (m_scanner->currentToken() != Token::Comma && m_scanner->currentToken() != oppositeToken) if (m_scanner->currentToken() != Token::Comma && m_scanner->currentToken() != oppositeToken)
@ -1055,6 +1056,8 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
if (m_scanner->currentToken() == oppositeToken) if (m_scanner->currentToken() == oppositeToken)
break; break;
else if (m_scanner->currentToken() == Token::Comma) else if (m_scanner->currentToken() == Token::Comma)
if (isArray && (m_scanner->peekNextToken() == (Token::Comma || oppositeToken)))
fatalParserError("Expected value in array cell after ',' .");
m_scanner->next(); m_scanner->next();
} }
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();

View File

@ -1051,16 +1051,30 @@ BOOST_AUTO_TEST_CASE(inline_array_declaration)
{ {
char const* text = R"( char const* text = R"(
contract c { contract c {
uint[] a; uint[] a;
function f() returns (uint, uint) { function f() returns (uint, uint) {
a = [1,2,3]; a = [1,2,3];
return (a[3], [3,4][0]); return (a[3], [3,4][0]);
} }
} }
)"; )";
BOOST_CHECK(successParse(text)); BOOST_CHECK(successParse(text));
} }
BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check)
{
char const* text = R"(
contract c {
uint[] a;
function f() returns (uint, uint) {
a = [,2,3];
return (a[3], [3,4][0]);
}
}
)";
BOOST_CHECK(!successParse(text));
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }