Skip to content

Don’t visit parts of the syntax tree that don’t contain any SequenceExpr in SequenceFolder #867

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
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
9 changes: 9 additions & 0 deletions Sources/SwiftOperators/OperatorTable+Folding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,15 @@ extension OperatorTable {
self.errorHandler = errorHandler
}

override func visitAny(_ node: Syntax) -> Syntax? {
/// Return a non-nil value from `visitAny` to indicate to the SyntaxRewriter
/// that it shouldn't visit any of these node's children.
if !node.hasSequenceExpr {
return node
}
return nil
}

override func visit(_ node: SequenceExprSyntax) -> ExprSyntax {
// If the error handler threw in response to an error, don't
// continue folding.
Expand Down
12 changes: 10 additions & 2 deletions Sources/SwiftSyntax/Raw/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct RecursiveRawSyntaxFlags: OptionSet {

/// Whether the tree contained by this layout has any missing or unexpected nodes.
static let hasError = RecursiveRawSyntaxFlags(rawValue: 1 << 0)
static let hasSequenceExpr = RecursiveRawSyntaxFlags(rawValue: 1 << 1)
}

/// Node data for RawSyntax tree. Tagged union plus common data.
Expand Down Expand Up @@ -612,8 +613,12 @@ extension RawSyntax {
) -> RawSyntax {
validateLayout(layout: layout, as: kind)
let payload = RawSyntaxData.Layout(
kind: kind, layout: layout,
byteLength: byteLength, descendantCount: descendantCount, recursiveFlags: recursiveFlags)
kind: kind,
layout: layout,
byteLength: byteLength,
descendantCount: descendantCount,
recursiveFlags: recursiveFlags
)
return RawSyntax(arena: arena, payload: .layout(payload))
}

Expand Down Expand Up @@ -647,6 +652,9 @@ extension RawSyntax {
recursiveFlags.insert(node.recursiveFlags)
arena.addChild(node.arenaReference)
}
if kind == .sequenceExpr {
recursiveFlags.insert(.hasSequenceExpr)
}
return .layout(
kind: kind,
layout: RawSyntaxBuffer(layoutBuffer),
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ public extension SyntaxProtocol {
return raw.recursiveFlags.contains(.hasError)
}

/// Whether this tree contains a missing token or unexpected node.
var hasSequenceExpr: Bool {
return raw.recursiveFlags.contains(.hasSequenceExpr)
}

/// The parent of this syntax node, or `nil` if this node is the root.
var parent: Syntax? {
return data.parent.map(Syntax.init(_:))
Expand Down