Skip to content

Commit 9436722

Browse files
committed
Move UnknownSyntax and TokenSyntax to their own file
1 parent 69c5ba0 commit 9436722

File tree

5 files changed

+181
-192
lines changed

5 files changed

+181
-192
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -388,146 +388,6 @@ public extension SyntaxProtocol {
388388
}
389389
}
390390

391-
392-
/// MARK: - Nodes
393-
394-
/// A Syntax node representing a single token.
395-
public struct TokenSyntax: SyntaxProtocol {
396-
public let _syntaxNode: Syntax
397-
398-
/// Converts the given `Syntax` node to a `TokenSyntax` if possible. Returns
399-
/// `nil` if the conversion is not possible.
400-
public init?(_ syntax: Syntax) {
401-
guard syntax.raw.kind == .token else { return nil }
402-
self._syntaxNode = syntax
403-
}
404-
405-
/// Creates a Syntax node from the given `SyntaxData`. This assumes
406-
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
407-
/// is undefined.
408-
internal init(_ data: SyntaxData) {
409-
assert(data.raw.kind == .token)
410-
self._syntaxNode = Syntax(data)
411-
}
412-
413-
public var presence: SourcePresence {
414-
return raw.presence
415-
}
416-
417-
/// The text of the token as written in the source code.
418-
public var text: String {
419-
return tokenKind.text
420-
}
421-
422-
/// Returns a new TokenSyntax with its kind replaced
423-
/// by the provided token kind.
424-
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
425-
guard raw.kind == .token else {
426-
fatalError("TokenSyntax must have token as its raw")
427-
}
428-
let newRaw = RawSyntax.createAndCalcLength(kind: tokenKind,
429-
leadingTrivia: raw.formLeadingTrivia()!, trailingTrivia: raw.formTrailingTrivia()!,
430-
presence: raw.presence)
431-
let newData = data.replacingSelf(newRaw)
432-
return TokenSyntax(newData)
433-
}
434-
435-
/// Returns a new TokenSyntax with its leading trivia replaced
436-
/// by the provided trivia.
437-
public func withLeadingTrivia(_ leadingTrivia: Trivia) -> TokenSyntax {
438-
guard raw.kind == .token else {
439-
fatalError("TokenSyntax must have token as its raw")
440-
}
441-
return TokenSyntax(data.withLeadingTrivia(leadingTrivia))
442-
}
443-
444-
/// Returns a new TokenSyntax with its trailing trivia replaced
445-
/// by the provided trivia.
446-
public func withTrailingTrivia(_ trailingTrivia: Trivia) -> TokenSyntax {
447-
guard raw.kind == .token else {
448-
fatalError("TokenSyntax must have token as its raw")
449-
}
450-
return TokenSyntax(data.withTrailingTrivia(trailingTrivia))
451-
}
452-
453-
/// Returns a new TokenSyntax with its leading trivia removed.
454-
public func withoutLeadingTrivia() -> TokenSyntax {
455-
return withLeadingTrivia([])
456-
}
457-
458-
/// Returns a new TokenSyntax with its trailing trivia removed.
459-
public func withoutTrailingTrivia() -> TokenSyntax {
460-
return withTrailingTrivia([])
461-
}
462-
463-
/// Returns a new TokenSyntax with all trivia removed.
464-
public func withoutTrivia() -> TokenSyntax {
465-
return withoutLeadingTrivia().withoutTrailingTrivia()
466-
}
467-
468-
/// The leading trivia (spaces, newlines, etc.) associated with this token.
469-
public var leadingTrivia: Trivia {
470-
get {
471-
return raw.formTokenLeadingTrivia()!
472-
}
473-
set {
474-
self = withLeadingTrivia(newValue)
475-
}
476-
}
477-
478-
/// The trailing trivia (spaces, newlines, etc.) associated with this token.
479-
public var trailingTrivia: Trivia {
480-
get {
481-
return raw.formTokenTrailingTrivia()!
482-
}
483-
set {
484-
self = withTrailingTrivia(newValue)
485-
}
486-
}
487-
488-
/// The kind of token this node represents.
489-
public var tokenKind: TokenKind {
490-
get {
491-
return raw.formTokenKind()!
492-
}
493-
set {
494-
self = withKind(newValue)
495-
}
496-
}
497-
498-
/// The length this node takes up spelled out in the source, excluding its
499-
/// leading or trailing trivia.
500-
public var contentLength: SourceLength {
501-
return raw.tokenContentLength
502-
}
503-
504-
/// The length this node's leading trivia takes up spelled out in source.
505-
public var leadingTriviaLength: SourceLength {
506-
return raw.tokenLeadingTriviaLength
507-
}
508-
509-
/// The length this node's trailing trivia takes up spelled out in source.
510-
public var trailingTriviaLength: SourceLength {
511-
return raw.tokenTrailingTriviaLength
512-
}
513-
514-
/// The length of this node including all of its trivia.
515-
public var totalLength: SourceLength {
516-
return raw.totalLength
517-
}
518-
}
519-
520-
extension TokenSyntax: CustomReflectable {
521-
public var customMirror: Mirror {
522-
return Mirror(self, children: [
523-
"text": text,
524-
"leadingTrivia": leadingTrivia,
525-
"trailingTrivia": trailingTrivia,
526-
"tokenKind": tokenKind,
527-
])
528-
}
529-
}
530-
531391
/// Sequence of tokens that are part of the provided Syntax node.
532392
public struct TokenSequence: Sequence {
533393
public struct Iterator: IteratorProtocol {

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,6 @@ Each node will have:
3838
"""
3939
}%
4040

41-
/// A wrapper around a raw Syntax layout.
42-
public struct UnknownSyntax: SyntaxProtocol {
43-
public let _syntaxNode: Syntax
44-
45-
/// Convert the given `Syntax` node to an `UnknownSyntax` if possible. Return
46-
/// `nil` if the conversion is not possible.
47-
public init?(_ syntax: Syntax) {
48-
guard syntax.raw.kind == .unknown else { return nil }
49-
self._syntaxNode = syntax
50-
}
51-
52-
/// Creates an `UnknownSyntax` node from the given `SyntaxData`. This assumes
53-
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
54-
/// is undefined.
55-
internal init(_ data: SyntaxData) {
56-
assert(data.raw.kind == .unknown)
57-
self._syntaxNode = Syntax(data)
58-
}
59-
}
60-
61-
extension UnknownSyntax: CustomReflectable {
62-
public var customMirror: Mirror {
63-
return Mirror(self, children: [:])
64-
}
65-
}
66-
6741
% for node in SYNTAX_NODES:
6842
% base_type = node.base_type
6943
/// Protocol to which all `${node.name}` nodes conform. Extension point to add
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
//// Automatically Generated From SyntaxNodes.swift.gyb.
2+
//// Do Not Edit Directly!
3+
//===------------ SyntaxNodes.swift - Syntax Node definitions -------------===//
4+
//
5+
// This source file is part of the Swift.org open source project
6+
//
7+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
8+
// Licensed under Apache License v2.0 with Runtime Library Exception
9+
//
10+
// See https://swift.org/LICENSE.txt for license information
11+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
// MARK: UnknownSyntax
16+
17+
/// A wrapper around a raw Syntax layout.
18+
public struct UnknownSyntax: SyntaxProtocol {
19+
public let _syntaxNode: Syntax
20+
21+
/// Convert the given `Syntax` node to an `UnknownSyntax` if possible. Return
22+
/// `nil` if the conversion is not possible.
23+
public init?(_ syntax: Syntax) {
24+
guard syntax.raw.kind == .unknown else { return nil }
25+
self._syntaxNode = syntax
26+
}
27+
28+
/// Creates an `UnknownSyntax` node from the given `SyntaxData`. This assumes
29+
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
30+
/// is undefined.
31+
internal init(_ data: SyntaxData) {
32+
assert(data.raw.kind == .unknown)
33+
self._syntaxNode = Syntax(data)
34+
}
35+
}
36+
37+
extension UnknownSyntax: CustomReflectable {
38+
public var customMirror: Mirror {
39+
return Mirror(self, children: [:])
40+
}
41+
}
42+
43+
// MARK: TokenSyntax
44+
45+
/// A Syntax node representing a single token.
46+
public struct TokenSyntax: SyntaxProtocol {
47+
public let _syntaxNode: Syntax
48+
49+
/// Converts the given `Syntax` node to a `TokenSyntax` if possible. Returns
50+
/// `nil` if the conversion is not possible.
51+
public init?(_ syntax: Syntax) {
52+
guard syntax.raw.kind == .token else { return nil }
53+
self._syntaxNode = syntax
54+
}
55+
56+
/// Creates a Syntax node from the given `SyntaxData`. This assumes
57+
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
58+
/// is undefined.
59+
internal init(_ data: SyntaxData) {
60+
assert(data.raw.kind == .token)
61+
self._syntaxNode = Syntax(data)
62+
}
63+
64+
public var presence: SourcePresence {
65+
return raw.presence
66+
}
67+
68+
/// The text of the token as written in the source code.
69+
public var text: String {
70+
return tokenKind.text
71+
}
72+
73+
/// Returns a new TokenSyntax with its kind replaced
74+
/// by the provided token kind.
75+
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
76+
guard raw.kind == .token else {
77+
fatalError("TokenSyntax must have token as its raw")
78+
}
79+
let newRaw = RawSyntax.createAndCalcLength(kind: tokenKind,
80+
leadingTrivia: raw.formLeadingTrivia()!, trailingTrivia: raw.formTrailingTrivia()!,
81+
presence: raw.presence)
82+
let newData = data.replacingSelf(newRaw)
83+
return TokenSyntax(newData)
84+
}
85+
86+
/// Returns a new TokenSyntax with its leading trivia replaced
87+
/// by the provided trivia.
88+
public func withLeadingTrivia(_ leadingTrivia: Trivia) -> TokenSyntax {
89+
guard raw.kind == .token else {
90+
fatalError("TokenSyntax must have token as its raw")
91+
}
92+
return TokenSyntax(data.withLeadingTrivia(leadingTrivia))
93+
}
94+
95+
/// Returns a new TokenSyntax with its trailing trivia replaced
96+
/// by the provided trivia.
97+
public func withTrailingTrivia(_ trailingTrivia: Trivia) -> TokenSyntax {
98+
guard raw.kind == .token else {
99+
fatalError("TokenSyntax must have token as its raw")
100+
}
101+
return TokenSyntax(data.withTrailingTrivia(trailingTrivia))
102+
}
103+
104+
/// Returns a new TokenSyntax with its leading trivia removed.
105+
public func withoutLeadingTrivia() -> TokenSyntax {
106+
return withLeadingTrivia([])
107+
}
108+
109+
/// Returns a new TokenSyntax with its trailing trivia removed.
110+
public func withoutTrailingTrivia() -> TokenSyntax {
111+
return withTrailingTrivia([])
112+
}
113+
114+
/// Returns a new TokenSyntax with all trivia removed.
115+
public func withoutTrivia() -> TokenSyntax {
116+
return withoutLeadingTrivia().withoutTrailingTrivia()
117+
}
118+
119+
/// The leading trivia (spaces, newlines, etc.) associated with this token.
120+
public var leadingTrivia: Trivia {
121+
get {
122+
return raw.formTokenLeadingTrivia()!
123+
}
124+
set {
125+
self = withLeadingTrivia(newValue)
126+
}
127+
}
128+
129+
/// The trailing trivia (spaces, newlines, etc.) associated with this token.
130+
public var trailingTrivia: Trivia {
131+
get {
132+
return raw.formTokenTrailingTrivia()!
133+
}
134+
set {
135+
self = withTrailingTrivia(newValue)
136+
}
137+
}
138+
139+
/// The kind of token this node represents.
140+
public var tokenKind: TokenKind {
141+
get {
142+
return raw.formTokenKind()!
143+
}
144+
set {
145+
self = withKind(newValue)
146+
}
147+
}
148+
149+
/// The length this node takes up spelled out in the source, excluding its
150+
/// leading or trailing trivia.
151+
public var contentLength: SourceLength {
152+
return raw.tokenContentLength
153+
}
154+
155+
/// The length this node's leading trivia takes up spelled out in source.
156+
public var leadingTriviaLength: SourceLength {
157+
return raw.tokenLeadingTriviaLength
158+
}
159+
160+
/// The length this node's trailing trivia takes up spelled out in source.
161+
public var trailingTriviaLength: SourceLength {
162+
return raw.tokenTrailingTriviaLength
163+
}
164+
165+
/// The length of this node including all of its trivia.
166+
public var totalLength: SourceLength {
167+
return raw.totalLength
168+
}
169+
}
170+
171+
extension TokenSyntax: CustomReflectable {
172+
public var customMirror: Mirror {
173+
return Mirror(self, children: [
174+
"text": text,
175+
"leadingTrivia": leadingTrivia,
176+
"trailingTrivia": trailingTrivia,
177+
"tokenKind": tokenKind,
178+
])
179+
}
180+
}

Sources/SwiftSyntax/gyb_generated/SyntaxNodes.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,6 @@
1515
import _InternalSwiftSyntaxParser
1616

1717

18-
/// A wrapper around a raw Syntax layout.
19-
public struct UnknownSyntax: SyntaxProtocol {
20-
public let _syntaxNode: Syntax
21-
22-
/// Convert the given `Syntax` node to an `UnknownSyntax` if possible. Return
23-
/// `nil` if the conversion is not possible.
24-
public init?(_ syntax: Syntax) {
25-
guard syntax.raw.kind == .unknown else { return nil }
26-
self._syntaxNode = syntax
27-
}
28-
29-
/// Creates an `UnknownSyntax` node from the given `SyntaxData`. This assumes
30-
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
31-
/// is undefined.
32-
internal init(_ data: SyntaxData) {
33-
assert(data.raw.kind == .unknown)
34-
self._syntaxNode = Syntax(data)
35-
}
36-
}
37-
38-
extension UnknownSyntax: CustomReflectable {
39-
public var customMirror: Mirror {
40-
return Mirror(self, children: [:])
41-
}
42-
}
43-
4418
/// Protocol to which all `DeclSyntax` nodes conform. Extension point to add
4519
/// common methods to all `DeclSyntax` nodes.
4620
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!

0 commit comments

Comments
 (0)