Skip to content

Commit 7c6c6df

Browse files
authored
Merge pull request #589 from rintaro/main
Fix RawTokenSyntax initializer
2 parents 3b566c3 + 4005231 commit 7c6c6df

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

Sources/SwiftSyntax/RawSyntaxNodeProtocol.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/// All typed raw syntax nodes conform to this protocol.
1414
/// `RawXXXSyntax` is a typed wrappeer of `RawSyntax`.
1515
@_spi(RawSyntax)
16-
public protocol RawSyntaxNodeProtocol {
16+
public protocol RawSyntaxNodeProtocol: CustomStringConvertible, TextOutputStreamable {
1717
/// Returns `true` if `raw` can be cast to this concrete raw syntax type.
1818
static func isKindOf(_ raw: RawSyntax) -> Bool
1919

@@ -34,6 +34,14 @@ public extension RawSyntaxNodeProtocol {
3434
func `is`<Node: RawSyntaxNodeProtocol>(_: Node.Type) -> Bool {
3535
Node.isKindOf(self.raw)
3636
}
37+
38+
var description: String {
39+
raw.description
40+
}
41+
42+
func write<Target>(to target: inout Target) where Target : TextOutputStream {
43+
raw.write(to: &target)
44+
}
3745
}
3846

3947
/// `RawSyntax` itself conforms to `RawSyntaxNodeProtocol`.
@@ -98,7 +106,7 @@ public struct RawTokenSyntax: RawSyntaxNodeProtocol {
98106
assert(arena.contains(text: text), "token text must be managed by the arena")
99107
let totalTriviaCount = leadingTriviaPieces.count + trailingTriviaPieces.count
100108
let buffer = arena.allocateRawTriviaPieceBuffer(count: totalTriviaCount)
101-
var byteLength = 0
109+
var byteLength = text.count
102110
if totalTriviaCount != 0 {
103111
var ptr = buffer.baseAddress!
104112
for piece in leadingTriviaPieces + trailingTriviaPieces {

Tests/SwiftSyntaxTest/RawSyntaxTests.swift

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,49 @@ fileprivate func cannedStructDecl(arena: SyntaxArena) -> RawStructDeclSyntax {
3636

3737
final class RawSyntaxTests: XCTestCase {
3838

39-
func testFactory() throws {
40-
withExtendedLifetime(SyntaxArena()) { arena in
41-
let structDecl = cannedStructDecl(arena: arena)
42-
XCTAssertEqual("\(structDecl.raw)",
39+
func testFactory() throws {
40+
withExtendedLifetime(SyntaxArena()) { arena in
41+
let structDecl = cannedStructDecl(arena: arena)
42+
XCTAssertEqual("\(structDecl.raw)",
4343
"""
4444
struct Foo {
4545
}
4646
""")
47-
}
4847
}
48+
}
4949

50-
func testAccessor() throws {
51-
withExtendedLifetime(SyntaxArena()) { arena in
52-
let structDecl = cannedStructDecl(arena: arena)
53-
XCTAssertEqual(structDecl.identifier.tokenKind, .identifier)
54-
XCTAssertEqual(structDecl.structKeyword.tokenText, "struct")
55-
XCTAssertEqual(structDecl.members.leftBrace.tokenText, "{")
56-
XCTAssertEqual(structDecl.members.members.elements.count, 0)
50+
func testAccessor() throws {
51+
withExtendedLifetime(SyntaxArena()) { arena in
52+
let structDecl = cannedStructDecl(arena: arena)
53+
XCTAssertEqual(structDecl.identifier.tokenKind, .identifier)
54+
XCTAssertEqual(structDecl.structKeyword.tokenText, "struct")
55+
XCTAssertEqual(structDecl.members.leftBrace.tokenText, "{")
56+
XCTAssertEqual(structDecl.members.members.elements.count, 0)
5757

58-
XCTAssert(structDecl.is(RawDeclSyntax.self))
59-
XCTAssertNotNil(structDecl.as(RawDeclSyntax.self))
60-
XCTAssertNil(structDecl.as(RawClassDeclSyntax.self))
61-
XCTAssertFalse(structDecl.is(RawTypeSyntax.self))
62-
}
58+
XCTAssert(structDecl.is(RawDeclSyntax.self))
59+
XCTAssertNotNil(structDecl.as(RawDeclSyntax.self))
60+
XCTAssertNil(structDecl.as(RawClassDeclSyntax.self))
61+
XCTAssertFalse(structDecl.is(RawTypeSyntax.self))
6362
}
63+
}
64+
65+
func testMaterializedToken() throws {
66+
withExtendedLifetime(SyntaxArena()) { arena in
67+
let ident = RawTokenSyntax(
68+
kind: .identifier, text: arena.intern("foo"),
69+
leadingTriviaPieces: [], trailingTriviaPieces: [],
70+
arena: arena)
71+
XCTAssertEqual(ident.tokenKind, .identifier)
72+
XCTAssertEqual(ident.tokenText, "foo")
73+
XCTAssertEqual(ident.presence, .present)
74+
XCTAssertEqual(ident.description, "foo")
75+
76+
let missingIdent = RawTokenSyntax(missing: .identifier, arena: arena)
77+
XCTAssertEqual(missingIdent.presence, .missing)
78+
XCTAssertEqual(missingIdent.tokenText, "")
79+
XCTAssertEqual(missingIdent.description, "")
80+
}
81+
}
82+
6483

6584
}

0 commit comments

Comments
 (0)