Skip to content

Modify DSL test to test for uncaptured backreference #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,54 @@ class RegexDSLTests: XCTestCase {
}
}
}

// Post-hoc captured reference w/ attempted match before capture
// #"(?:\w\1|(\w):)+"#
//
// This tests that the reference `a` simply fails to match instead of
// erroring when encountered before a match is captured into `a`. The
// matching process here goes like this:
// - the first time through, the first alternation is taken
// - `.word` matches on "a"
// - the `a` backreference fails on ":", because `a` hasn't matched yet
// - backtrack to the beginning of the input
// - now the second alternation is taken
// - `.word` matches on "a" and is captured as `a`
// - the literal ":" matches
// - proceeding from the position of the first "b" in the first alternation
// - `.word` matches on "b"
// - the `a` backreference now contains "a", and matches on "a"
// - proceeding from the position of the first "c" in the first alternation
// - `.word` matches on "c"
// - the `a` backreference still contains "a", and matches on "a"
// - proceeding from the position of the first "o" in the first alternation
// - `.word` matches on "o"
// - the `a` backreference still contains "a", so it fails on ":"
// - now the second alternation is taken
// - `.word` matches on "o" and is captured as `a`
// - the literal ":" matches
// - continuing as above from the second "b"...
try _testDSLCaptures(
("a:bacao:boco", ("a:bacao:boco", "o")),
matchType: (Substring, Substring?).self,
==
) {
// NOTE: "expression too complex to type check" when inferring the generic
// parameter.
OneOrMore {
let a = Reference(Substring.self)
ChoiceOf<(Substring, Substring?)> {
Regex {
.word
a
}
Regex {
Capture(.word, as: a)
":"
}
}
}
}
}

func testSemanticVersionExample() {
Expand Down