swiftlang/swift-syntax

Inconsistent parser: SequenceExprSyntax vs InfixOperatorExpr

SolaWing opened this issue · 3 comments

Description

for test code

#if !(canImport(XXX) && compiler(>=5))
#endif

https://swift-ast-explorer.com/ parse to InfixOperatorExpr
图片

and Parser in code:

let syntax = Parser.parse(source: """
#if !(canImport(XXX) && compiler(>=5))
#endif
""")
print(syntax.debugDescription)

parse to SequenceExprSyntax:

SourceFileSyntax
├─statements: CodeBlockItemListSyntax
│ ╰─[0]: CodeBlockItemSyntax
│   ╰─item: IfConfigDeclSyntax
│     ├─clauses: IfConfigClauseListSyntax
│     │ ╰─[0]: IfConfigClauseSyntax
│     │   ├─poundKeyword: poundIf
│     │   ├─condition: PrefixOperatorExprSyntax
│     │   │ ├─operator: prefixOperator("!")
│     │   │ ╰─expression: TupleExprSyntax
│     │   │   ├─leftParen: leftParen
│     │   │   ├─elements: LabeledExprListSyntax
│     │   │   │ ╰─[0]: LabeledExprSyntax
│     │   │   │   ╰─expression: SequenceExprSyntax
│     │   │   │     ╰─elements: ExprListSyntax
│     │   │   │       ├─[0]: FunctionCallExprSyntax
│     │   │   │       │ ├─calledExpression: DeclReferenceExprSyntax
│     │   │   │       │ │ ╰─baseName: identifier("canImport")
│     │   │   │       │ ├─leftParen: leftParen
│     │   │   │       │ ├─arguments: LabeledExprListSyntax
│     │   │   │       │ │ ╰─[0]: LabeledExprSyntax
│     │   │   │       │ │   ╰─expression: DeclReferenceExprSyntax
│     │   │   │       │ │     ╰─baseName: identifier("XXX")
│     │   │   │       │ ├─rightParen: rightParen
│     │   │   │       │ ╰─additionalTrailingClosures: MultipleTrailingClosureElementListSyntax
│     │   │   │       ├─[1]: BinaryOperatorExprSyntax
│     │   │   │       │ ╰─operator: binaryOperator("&&")
│     │   │   │       ╰─[2]: FunctionCallExprSyntax
│     │   │   │         ├─calledExpression: DeclReferenceExprSyntax
│     │   │   │         │ ╰─baseName: identifier("compiler")
│     │   │   │         ├─leftParen: leftParen
│     │   │   │         ├─arguments: LabeledExprListSyntax
│     │   │   │         │ ╰─[0]: LabeledExprSyntax
│     │   │   │         │   ╰─expression: PrefixOperatorExprSyntax
│     │   │   │         │     ├─operator: prefixOperator(">=")
│     │   │   │         │     ╰─expression: IntegerLiteralExprSyntax
│     │   │   │         │       ╰─literal: integerLiteral("5")
│     │   │   │         ├─rightParen: rightParen
│     │   │   │         ╰─additionalTrailingClosures: MultipleTrailingClosureElementListSyntax
│     │   │   ╰─rightParen: rightParen
│     │   ╰─elements: CodeBlockItemListSyntax
│     ╰─poundEndif: poundEndif
╰─endOfFileToken: endOfFile

tested in 509.1.1

I also notice there has similar issue #2152 and #2132 claim to fixit. but seems there has other cases still inconsistent.

Steps to Reproduce

No response

Tracked in Apple’s issue tracker as rdar://125772931

I believe this is working as intended; swift-ast-explorer.com has "Fold Sequence Expressions" checked by default:

image

To get the same effect, you would need to call OperatorTable.foldAll on the AST after you parse it. Here's an example: https://github.com/apple/swift-format/blob/5eb3d201eca3e7a8b47cf81b75be078628358600/Tests/SwiftFormatTests/PrettyPrint/PrettyPrintTestCase.swift#L93-L95

@allevato Thank you for your response. I wonder why it's not defaults nor document about it.