Skip to content

Generate Node, Child, Classification, NodeSerializationCodes, Traits and Trivia #469

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 1 commit into from
Jun 30, 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
17 changes: 16 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,22 @@ let package = Package(
name: "SwiftSyntaxBuilderGeneration",
dependencies: ["SwiftSyntaxBuilder"],
exclude: [
"Tokens.swift.gyb"
"gyb_helpers",
"AttributeNodes.swift.gyb",
"AvailabilityNodes.swift.gyb",
"Classification.swift.gyb",
"CommonNodes.swift.gyb",
"DeclNodes.swift.gyb",
"ExprNodes.swift.gyb",
"GenericNodes.swift.gyb",
"Kinds.swift.gyb",
"NodeSerializationCodes.swift.gyb",
"PatternNodes.swift.gyb",
"StmtNodes.swift.gyb",
"Tokens.swift.gyb",
"Traits.swift.gyb",
"Trivia.swift.gyb",
"TypeNodes.swift.gyb"
]
),
.testTarget(
Expand Down
26 changes: 26 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/AttributeNodes.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
from gyb_syntax_support import *
from gyb_helpers import make_swift_child, make_swift_node
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From AttributeNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

% end
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
from gyb_syntax_support import *
from gyb_helpers import make_swift_child, make_swift_node
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From AvailabilityNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

% end
]
119 changes: 119 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/Child.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

/// 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?
/// A restricted set of token kinds that will be accepted for this child.
let tokenChoices: [Token]

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

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

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? {
if syntaxKind.hasSuffix("Token") {
return syntaxKind
} else {
return nil
}
}

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

var token: Token? {
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: Token? {
return tokenChoices.first
}

/// If a classification is passed, it specifies the color identifiers in
/// that subtree should inherit for syntax coloring. Must be a member of
/// SyntaxClassification in SyntaxClassifier.h.gyb
/// If force_classification is also set to true, all child nodes (not only
/// identifiers) inherit the syntax classification.
init(name: String,
kind: String,
description: String? = nil,
isOptional: Bool = false,
tokenChoices: [String] = [],
textChoices: [String] = [],
nodeChoices: [Child] = [],
collectionElementName: String? = nil,
classification: String? = nil,
forceClassification: Bool = false,
isIndented: Bool = false,
requiresLeadingNewline: Bool = false) {
self.name = name
self.syntaxKind = kind
self.description = description
self.collectionElementName = collectionElementName
self.classification = classificationByName(classification)
self.forceClassification = forceClassification
self.isIndented = isIndented
self.requiresLeadingNewline = requiresLeadingNewline
self.isOptional = isOptional

var mappedTokenChoices = [Token]()

if syntaxKind.hasSuffix("Token"), let token = SYNTAX_TOKEN_MAP[syntaxKind] {
mappedTokenChoices.append(token)
}

mappedTokenChoices.append(contentsOf: tokenChoices.compactMap { SYNTAX_TOKEN_MAP[$0] })
self.tokenChoices = mappedTokenChoices

// A list of valid text for tokens, if specified.
// This will force validation logic to check the text passed into the
// token against the choices.
self.textChoices = textChoices

// A list of valid choices for a child
self.nodeChoices = nodeChoices

// Check the choices are either empty or multiple
assert(nodeChoices.count != 1)

// Check node choices are well-formed
for choice in self.nodeChoices {
assert(!choice.isOptional, "node choice \(choice.name) cannot be optional")
assert(choice.nodeChoices.isEmpty, "node choice \(choice.name) cannot have further choices")
}
}
}
50 changes: 50 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/Classification.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
%{
from gyb_syntax_support import *
from gyb_syntax_support.Token import *
from gyb_syntax_support.kinds import lowercase_first_word
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From Classification.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

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

public init(name: String, description: String) {
self.name = name
self.description = description
}
}

let SYNTAX_CLASSIFICATIONS: [SyntaxClassification] = [
% for syntaxClassification in SYNTAX_CLASSIFICATIONS:
SyntaxClassification(name: "${syntaxClassification.name}", description: "${syntaxClassification.description.strip()}"),
% end
]

func classificationByName(_ name: String?) -> SyntaxClassification? {
guard let name = name else { return nil }
for classification in SYNTAX_CLASSIFICATIONS where classification.name == name {
return classification
}

fatalError("Unknown syntax classification '\(name)'")
}
26 changes: 26 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/CommonNodes.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
from gyb_syntax_support import *
from gyb_helpers import make_swift_child, make_swift_node
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From CommonNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

% end
]
26 changes: 26 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/DeclNodes.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
from gyb_syntax_support import *
from gyb_helpers import make_swift_child, make_swift_node
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From DeclNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

% end
]
26 changes: 26 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/ExprNodes.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
from gyb_syntax_support import *
from gyb_helpers import make_swift_child, make_swift_node
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From ExprNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

% end
]
26 changes: 26 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/GenericNodes.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
from gyb_syntax_support import *
from gyb_helpers import make_swift_child, make_swift_node
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From GenericNodes.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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

% end
]
24 changes: 24 additions & 0 deletions Sources/SwiftSyntaxBuilderGeneration/Kinds.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%{
from gyb_syntax_support import *
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From Kinds.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

% quotedSyntaxBaseKinds = map(lambda x: '"%s"' % x, SYNTAX_BASE_KINDS)
let SyntaxBaseKinds: [String] = [
${",\n ".join(quotedSyntaxBaseKinds)}
]
Loading