Skip to content

Move Format from SwiftSyntaxBuilder into a separate module #830

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 4 commits into from
Sep 26, 2022
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
29 changes: 27 additions & 2 deletions CodeGeneration/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,36 @@ let package = Package(
.macOS(.v10_15),
],
products: [
.executable(name: "generate-swiftbasicformat", targets: ["generate-swiftbasicformat"]),
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", revision: "dcd692d759e09730098e45ba7276d0d96d004bac"),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
],
targets: [
.executableTarget(
name: "generate-swiftbasicformat",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"SyntaxSupport",
"Utils"
]
),
.executableTarget(
name: "generate-swiftsyntaxbuilder",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "ArgumentParser", package: "swift-argument-parser")
],
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"SyntaxSupport",
"Utils"
]
),
.target(
name: "SyntaxSupport",
exclude: [
"gyb_helpers",
"AttributeNodes.swift.gyb",
Expand All @@ -42,5 +59,13 @@ let package = Package(
"TypeNodes.swift.gyb"
]
),
.target(
name: "Utils",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
"SyntaxSupport"
]
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let ATTRIBUTE_NODES: [Node] = [
public let ATTRIBUTE_NODES: [Node] = [
% for node in ATTRIBUTE_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let AVAILABILITY_NODES: [Node] = [
public let AVAILABILITY_NODES: [Node] = [
% for node in AVAILABILITY_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let BUILDER_INITIALIZABLE_TYPES: [String: String?] = [
public let BUILDER_INITIALIZABLE_TYPES: [String: String?] = [
% for type, resolved_type in BUILDER_INITIALIZABLE_TYPES.items():
"${type}": ${'"' + resolved_type + '"' if resolved_type is not None else 'nil'},
% end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@

/// A child of a node, that may be declared optional or a token with a
/// restricted subset of acceptable kinds or texts.
class Child {
let name: String
let syntaxKind: String
let description: String?
let collectionElementName: String?
let forceClassification: Bool
let isIndented: Bool
let requiresLeadingNewline: Bool
let isOptional: Bool
let textChoices: [String]
let nodeChoices: [Child]
let classification: SyntaxClassification?
public class Child {
public let name: String
public let syntaxKind: String
public let description: String?
public let collectionElementName: String?
public let forceClassification: Bool
public let isIndented: Bool
public let requiresLeadingNewline: Bool
public let isOptional: Bool
public let textChoices: [String]
public let nodeChoices: [Child]
public let classification: SyntaxClassification?
/// A restricted set of token kinds that will be accepted for this child.
let tokenChoices: [TokenSpec]
let tokenCanContainArbitraryText: Bool
public let tokenChoices: [TokenSpec]
public let tokenCanContainArbitraryText: Bool

var swiftName: String {
public var swiftName: String {
return lowercaseFirstWord(name: name)
}

var swiftSyntaxKind: String {
public var swiftSyntaxKind: String {
return lowercaseFirstWord(name: syntaxKind)
}

var typeName: String {
public var typeName: String {
return kindToType(kind: syntaxKind)
}

/// If the child ends with "token" in the kind, it's considered a token node. Grab the existing reference to that token from the global list.
var tokenKind: String? {
public var tokenKind: String? {
if syntaxKind.hasSuffix("Token") {
return syntaxKind
} else {
Expand All @@ -50,22 +50,22 @@ class Child {
}

/// Returns `true` if this child has a token kind.
var isToken: Bool {
public var isToken: Bool {
return tokenKind != nil
}

var token: TokenSpec? {
public var token: TokenSpec? {
guard let tokenKind = tokenKind else { return nil }
return SYNTAX_TOKEN_MAP[tokenKind]
}

/// Returns the first choice from the `tokenChoices` if there are any, otherwise returns `nil`.
var mainToken: TokenSpec? {
public var mainToken: TokenSpec? {
return tokenChoices.first
}

/// Whether this child has syntax kind `UnexpectedNodes`.
var isUnexpectedNodes: Bool {
public var isUnexpectedNodes: Bool {
syntaxKind == "UnexpectedNodes"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//===----------------------------------------------------------------------===//

/// Represents a classification a token can receive for syntax highlighting.
class SyntaxClassification {
public class SyntaxClassification {
public let name: String
public let description: String

Expand All @@ -34,7 +34,7 @@ class SyntaxClassification {
}
}

let SYNTAX_CLASSIFICATIONS: [SyntaxClassification] = [
public let SYNTAX_CLASSIFICATIONS: [SyntaxClassification] = [
% for syntaxClassification in SYNTAX_CLASSIFICATIONS:
SyntaxClassification(name: "${syntaxClassification.name}", description: "${syntaxClassification.description.strip()}"),
% end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let COMMON_NODES: [Node] = [
public let COMMON_NODES: [Node] = [
% for node in COMMON_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let DECL_NODES: [Node] = [
public let DECL_NODES: [Node] = [
% for node in DECL_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let EXPR_NODES: [Node] = [
public let EXPR_NODES: [Node] = [
% for node in EXPR_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES: [String: [String]] = [
public let SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES: [String: [String]] = [
% for buildable, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
"${buildable}": [
% for conformance in conformances:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let GENERIC_NODES: [Node] = [
public let GENERIC_NODES: [Node] = [
% for node in GENERIC_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,65 @@
//
//===----------------------------------------------------------------------===//

import Foundation

/// A Syntax node, possibly with children.
/// If the kind is "SyntaxCollection", then this node is considered a Syntax
/// Collection that will expose itself as a typedef rather than a concrete
/// subclass.
class Node {
let syntaxKind: String
let swiftSyntaxKind: String
let name: String
let nameForDiagnostics: String?
let description: String?
let baseKind: String
let traits: [String]
let children: [Child]
let nonUnexpectedChildren: [Child]
let collectionElementName: String?
let collectionElementChoices: [String]?
let omitWhenEmpty: Bool
let elementsSeparatedByNewline: Bool
let collectionElement: String
public class Node {
public let syntaxKind: String
public let swiftSyntaxKind: String
public let name: String
public let nameForDiagnostics: String?
public let description: String?
public let baseKind: String
public let traits: [String]
public let children: [Child]
public let nonUnexpectedChildren: [Child]
public let collectionElementName: String?
public let collectionElementChoices: [String]?
public let omitWhenEmpty: Bool
public let elementsSeparatedByNewline: Bool
public let collectionElement: String

/// Returns `True` if this node declares one of the base syntax kinds.
var isBase: Bool {
public var isBase: Bool {
return SYNTAX_BASE_KINDS.contains(syntaxKind)
}

/// Returns `True` if this node is a subclass of SyntaxCollection.
var isSyntaxCollection: Bool {
public var isSyntaxCollection: Bool {
return baseKind == "SyntaxCollection"
}

/// Returns `True` if this node should have a `validate` method associated.
var requiresValidation: Bool {
public var requiresValidation: Bool {
return isBuildable
}

/// Returns `True` if this node is an `Unknown` syntax subclass.
var isUnknown: Bool {
public var isUnknown: Bool {
return syntaxKind.contains("Unknown")
}

/// Returns `True` if this node is an `Unknown` syntax subclass.
var isMissing: Bool {
public var isMissing: Bool {
return syntaxKind.contains("Missing")
}

/// Returns `True` if this node should have a builder associated.
var isBuildable: Bool {
public var isBuildable: Bool {
return !isBase && !isUnknown && !isMissing && !isSyntaxCollection
}

/// Returns 'True' if this node shall not be created while parsing if it has no children.
var shallBeOmittedWhenEmpty: Bool {
public var shallBeOmittedWhenEmpty: Bool {
return omitWhenEmpty
}

/// Returns true if this child has a token kind.
var isToken: Bool {
public var isToken: Bool {
return syntaxKind.contains("Token") || collectionElement.contains("Token")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let PATTERN_NODES: [Node] = [
public let PATTERN_NODES: [Node] = [
% for node in PATTERN_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let STMT_NODES: [Node] = [
public let STMT_NODES: [Node] = [
% for node in STMT_NODES:
${make_swift_node(node)},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
//===----------------------------------------------------------------------===//

let SYNTAX_BASE_KINDS: Set<String> = [
public let SYNTAX_BASE_KINDS: Set<String> = [
% for name in SYNTAX_BASE_KINDS:
"${name}",
% end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

let SYNTAX_NODES: [Node] = COMMON_NODES
public let SYNTAX_NODES: [Node] = COMMON_NODES
+ EXPR_NODES
+ DECL_NODES
+ ATTRIBUTE_NODES
Expand All @@ -21,6 +21,6 @@ let SYNTAX_NODES: [Node] = COMMON_NODES
+ AVAILABILITY_NODES

/// A lookup table of nodes indexed by their kind.
let SYNTAX_NODE_MAP: [String: Node] = Dictionary(
public let SYNTAX_NODE_MAP: [String: Node] = Dictionary(
uniqueKeysWithValues: SYNTAX_NODES.map { node in (node.syntaxKind, node) }
)
Loading