Skip to content

Commit 7b43ad0

Browse files
committed
Better filter trivia in dumps
Make sure we don't try and print things like empty comma lists `,,,,` or redundant parens for concatenations that had their trivia filtered out.
1 parent bb1af00 commit 7b43ad0

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Sources/_RegexParser/Regex/Printing/DumpAST.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,23 @@ extension _ASTPrintable {
4444
guard let children = _children else {
4545
return _dumpBase
4646
}
47-
let sub = children.lazy.compactMap {
47+
let childDump = children.compactMap { child -> String? in
4848
// Exclude trivia for now, as we don't want it to appear when performing
4949
// comparisons of dumped output in tests.
5050
// TODO: We should eventually have some way of filtering out trivia for
5151
// tests, so that it can appear in regular dumps.
52-
if $0.isTrivia { return nil }
53-
return $0._dump()
54-
}.joined(separator: ",")
55-
if sub.isEmpty {
56-
return "\(_dumpBase)"
52+
if child.isTrivia { return nil }
53+
let dump = child._dump()
54+
return !dump.isEmpty ? dump : nil
5755
}
58-
return "\(_dumpBase)(\(sub))"
56+
let base = "\(_dumpBase)"
57+
if childDump.isEmpty {
58+
return base
59+
}
60+
if childDump.count == 1, base.isEmpty {
61+
return "\(childDump[0])"
62+
}
63+
return "\(base)(\(childDump.joined(separator: ",")))"
5964
}
6065
}
6166

@@ -77,7 +82,7 @@ extension AST.Node: _ASTPrintable {
7782
}
7883

7984
extension AST.Alternation {
80-
public var _dumpBase: String { "alternation" }
85+
public var _dumpBase: String { "alternation<\(children.count)>" }
8186
}
8287

8388
extension AST.Concatenation {

0 commit comments

Comments
 (0)