Skip to content

Move gyb file to SwiftSyntaxBuilder cod gen #1109

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
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
4 changes: 3 additions & 1 deletion CodeGeneration/Sources/SyntaxSupport/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Node {
public let nameForDiagnostics: String?
public let description: String?
public let baseKind: String
public let parserFunction: String?
public let traits: [String]
public let children: [Child]
public let nonUnexpectedChildren: [Child]
Expand Down Expand Up @@ -77,6 +78,7 @@ public class Node {
description: String? = nil,
kind: String,
traits: [String] = [],
parserFunction: String? = nil,
children: [Child] = [],
element: String = "",
elementName: String? = nil,
Expand All @@ -88,7 +90,7 @@ public class Node {
self.name = kindToType(kind: self.syntaxKind)
self.nameForDiagnostics = nameForDiagnostics
self.description = description

self.parserFunction = parserFunction
self.traits = traits
self.baseKind = kind

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@
public let COMMON_NODES: [Node] = [
Node(name: "Decl",
nameForDiagnostics: "declaration",
kind: "Syntax"),
kind: "Syntax",
parserFunction: "parseDeclaration"),

Node(name: "Expr",
nameForDiagnostics: "expression",
kind: "Syntax"),
kind: "Syntax",
parserFunction: "parseExpression"),

Node(name: "Stmt",
nameForDiagnostics: "statement",
kind: "Syntax"),
kind: "Syntax",
parserFunction: "parseStatement"),

Node(name: "Type",
nameForDiagnostics: "type",
kind: "Syntax"),
kind: "Syntax",
parserFunction: "parseType"),

Node(name: "Pattern",
nameForDiagnostics: "pattern",
kind: "Syntax"),
kind: "Syntax",
parserFunction: "parsePattern"),

Node(name: "UnknownDecl",
nameForDiagnostics: "declaration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ public let DECL_NODES: [Node] = [
traits: [
"Braced"
],
parserFunction: "parseMemberDeclList",
children: [
Child(name: "LeftBrace",
kind: "LeftBraceToken",
Expand Down Expand Up @@ -693,6 +694,7 @@ public let DECL_NODES: [Node] = [
traits: [
"WithStatements"
],
parserFunction: "parseSourceFile",
children: [
Child(name: "Statements",
kind: "CodeBlockItemList",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public let GENERIC_NODES: [Node] = [
Node(name: "GenericParameterClause",
nameForDiagnostics: "generic parameter clause",
kind: "Syntax",
parserFunction: "parseGenericParameters",
children: [
Child(name: "LeftAngleBracket",
kind: "LeftAngleToken",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public let STMT_NODES: [Node] = [
traits: [
"WithStatements"
],
parserFunction: "parseSwitchCase",
children: [
Child(name: "UnknownAttr",
kind: "Attribute",
Expand Down Expand Up @@ -660,6 +661,7 @@ public let STMT_NODES: [Node] = [
traits: [
"WithCodeBlock"
],
parserFunction: "parseCatchClause",
children: [
Child(name: "CatchKeyword",
kind: "CatchToken",
Expand Down
3 changes: 3 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/gyb_helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def make_swift_node(node):
mapped_traits = map(lambda x: (' ' * spaces) + '"%s"' % x, node.traits)
parameters += ['traits: [\n%s\n' % ',\n'.join(mapped_traits) + (' ' * (spaces - 2)) + ']']

if node.parser_function:
parameters += ['parserFunction: "%s"' % node.parser_function]

if node.non_unexpected_children:
children = []
for child in node.non_unexpected_children:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct GenerateSwiftSyntaxBuilder: ParsableCommand {
(buildableCollectionNodesFile, "BuildableCollectionNodes.swift"),
(buildableNodesFile, "BuildableNodes.swift"),
(resultBuildersFile, "ResultBuilders.swift"),
(syntaxExpressibleByStringInterpolationConformancesFile, "SyntaxExpressibleByStringInterpolationConformances.swift"),
(tokenFile, "Token.swift"),
(typealiasesFile, "Typealiases.swift")
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SyntaxSupport
import SwiftSyntaxBuilder
import Utils

let syntaxExpressibleByStringInterpolationConformancesFile = SourceFile {
ImportDecl(
"""
\(raw: generateCopyrightHeader(for: "generate-swiftsyntaxbuilder"))
import SwiftSyntax
""")
ImportDecl("import SwiftParser")
ImportDecl("import SwiftParserDiagnostics")

ExtensionDecl("extension SyntaxParseable") {
InitializerDecl(
"""
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
return Self.parse(from: &parser)
})
}
""")
}

for node in SYNTAX_NODES {
if node.parserFunction != nil && node.isBase {
ExtensionDecl("extension \(node.name)Protocol") {
InitializerDecl(
"""
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
let node = \(raw: node.name).parse(from: &parser)
guard let result = node.as(Self.self) else {
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualType: node.kind.syntaxNodeType)
}
return result
})
}
""")

}

ExtensionDecl("extension \(node.name): SyntaxExpressibleByStringInterpolation") {
InitializerDecl(
"""
public init(stringInterpolationOrThrow stringInterpolation: SyntaxStringInterpolation) throws {
self = try performParse(source: stringInterpolation.sourceText, parse: { parser in
return Self.parse(from: &parser)
})
}
""")
}
} else if node.parserFunction != nil || (node.baseType.baseName != "Syntax" && node.baseType.baseName != "SyntaxCollection") {
ExtensionDecl("extension \(raw: node.name): SyntaxExpressibleByStringInterpolation { }")
}
}

FunctionDecl(
"""
// TODO: This should be fileprivate, but is currently used in
// `ConvenienceInitializers.swift`. See the corresponding TODO there.
func performParse<SyntaxType: SyntaxProtocol>(source: [UInt8], parse: (inout Parser) throws -> SyntaxType) throws -> SyntaxType {
return try source.withUnsafeBufferPointer { buffer in
var parser = Parser(buffer)
// FIXME: When the parser supports incremental parsing, put the
// interpolatedSyntaxNodes in so we don't have to parse them again.
let result = try parse(&parser)
if result.hasError {
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: result)
assert(!diagnostics.isEmpty)
throw SyntaxStringInterpolationError.diagnostics(diagnostics, tree: Syntax(result))
}
return result
}
}
""")
}
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ let package = Package(
dependencies: ["SwiftBasicFormat", "SwiftSyntax", "SwiftParser", "SwiftParserDiagnostics"],
exclude: [
"CMakeLists.txt",
"SyntaxExpressibleByStringInterpolationConformances.swift.gyb",
]
),
.target(
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntaxBuilder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ add_swift_host_library(SwiftSyntaxBuilder
generated/ResultBuilders.swift
generated/Token.swift
generated/Typealiases.swift
gyb_generated/SyntaxExpressibleByStringInterpolationConformances.swift
generated/SyntaxExpressibleByStringInterpolationConformances.swift
)

target_link_libraries(SwiftSyntaxBuilder PUBLIC
Expand Down

This file was deleted.

Loading