Skip to content

Commit 62217b0

Browse files
committed
Generate ExpressibleByStringInterpolation conformances using gyb
1 parent 9ce266f commit 62217b0

11 files changed

+1303
-349
lines changed

Sources/SwiftParser/Parser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension Parser {
4343
// be alive until `Syntax(raw:)` retains the arena.
4444
return withExtendedLifetime(parser) {
4545
let rawSourceFile = parser.parseSourceFile()
46-
return Syntax(raw: rawSourceFile.raw).as(SourceFileSyntax.self)!
46+
return rawSourceFile.syntax
4747
}
4848
}
4949
}

Sources/SwiftParser/Syntax+StringInterpolation.swift

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -142,68 +142,3 @@ extension SyntaxExpressibleByStringInterpolation {
142142
self.init(stringInterpolation: interpolation)
143143
}
144144
}
145-
146-
private func castRawToSyntaxNode<OutputType: SyntaxProtocol, RawType: RawSyntaxNodeProtocol>(_ raw: RawType) -> OutputType {
147-
let syntax = Syntax(raw: raw.raw)
148-
guard let result = syntax.as(OutputType.self) else {
149-
fatalError("Parsing was expected to produce a \(OutputType.self) but produced \(type(of: syntax.asProtocol(SyntaxProtocol.self)))")
150-
}
151-
return result
152-
}
153-
154-
// Parsing support for the main kinds of syntax nodes.
155-
extension DeclSyntaxProtocol {
156-
public static func parse(from parser: inout Parser) -> Self {
157-
return castRawToSyntaxNode(parser.parseDeclaration())
158-
}
159-
}
160-
161-
extension ExprSyntaxProtocol {
162-
public static func parse(from parser: inout Parser) -> Self {
163-
return castRawToSyntaxNode(parser.parseExpression())
164-
}
165-
}
166-
167-
extension StmtSyntaxProtocol {
168-
public static func parse(from parser: inout Parser) -> Self {
169-
return castRawToSyntaxNode(parser.parseStatement())
170-
}
171-
}
172-
173-
extension TypeSyntaxProtocol {
174-
public static func parse(from parser: inout Parser) -> Self {
175-
return castRawToSyntaxNode(parser.parseType())
176-
}
177-
}
178-
179-
extension PatternSyntaxProtocol {
180-
public static func parse(from parser: inout Parser) -> Self {
181-
return castRawToSyntaxNode(parser.parsePattern())
182-
}
183-
}
184-
185-
// String interpolation support for the primary node kinds.
186-
extension SourceFileSyntax: SyntaxExpressibleByStringInterpolation {
187-
public static func parse(from parser: inout Parser) -> Self {
188-
return castRawToSyntaxNode(parser.parseSourceFile())
189-
}
190-
}
191-
192-
extension DeclSyntax: SyntaxExpressibleByStringInterpolation { }
193-
extension ExprSyntax: SyntaxExpressibleByStringInterpolation { }
194-
extension StmtSyntax: SyntaxExpressibleByStringInterpolation { }
195-
extension TypeSyntax: SyntaxExpressibleByStringInterpolation { }
196-
extension PatternSyntax: SyntaxExpressibleByStringInterpolation { }
197-
extension FunctionDeclSyntax: SyntaxExpressibleByStringInterpolation { }
198-
extension ReturnStmtSyntax: SyntaxExpressibleByStringInterpolation { }
199-
extension SwitchStmtSyntax: SyntaxExpressibleByStringInterpolation { }
200-
extension FunctionSignatureSyntax: SyntaxExpressibleByStringInterpolation {
201-
public static func parse(from parser: inout Parser) -> SwiftSyntax.FunctionSignatureSyntax {
202-
return castRawToSyntaxNode(parser.parseFunctionSignature())
203-
}
204-
}
205-
extension SwitchCaseSyntax: SyntaxExpressibleByStringInterpolation {
206-
public static func parse(from parser: inout Parser) -> SwiftSyntax.SwitchCaseSyntax {
207-
return castRawToSyntaxNode(parser.parseSwitchCase())
208-
}
209-
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
%{
2+
from gyb_syntax_support import *
3+
NODE_MAP = create_node_map()
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From SyntaxExpressilbeByStringInterpolationConformances.swift.gyb.
8+
//// Do Not Edit Directly!
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This source file is part of the Swift.org open source project
12+
//
13+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
14+
// Licensed under Apache License v2.0 with Runtime Library Exception
15+
//
16+
// See https://swift.org/LICENSE.txt for license information
17+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
@_spi(RawSyntax) import SwiftSyntax
22+
23+
% STRING_INTERPOLATION_BASE_KINDS = [base_kind for base_kind in SYNTAX_BASE_KINDS if base_kind != 'Syntax' and base_kind != 'SyntaxCollection']
24+
% for base_kind in STRING_INTERPOLATION_BASE_KINDS:
25+
% node = NODE_MAP[base_kind]
26+
extension ${base_kind}SyntaxProtocol {
27+
public static func parse(from parser: inout Parser) -> Self {
28+
let node = parser.${node.parser_function}().syntax
29+
guard let result = node.as(Self.self) else {
30+
fatalError("Parsing was expected to produce a \(Self.self) but produced \(type(of: node.asProtocol(${base_kind}SyntaxProtocol.self)))")
31+
}
32+
return result
33+
}
34+
}
35+
36+
% end
37+
38+
%{
39+
def parser_invocation(node):
40+
if node.kind == 'Expr':
41+
return "return ExprSyntax(parser.parseExpression().syntax).castOrFatalError(Self.self)"
42+
elif node.kind == 'Stmt':
43+
return "return StmtSyntax(parser.parseStatement().syntax).castOrFatalError(Self.self)"
44+
elif node.kind == 'Type':
45+
return "return TypeSyntax(parser.parseType().syntax).castOrFatalError(Self.self)"
46+
elif node.kind == 'Pattern':
47+
return "return PatternSyntax(parser.parsePattern().syntax).castOrFatalError(Self.self)"
48+
elif node.parser_function is not None:
49+
return f"return parser.{node.parser_function}().syntax"
50+
else:
51+
return None
52+
}%
53+
% for node in SYNTAX_NODES:
54+
% if node.kind in STRING_INTERPOLATION_BASE_KINDS:
55+
extension ${node.name}: SyntaxExpressibleByStringInterpolation {}
56+
57+
% elif node.parser_function:
58+
extension ${node.name}: SyntaxExpressibleByStringInterpolation {
59+
public static func parse(from parser: inout Parser) -> Self {
60+
return parser.${node.parser_function}().syntax
61+
}
62+
}
63+
64+
% end
65+
% end

0 commit comments

Comments
 (0)