-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSyntaxFactory.swift
119 lines (97 loc) · 3.76 KB
/
SyntaxFactory.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: OS=macosx
// REQUIRES: objc_interop
import Foundation
import StdlibUnittest
import SwiftSyntax
func cannedStructDecl() -> StructDeclSyntax {
let structKW = SyntaxFactory.makeStructKeyword(trailingTrivia: .spaces(1))
let fooID = SyntaxFactory.makeIdentifier("Foo", trailingTrivia: .spaces(1))
let rBrace = SyntaxFactory.makeRightBraceToken(leadingTrivia: .newlines(1))
let members = MemberDeclBlockSyntax {
$0.useLeftBrace(SyntaxFactory.makeLeftBraceToken())
$0.useRightBrace(rBrace)
}
return StructDeclSyntax {
$0.useStructKeyword(structKW)
$0.useIdentifier(fooID)
$0.useMembers(members)
}
}
var SyntaxFactoryAPI = TestSuite("SyntaxFactoryAPI")
SyntaxFactoryAPI.test("Generated") {
let structDecl = cannedStructDecl()
expectEqual("\(structDecl)",
"""
struct Foo {
}
""")
let forType = SyntaxFactory.makeIdentifier("for",
leadingTrivia: .backticks(1),
trailingTrivia: [
.backticks(1), .spaces(1)
])
let newBrace = SyntaxFactory.makeRightBraceToken(leadingTrivia: .newlines(2))
let renamed = structDecl.withIdentifier(forType)
.withMembers(structDecl.members
.withRightBrace(newBrace))
expectEqual("\(renamed)",
"""
struct `for` {
}
""")
expectNotEqual(structDecl.members, renamed.members)
expectEqual(structDecl, structDecl.root as? StructDeclSyntax)
expectNil(structDecl.parent)
expectNotNil(structDecl.members.parent)
expectEqual(structDecl.members.parent as? StructDeclSyntax, structDecl)
// Ensure that accessing children via named identifiers is exactly the
// same as accessing them as their underlying data.
expectEqual(structDecl.members,
structDecl.child(at: 7) as? MemberDeclBlockSyntax)
expectEqual("\(structDecl.members.rightBrace)",
"""
}
""")
}
SyntaxFactoryAPI.test("TokenSyntax") {
let tok = SyntaxFactory.makeStructKeyword()
expectEqual("\(tok)", "struct")
expectTrue(tok.isPresent)
let preSpacedTok = tok.withLeadingTrivia(.spaces(3))
expectEqual("\(preSpacedTok)", " struct")
let postSpacedTok = tok.withTrailingTrivia(.spaces(6))
expectEqual("\(postSpacedTok)", "struct ")
let prePostSpacedTok = preSpacedTok.withTrailingTrivia(.spaces(4))
expectEqual("\(prePostSpacedTok)", " struct ")
}
SyntaxFactoryAPI.test("FunctionCallSyntaxBuilder") {
let string = SyntaxFactory.makeStringLiteralExpr("Hello, world!")
let printID = SyntaxFactory.makeVariableExpr("print")
let arg = FunctionCallArgumentSyntax {
$0.useExpression(string)
}
let call = FunctionCallExprSyntax {
$0.useCalledExpression(printID)
$0.useLeftParen(SyntaxFactory.makeLeftParenToken())
$0.addFunctionCallArgument(arg)
$0.useRightParen(SyntaxFactory.makeRightParenToken())
}
expectEqual("\(call)", "print(\"Hello, world!\")")
let terminatorArg = FunctionCallArgumentSyntax {
$0.useLabel(SyntaxFactory.makeIdentifier("terminator"))
$0.useColon(SyntaxFactory.makeColonToken(trailingTrivia: .spaces(1)))
$0.useExpression(SyntaxFactory.makeStringLiteralExpr(" "))
}
let callWithTerminator = call.withArgumentList(
SyntaxFactory.makeFunctionCallArgumentList([
arg.withTrailingComma(
SyntaxFactory.makeCommaToken(trailingTrivia: .spaces(1))),
terminatorArg
])
)
expectEqual("\(callWithTerminator)",
"print(\"Hello, world!\", terminator: \" \")")
}
runAllTests()