Skip to content

Commit 65a49c4

Browse files
committed
Split SyntaxNodes.swift.gyb into multiple files
This way the generated files get a more managable size and can be viewed more easily.
1 parent ed00260 commit 65a49c4

21 files changed

+24823
-33349
lines changed

Sources/SwiftSyntax/Misc.swift.gyb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
%{
2+
# -*- mode: Swift -*-
3+
from gyb_syntax_support import *
4+
from gyb_syntax_support.Traits import TRAITS
5+
NODE_MAP = create_node_map()
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From SyntaxNodes.swift.gyb.
9+
//// Do Not Edit Directly!
10+
//===---------- Misc.swift - Miscelaneous SwiftSyntax definitions ---------===//
11+
//
12+
// This source file is part of the Swift.org open source project
13+
//
14+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
15+
// Licensed under Apache License v2.0 with Runtime Library Exception
16+
//
17+
// See https://swift.org/LICENSE.txt for license information
18+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
import _InternalSwiftSyntaxParser
23+
24+
extension SyntaxNode {
25+
public var isUnknown: Bool { return raw.kind.isUnknown }
26+
public var asUnknown: UnknownSyntax? {
27+
guard isUnknown else { return nil }
28+
return UnknownSyntax(asSyntaxData)
29+
}
30+
% for node in SYNTAX_NODES:
31+
% if not node.is_base():
32+
33+
public var is${node.syntax_kind}: Bool { return raw.kind == .${node.swift_syntax_kind} }
34+
public var as${node.syntax_kind}: ${node.name}? {
35+
guard is${node.syntax_kind} else { return nil }
36+
return ${node.name}(asSyntaxData)
37+
}
38+
% end
39+
% end
40+
}
41+
42+
public extension Syntax {
43+
/// Retrieve the concretely typed node that this Syntax node wraps.
44+
/// This property is exposed for testing purposes only.
45+
var _asConcreteType: Any {
46+
switch self.asSyntaxEnum {
47+
case .token(let node):
48+
return node
49+
case .unknown(let node):
50+
return node
51+
% for node in SYNTAX_NODES:
52+
case .${node.swift_syntax_kind}(let node):
53+
return node
54+
% end
55+
}
56+
}
57+
}
58+
59+
extension SyntaxParser {
60+
static func verifyNodeDeclarationHash() -> Bool {
61+
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
62+
"${calculate_node_hash()}"
63+
}
64+
}

Sources/SwiftSyntax/Syntax.swift

Lines changed: 62 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,36 @@ public struct Syntax: SyntaxProtocol {
3838
}
3939
}
4040

41-
/// Provide common functionality for specialized syntax nodes. Extend this
41+
// Casting functions to specialized syntax nodes.
42+
extension Syntax {
43+
public func `is`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> Bool {
44+
return self.as(syntaxType) != nil
45+
}
46+
47+
public func `as`<S: SyntaxProtocol>(_ syntaxType: S.Type) -> S? {
48+
return S.init(self)
49+
}
50+
}
51+
52+
extension Syntax: CustomReflectable {
53+
/// Reconstructs the real syntax type for this type from the node's kind and
54+
/// provides a mirror that reflects this type.
55+
public var customMirror: Mirror {
56+
return Mirror(reflecting: self._asConcreteType)
57+
}
58+
}
59+
60+
/// Provide common functionality for specialized syntax nodes. Extend this
4261
/// protocol to provide common functionality for all syntax nodes.
4362
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
44-
public protocol SyntaxProtocol: CustomStringConvertible,
63+
public protocol SyntaxProtocol: CustomStringConvertible,
4564
CustomDebugStringConvertible, TextOutputStreamable, Hashable {
4665

4766
/// Retrieve the generic syntax node that is represented by this node.
4867
/// Do not retrieve this property directly. Use `Syntax(self)` instead.
4968
var _syntaxNode: Syntax { get }
5069

51-
/// Converts the given `Syntax` node to this type. Returns `nil` if the
70+
/// Converts the given `Syntax` node to this type. Returns `nil` if the
5271
/// conversion is not possible.
5372
init?(_ syntaxNode: Syntax)
5473
}
@@ -238,14 +257,24 @@ public extension SyntaxProtocol {
238257
/// the first token syntax contained by this node. Without such token, this
239258
/// property will return nil.
240259
var leadingTrivia: Trivia? {
241-
return raw.formLeadingTrivia()
260+
get {
261+
return raw.formLeadingTrivia()
262+
}
263+
set {
264+
self = withLeadingTrivia(newValue ?? [])
265+
}
242266
}
243267

244268
/// The trailing trivia of this syntax node. Trailing trivia is attached to
245269
/// the last token syntax contained by this node. Without such token, this
246270
/// property will return nil.
247271
var trailingTrivia: Trivia? {
248-
return raw.formTrailingTrivia()
272+
get {
273+
return raw.formTrailingTrivia()
274+
}
275+
set {
276+
self = withTrailingTrivia(newValue ?? [])
277+
}
249278
}
250279

251280
/// The length this node's leading trivia takes up spelled out in source.
@@ -258,6 +287,33 @@ public extension SyntaxProtocol {
258287
return raw.trailingTriviaLength
259288
}
260289

290+
/// Returns a new syntax node with its leading trivia replaced
291+
/// by the provided trivia.
292+
func withLeadingTrivia(_ leadingTrivia: Trivia) -> Self {
293+
return Self(Syntax(data.withLeadingTrivia(leadingTrivia)))!
294+
}
295+
296+
/// Returns a new syntax node with its trailing trivia replaced
297+
/// by the provided trivia.
298+
func withTrailingTrivia(_ trailingTrivia: Trivia) -> Self {
299+
return Self(Syntax(data.withTrailingTrivia(trailingTrivia)))!
300+
}
301+
302+
/// Returns a new `${node.name}` with its leading trivia removed.
303+
func withoutLeadingTrivia() -> Self {
304+
return withLeadingTrivia([])
305+
}
306+
307+
/// Returns a new `${node.name}` with its trailing trivia removed.
308+
func withoutTrailingTrivia() -> Self {
309+
return withTrailingTrivia([])
310+
}
311+
312+
/// Returns a new `${node.name}` with all trivia removed.
313+
func withoutTrivia() -> Self {
314+
return withoutLeadingTrivia().withoutTrailingTrivia()
315+
}
316+
261317
/// The length of this node including all of its trivia.
262318
var totalLength: SourceLength {
263319
return raw.totalLength
@@ -369,146 +425,6 @@ public extension SyntaxProtocol {
369425
}
370426
}
371427

372-
373-
/// MARK: - Nodes
374-
375-
/// A Syntax node representing a single token.
376-
public struct TokenSyntax: SyntaxProtocol {
377-
public let _syntaxNode: Syntax
378-
379-
/// Converts the given `Syntax` node to a `TokenSyntax` if possible. Returns
380-
/// `nil` if the conversion is not possible.
381-
public init?(_ syntax: Syntax) {
382-
guard syntax.raw.kind == .token else { return nil }
383-
self._syntaxNode = syntax
384-
}
385-
386-
/// Creates a Syntax node from the given `SyntaxData`. This assumes
387-
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
388-
/// is undefined.
389-
internal init(_ data: SyntaxData) {
390-
assert(data.raw.kind == .token)
391-
self._syntaxNode = Syntax(data)
392-
}
393-
394-
public var presence: SourcePresence {
395-
return raw.presence
396-
}
397-
398-
/// The text of the token as written in the source code.
399-
public var text: String {
400-
return tokenKind.text
401-
}
402-
403-
/// Returns a new TokenSyntax with its kind replaced
404-
/// by the provided token kind.
405-
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
406-
guard raw.kind == .token else {
407-
fatalError("TokenSyntax must have token as its raw")
408-
}
409-
let newRaw = RawSyntax.createAndCalcLength(kind: tokenKind,
410-
leadingTrivia: raw.formLeadingTrivia()!, trailingTrivia: raw.formTrailingTrivia()!,
411-
presence: raw.presence)
412-
let newData = data.replacingSelf(newRaw)
413-
return TokenSyntax(newData)
414-
}
415-
416-
/// Returns a new TokenSyntax with its leading trivia replaced
417-
/// by the provided trivia.
418-
public func withLeadingTrivia(_ leadingTrivia: Trivia) -> TokenSyntax {
419-
guard raw.kind == .token else {
420-
fatalError("TokenSyntax must have token as its raw")
421-
}
422-
return TokenSyntax(data.withLeadingTrivia(leadingTrivia))
423-
}
424-
425-
/// Returns a new TokenSyntax with its trailing trivia replaced
426-
/// by the provided trivia.
427-
public func withTrailingTrivia(_ trailingTrivia: Trivia) -> TokenSyntax {
428-
guard raw.kind == .token else {
429-
fatalError("TokenSyntax must have token as its raw")
430-
}
431-
return TokenSyntax(data.withTrailingTrivia(trailingTrivia))
432-
}
433-
434-
/// Returns a new TokenSyntax with its leading trivia removed.
435-
public func withoutLeadingTrivia() -> TokenSyntax {
436-
return withLeadingTrivia([])
437-
}
438-
439-
/// Returns a new TokenSyntax with its trailing trivia removed.
440-
public func withoutTrailingTrivia() -> TokenSyntax {
441-
return withTrailingTrivia([])
442-
}
443-
444-
/// Returns a new TokenSyntax with all trivia removed.
445-
public func withoutTrivia() -> TokenSyntax {
446-
return withoutLeadingTrivia().withoutTrailingTrivia()
447-
}
448-
449-
/// The leading trivia (spaces, newlines, etc.) associated with this token.
450-
public var leadingTrivia: Trivia {
451-
get {
452-
return raw.formTokenLeadingTrivia()!
453-
}
454-
set {
455-
self = withLeadingTrivia(newValue)
456-
}
457-
}
458-
459-
/// The trailing trivia (spaces, newlines, etc.) associated with this token.
460-
public var trailingTrivia: Trivia {
461-
get {
462-
return raw.formTokenTrailingTrivia()!
463-
}
464-
set {
465-
self = withTrailingTrivia(newValue)
466-
}
467-
}
468-
469-
/// The kind of token this node represents.
470-
public var tokenKind: TokenKind {
471-
get {
472-
return raw.formTokenKind()!
473-
}
474-
set {
475-
self = withKind(newValue)
476-
}
477-
}
478-
479-
/// The length this node takes up spelled out in the source, excluding its
480-
/// leading or trailing trivia.
481-
public var contentLength: SourceLength {
482-
return raw.tokenContentLength
483-
}
484-
485-
/// The length this node's leading trivia takes up spelled out in source.
486-
public var leadingTriviaLength: SourceLength {
487-
return raw.tokenLeadingTriviaLength
488-
}
489-
490-
/// The length this node's trailing trivia takes up spelled out in source.
491-
public var trailingTriviaLength: SourceLength {
492-
return raw.tokenTrailingTriviaLength
493-
}
494-
495-
/// The length of this node including all of its trivia.
496-
public var totalLength: SourceLength {
497-
return raw.totalLength
498-
}
499-
}
500-
501-
extension TokenSyntax: CustomReflectable {
502-
public var customMirror: Mirror {
503-
return Mirror(self, children: [
504-
"text": text,
505-
"leadingTrivia": leadingTrivia,
506-
"trailingTrivia": trailingTrivia,
507-
"tokenKind": tokenKind,
508-
])
509-
}
510-
}
511-
512428
/// Sequence of tokens that are part of the provided Syntax node.
513429
public struct TokenSequence: Sequence {
514430
public struct Iterator: IteratorProtocol {
@@ -621,7 +537,7 @@ public struct SyntaxNode {
621537
/// Converts this node to a `SyntaxData` object.
622538
///
623539
/// This operation results in wrapping all of the node's parents into
624-
/// `SyntaxData` objects. There's a cost associated with it that should be
540+
/// `SyntaxData` objects. There's a cost associated with it that should be
625541
/// taken into account before used inside performance critical code.
626542
internal var asSyntaxData: SyntaxData {
627543
return SyntaxData(absoluteRaw, parent: parent?.asSyntax)

0 commit comments

Comments
 (0)