Skip to content

Commit aa2489d

Browse files
authored
Merge pull request swiftlang#46 from nkcsgexi/print-tree-with-kinds
test: add a utility to print nodes with kind for debugging purposes.
2 parents 8ff6eab + edf769d commit aa2489d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Sources/lit-test-helper/main.swift

+23
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ func performClassifySyntax(args: CommandLineArguments) throws {
124124
}
125125
}
126126

127+
class NodePrinter: SyntaxVisitor {
128+
override func visitPre(_ node: Syntax) {
129+
assert(!node.isUnknown)
130+
print("<\(type(of: node))>", terminator:"")
131+
}
132+
override func visitPost(_ node: Syntax) {
133+
print("</\(type(of: node))>", terminator:"")
134+
}
135+
override func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
136+
print(token, terminator:"")
137+
return .visitChildren
138+
}
139+
}
140+
141+
func printSyntaxTree(args: CommandLineArguments) throws {
142+
let treeURL = URL(fileURLWithPath: try args.getRequired("-source-file"))
143+
let swiftcURL = args["-swiftc"].map(URL.init(fileURLWithPath:))
144+
let tree = try SyntaxTreeParser.parse(treeURL, swiftcURL: swiftcURL)
145+
tree.walk(NodePrinter())
146+
}
147+
127148
do {
128149
let args = try CommandLineArguments.parse(CommandLine.arguments.dropFirst())
129150

@@ -133,6 +154,8 @@ do {
133154
try performClassifySyntax(args: args)
134155
} else if args.has("-deserialize") {
135156
try performDeserialize(args: args)
157+
} else if args.has("-print-source") {
158+
try printSyntaxTree(args: args)
136159
} else if args.has("-help") {
137160
printHelp()
138161
} else {

0 commit comments

Comments
 (0)