@@ -25,23 +25,38 @@ let layoutNodesParsableFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
25
25
}
26
26
"""
27
27
)
28
+ DeclSyntax (
29
+ """
30
+ extension SyntaxParseable {
31
+ fileprivate static func parse(
32
+ from parser: inout Parser,
33
+ parse: (_ parser: inout Parser) -> some RawSyntaxNodeProtocol
34
+ ) -> Self {
35
+ // Keep the parser alive so that the arena in which `raw` is allocated
36
+ // doesn’t get deallocated before we have a chance to create a syntax node
37
+ // from it. We can’t use `parser.arena` as the parameter to
38
+ // `Syntax(raw:arena:)` because the node might have been re-used during an
39
+ // incremental parse and would then live in a different arena than
40
+ // `parser.arena`.
41
+ defer {
42
+ withExtendedLifetime(parser) {
43
+ }
44
+ }
45
+ let node = parse(&parser)
46
+ let raw = RawSyntax(parser.parseRemainder(into: node))
47
+ return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
48
+ }
49
+ }
50
+ """
51
+ )
28
52
29
53
for node in SYNTAX_NODES {
30
54
if let parserFunction = node. parserFunction {
31
55
DeclSyntax (
32
56
"""
33
57
extension \( node. kind. syntaxType) : SyntaxParseable {
34
58
public static func parse(from parser: inout Parser) -> Self {
35
- // Keep the parser alive so that the arena in which `raw` is allocated
36
- // doesn’t get deallocated before we have a chance to create a syntax node
37
- // from it. We can’t use `parser.arena` as the parameter to
38
- // `Syntax(raw:arena:)` because the node might have been re-used during an
39
- // incremental parse and would then live in a different arena than
40
- // `parser.arena`.
41
- defer { withExtendedLifetime(parser) {} }
42
- let node = parser. \( parserFunction) ()
43
- let raw = RawSyntax(parser.parseRemainder(into: node))
44
- return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
59
+ parse(from: &parser) { $0. \( parserFunction) () }
45
60
}
46
61
}
47
62
"""
0 commit comments