Skip to content

Update swift syntax #1217

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion CodeGeneration/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.executable(name: "generate-swiftsyntax", targets: ["generate-swiftsyntax"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", revision: "d856f486d15a61625e90c1740729b3fb5244fad1"),
.package(url: "https://github.com/apple/swift-syntax.git", revision: "8e6ae6eef6259c1f50692b509f776e66564ae189"),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.4")),
],
targets: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,21 @@ let syntaxBaseNodesFile = SourceFile(leadingTrivia: [.blockComment(generateCopyr
}
""")

InitializerDecl("public init?<S: SyntaxProtocol>(_ node: S)") {
SwitchStmt(expression: MemberAccessExpr("node.raw.kind")) {
SwitchCaseList {
SwitchCase(
InitializerDeclSyntax("public init?<S: SyntaxProtocol>(_ node: S)") {
SwitchStmt(expression: MemberAccessExprSyntax("node.raw.kind")) {
SwitchCaseListSyntax {
SwitchCaseSyntax(
label: .case(SwitchCaseLabel {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
CaseItem(
pattern: EnumCasePattern(
period: .period,
caseName: .identifier(childNode.swiftSyntaxKind))
)
CaseItemSyntax(
pattern: ExpressionPatternSyntax(
expression: MemberAccessExprSyntax(
base: nil,
dot: .periodToken(),
name: .identifier(childNode.swiftSyntaxKind)
)
)
)
}
})) {
Expr("self._syntaxNode = node._syntaxNode")
Expand Down Expand Up @@ -127,11 +131,15 @@ let syntaxBaseNodesFile = SourceFile(leadingTrivia: [.blockComment(generateCopyr
SwitchCase(
label: .case(SwitchCaseLabel {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
CaseItem(
pattern: EnumCasePatternSyntax(
period: .period,
caseName: .identifier(childNode.swiftSyntaxKind))
)
CaseItemSyntax(
pattern: ExpressionPatternSyntax(
expression: MemberAccessExprSyntax(
base: nil,
dot: .periodToken(),
name: .identifier(childNode.swiftSyntaxKind)
)
)
)
}
})) {
BreakStmt()
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class BasicFormat: SyntaxRewriter {
Trivia(pieces: [.newlines(1), indentation])
}

private var lastRewrittenToken: TokenSyntax?
private var lastRewrittenToken: TokenSyntax?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we in basic format make if next token == nil return no trailing space


private var putNextTokenOnNewLine: Bool = false

Expand All @@ -48,13 +48,13 @@ open class BasicFormat: SyntaxRewriter {
open override func visit(_ node: TokenSyntax) -> TokenSyntax {
var leadingTrivia = node.leadingTrivia
var trailingTrivia = node.trailingTrivia
if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken? .trailingTrivia.isEmpty != false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 This seems like a regression. It’s fine now but it would be nice to fix this in BasicFormat for the future

leadingTrivia += .space
}
if requiresTrailingSpace(node) && trailingTrivia.isEmpty {
trailingTrivia += .space
}
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first? .isNewline ?? false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We missed some cases here.
Will open a new PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leadingTrivia = .newline + leadingTrivia
}
leadingTrivia = leadingTrivia.indented(indentation: indentation)
Expand Down Expand Up @@ -136,7 +136,7 @@ open class BasicFormat: SyntaxRewriter {
}

open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
switch (token.previousToken(viewMode: .sourceAccurate)? .tokenKind, token.tokenKind) {
case (.leftParen, .spacedBinaryOperator):
return false
default:
Expand Down Expand Up @@ -164,10 +164,10 @@ open class BasicFormat: SyntaxRewriter {

open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool {
// Format `[:]` as-is.
if token.tokenKind == .colon && token.parent?.kind == .dictionaryExpr {
if token.tokenKind == .colon && token.parent? .kind == .dictionaryExpr {
return false
}
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)? .tokenKind) {
case (.asKeyword, .exclamationMark), // Ensures there is not space in `as!`
(.asKeyword, .postfixQuestionMark), // Ensures there is not space in `as?`
(.exclamationMark, .leftParen), // Ensures there is not space in `myOptionalClosure!()`
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftParser/generated/Parser+Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Parser {
/// Parse the source code in the given string as Swift source file. See
/// `Parser.init` for more details.
public static func parse(
source: UnsafeBufferPointer < UInt8 > ,
source: UnsafeBufferPointer<UInt8>,
maximumNestingLevel: Int? = nil,
parseTransition: IncrementalParseTransition? = nil
) -> SourceFileSyntax {
Expand Down Expand Up @@ -132,7 +132,7 @@ extension TypeSyntax: SyntaxParseable {
}

fileprivate extension Parser {
mutating func parseRemainder < R: RawSyntaxNodeProtocol > (into: R) -> R {
mutating func parseRemainder<R: RawSyntaxNodeProtocol>(into: R) -> R {
guard !into.raw.kind.isSyntaxCollection, let layout = into.raw.layoutView else {
assertionFailure("Only support parsing of non-collection layout nodes")
return into
Expand All @@ -145,6 +145,6 @@ fileprivate extension Parser {

let unexpected = RawUnexpectedNodesSyntax(elements: remainingTokens, arena: self.arena)
let withUnexpected = layout.replacingChild(at: layout.children.count - 1, with: unexpected.raw, arena: self.arena)
return R.init(withUnexpected)!
return R.init(withUnexpected)!
}
}
Loading