-
Notifications
You must be signed in to change notification settings - Fork 9
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
Interpolating RegExps with capturing groups changes group numbering of groups in the template. #1
Comments
There are probably legitimate use cases for interpolating capturing groups. Strawman 1:
So in
templateGroups[0] is always 0. We could also attach a property interpGroups such that
is the similarly defined array but for interpolated RegExps so in the above example
but this would mean that |
We should work through some more examples. But my immediate reaction is that I like the templateGroups approach much better. |
Fixed at 2e3d0ae |
Can you reopen this, please? I don't think Creating a matcher that does special things about groups doesn't work well with subclassing imo. Or does Maybe the more fundamental problem is that we cannot easily get a count of groups for a regex instance. If we'd just give every const a = /(foo)/;
const b = RegExp.make`${a}(bar)`
let m = b.exec("foobar");
m[0]; // foo
m[a.groupCount + 0]; // bar |
Reopening as requested. RegExp.make does not return a subclass. AFAIK the possibility has not been considered. Why might it help? I don't see it. |
Yeah, Also, this approach doesn't compose well. If I put multiple regexes, some of them RegExp.made, into another RegExp.make call, then how do I access the groups of the inner ones? const a = /(foo)/;
const b = /(bar)/;
const c = RegExp.make `${b} (baz)`;
const d = RegExp.make `${a}${c}(n)`
let match = d.match("foobar bazn"); // ["foo", "bar", "baz", "n"]
match[0 + 0] == "foo"; // first group in first interpolated regex
match[d.templateGroups[0]] == "n"; // first group in outermost (d) template
match[??? 0] == "bar"; // first group in first regex after how many groups?
match[??? c.templateGroups[0]] == "baz"; // first group in (c) template after how many groups? Not that nested composition is really an actual use case, but I think that it hints at the inferiority of the design. |
var re = RegExp.make`${ /(.)/ }(.)`
// Case 1 - Match by Method
re.exec("ab")
// Case 2 - Oblique Match
"ab".replace(re, "$1 $2") // calls re[@@replace] internally
// Case 3 - Explicit call to method on RegExp
RegExp.prototype.exec.call(re, "ab") Strawman I - Do nothingFor all of the above cases, capturing groups are Strawman II - Convert capturing groups in interpolated regexps to non-capturing groupsError out if there is a backreference in an interpolated regexp. For all of the above cases, capturing groups are Strawman III - Like II, but treat every interpolation as an implicit capturing groupFor all of the above cases, capturing groups are Strawman IV - Like II, but instrument to add structure.For case 1, and 2, the method calls come to our custom implementations that adjust Since we treat For 3, the explicit call to super class, we get We could instrument by defining a subclass of Strawman V - A combination of II and IIIConvert nested groups to non-capturing and ban unresolvable backreferences, but For all 3, we get Any other options? |
RegExp.make
${ /(foo)/ }(bar)``has 1 group in the template, (bar).
In the matched output, this group will be at an index 1 + numberOfGroupsIn(/(foo)/), in this case 2.
Figure out what the semantics should be here.
The text was updated successfully, but these errors were encountered: