Skip to content

Commit 0efba6d

Browse files
authored
Merge pull request swiftlang#82 from hamishknight/dump
2 parents 00c27a0 + 2260ab2 commit 0efba6d

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

Sources/_MatchingEngine/Regex/AST/ASTProtocols.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ extension _ASTPrintable {
5151
public var debugDescription: String { _dump() }
5252

5353
var _children: [AST]? {
54-
(self as? _ASTParent)?.children
54+
if let children = (self as? _ASTParent)?.children {
55+
return children
56+
}
57+
if let children = (self as? AST)?.children {
58+
return children
59+
}
60+
return nil
5561
}
5662

5763
func _print() -> String {
@@ -62,8 +68,13 @@ extension _ASTPrintable {
6268
guard let children = _children else {
6369
return _dumpBase
6470
}
65-
let sub = children.lazy.map {
66-
$0._dump()
71+
let sub = children.lazy.compactMap {
72+
// Exclude trivia for now, as we don't want it to appear when performing
73+
// comparisons of dumped output in tests.
74+
// TODO: We should eventually have some way of filtering out trivia for
75+
// tests, so that it can appear in regular dumps.
76+
if $0.isTrivia { return nil }
77+
return $0._dump()
6778
}.joined(separator: ",")
6879
return "\(_dumpBase)(\(sub))"
6980
}

Sources/_MatchingEngine/Regex/AST/CustomCharClass.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,19 @@ extension CustomCC {
5353

5454
extension CustomCC: _ASTNode {
5555
public var _dumpBase: String {
56-
// FIXME: print out members...
57-
"customCharacterClass"
56+
"customCharacterClass(\(members))"
5857
}
5958
}
6059

60+
extension CustomCC.Member: _ASTPrintable {
61+
public var _dumpBase: String {
62+
switch self {
63+
case .custom(let cc): return "\(cc)"
64+
case .atom(let a): return "\(a)"
65+
case .range(let lhs, let rhs):
66+
return "range \(lhs) to \(rhs)"
67+
case .setOperation(let lhs, let op, let rhs):
68+
return "op \(lhs) \(op.value) \(rhs)"
69+
}
70+
}
71+
}

Sources/_MatchingEngine/Regex/AST/Group.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension AST.Group.Kind: _ASTPrintable {
7171
public var _dumpBase: String {
7272
switch self {
7373
case .capture: return "capture"
74-
case .namedCapture(let s): return "capture<\(s)>"
74+
case .namedCapture(let s): return "capture<\(s.value)>"
7575
case .nonCapture: return "nonCapture"
7676
case .nonCaptureReset: return "nonCaptureReset"
7777
case .atomicNonCapturing: return "atomicNonCapturing"

Tests/RegexTests/ParseTests.swift

+41
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ func parseWithDelimitersTest(_ input: String, _ expecting: AST) {
5757
}
5858
}
5959

60+
/// Make sure the AST for two regex strings get compared differently.
61+
func parseNotEqualTest(_ lhs: String, _ rhs: String,
62+
syntax: SyntaxOptions = .traditional) {
63+
let lhsAST = try! parse(lhs, syntax)
64+
let rhsAST = try! parse(rhs, syntax)
65+
if lhsAST == rhsAST || lhsAST._dump() == rhsAST._dump() {
66+
XCTFail("""
67+
AST: \(lhsAST._dump())
68+
Should not be equal to: \(rhsAST._dump())
69+
""")
70+
}
71+
}
72+
6073
extension RegexTests {
6174
func testParse() {
6275
parseTest(
@@ -194,6 +207,16 @@ extension RegexTests {
194207
parseTest("[[:word:]]", charClass(posixProp_m(.posix(.word))))
195208
parseTest("[[:xdigit:]]", charClass(posixProp_m(.posix(.xdigit))))
196209

210+
parseTest("[[:ascii:]]", charClass(posixProp_m(.ascii)))
211+
parseTest("[[:cntrl:]]", charClass(posixProp_m(.generalCategory(.control))))
212+
parseTest("[[:digit:]]", charClass(posixProp_m(.generalCategory(.decimalNumber))))
213+
parseTest("[[:lower:]]", charClass(posixProp_m(.binary(.lowercase))))
214+
parseTest("[[:punct:]]", charClass(posixProp_m(.generalCategory(.punctuation))))
215+
parseTest("[[:space:]]", charClass(posixProp_m(.binary(.whitespace))))
216+
parseTest("[[:upper:]]", charClass(posixProp_m(.binary(.uppercase))))
217+
218+
parseTest("[[:UPPER:]]", charClass(posixProp_m(.binary(.uppercase))))
219+
197220
parseTest("[[:isALNUM:]]", charClass(posixProp_m(.posix(.alnum))))
198221
parseTest("[[:AL_NUM:]]", charClass(posixProp_m(.posix(.alnum))))
199222
parseTest("[[:script=Greek:]]", charClass(posixProp_m(.script(.greek))))
@@ -436,6 +459,24 @@ extension RegexTests {
436459
parseWithDelimitersTest("'/a b/'", concat("a", " ", "b"))
437460
parseWithDelimitersTest("'|a b|'", concat("a", "b"))
438461

462+
// Make sure dumping output correctly reflects differences in AST.
463+
parseNotEqualTest(#"abc"#, #"abd"#)
464+
465+
parseNotEqualTest(#"[abc[:space:]\d]+"#,
466+
#"[abc[:upper:]\d]+"#)
467+
468+
parseNotEqualTest(#"[abc[:space:]\d]+"#,
469+
#"[ac[:space:]\d]+"#)
470+
471+
parseNotEqualTest(#"[abc[:space:]\d]+"#,
472+
#"[acc[:space:]\s]+"#)
473+
474+
parseNotEqualTest(#"[abc[:space:]\d]+"#,
475+
#"[acc[:space:]\d]*"#)
476+
477+
parseNotEqualTest(#"([a-c&&e]*)+"#,
478+
#"([a-d&&e]*)+"#)
479+
439480
// TODO: failure tests
440481
}
441482

Tests/RegexTests/SyntaxOptionsTests.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ private let dplus = oneOrMore(
77
.greedy, atom(.escaped(.decimalDigit)))
88
private let dotAST = concat(
99
dplus, ".", dplus, ".", dplus, ".", dplus)
10+
private let dotASTQuoted = concat(
11+
dplus, quote("."), dplus, quote("."), dplus, quote("."), dplus)
1012

1113
extension RegexTests {
1214

@@ -42,7 +44,7 @@ extension RegexTests {
4244
dotAST, syntax: .modern)
4345
parseTest(
4446
#" \d+ "." \d+ "." \d+ "." \d+ "#,
45-
dotAST, syntax: .modern)
47+
dotASTQuoted, syntax: .modern)
4648
}
4749

4850
func testModernRanges() {

0 commit comments

Comments
 (0)