Skip to content

[Syntax] Introduce UnresolvedTernaryExpr #60629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
SmallVector<Expr*, 8> SequencedExprs;
SourceLoc startLoc = Tok.getLoc();
ParserStatus SequenceStatus;
bool PendingTernary = false;

while (true) {
if (isForConditionalDirective && Tok.isAtStartOfLine())
Expand All @@ -205,12 +204,6 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
}
SequencedExprs.push_back(Primary.get());

// We know we can make a syntax node for ternary expression.
if (PendingTernary) {
SyntaxContext->createNodeInPlace(SyntaxKind::TernaryExpr);
PendingTernary = false;
}

if (SequenceStatus.isError() && !SequenceStatus.hasCodeCompletion())
break;

Expand Down Expand Up @@ -245,6 +238,8 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
}

case tok::question_infix: {
SyntaxParsingContext TernaryCtx(SyntaxContext,
SyntaxKind::UnresolvedTernaryExpr);
// Save the '?'.
SourceLoc questionLoc = consumeToken();

Expand Down Expand Up @@ -280,10 +275,6 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
colonLoc);
SequencedExprs.push_back(unresolvedIf);
Message = diag::expected_expr_after_if_colon;

// Wait for the next expression to make a syntax node for ternary
// expression.
PendingTernary = true;
break;
}

Expand Down
6 changes: 3 additions & 3 deletions test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class C <MemberDeclBlock>{<MemberDeclListItem><FunctionDecl>
func boolOr<FunctionSignature><ParameterClause>() </ParameterClause><ReturnClause>-> <SimpleTypeIdentifier>Bool </SimpleTypeIdentifier></ReturnClause></FunctionSignature><CodeBlock>{ <ReturnStmt>return <SequenceExpr><BooleanLiteralExpr>true </BooleanLiteralExpr><BinaryOperatorExpr>|| </BinaryOperatorExpr><BooleanLiteralExpr>false </BooleanLiteralExpr></SequenceExpr></ReturnStmt>}</CodeBlock></FunctionDecl></MemberDeclListItem><MemberDeclListItem><FunctionDecl>

func foo2<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{<SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: <IntegerLiteralExpr>0</IntegerLiteralExpr></TernaryExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><TernaryExpr><TupleExpr>(<TupleExprElement><SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: <IntegerLiteralExpr>0</IntegerLiteralExpr></TernaryExpr></SequenceExpr></TupleExprElement>) </TupleExpr>? <TupleExpr>(<TupleExprElement><SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: <IntegerLiteralExpr>0</IntegerLiteralExpr></TernaryExpr></SequenceExpr></TupleExprElement>) </TupleExpr>: <TupleExpr>(<TupleExprElement><SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: <IntegerLiteralExpr>0</IntegerLiteralExpr></TernaryExpr></SequenceExpr></TupleExprElement>)</TupleExpr></TernaryExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><BooleanLiteralExpr>true </BooleanLiteralExpr><UnresolvedTernaryExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: </UnresolvedTernaryExpr><IntegerLiteralExpr>0</IntegerLiteralExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><TupleExpr>(<TupleExprElement><SequenceExpr><BooleanLiteralExpr>true </BooleanLiteralExpr><UnresolvedTernaryExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: </UnresolvedTernaryExpr><IntegerLiteralExpr>0</IntegerLiteralExpr></SequenceExpr></TupleExprElement>) </TupleExpr><UnresolvedTernaryExpr>? <TupleExpr>(<TupleExprElement><SequenceExpr><BooleanLiteralExpr>true </BooleanLiteralExpr><UnresolvedTernaryExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: </UnresolvedTernaryExpr><IntegerLiteralExpr>0</IntegerLiteralExpr></SequenceExpr></TupleExprElement>) </TupleExpr>: </UnresolvedTernaryExpr><TupleExpr>(<TupleExprElement><SequenceExpr><BooleanLiteralExpr>true </BooleanLiteralExpr><UnresolvedTernaryExpr>? <IntegerLiteralExpr>1 </IntegerLiteralExpr>: </UnresolvedTernaryExpr><IntegerLiteralExpr>0</IntegerLiteralExpr></SequenceExpr></TupleExprElement>)</TupleExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><TupleExpr>(<TupleExprElement><IntegerLiteralExpr>1</IntegerLiteralExpr>, </TupleExprElement><TupleExprElement><IntegerLiteralExpr>2</IntegerLiteralExpr></TupleExprElement>)</TupleExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><TupleExpr>(<TupleExprElement>first: <IntegerLiteralExpr>1</IntegerLiteralExpr>, </TupleExprElement><TupleExprElement>second: <IntegerLiteralExpr>2</IntegerLiteralExpr></TupleExprElement>)</TupleExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
_ </DiscardAssignmentExpr><AssignmentExpr>= </AssignmentExpr><TupleExpr>(<TupleExprElement><IntegerLiteralExpr>1</IntegerLiteralExpr></TupleExprElement>)</TupleExpr></SequenceExpr><SequenceExpr><DiscardAssignmentExpr>
Expand Down Expand Up @@ -196,7 +196,7 @@ protocol P<TypeInheritanceClause>: <InheritedType><ClassRestrictionType>class </
func foo<FunctionSignature><ParameterClause>(<FunctionParameter>_ _: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>,</FunctionParameter><FunctionParameter>
a b: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <SequenceExpr><IntegerLiteralExpr>3 </IntegerLiteralExpr><BinaryOperatorExpr>+ </BinaryOperatorExpr><IntegerLiteralExpr>2</IntegerLiteralExpr></SequenceExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
_ c: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <IntegerLiteralExpr>2</IntegerLiteralExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <SequenceExpr><TernaryExpr><BooleanLiteralExpr>true </BooleanLiteralExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: <IntegerLiteralExpr>3</IntegerLiteralExpr></TernaryExpr></SequenceExpr></InitializerClause>,</FunctionParameter><FunctionParameter><Attribute>
d _: <SimpleTypeIdentifier>Int </SimpleTypeIdentifier><InitializerClause>= <SequenceExpr><BooleanLiteralExpr>true </BooleanLiteralExpr><UnresolvedTernaryExpr>? <IntegerLiteralExpr>2</IntegerLiteralExpr>: </UnresolvedTernaryExpr><IntegerLiteralExpr>3</IntegerLiteralExpr></SequenceExpr></InitializerClause>,</FunctionParameter><FunctionParameter><Attribute>
@objc </Attribute>e: <SimpleTypeIdentifier>X </SimpleTypeIdentifier><InitializerClause>= <BooleanLiteralExpr>true</BooleanLiteralExpr></InitializerClause>,</FunctionParameter><FunctionParameter>
f: <AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>,</FunctionParameter><FunctionParameter>
g: <SimpleTypeIdentifier>Int</SimpleTypeIdentifier>...,</FunctionParameter><FunctionParameter>
Expand Down
14 changes: 14 additions & 0 deletions utils/gyb_syntax_support/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,21 @@
])
]),

# ? expr :
# Ternary expression without the condition and the second choice.
# NOTE: This appears only in SequenceExpr.
Node('UnresolvedTernaryExpr', kind='Expr',
children=[
Child("QuestionMark", kind='InfixQuestionMarkToken'),
Child("FirstChoice", kind='Expr'),
Child("ColonMark", kind='ColonToken'),
]),


# a ? 1 : 0
# NOTE: This won't come directly out of the parser. Rather, it is the
# result of "folding" a SequenceExpr based on knowing the precedence
# relationships amongst the different infix operators.
Node('TernaryExpr', kind='Expr',
children=[
Child("ConditionExpression", kind='Expr'),
Expand Down
1 change: 1 addition & 0 deletions utils/gyb_syntax_support/NodeSerializationCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
'LabeledStmt': 267,
'InfixOperatorExpr': 268,
'MoveExpr': 269,
'UnresolvedTernaryExpr': 270,
}


Expand Down