Skip to content

Commit fcf6619

Browse files
committed
Move SyntaxAnyVisitor.swift, SyntaxBaseNodes.swift gyb files to codegen
1 parent 724b5d3 commit fcf6619

15 files changed

+1860
-1430
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,21 @@ public class Child {
119119
}
120120
}
121121
}
122+
123+
extension Child: Equatable {
124+
public static func == (lhs: Child, rhs: Child) -> Bool {
125+
return lhs.name == rhs.name &&
126+
lhs.syntaxKind == rhs.syntaxKind &&
127+
lhs.description == rhs.description &&
128+
lhs.collectionElementName == rhs.collectionElementName &&
129+
lhs.forceClassification == rhs.forceClassification &&
130+
lhs.isIndented == rhs.isIndented &&
131+
lhs.requiresLeadingNewline == rhs.requiresLeadingNewline &&
132+
lhs.isOptional == rhs.isOptional &&
133+
lhs.textChoices == rhs.textChoices &&
134+
lhs.nodeChoices == rhs.nodeChoices &&
135+
lhs.classification == rhs.classification &&
136+
lhs.tokenChoices == rhs.tokenChoices &&
137+
lhs.tokenCanContainArbitraryText == rhs.tokenCanContainArbitraryText
138+
}
139+
}

CodeGeneration/Sources/SyntaxSupport/Classification.swift.gyb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public class SyntaxClassification {
3434
}
3535
}
3636

37+
extension SyntaxClassification: Equatable {
38+
public static func == (lhs: SyntaxClassification, rhs: SyntaxClassification) -> Bool {
39+
return lhs.name == rhs.name && lhs.description == rhs.description
40+
}
41+
}
42+
3743
public class ChildClassification {
3844
public let parent: Node
3945
public let childIndex: Int

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public class Node {
7373
return syntaxKind.contains("Token") || collectionElement.contains("Token")
7474
}
7575

76+
public var isVisitable: Bool {
77+
return !isBase
78+
}
79+
7680
init(name: String,
7781
nameForDiagnostics: String?,
7882
description: String? = nil,
@@ -137,3 +141,23 @@ public class Node {
137141
assert(!isSyntaxCollection || elementName != nil || element != "")
138142
}
139143
}
144+
145+
extension Node: Equatable {
146+
public static func == (lhs: Node, rhs: Node) -> Bool {
147+
return lhs.syntaxKind == rhs.syntaxKind &&
148+
lhs.swiftSyntaxKind == rhs.swiftSyntaxKind &&
149+
lhs.name == rhs.name &&
150+
lhs.nameForDiagnostics == rhs.nameForDiagnostics &&
151+
lhs.description == rhs.description &&
152+
lhs.baseKind == rhs.baseKind &&
153+
lhs.parserFunction == rhs.parserFunction &&
154+
lhs.traits == rhs.traits &&
155+
lhs.children == rhs.children &&
156+
lhs.nonUnexpectedChildren == rhs.nonUnexpectedChildren &&
157+
lhs.collectionElementName == rhs.collectionElementName &&
158+
lhs.collectionElementChoices == rhs.collectionElementChoices &&
159+
lhs.omitWhenEmpty == rhs.omitWhenEmpty &&
160+
lhs.elementsSeparatedByNewline == rhs.elementsSeparatedByNewline &&
161+
lhs.collectionElement == rhs.collectionElement
162+
}
163+
}

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift.gyb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public class TokenSpec {
6767
}
6868
}
6969

70+
extension TokenSpec: Equatable {
71+
public static func == (lhs: TokenSpec, rhs: TokenSpec) -> Bool {
72+
return lhs.name == rhs.name &&
73+
lhs.kind == rhs.kind &&
74+
lhs.nameForDiagnostics == rhs.nameForDiagnostics &&
75+
lhs.unprefixedKind == rhs.unprefixedKind &&
76+
lhs.text == rhs.text &&
77+
lhs.classification == rhs.classification &&
78+
lhs.isKeyword == rhs.isKeyword &&
79+
lhs.requiresLeadingSpace == rhs.requiresLeadingSpace &&
80+
lhs.requiresTrailingSpace == rhs.requiresTrailingSpace
81+
}
82+
}
83+
7084
/// Represents a keyword token.
7185
public class KeywordSpec: TokenSpec {
7286
init(

CodeGeneration/Sources/SyntaxSupport/gyb_generated/Classification.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public class SyntaxClassification {
2727
}
2828
}
2929

30+
extension SyntaxClassification: Equatable {
31+
public static func == (lhs: SyntaxClassification, rhs: SyntaxClassification) -> Bool {
32+
return lhs.name == rhs.name && lhs.description == rhs.description
33+
}
34+
}
35+
3036
public class ChildClassification {
3137
public let parent: Node
3238
public let childIndex: Int

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TokenSpec.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ public class TokenSpec {
6161
}
6262
}
6363

64+
extension TokenSpec: Equatable {
65+
public static func == (lhs: TokenSpec, rhs: TokenSpec) -> Bool {
66+
return lhs.name == rhs.name &&
67+
lhs.kind == rhs.kind &&
68+
lhs.nameForDiagnostics == rhs.nameForDiagnostics &&
69+
lhs.unprefixedKind == rhs.unprefixedKind &&
70+
lhs.text == rhs.text &&
71+
lhs.classification == rhs.classification &&
72+
lhs.isKeyword == rhs.isKeyword &&
73+
lhs.requiresLeadingSpace == rhs.requiresLeadingSpace &&
74+
lhs.requiresTrailingSpace == rhs.requiresTrailingSpace
75+
}
76+
}
77+
6478
/// Represents a keyword token.
6579
public class KeywordSpec: TokenSpec {
6680
init(

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct GenerateSwiftSyntax: ParsableCommand {
2727
try generateTemplates(
2828
templates: [
2929
(miscFile, "Misc.swift"),
30+
(syntaxAnyVisitorFile, "SyntaxAnyVisitor.swift"),
31+
(syntaxBaseNodesFile, "SyntaxBaseNodes.swift"),
3032
],
3133
destination: URL(fileURLWithPath: generatedPath),
3234
verbose: verbose
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 SwiftSyntaxBuilder
15+
import SyntaxSupport
16+
import Utils
17+
18+
let syntaxAnyVisitorFile = SourceFile {
19+
ClassDecl("""
20+
\(generateCopyrightHeader(for: "generate-swiftsyntax"))
21+
22+
/// A `SyntaxVisitor` that can visit the nodes as generic `Syntax` values.
23+
///
24+
/// This subclass of `SyntaxVisitor` is slower than the type-specific visitation
25+
/// of `SyntaxVisitor`. Use `SyntaxAnyVisitor` if the `visitAny(_)` function
26+
/// would be useful to have, otherwise inherit from `SyntaxVisitor`.
27+
///
28+
/// This works by overriding the type-specific visit function that delegate to
29+
/// `visitAny(_)`. A subclass that provides a custom type-specific visit
30+
/// function, should also call `visitAny(_)` in its implementation, if calling
31+
/// `visitAny` is needed:
32+
///
33+
/// struct MyVisitor: SyntaxAnyVisitor {
34+
/// func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
35+
/// <code>
36+
/// }
37+
///
38+
/// func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
39+
/// <code>
40+
/// // Call this to pass tokens to `visitAny(_)` as well if needed
41+
/// visitAny(token)
42+
/// }
43+
open class SyntaxAnyVisitor: SyntaxVisitor
44+
""") {
45+
FunctionDecl("""
46+
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
47+
return .visitChildren
48+
}
49+
""")
50+
51+
FunctionDecl("""
52+
/// The function called after visiting the node and its descendents.
53+
/// - node: the node we just finished visiting.
54+
open func visitAnyPost(_ node: Syntax) {}
55+
""")
56+
57+
FunctionDecl("""
58+
// MARK: Override type specific visit methods
59+
60+
override open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
61+
return visitAny(token._syntaxNode)
62+
}
63+
""")
64+
65+
FunctionDecl("""
66+
override open func visitPost(_ node: TokenSyntax) {
67+
visitAnyPost(node._syntaxNode)
68+
}
69+
""")
70+
71+
for node in SYNTAX_NODES where node.isVisitable {
72+
FunctionDecl("""
73+
override open func visit(_ node: \(raw: node.name)) -> SyntaxVisitorContinueKind {
74+
return visitAny(node._syntaxNode)
75+
}
76+
""")
77+
78+
FunctionDecl("""
79+
override open func visitPost(_ node: \(raw: node.name)) {
80+
visitAnyPost(node._syntaxNode)
81+
}
82+
""")
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)