Skip to content

Commit 20f5c6e

Browse files
committed
Move file to SwiftSyntaxBuilder cod gen
1 parent 7cbed94 commit 20f5c6e

13 files changed

+257
-492
lines changed

CodeGeneration/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/apple/swift-syntax.git", revision: "6e3dfb332553ad1462f0a3d45b4d91e349ce4013"),
16+
.package(url: "https://github.com/apple/swift-syntax.git", revision: "7cbed94a9597cc12ec5adc8e63358a54391129fa"),
1717
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
1818
],
1919
targets: [

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Node {
2323
public let nameForDiagnostics: String?
2424
public let description: String?
2525
public let baseKind: String
26+
public let parserFunction: String?
2627
public let traits: [String]
2728
public let children: [Child]
2829
public let nonUnexpectedChildren: [Child]
@@ -77,6 +78,7 @@ public class Node {
7778
description: String? = nil,
7879
kind: String,
7980
traits: [String] = [],
81+
parserFunction: String? = nil,
8082
children: [Child] = [],
8183
element: String = "",
8284
elementName: String? = nil,
@@ -88,7 +90,7 @@ public class Node {
8890
self.name = kindToType(kind: self.syntaxKind)
8991
self.nameForDiagnostics = nameForDiagnostics
9092
self.description = description
91-
93+
self.parserFunction = parserFunction
9294
self.traits = traits
9395
self.baseKind = kind
9496

CodeGeneration/Sources/SyntaxSupport/gyb_generated/CommonNodes.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,28 @@
1515
public let COMMON_NODES: [Node] = [
1616
Node(name: "Decl",
1717
nameForDiagnostics: "declaration",
18-
kind: "Syntax"),
18+
kind: "Syntax",
19+
parserFunction: "parseDeclaration"),
1920

2021
Node(name: "Expr",
2122
nameForDiagnostics: "expression",
22-
kind: "Syntax"),
23+
kind: "Syntax",
24+
parserFunction: "parseExpression"),
2325

2426
Node(name: "Stmt",
2527
nameForDiagnostics: "statement",
26-
kind: "Syntax"),
28+
kind: "Syntax",
29+
parserFunction: "parseStatement"),
2730

2831
Node(name: "Type",
2932
nameForDiagnostics: "type",
30-
kind: "Syntax"),
33+
kind: "Syntax",
34+
parserFunction: "parseType"),
3135

3236
Node(name: "Pattern",
3337
nameForDiagnostics: "pattern",
34-
kind: "Syntax"),
38+
kind: "Syntax",
39+
parserFunction: "parsePattern"),
3540

3641
Node(name: "UnknownDecl",
3742
nameForDiagnostics: "declaration",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ public let DECL_NODES: [Node] = [
645645
traits: [
646646
"Braced"
647647
],
648+
parserFunction: "parseMemberDeclList",
648649
children: [
649650
Child(name: "LeftBrace",
650651
kind: "LeftBraceToken",
@@ -693,6 +694,7 @@ public let DECL_NODES: [Node] = [
693694
traits: [
694695
"WithStatements"
695696
],
697+
parserFunction: "parseSourceFile",
696698
children: [
697699
Child(name: "Statements",
698700
kind: "CodeBlockItemList",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/GenericNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public let GENERIC_NODES: [Node] = [
196196
Node(name: "GenericParameterClause",
197197
nameForDiagnostics: "generic parameter clause",
198198
kind: "Syntax",
199+
parserFunction: "parseGenericParameters",
199200
children: [
200201
Child(name: "LeftAngleBracket",
201202
kind: "LeftAngleToken",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/StmtNodes.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ public let STMT_NODES: [Node] = [
560560
traits: [
561561
"WithStatements"
562562
],
563+
parserFunction: "parseSwitchCase",
563564
children: [
564565
Child(name: "UnknownAttr",
565566
kind: "Attribute",
@@ -660,6 +661,7 @@ public let STMT_NODES: [Node] = [
660661
traits: [
661662
"WithCodeBlock"
662663
],
664+
parserFunction: "parseCatchClause",
663665
children: [
664666
Child(name: "CatchKeyword",
665667
kind: "CatchToken",

CodeGeneration/Sources/SyntaxSupport/gyb_helpers/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def make_swift_node(node):
2020
mapped_traits = map(lambda x: (' ' * spaces) + '"%s"' % x, node.traits)
2121
parameters += ['traits: [\n%s\n' % ',\n'.join(mapped_traits) + (' ' * (spaces - 2)) + ']']
2222

23+
if node.parser_function:
24+
parameters += ['parserFunction: "%s"' % node.parser_function]
25+
2326
if node.non_unexpected_children:
2427
children = []
2528
for child in node.non_unexpected_children:

CodeGeneration/Sources/generate-swiftsyntaxbuilder/GenerateSwiftSyntaxBuilder.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct GenerateSwiftSyntaxBuilder: ParsableCommand {
2929
(buildableCollectionNodesFile, "BuildableCollectionNodes.swift"),
3030
(buildableNodesFile, "BuildableNodes.swift"),
3131
(resultBuildersFile, "ResultBuilders.swift"),
32+
(syntaxExpressibleByStringInterpolationConformancesFile, "SyntaxExpressibleByStringInterpolationConformances.swift"),
3233
(tokenFile, "Token.swift"),
3334
(typealiasesFile, "Typealiases.swift")
3435
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SyntaxSupport
15+
import SwiftSyntaxBuilder
16+
import Utils
17+
18+
let syntaxExpressibleByStringInterpolationConformancesFile = SourceFile {
19+
ImportDecl(
20+
"""
21+
\(raw: generateCopyrightHeader(for: "generate-swiftsyntaxbuilder"))
22+
import SwiftSyntax
23+
""")
24+
ImportDecl("import SwiftParser")
25+
ImportDecl("import SwiftParserDiagnostics")
26+
27+
ExtensionDecl("extension SyntaxParseable") {
28+
InitializerDecl(
29+
"""
30+
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
31+
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
32+
return Self.parse(from: &parser)
33+
})
34+
}
35+
""")
36+
}
37+
38+
for node in SYNTAX_NODES where node.parserFunction != nil && node.isBase {
39+
if node.parserFunction != nil && node.isBase {
40+
ExtensionDecl("extension \(node.name)Protocol") {
41+
InitializerDecl(
42+
"""
43+
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
44+
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
45+
let node = \(raw: node.name).parse(from: &parser)
46+
guard let result = node.as(Self.self) else {
47+
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualType: node.kind.syntaxNodeType)
48+
}
49+
return result
50+
})
51+
}
52+
""")
53+
54+
}
55+
56+
ExtensionDecl("extension \(node.name): SyntaxExpressibleByStringInterpolation") {
57+
InitializerDecl(
58+
"""
59+
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
60+
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
61+
return Self.parse(from: &parser)
62+
})
63+
}
64+
""")
65+
}
66+
} else if node.parserFunction != nil || (node.baseType.baseName != "Syntax" && node.baseType.baseName != "SyntaxCollection") {
67+
ExtensionDecl("extension \(raw: node.name): SyntaxExpressibleByStringInterpolation")
68+
}
69+
}
70+
71+
FunctionDecl(
72+
"""
73+
// TODO: This should be fileprivate, but is currently used in
74+
// `ConvenienceInitializers.swift`. See the corresponding TODO there.
75+
func performParse<SyntaxType: SyntaxProtocol>(source: [UInt8], parse: (inout Parser) throws -> SyntaxType) throws -> SyntaxType {
76+
return try source.withUnsafeBufferPointer { buffer in
77+
var parser = Parser(buffer)
78+
// FIXME: When the parser supports incremental parsing, put the
79+
// interpolatedSyntaxNodes in so we don't have to parse them again.
80+
let result = try parse(&parser)
81+
if result.hasError {
82+
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: result)
83+
assert(!diagnostics.isEmpty)
84+
throw SyntaxStringInterpolationError.diagnostics(diagnostics, tree: Syntax(result))
85+
}
86+
return result
87+
}
88+
}
89+
""")
90+
}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ let package = Package(
8888
dependencies: ["SwiftBasicFormat", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics"],
8989
exclude: [
9090
"CMakeLists.txt",
91-
"SyntaxExpressibleByStringInterpolationConformances.swift.gyb",
9291
]
9392
),
9493
.target(

Sources/SwiftSyntaxBuilder/SyntaxExpressibleByStringInterpolationConformances.swift.gyb

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)