mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Syntax] Parse "super" and its postfix expressions
This commit is contained in:
@@ -834,6 +834,7 @@ static VarDecl *getImplicitSelfDeclForSuperContext(Parser &P,
|
||||
/// expr-super-subscript:
|
||||
/// 'super' '[' expr ']'
|
||||
ParserResult<Expr> Parser::parseExprSuper(bool isExprBasic) {
|
||||
SyntaxParsingContext SuperCtxt(SyntaxContext, SyntaxContextKind::Expr);
|
||||
// Parse the 'super' reference.
|
||||
SourceLoc superLoc = consumeToken(tok::kw_super);
|
||||
|
||||
@@ -842,6 +843,7 @@ ParserResult<Expr> Parser::parseExprSuper(bool isExprBasic) {
|
||||
superLoc);
|
||||
bool ErrorOccurred = selfDecl == nullptr;
|
||||
|
||||
SyntaxContext->createNodeInPlace(SyntaxKind::SuperRefExpr);
|
||||
Expr *superRef = !ErrorOccurred
|
||||
? cast<Expr>(new (Context) SuperRefExpr(selfDecl, superLoc,
|
||||
/*Implicit=*/false))
|
||||
@@ -869,6 +871,7 @@ ParserResult<Expr> Parser::parseExprSuper(bool isExprBasic) {
|
||||
if (!name)
|
||||
return nullptr;
|
||||
|
||||
SyntaxContext->createNodeInPlace(SyntaxKind::MemberAccessExpr);
|
||||
return makeParserResult(
|
||||
new (Context) UnresolvedDotExpr(superRef, dotLoc, name, nameLoc,
|
||||
/*Implicit=*/false));
|
||||
@@ -901,6 +904,7 @@ ParserResult<Expr> Parser::parseExprSuper(bool isExprBasic) {
|
||||
return makeParserCodeCompletionResult<Expr>();
|
||||
if (status.isError())
|
||||
return nullptr;
|
||||
SyntaxContext->createNodeInPlace(SyntaxKind::SubscriptExpr);
|
||||
return makeParserResult(
|
||||
SubscriptExpr::create(Context, superRef, lSquareLoc, indexArgs,
|
||||
indexArgLabels, indexArgLabelLocs, rSquareLoc,
|
||||
|
||||
@@ -110,6 +110,7 @@ void SyntaxParsingContext::createNodeInPlace(SyntaxKind Kind) {
|
||||
return;
|
||||
|
||||
switch (Kind) {
|
||||
case SyntaxKind::SuperRefExpr:
|
||||
case SyntaxKind::MemberAccessExpr:
|
||||
case SyntaxKind::OptionalChainingExpr:
|
||||
case SyntaxKind::ForcedValueExpr:
|
||||
|
||||
@@ -64,6 +64,13 @@ class C <MemberDeclBlock>{<FunctionDecl>
|
||||
(<TupleElement><SequenceExpr><IntegerLiteralExpr>1 </IntegerLiteralExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><IntegerLiteralExpr>1</IntegerLiteralExpr></SequenceExpr></TupleElement>)</TupleExpr>.a</MemberAccessExpr>.b</MemberAccessExpr>.foo</MemberAccessExpr><SequenceExpr><DiscardAssignmentExpr>
|
||||
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><IdentifierExpr>a </IdentifierExpr><AsExpr>as <SimpleTypeIdentifier>Bool </SimpleTypeIdentifier></AsExpr><BinaryOperatorExpr>|| </BinaryOperatorExpr><IdentifierExpr>a </IdentifierExpr><AsExpr>as! <SimpleTypeIdentifier>Bool </SimpleTypeIdentifier></AsExpr><BinaryOperatorExpr>|| </BinaryOperatorExpr><IdentifierExpr>a </IdentifierExpr><AsExpr>as? <SimpleTypeIdentifier>Bool</SimpleTypeIdentifier></AsExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
|
||||
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><IdentifierExpr>a </IdentifierExpr><IsExpr>is <SimpleTypeIdentifier>Bool</SimpleTypeIdentifier></IsExpr></SequenceExpr>
|
||||
}</CodeBlock></FunctionDecl><FunctionDecl>
|
||||
|
||||
func superExpr<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{<SequenceExpr><DiscardAssignmentExpr>
|
||||
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><MemberAccessExpr><SuperRefExpr>super</SuperRefExpr>.foo</MemberAccessExpr></SequenceExpr><FunctionCallExpr><MemberAccessExpr><SuperRefExpr>
|
||||
super</SuperRefExpr>.bar</MemberAccessExpr>()</FunctionCallExpr><SequenceExpr><SubscriptExpr><SuperRefExpr>
|
||||
super</SuperRefExpr>[<FunctionCallArgument><IntegerLiteralExpr>12</IntegerLiteralExpr></FunctionCallArgument>] </SubscriptExpr><AssignmentExpr>= </AssignmentExpr><IntegerLiteralExpr>1</IntegerLiteralExpr></SequenceExpr><FunctionCallExpr><MemberAccessExpr><SuperRefExpr>
|
||||
super</SuperRefExpr>.init</MemberAccessExpr>()</FunctionCallExpr>
|
||||
}</CodeBlock></FunctionDecl>
|
||||
}</MemberDeclBlock></ClassDecl>
|
||||
|
||||
|
||||
@@ -65,6 +65,13 @@ class C {
|
||||
_ = a as Bool || a as! Bool || a as? Bool
|
||||
_ = a is Bool
|
||||
}
|
||||
|
||||
func superExpr() {
|
||||
_ = super.foo
|
||||
super.bar()
|
||||
super[12] = 1
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -50,6 +50,12 @@ EXPR_NODES = [
|
||||
Child('Identifier', kind='IdentifierToken'),
|
||||
]),
|
||||
|
||||
# An 'super' expression.
|
||||
Node('SuperRefExpr', kind='Expr',
|
||||
children=[
|
||||
Child('SuperKeyword', kind='SuperToken'),
|
||||
]),
|
||||
|
||||
# A nil expression.
|
||||
Node('NilLiteralExpr', kind='Expr',
|
||||
children=[
|
||||
|
||||
Reference in New Issue
Block a user