User-defined literal suffixes: Parsing

This commit is contained in:
Kamil Śliwak 2023-04-06 22:58:40 +02:00
parent 2986772f3f
commit 00509830d5
4 changed files with 41 additions and 3 deletions

View File

@ -2050,7 +2050,44 @@ ASTPointer<Expression> Parser::parseLiteral()
return nodeFactory.createNode<Literal>(initialToken, std::move(value), subDenomination);
}
return nodeFactory.createNode<Literal>(initialToken, std::move(value), Literal::SubDenomination::None);
ASTPointer<Literal> literal = nodeFactory.createNode<Literal>(initialToken, std::move(value), Literal::SubDenomination::None);
if (m_scanner->currentToken() != Token::Identifier)
return literal;
ASTPointer<Expression> suffix = parseLiteralSuffix();
nodeFactory.setEndPositionFromNode(suffix);
return nodeFactory.createNode<FunctionCall>(
std::move(suffix),
std::vector<ASTPointer<Expression>>{std::move(literal)},
vector<ASTPointer<ASTString>>{},
vector<SourceLocation>{},
true /* _isSuffixCall */
);
}
ASTPointer<Expression> Parser::parseLiteralSuffix()
{
RecursionGuard recursionGuard(*this);
ASTNodeFactory nodeFactory(*this);
nodeFactory.markEndPosition();
ASTPointer<Expression> suffix = nodeFactory.createNode<Identifier>(expectIdentifierToken());
while (m_scanner->currentToken() == Token::Period)
{
advance();
SourceLocation memberLocation = currentLocation();
nodeFactory.markEndPosition();
suffix = nodeFactory.createNode<MemberAccess>(
std::move(suffix),
expectIdentifierToken(),
std::move(memberLocation)
);
}
return suffix;
}
ASTPointer<Expression> Parser::parsePrimaryExpression()

View File

@ -161,6 +161,7 @@ private:
ASTPointer<Expression> const& _partiallyParsedExpression = ASTPointer<Expression>()
);
ASTPointer<Expression> parseLiteral();
ASTPointer<Expression> parseLiteralSuffix();
ASTPointer<Expression> parsePrimaryExpression();
std::vector<ASTPointer<Expression>> parseFunctionCallListArguments();

View File

@ -4,4 +4,4 @@ contract C {
}
}
// ----
// ParserError 2314: (58-64): Expected ';' but got identifier
// DeclarationError 7576: (58-64): Undeclared identifier.

View File

@ -4,4 +4,4 @@ contract C {
}
}
// ----
// ParserError 2314: (58-63): Expected ';' but got identifier
// DeclarationError 7576: (58-63): Undeclared identifier.