updated tests and much simpler algorithm for parsing errors

This commit is contained in:
RJ Catalano 2015-12-16 16:47:37 -06:00
parent 559c210643
commit 905d55e34f
2 changed files with 25 additions and 39 deletions

View File

@ -1038,26 +1038,25 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
case Token::LBrack: case Token::LBrack:
{ {
// Tuple/parenthesized expression or inline array/bracketed expression. // Tuple/parenthesized expression or inline array/bracketed expression.
// Special cases: ()/[] is empty tuple/array type, (x)/[] is not a real tuple/array, // Special cases: ()/[] is empty tuple/array type, (x) is not a real tuple,
// (x,) is one-dimensional tuple // (x,) is one-dimensional tuple, elements in arrays cannot be left out, only in tuples.
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::LBrack ? true : false); bool isArray = (token == Token::LBrack);
if (isArray && (m_scanner->currentToken() == Token::Comma))
fatalParserError(std::string("Expected value in array cell after '[' ."));
if (m_scanner->currentToken() != oppositeToken) 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)
components.push_back(parseExpression()); components.push_back(parseExpression());
else if (isArray)
parserError(std::string("Expected value in array cell after"));
else else
components.push_back(ASTPointer<Expression>()); components.push_back(ASTPointer<Expression>());
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 || m_scanner->peekNextToken() == oppositeToken))
fatalParserError(std::string("Expected value in array cell after ',' ."));
m_scanner->next(); m_scanner->next();
} }
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();

View File

@ -1047,55 +1047,42 @@ BOOST_AUTO_TEST_CASE(using_for)
BOOST_CHECK(successParse(text)); BOOST_CHECK(successParse(text));
} }
BOOST_AUTO_TEST_CASE(inline_array_declaration_lvalue) BOOST_AUTO_TEST_CASE(inline_array_declaration)
{
char const* text = R"(
contract c {
uint[] a;
function f() returns (uint) {
a = [1,2,3];
return (a[3]);
}
}
)";
BOOST_CHECK(successParse(text));
}
BOOST_AUTO_TEST_CASE(inline_array_declaration_self)
{
char const* text = R"(
contract c {
uint[] a;
function f() returns (uint) {
return ([1,2,3][0]);
}
}
)";
BOOST_CHECK(successParse(text));
}
BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_beginning)
{ {
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];
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]; a = [,2,3];
return (a[3], [,3,4][0]); return (a[0]);
} }
} }
)"; )";
BOOST_CHECK(!successParse(text)); BOOST_CHECK(!successParse(text));
} }
BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_commas) BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_without_lvalue)
{ {
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, ,3]; return ([3, ,4][0]);
return (a[3], [3, ,4][0]);
} }
} }
)"; )";