Skip to content

[CodeGeneration] Minimize per-type parse(from:) code #2974

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 1 commit into from
Feb 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,38 @@ let layoutNodesParsableFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}
"""
)
DeclSyntax(
"""
extension SyntaxParseable {
fileprivate static func parse(
from parser: inout Parser,
parse: (_ parser: inout Parser) -> some RawSyntaxNodeProtocol
) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parse(&parser)
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}
"""
)

for node in SYNTAX_NODES {
if let parserFunction = node.parserFunction {
DeclSyntax(
"""
extension \(node.kind.syntaxType): SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer { withExtendedLifetime(parser) {} }
let node = parser.\(parserFunction)()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
parse(from: &parser) { $0.\(parserFunction)() }
}
}
"""
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftParser/CollectionNodes+Parsable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@_spi(RawSyntax) import SwiftSyntax
#endif

fileprivate extension SyntaxCollection {
static func parse(
extension SyntaxCollection where Self: SyntaxParseable {
fileprivate static func parse(
from parser: inout Parser,
parse: (_ parser: inout Parser) -> some RawSyntaxNodeProtocol,
makeMissing: (_ remainingTokens: [RawSyntax], _ arena: RawSyntaxArena) -> some RawSyntaxNodeProtocol
Expand Down
Loading